use std::collections::BTreeMap;
use ocmf::{Curve, Limits, PublicKey, Record, json};
#[cfg_attr(
not(feature = "verify"),
allow(
dead_code,
reason = "OpenSSL's verdict is the oracle for verification, and this build does none"
)
)]
struct Entry {
source: String,
record: String,
key: Option<String>,
openssl_verified: Option<bool>,
}
fn corpus() -> Vec<Entry> {
let raw = include_str!("corpus/records.json");
let mut dev = Vec::new();
let value = json::parse(raw, &Limits::UNLIMITED, &mut dev).expect("fixture is JSON");
let obj = value.as_object().expect("fixture is an object");
let entries = obj.get("entries").unwrap().as_array().unwrap();
entries
.items
.iter()
.map(|e| {
let o = e.as_object().unwrap();
let text = |k: &str| {
o.get(k)
.and_then(json::Value::as_str)
.map(|s| s.decode().into_owned())
};
Entry {
source: text("source").unwrap(),
record: text("record").unwrap(),
key: text("key"),
openssl_verified: o.get("openssl_verified").and_then(json::Value::as_bool),
}
})
.collect()
}
#[test]
fn every_real_record_parses() {
let corpus = corpus();
assert!(
corpus.len() > 250,
"the fixture should hold the whole corpus"
);
for e in &corpus {
Record::parse(&e.record).unwrap_or_else(|err| panic!("{}: {err}", e.source));
}
}
#[test]
fn every_real_record_reproduces_itself_byte_for_byte() {
for e in corpus() {
let r = Record::parse(&e.record).unwrap();
assert_eq!(r.to_string(), e.record, "{}", e.source);
assert_eq!(
r.signed_bytes(),
e.record.split('|').nth(1).unwrap().as_bytes(),
"{}: the signed span must match what the reference implementation splits out",
e.source
);
}
}
#[test]
#[cfg(feature = "verify")]
fn this_crate_and_openssl_agree_on_every_record() {
let mut agreed = 0usize;
let mut skipped_unsupported = 0usize;
let mut disagreements = Vec::new();
for e in corpus() {
let (Some(key_text), Some(expected)) = (&e.key, e.openssl_verified) else {
continue;
};
let record = Record::parse(&e.record).unwrap();
let hint = record.signature().curve();
let Ok(key) = PublicKey::from_text(key_text, hint) else {
panic!("{}: OpenSSL read this key and we could not", e.source);
};
match ocmf::verify(&record, &key) {
Err(ocmf::VerifyError::Unsupported { .. }) => skipped_unsupported += 1,
Ok(_) if expected => agreed += 1,
Err(_) if !expected => agreed += 1,
other => disagreements.push(format!(
"{}: openssl said {expected}, we said {}",
e.source,
match other {
Ok(_) => "verified".to_string(),
Err(err) => err.to_string(),
}
)),
}
}
assert!(
disagreements.is_empty(),
"{} disagreement(s) with OpenSSL:\n{}",
disagreements.len(),
disagreements.join("\n")
);
let lacking = corpus()
.iter()
.filter(|e| e.key.is_some() && e.openssl_verified.is_some())
.filter(|e| {
let curve = Record::parse(&e.record).unwrap().signature().curve();
curve.is_some_and(|c| !ocmf::verify::is_supported(c))
})
.count();
assert!(
skipped_unsupported <= lacking,
"{skipped_unsupported} skipped, but only {lacking} records are on curves this build lacks"
);
assert_eq!(
agreed + skipped_unsupported,
251,
"247 authentic records and 4 that are not"
);
#[cfg(feature = "backend-openssl")]
assert_eq!(skipped_unsupported, 0, "every curve is available");
#[cfg(all(feature = "curves-pure", not(feature = "backend-openssl")))]
assert_eq!(
skipped_unsupported, 2,
"the two secp192k1 records need OpenSSL"
);
}
#[test]
#[cfg(all(feature = "verify", feature = "backend-openssl"))]
fn the_isabellenhuette_record_only_this_crate_can_check() {
let e = corpus()
.into_iter()
.find(|e| e.source.contains("OCMF-receipt-with_publickey_and_data"))
.expect("the ISA record is in the corpus");
let key_text = e.key.expect("it ships with a key");
assert_eq!(key_text.len(), 128, "64 bytes of hex, no SEC1 prefix");
let record = Record::parse(&e.record).unwrap();
let hint = record.signature().curve();
let key = PublicKey::from_text(&key_text, hint).unwrap();
let verified = ocmf::verify(&record, &key).expect("it is authentic");
assert!(
verified
.deviations()
.iter()
.any(|d| d.kind == ocmf::DeviationKind::RawSignatureNotDer),
"and the crate says so rather than hiding it"
);
}
#[derive(Default)]
struct SignatureFieldsWritten {
algorithm: usize,
encoding: usize,
mime: usize,
}
#[test]
fn the_measurements_that_shaped_the_design_still_hold() {
let corpus = corpus();
let mut records = 0usize;
let mut readings = 0usize;
let mut kinds: BTreeMap<&str, usize> = BTreeMap::new();
let mut no_meter_serial = 0usize;
let mut no_time = 0usize;
let mut quoted_rv = 0usize;
let mut pretty = 0usize;
let mut max_payload = 0usize;
let mut max_readings = 0usize;
let mut fiscal = 0usize;
let mut written = SignatureFieldsWritten::default();
let mut identification_flags_absent = 0usize;
for e in &corpus {
let r = Record::parse(&e.record).unwrap();
records += 1;
readings += r.payload().readings().len();
max_payload = max_payload.max(r.payload_text().len());
max_readings = max_readings.max(r.payload().readings().len());
if r.payload().meter_serial().is_none() {
no_meter_serial += 1;
}
if r.payload().pagination().map(|p| p.context()) == Some(ocmf::PaginationContext::Fiscal) {
fiscal += 1;
}
written.algorithm += usize::from(r.signature().algorithm_was_written());
written.encoding += usize::from(r.signature().encoding_was_written());
written.mime += usize::from(r.signature().mime_was_written());
if r.payload().identification_flags().is_none() {
identification_flags_absent += 1;
}
for reading in r.payload().readings() {
if !reading.explicit().has(ocmf::Explicit::TIME) {
no_time += 1;
}
if reading.value().is_some_and(ocmf::Number::was_quoted) {
quoted_rv += 1;
}
}
for d in r.deviations() {
*kinds.entry(d.kind.name()).or_default() += 1;
if d.kind == ocmf::DeviationKind::PrettyPrintedPayload {
pretty += 1;
}
}
}
assert_eq!(records, 256);
assert_eq!(readings, 705);
assert_eq!(no_meter_serial, 229, "89 % of records omit a mandatory MS");
assert_eq!(
no_time, 205,
"29 % of readings rely on carry-forward for TM"
);
assert_eq!(quoted_rv, 23, "RV arrives as a JSON string this often");
assert_eq!(
pretty, 9,
"records whose payload must never be re-serialised"
);
assert_eq!(
fiscal, 0,
"the fiscal pagination context is untested by the corpus"
);
assert_eq!(
written.algorithm, 23,
"records that name their own algorithm"
);
assert_eq!(
written.encoding, 1,
"records that name their own SD encoding"
);
assert_eq!(written.mime, 0, "the field that would announce a raw r||s");
assert_eq!(
identification_flags_absent, 12,
"records with no `IF` at all"
);
assert!(max_payload <= 932, "the largest real payload, in bytes");
assert!(max_readings <= 6, "the largest real reading count");
assert!(
kinds.contains_key("ObisNonCanonical"),
"kinds seen: {kinds:?}"
);
}
#[test]
fn not_one_obis_code_in_the_corpus_is_written_the_way_the_table_says() {
let mut obis: BTreeMap<String, usize> = BTreeMap::new();
let mut readings = 0usize;
for e in &corpus() {
let r = Record::parse(&e.record).unwrap();
for reading in r.payload().readings() {
readings += 1;
if reading.explicit().has(ocmf::Explicit::OBIS)
&& let Some(code) = reading.obis()
{
*obis.entry(code.as_str().to_string()).or_default() += 1;
assert!(
!code.is_canonical(),
"{}: {code} is written the way [OCMF Tab. 25] specifies, \
which no record in the corpus was thought to do",
e.source
);
}
}
}
assert_eq!(obis.get("1-b:1.8.0"), Some(&462), "obis seen: {obis:?}");
assert_eq!(obis.get("1-b:1.9.0"), Some(&200));
assert_eq!(obis.get("1-b:1.8.e"), Some(&14));
assert_eq!(obis.get("01-00:01.08.00.FF"), Some(&6));
assert_eq!(
obis.values().sum::<usize>(),
readings,
"every reading names a register, one way or another"
);
assert_eq!(
obis.len(),
12,
"distinct spellings, none of them the table's: {obis:?}"
);
}
#[test]
fn the_corpus_writes_values_the_tables_do_not_define_and_they_are_reported() {
let mut undefined: BTreeMap<String, usize> = BTreeMap::new();
let mut id_format = 0usize;
let mut cp_format = 0usize;
let mut records_affected = 0usize;
for e in &corpus() {
let r = Record::parse(&e.record).unwrap();
records_affected += usize::from(r.deviations().iter().any(|d| {
matches!(
d.kind,
ocmf::DeviationKind::UndefinedTableValue
| ocmf::DeviationKind::IdentificationDataFormat
| ocmf::DeviationKind::ChargePointIdFormat
)
}));
for d in r.deviations() {
let field = d.at.path.clone().unwrap_or_default();
match d.kind {
ocmf::DeviationKind::UndefinedTableValue => {
let field = field.rsplit('.').next().unwrap_or(&field).to_string();
*undefined.entry(field).or_default() += 1;
}
ocmf::DeviationKind::IdentificationDataFormat => id_format += 1,
ocmf::DeviationKind::ChargePointIdFormat => cp_format += 1,
_ => {}
}
}
}
assert_eq!(
undefined.get("RU"),
Some(&2),
"two readings say `sec`: {undefined:?}"
);
assert_eq!(
undefined.get("CT"),
Some(&7),
"seven records name a charge-point id type Tab. 18 does not define"
);
assert_eq!(
undefined.len(),
2,
"and nothing else in the corpus is outside its table: {undefined:?}"
);
assert_eq!(id_format, 13, "records whose ID does not match its IT");
assert_eq!(cp_format, 8, "CBIDC records with no connector id");
assert_eq!(
undefined.values().sum::<usize>() + id_format + cp_format,
30
);
assert_eq!(
records_affected, 26,
"records carrying at least one of them"
);
for e in &corpus() {
for d in Record::parse(&e.record).unwrap().deviations() {
assert_ne!(
d.kind,
ocmf::DeviationKind::MandatoryReadingFieldMissing,
"{}: {d}",
e.source
);
assert_ne!(
d.kind,
ocmf::DeviationKind::IdentificationStatusMissing,
"{}: {d}",
e.source
);
}
}
}
#[test]
fn the_rules_the_specifications_prose_states_are_checked_against_real_records() {
let mut cardinality = 0usize;
let mut duplicate_group = 0usize;
let mut unidentifiable = 0usize;
for e in &corpus() {
let r = Record::parse(&e.record).unwrap();
for d in r.deviations() {
match d.kind {
ocmf::DeviationKind::IdentificationFlagsCardinality => cardinality += 1,
ocmf::DeviationKind::IdentificationFlagsDuplicateGroup => duplicate_group += 1,
ocmf::DeviationKind::SourceUnidentifiable => unidentifiable += 1,
_ => {}
}
}
}
assert_eq!(
cardinality, 0,
"`[OCMF Tab. 4]` says 0..4 and the field obeys"
);
assert_eq!(
duplicate_group, 0,
"no record states two things about one group"
);
assert_eq!(
unidentifiable, 0,
"every real record names a serial number or a charge point — the rule \
holds in the field, which is worth knowing before it is enforced"
);
}
#[test]
fn every_curve_in_the_corpus_is_one_this_crate_names() {
let mut seen = BTreeMap::new();
for e in corpus() {
let r = Record::parse(&e.record).unwrap();
let Some(c) = r.signature().curve() else {
continue;
};
*seen.entry(c.name()).or_insert(0usize) += 1;
if let Some(k) = &e.key
&& let Ok(key) = PublicKey::from_text(k, Some(c))
&& key.curve() != c
{
assert!(
e.source.contains("fail"),
"{}: unexpected key/SA mismatch",
e.source
);
}
}
assert!(seen.contains_key(Curve::Secp256r1.name()));
assert!(seen.len() >= 2, "curves seen: {seen:?}");
}