structured-cms 0.9.1

Borrowed CMS SignedData processing with explicit providers and resource accounting
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
//! Generation bounds use the existing UTC core, not a second calendar engine.

use structured_cms::timestamp::{Accuracy, BoundUncertainty, earliest, latest};
use structured_pkix::temporal::{Instant, LeapTable, Reading, Uncertainty};

#[test]
fn unstated_accuracy_precedes_missing_utc_coverage() {
    // An omitted complete Accuracy supplies no endpoint. Even unknown UTC
    // must not hide that primary limitation or imply a zero-width interval.
    let utc = LeapTable::new(0, 10, &[]).unwrap();
    let reading = Reading::from(Instant::new(20, 0).unwrap());
    assert_eq!(
        earliest(reading, None, &utc),
        Err(BoundUncertainty::AccuracyUnstated)
    );
    assert_eq!(
        latest(reading, None, &utc),
        Err(BoundUncertainty::AccuracyUnstated)
    );
}

#[test]
fn stated_zero_and_all_accuracy_units_keep_exact_endpoints() {
    // Missing parts of a present Accuracy default to zero; missing the whole
    // value is distinct. Every unit contributes to both factual endpoints.
    let utc = LeapTable::new(0, 100, &[]).unwrap();
    let reading = Reading::from(Instant::new(50, 0).unwrap());
    assert_eq!(
        earliest(reading, Some(Accuracy::new(0, 0, 0)), &utc),
        Ok(reading.start())
    );
    assert_eq!(
        latest(reading, Some(Accuracy::new(0, 0, 0)), &utc),
        Ok(reading.start())
    );
    let accuracy = Accuracy::new(1, 2, 3);
    assert_eq!(accuracy.nanos(), 1_002_003_000);
    assert_eq!(
        earliest(reading, Some(accuracy), &utc),
        Ok(Instant::new(48, 997_997_000).unwrap())
    );
    assert_eq!(
        latest(reading, Some(accuracy), &utc),
        Ok(Instant::new(51, 2_003_000).unwrap())
    );
    assert_eq!(
        latest(
            Reading::from(Instant::new(200, 0).unwrap()),
            Some(accuracy),
            &utc
        ),
        Err(BoundUncertainty::Utc(Uncertainty::OutsideCoverage))
    );
}

fn reading(label: &[u8]) -> Reading {
    // Shared decoding is the only calendar/parser implementation in this test.
    use std::sync::Arc;
    use structured_asn1::{
        Parser,
        memory::{Memory, MemoryAccount},
    };
    let parser = Parser::ber(Memory::new(Arc::new(MemoryAccount::new(4096, 65536))));
    let mut bytes = vec![0x18, u8::try_from(label.len()).unwrap()];
    bytes.extend_from_slice(label);
    let mut reader = parser.read(&bytes, 400).unwrap();
    let element = reader.next_element().unwrap().unwrap();
    structured_pkix::temporal::read_generalized(&reader, &element, "tstInfo.genTime").unwrap()
}

#[test]
fn fine_precision_rounds_into_a_real_inserted_second_only_with_utc_facts() {
    // RFC3161 accuracy arithmetic must preserve the same leap/rounding core
    // as the timestamp reader rather than aliasing an inserted label to midnight.
    use structured_pkix::temporal::Transition;
    let day_end = 181 * 86400;
    let transitions = [Transition::new(day_end, 1)];
    let utc = LeapTable::new(0, day_end + 86400, &transitions).unwrap();
    let recorded = reading(b"19700630235959.9999999991Z");
    let zero = Some(Accuracy::new(0, 0, 0));
    assert_eq!(earliest(recorded, zero, &utc), Ok(recorded.start()));
    let upper = latest(recorded, zero, &utc).unwrap();
    assert!(upper.is_leap_second());
    assert_eq!(upper.seconds(), day_end - 1);
    assert_eq!(upper.nanos(), 0);
    let shifted = latest(recorded, Some(Accuracy::new(1, 0, 0)), &utc).unwrap();
    assert_eq!(shifted, Instant::new(day_end, 0).unwrap());
    let without_insertion = LeapTable::new(0, day_end + 86400, &[]).unwrap();
    assert_eq!(
        latest(recorded, zero, &without_insertion),
        Ok(Instant::new(day_end, 0).unwrap())
    );
}

#[test]
fn unstated_accuracy_stays_primary_for_fine_precision_and_deleted_utc_labels() {
    // Rounding and a contradictory UTC label must not manufacture an endpoint
    // or mask the separate absence of any supplied accuracy bound.
    use structured_pkix::temporal::Transition;
    let recorded = reading(b"19700101000009.9999999991Z");
    let utc = LeapTable::new(0, 10, &[]).unwrap();
    assert_eq!(
        latest(recorded, None, &utc),
        Err(BoundUncertainty::AccuracyUnstated)
    );
    assert_eq!(
        latest(recorded, Some(Accuracy::new(0, 0, 0)), &utc),
        Err(BoundUncertainty::Utc(Uncertainty::OutsideCoverage))
    );
    let day_end = 181 * 86400;
    let transitions = [Transition::new(day_end, -1)];
    let utc = LeapTable::new(0, day_end + 86400, &transitions).unwrap();
    let deleted = Reading::from(Instant::new(day_end - 1, 0).unwrap());
    assert_eq!(
        earliest(deleted, None, &utc),
        Err(BoundUncertainty::AccuracyUnstated)
    );
    assert_eq!(
        latest(deleted, None, &utc),
        Err(BoundUncertainty::AccuracyUnstated)
    );
    assert_eq!(
        latest(deleted, Some(Accuracy::new(0, 0, 0)), &utc),
        Err(BoundUncertainty::Utc(Uncertainty::NotInUtc))
    );
}

#[test]
fn widest_scalar_accuracy_does_not_wrap_or_claim_a_narrow_bound() {
    // Inspection retains the raw scalar values. Exact wide arithmetic cannot
    // narrow the interval by overflow, and is not itself schema acceptance.
    let accuracy = Accuracy::new(u64::MAX, u64::MAX, u64::MAX);
    assert_eq!(accuracy.seconds(), u64::MAX);
    assert_eq!(accuracy.millis(), u64::MAX);
    assert_eq!(accuracy.micros(), u64::MAX);
    assert_eq!(accuracy.nanos(), u128::from(u64::MAX) * 1_001_001_000);
    let utc = LeapTable::new(0, 100, &[]).unwrap();
    let recorded = Reading::from(Instant::new(50, 0).unwrap());
    assert_eq!(
        earliest(recorded, Some(accuracy), &utc),
        Err(BoundUncertainty::Utc(Uncertainty::OutOfRange))
    );
    assert_eq!(
        latest(recorded, Some(accuracy), &utc),
        Err(BoundUncertainty::Utc(Uncertainty::OutOfRange))
    );
}

#[test]
fn accuracy_reader_distinguishes_absent_zero_and_readable_bounds_faults() {
    // Present empty SEQUENCE is stated zero. Written millis=0/1000 remain
    // original values plus faults, not absence, clamped values or parser refusal.
    use std::sync::Arc;
    use structured_asn1::{
        Parser,
        memory::{Memory, MemoryAccount},
    };
    use structured_cms::{
        ErrorCode,
        timestamp::{AccuracyReading, Component},
    };
    let memory = Memory::new(Arc::new(MemoryAccount::new(65536, 65536)));
    let parser = Parser::ber(memory.clone());
    for (bytes, millis, fault_count) in [
        (&[0x30, 0][..], None, 0),
        (&[0x30, 3, 0x80, 1, 0][..], Some(0), 1),
        (&[0x30, 4, 0x80, 2, 3, 0xe8][..], Some(1000), 1),
    ] {
        let mut parent = parser.read(bytes, 400).unwrap();
        let element = parent.next_element().unwrap().unwrap();
        let accuracy = AccuracyReading::read(&parent, &element).unwrap();
        assert!(accuracy.component(Component::Seconds).is_none());
        assert_eq!(
            accuracy
                .component(Component::Millis)
                .map(|part| part.value().as_u64().unwrap()),
            millis
        );
        assert_eq!(accuracy.deviation().unwrap().millis(), millis.unwrap_or(0));
        assert_eq!(accuracy.faults().len(), fault_count);
        if fault_count != 0 {
            assert_eq!(accuracy.faults()[0].code(), ErrorCode::MalformedValue);
            assert_eq!(accuracy.faults()[0].offset(), 402);
        }
        drop(accuracy);
        assert_eq!(memory.used(), 0);
    }
}

#[test]
fn accuracy_reader_keeps_original_integer_views_and_diagnostic_ownership() {
    // Shared INTEGER recovery preserves the original TLV while the minimal
    // numeric view avoids repeat scans. Unknown/order faults retain exact offsets.
    use std::sync::Arc;
    use structured_asn1::{
        Parser,
        memory::{Memory, MemoryAccount},
    };
    use structured_cms::{
        ErrorCode,
        timestamp::{AccuracyReading, Component},
    };
    let memory = Memory::new(Arc::new(MemoryAccount::new(65536, 65536)));
    let parser = Parser::ber(memory.clone());
    let bytes = [0x30, 9, 0x81, 2, 0, 1, 5, 0, 2, 1, 3];
    let mut parent = parser.read(&bytes, 400).unwrap();
    let element = parent.next_element().unwrap().unwrap();
    let accuracy = AccuracyReading::read(&parent, &element).unwrap();
    let micros = accuracy.component(Component::Micros).unwrap();
    assert_eq!(micros.element().contents(), &[0, 1]);
    assert_eq!(micros.value().as_bytes(), &[1]);
    assert_eq!(micros.element().as_bytes().as_ptr(), bytes[2..].as_ptr());
    assert_eq!(accuracy.deviation().unwrap(), Accuracy::new(3, 0, 1));
    assert_eq!(
        accuracy
            .faults()
            .iter()
            .map(|fault| (fault.code(), fault.offset()))
            .collect::<Vec<_>>(),
        vec![
            (
                ErrorCode::Encoding(structured_asn1::ErrorCode::IntegerNotMinimal),
                402
            ),
            (ErrorCode::UnknownField, 406),
            (ErrorCode::FieldOrder, 408),
        ]
    );
    let faults = accuracy.into_faults();
    let held = memory.used();
    drop(parent);
    drop(parser);
    assert_eq!(memory.used(), held);
    assert!(held > 0);
    drop(faults);
    assert_eq!(memory.used(), 0);
}

#[test]
fn accuracy_reader_never_replaces_negative_or_too_wide_values_with_a_number() {
    // A unique numeric reading remains reportable even when deviation cannot
    // fit; neither u64::MAX nor zero can silently stand for that original value.
    use std::sync::Arc;
    use structured_asn1::{
        Parser,
        memory::{Memory, MemoryAccount},
    };
    use structured_cms::timestamp::{AccuracyReading, Component, DeviationReason};
    let parser = Parser::ber(Memory::new(Arc::new(MemoryAccount::new(65536, 65536))));
    for (bytes, expected) in [
        (&[0x30, 3, 2, 1, 0xff][..], DeviationReason::Negative),
        (
            &[0x30, 11, 2, 9, 1, 0, 0, 0, 0, 0, 0, 0, 0][..],
            DeviationReason::TooWide,
        ),
    ] {
        let mut parent = parser.read(bytes, 400).unwrap();
        let element = parent.next_element().unwrap().unwrap();
        let accuracy = AccuracyReading::read(&parent, &element).unwrap();
        let refusal = accuracy.deviation().unwrap_err();
        assert_eq!(refusal.component, Component::Seconds);
        assert_eq!(refusal.reason, expected);
        assert_eq!(refusal.span.start, 402);
        assert_eq!(
            accuracy
                .component(Component::Seconds)
                .unwrap()
                .element()
                .contents(),
            &bytes[4..]
        );
        assert_eq!(accuracy.faults().len(), 1);
    }
}

#[test]
fn accuracy_equal_numeric_duplicates_retain_encoding_faults() {
    // Different sign padding must not destroy a unique numeric reading.
    // Equality includes negative and wide integers, never a u64 sentinel.
    use std::sync::Arc;
    use structured_asn1::{
        Parser,
        memory::{Memory, MemoryAccount},
    };
    use structured_cms::{
        ErrorCode,
        timestamp::{AccuracyReading, Component},
    };
    for (tag, component, minimal, padded) in [
        (0x80, Component::Millis, &[1][..], &[0, 1][..]),
        (0x81, Component::Micros, &[0xff][..], &[0xff, 0xff][..]),
        (
            2,
            Component::Seconds,
            &[1, 0, 0, 0, 0, 0, 0, 0, 0][..],
            &[0, 1, 0, 0, 0, 0, 0, 0, 0, 0][..],
        ),
    ] {
        let mut body = vec![tag, u8::try_from(minimal.len()).unwrap()];
        body.extend_from_slice(minimal);
        let duplicate_at = 402 + u64::try_from(body.len()).unwrap();
        body.extend([tag, u8::try_from(padded.len()).unwrap()]);
        body.extend_from_slice(padded);
        let mut bytes = vec![0x30, u8::try_from(body.len()).unwrap()];
        bytes.extend(body);
        let account = Memory::new(Arc::new(MemoryAccount::new(65536, 65536)));
        let parser = Parser::ber(account.clone());
        let mut parent = parser.read(&bytes, 400).unwrap();
        let element = parent.next_element().unwrap().unwrap();
        let reading = AccuracyReading::read(&parent, &element).unwrap();
        assert_eq!(
            reading.faults().len(),
            if component == Component::Millis { 2 } else { 4 }
        );
        assert_eq!(
            reading.component(component).unwrap().element().contents(),
            minimal
        );
        assert!(
            reading
                .faults()
                .iter()
                .any(|f| f.code() == ErrorCode::DuplicateField && f.offset() == duplicate_at)
        );
        assert!(reading.faults().iter().any(|f| f.code()
            == ErrorCode::Encoding(structured_asn1::ErrorCode::IntegerNotMinimal)
            && f.offset() == duplicate_at));
        drop(reading);
        assert_eq!(account.used(), 0);
    }
}

#[test]
fn accuracy_duplicate_recovery_keeps_original_spelling_and_refuses_conflicts() {
    // Recovery preserves the first original encoding and refuses conflicting
    // numbers. Equal numbers with encoding faults are covered separately.
    use std::sync::Arc;
    use structured_asn1::{
        Parser,
        memory::{Memory, MemoryAccount},
    };
    use structured_cms::{ErrorCode, timestamp::AccuracyReading};
    let memory = Memory::new(Arc::new(MemoryAccount::new(65536, 65536)));
    let parser = Parser::ber(memory.clone());
    let identical = [0x30, 6, 2, 1, 3, 2, 1, 3];
    let mut parent = parser.read(&identical, 400).unwrap();
    let element = parent.next_element().unwrap().unwrap();
    let accuracy = AccuracyReading::read(&parent, &element).unwrap();
    assert_eq!(accuracy.deviation().unwrap(), Accuracy::new(3, 0, 0));
    assert_eq!(accuracy.faults()[0].code(), ErrorCode::DuplicateField);
    assert_eq!(accuracy.faults()[0].offset(), 405);
    drop(accuracy);
    for bytes in [
        &[0x30, 6, 2, 1, 3, 2, 1, 4][..],
        &[0x30, 7, 2, 1, 3, 2, 2, 0, 4][..],
        &[0x30, 6, 2, 1, 0xff, 2, 1, 0xfe][..],
        &[
            0x30, 22, 2, 9, 1, 0, 0, 0, 0, 0, 0, 0, 0, 2, 9, 1, 0, 0, 0, 0, 0, 0, 0, 1,
        ][..],
    ] {
        let mut parent = parser.read(bytes, 400).unwrap();
        let element = parent.next_element().unwrap().unwrap();
        let refusal = AccuracyReading::read(&parent, &element).unwrap_err();
        assert_eq!(refusal.code(), ErrorCode::DuplicateField);
        assert_eq!(refusal.offset(), if bytes.len() == 24 { 413 } else { 405 });
        assert_eq!(memory.used(), 0);
    }
}

#[test]
fn accuracy_numeric_recovery_obeys_work_and_diagnostic_capacity() {
    // Exact funded work succeeds; one fewer work unit or insufficient retained
    // diagnostic capacity is terminal, never success with missing faults.
    use std::sync::Arc;
    use structured_asn1::{
        Encoding, Limits, Parser,
        memory::{Memory, MemoryAccount},
    };
    use structured_cms::timestamp::AccuracyReading;
    let bytes = [0x30, 7, 0x80, 1, 1, 0x80, 2, 0, 1];
    let account = Memory::new(Arc::new(MemoryAccount::new(65536, 65536)));
    let parser = Parser::ber(account.clone());
    let mut parent = parser.read(&bytes, 400).unwrap();
    let element = parent.next_element().unwrap().unwrap();
    let reading = AccuracyReading::read(&parent, &element).unwrap();
    let work = account.work_used();
    let storage = account.used();
    assert!(work > 0 && storage > 0);
    assert_eq!(reading.faults().len(), 2);
    drop(reading);
    assert_eq!(account.used(), 0);
    for (local, aggregate, capacity, success) in [
        (work, work, 65536, true),
        (work - 1, 65536, 65536, false),
        (65536, work - 1, 65536, false),
        (65536, 65536, storage - 1, false),
    ] {
        let account = Memory::new(Arc::new(MemoryAccount::new(capacity, aggregate)));
        let parser = Parser::new(
            Encoding::Ber,
            Limits {
                max_work: u64::try_from(local).unwrap(),
                ..Limits::default()
            },
            account.clone(),
        );
        let mut parent = parser.read(&bytes, 400).unwrap();
        let element = parent.next_element().unwrap().unwrap();
        let result = AccuracyReading::read(&parent, &element);
        if success {
            assert_eq!(result.unwrap().faults().len(), 2);
        } else {
            let error = result.unwrap_err();
            assert!(error.is_resource());
            assert_eq!(error.offset(), 405);
            drop(error);
        }
        assert_eq!(account.used(), 0);
    }
}