iscc-lib 0.6.0 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Cargo.lock +2027 -0
- data/Cargo.toml +17 -6
- data/README.md +6 -3
- data/core/CLAUDE.md +177 -0
- data/core/Cargo.lock +959 -0
- data/core/Cargo.toml +131 -0
- data/core/README.md +84 -0
- data/core/benches/benchmarks.rs +294 -0
- data/core/benches/iai_benches.rs +171 -0
- data/core/src/cdc.rs +488 -0
- data/core/src/codec.rs +2074 -0
- data/core/src/conformance.rs +754 -0
- data/core/src/dct.rs +148 -0
- data/core/src/lib.rs +2693 -0
- data/core/src/minhash.rs +286 -0
- data/core/src/simhash.rs +356 -0
- data/core/src/streaming.rs +688 -0
- data/core/src/types.rs +116 -0
- data/core/src/utils.rs +361 -0
- data/core/src/wtahash.rs +389 -0
- data/core/src/xxh32.rs +134 -0
- data/core/tests/codec_clean.rs +247 -0
- data/core/tests/data.json +645 -0
- data/core/tests/test_algorithm_primitives.rs +303 -0
- data/core/tests/test_seq.rs +565 -0
- data/core/tests/test_strict_base32.rs +158 -0
- data/core/tests/test_text_utils.rs +157 -0
- data/lib/iscc_lib/version.rb +1 -1
- data/src/lib.rs +108 -25
- metadata +43 -4
|
@@ -0,0 +1,565 @@
|
|
|
1
|
+
//! Tests for the ISCC-SEQ codec (IEP-0020, C2PA soft binding values).
|
|
2
|
+
//!
|
|
3
|
+
//! Port of iscc-core 1.5.0 `tests/test_seq.py` (Python-only type/bytes-like
|
|
4
|
+
//! cases omitted), plus the IEP-0020 decoder vectors and guards that the typed
|
|
5
|
+
//! codec (`iscc_decode` / `iscc_decompose`) keeps rejecting what `decode_seq`
|
|
6
|
+
//! accepts.
|
|
7
|
+
|
|
8
|
+
use iscc_lib::codec::{
|
|
9
|
+
MainType, SEQ_MAINTYPES, SubType, Version, decode_base32, encode_base32, encode_header,
|
|
10
|
+
read_unit,
|
|
11
|
+
};
|
|
12
|
+
use iscc_lib::{decode_seq, encode_seq, gen_data_code_v0, gen_instance_code_v0};
|
|
13
|
+
|
|
14
|
+
const UNITS: [&str; 4] = [
|
|
15
|
+
"ISCC:AADVPDD4R6733NMPFH3D57VTQ4KVH6VHL74XIWGUT37V5ZG56NV3NSI",
|
|
16
|
+
"ISCC:EAD2RASIYU5IKLENP2OFI4CHZGRWYQCSW2WKX3Y6FJGOCXSYNYGLGBI",
|
|
17
|
+
"ISCC:GADQLNA7GRZESMRF2J7NZPNWGI3II2ST5YUN5SS6GVQ2ZQGJXPPYDNI",
|
|
18
|
+
"ISCC:IAD2KIVPJIWJZP3KQCESJL6SVT5APEZUPOJWM6HVTAXCF7OT3VFA4NY",
|
|
19
|
+
];
|
|
20
|
+
|
|
21
|
+
const HEX: &str = concat!(
|
|
22
|
+
"0007578c7c8fbfbdb58f29f63efeb3871553faa75ff97458d49eff5ee4ddf36bb6c92007a88248c53a852c8d",
|
|
23
|
+
"7e9c547047c9a36c4052b6acabef1e2a4ce15e586e0cb305300705b41f3472493225d27edcbdb63236846a53",
|
|
24
|
+
"ee28deca5e3561acc0c9bbdf81b54007a522af4a2c9cbf6a808924afd2acfa0793347b936678f5982e22fdd3",
|
|
25
|
+
"dd4a0e37",
|
|
26
|
+
);
|
|
27
|
+
|
|
28
|
+
const B64: &str = concat!(
|
|
29
|
+
"AAdXjHyPv721jyn2Pv6zhxVT+qdf+XRY1J7/XuTd82u2ySAHqIJIxTqFLI1+nFRwR8mjbEBStqyr7x4qTOFeWG4M",
|
|
30
|
+
"swUwBwW0HzRySTIl0n7cvbYyNoRqU+4o3speNWGswMm734G1QAelIq9KLJy/aoCJJK/SrPoHkzR7k2Z49ZguIv3T",
|
|
31
|
+
"3UoONw==",
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
const TEXT: &str = concat!(
|
|
35
|
+
"ISCC:AADVPDD4R6733NMPFH3D57VTQ4KVH6VHL74XIWGUT37V5ZG56NV3NSJAA6UIESGFHKCSZDL6TRKHAR6JUNWE",
|
|
36
|
+
"AUVWVSV66HRKJTQV4WDOBSZQKMAHAW2B6NDSJEZCLUT63S63MMRWQRVFH3RI33FF4NLBVTAMTO67QG2UAB5FEKXUU",
|
|
37
|
+
"LE4X5VIBCJEV7JKZ6QHSM2HXE3GPD2ZQLRC7XJ52SQOG4",
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
const SINGLE_HEX: &str = "2007a88248c53a852c8d7e9c547047c9a36c4052b6acabef1e2a4ce15e586e0cb305";
|
|
41
|
+
|
|
42
|
+
const UNITS_128: [&str; 2] = [
|
|
43
|
+
"ISCC:GABQLNA7GRZESMRF2J7NZPNWGI3II",
|
|
44
|
+
"ISCC:IAB2KIVPJIWJZP3KQCESJL6SVT5AO",
|
|
45
|
+
];
|
|
46
|
+
const HEX_128: &str = "300305b41f3472493225d27edcbdb63236844003a522af4a2c9cbf6a808924afd2acfa07";
|
|
47
|
+
|
|
48
|
+
const UNITS_MIXED: [&str; 3] = [
|
|
49
|
+
"ISCC:EEAYYNMFG5XOELK2",
|
|
50
|
+
"ISCC:EIBZ6B7T76PAAYP7H334X77754376",
|
|
51
|
+
"ISCC:GADQLNA7GRZESMRF2J7NZPNWGI3II2ST5YUN5SS6GVQ2ZQGJXPPYDNI",
|
|
52
|
+
];
|
|
53
|
+
const HEX_MIXED: &str = concat!(
|
|
54
|
+
"21018c3585376ee22d5a22039f07f3ff9e0061ff3ef7cbffffef37ff300705b41f3472493225d27edcbdb632",
|
|
55
|
+
"36846a53ee28deca5e3561acc0c9bbdf81b5",
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
const ISCC_CODE_HEX: &str = "5005578c7c8fbfbdb58fa88248c53a852c8d05b41f3472493225a522af4a2c9cbf6a";
|
|
59
|
+
const ISCC_ID_HEX: &str = "60100000000000000000";
|
|
60
|
+
|
|
61
|
+
/// Multicodec prefix of ISCC (`0xcc01`), used to build multiformat inputs.
|
|
62
|
+
const MC_PREFIX: [u8; 2] = [0xcc, 0x01];
|
|
63
|
+
|
|
64
|
+
/// Content-Code header bytes for unknown SubType/Version values with Length 7,
|
|
65
|
+
/// as produced by iscc-core 1.5.0 `encode_header(MT.CONTENT, st, vs, 7)`.
|
|
66
|
+
const UNKNOWN_ST_HEADERS: [(u32, u32, &str); 14] = [
|
|
67
|
+
(7, 0, "2707"),
|
|
68
|
+
(7, 1, "2717"),
|
|
69
|
+
(8, 0, "280070"),
|
|
70
|
+
(8, 1, "280170"),
|
|
71
|
+
(71, 0, "2bf070"),
|
|
72
|
+
(71, 1, "2bf170"),
|
|
73
|
+
(72, 0, "2c0007"),
|
|
74
|
+
(72, 1, "2c0017"),
|
|
75
|
+
(583, 0, "2dff07"),
|
|
76
|
+
(583, 1, "2dff17"),
|
|
77
|
+
(584, 0, "2e000070"),
|
|
78
|
+
(584, 1, "2e000170"),
|
|
79
|
+
(4679, 0, "2efff070"),
|
|
80
|
+
(4679, 1, "2efff170"),
|
|
81
|
+
];
|
|
82
|
+
|
|
83
|
+
/// Decode a hex test constant into bytes.
|
|
84
|
+
fn hx(s: &str) -> Vec<u8> {
|
|
85
|
+
hex::decode(s).unwrap()
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/// Return the owned unit strings of a `&[&str]` constant.
|
|
89
|
+
fn owned(units: &[&str]) -> Vec<String> {
|
|
90
|
+
units.iter().map(|u| u.to_string()).collect()
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/// Borrow a list of owned strings as `&str` for `encode_seq`.
|
|
94
|
+
fn refs(units: &[String]) -> Vec<&str> {
|
|
95
|
+
units.iter().map(String::as_str).collect()
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/// Assert `result` is an error whose message contains `needle`.
|
|
99
|
+
fn assert_err_contains<T: std::fmt::Debug>(result: iscc_lib::IsccResult<T>, needle: &str) {
|
|
100
|
+
match result {
|
|
101
|
+
Ok(v) => panic!("expected error containing {needle:?}, got Ok({v:?})"),
|
|
102
|
+
Err(e) => {
|
|
103
|
+
let msg = e.to_string();
|
|
104
|
+
assert!(msg.contains(needle), "error {msg:?} lacks {needle:?}");
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/// Typed header for a SEQ-permitted MainType with SubType 0, Version 0.
|
|
110
|
+
fn header(mt: MainType, ln: u32) -> Vec<u8> {
|
|
111
|
+
encode_header(mt, SubType::None, Version::V0, ln).unwrap()
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// ---- Positive cases ----
|
|
115
|
+
|
|
116
|
+
#[test]
|
|
117
|
+
fn test_iep_example() {
|
|
118
|
+
let value = encode_seq(&UNITS).unwrap();
|
|
119
|
+
let query = data_encoding::BASE64.encode(&value);
|
|
120
|
+
assert_eq!(decode_seq(&value).unwrap(), owned(&UNITS));
|
|
121
|
+
let back = data_encoding::BASE64.decode(query.as_bytes()).unwrap();
|
|
122
|
+
assert_eq!(decode_seq(&back).unwrap(), owned(&UNITS));
|
|
123
|
+
let text = format!("ISCC:{}", encode_base32(&value));
|
|
124
|
+
assert_eq!(text, TEXT);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
#[test]
|
|
128
|
+
fn test_encode_seq_vector_1() {
|
|
129
|
+
let value = encode_seq(&UNITS).unwrap();
|
|
130
|
+
assert_eq!(value.len(), 136);
|
|
131
|
+
assert_eq!(hex::encode(&value), HEX);
|
|
132
|
+
assert_eq!(data_encoding::BASE64.encode(&value), B64);
|
|
133
|
+
assert_eq!(encode_base32(&value).len(), 218);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
#[test]
|
|
137
|
+
fn test_decode_seq_vector_1() {
|
|
138
|
+
assert_eq!(decode_seq(&hx(HEX)).unwrap(), owned(&UNITS));
|
|
139
|
+
let raw = data_encoding::BASE64.decode(B64.as_bytes()).unwrap();
|
|
140
|
+
assert_eq!(decode_seq(&raw).unwrap(), owned(&UNITS));
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
#[test]
|
|
144
|
+
fn test_seq_single_unit() {
|
|
145
|
+
let unit = UNITS[1];
|
|
146
|
+
let value = encode_seq(&[unit]).unwrap();
|
|
147
|
+
assert_eq!(
|
|
148
|
+
value,
|
|
149
|
+
decode_base32(unit.strip_prefix("ISCC:").unwrap()).unwrap()
|
|
150
|
+
);
|
|
151
|
+
assert_eq!(hex::encode(&value), SINGLE_HEX);
|
|
152
|
+
assert_eq!(decode_seq(&value).unwrap(), vec![unit.to_string()]);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
#[test]
|
|
156
|
+
fn test_seq_two_128_bit_units() {
|
|
157
|
+
let value = encode_seq(&UNITS_128).unwrap();
|
|
158
|
+
assert_eq!(hex::encode(&value), HEX_128);
|
|
159
|
+
assert_eq!(decode_seq(&value).unwrap(), owned(&UNITS_128));
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
#[test]
|
|
163
|
+
fn test_seq_mixed_lengths_vector_2() {
|
|
164
|
+
let value = encode_seq(&UNITS_MIXED).unwrap();
|
|
165
|
+
assert_eq!(hex::encode(&value), HEX_MIXED);
|
|
166
|
+
assert_eq!(decode_seq(&value).unwrap(), owned(&UNITS_MIXED));
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
#[test]
|
|
170
|
+
fn test_encode_seq_tolerant_input() {
|
|
171
|
+
let bare: Vec<String> = UNITS
|
|
172
|
+
.iter()
|
|
173
|
+
.map(|u| u.strip_prefix("ISCC:").unwrap().to_string())
|
|
174
|
+
.collect();
|
|
175
|
+
let lower: Vec<String> = UNITS.iter().map(|u| u.to_lowercase()).collect();
|
|
176
|
+
let bare_lower: Vec<String> = bare.iter().map(|u| u.to_lowercase()).collect();
|
|
177
|
+
let hyphen: Vec<String> = UNITS
|
|
178
|
+
.iter()
|
|
179
|
+
.map(|u| format!("{}-{}", &u[..9], &u[9..]))
|
|
180
|
+
.collect();
|
|
181
|
+
for variant in [bare, lower, bare_lower, hyphen] {
|
|
182
|
+
assert_eq!(hex::encode(encode_seq(&refs(&variant)).unwrap()), HEX);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
#[test]
|
|
187
|
+
fn test_encode_seq_multibase_like_content() {
|
|
188
|
+
for unit in [
|
|
189
|
+
"ISCC:FAAAAAAAAAAA",
|
|
190
|
+
"iscc:faaa-aaaaaaaa",
|
|
191
|
+
"FAAA-AAAAAAAA",
|
|
192
|
+
"faaaaaaaaaaa",
|
|
193
|
+
"faaa-aaaaaaaa",
|
|
194
|
+
] {
|
|
195
|
+
assert_eq!(
|
|
196
|
+
hex::encode(encode_seq(&[unit]).unwrap()),
|
|
197
|
+
"28000000000000",
|
|
198
|
+
"{unit}"
|
|
199
|
+
);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
#[test]
|
|
204
|
+
fn test_encode_seq_multibase_like_meta() {
|
|
205
|
+
assert_eq!(
|
|
206
|
+
hex::encode(encode_seq(&["baaa-aaaaaaaa"]).unwrap()),
|
|
207
|
+
"08000000000000"
|
|
208
|
+
);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
#[test]
|
|
212
|
+
fn test_seq_all_lengths_roundtrip() {
|
|
213
|
+
for &mt in SEQ_MAINTYPES {
|
|
214
|
+
for ln in 0..8u32 {
|
|
215
|
+
let mut raw = header(mt, ln);
|
|
216
|
+
raw.extend((0..4 * (ln + 1)).map(|b| b as u8));
|
|
217
|
+
let units = decode_seq(&raw).unwrap();
|
|
218
|
+
assert_eq!(units.len(), 1);
|
|
219
|
+
assert_eq!(encode_seq(&refs(&units)).unwrap(), raw, "{mt:?} ln={ln}");
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
#[test]
|
|
225
|
+
fn test_seq_unknown_subtype_version_roundtrip() {
|
|
226
|
+
for (st, vs, header_hex) in UNKNOWN_ST_HEADERS {
|
|
227
|
+
let mut raw = hx(header_hex);
|
|
228
|
+
raw.extend(0u8..32);
|
|
229
|
+
let units = decode_seq(&raw).unwrap();
|
|
230
|
+
assert_eq!(units.len(), 1, "st={st} vs={vs}");
|
|
231
|
+
assert_eq!(encode_seq(&refs(&units)).unwrap(), raw, "st={st} vs={vs}");
|
|
232
|
+
let bare = units[0].strip_prefix("ISCC:").unwrap();
|
|
233
|
+
let hyphen = format!("{}-{}", &bare[..4], &bare[4..]);
|
|
234
|
+
for variant in [
|
|
235
|
+
bare.to_string(),
|
|
236
|
+
bare.to_lowercase(),
|
|
237
|
+
hyphen.clone(),
|
|
238
|
+
hyphen.to_lowercase(),
|
|
239
|
+
] {
|
|
240
|
+
assert_eq!(encode_seq(&[&variant]).unwrap(), raw, "{variant}");
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
#[test]
|
|
246
|
+
fn test_seq_unknown_subtype_canonical_strings() {
|
|
247
|
+
// Exact unit strings from iscc-core 1.5.0 `decode_seq` (header + bytes 0..32).
|
|
248
|
+
let cases = [
|
|
249
|
+
(
|
|
250
|
+
"2707",
|
|
251
|
+
"ISCC:E4DQAAICAMCAKBQHBAEQUCYMBUHA6EARCIJRIFIWC4MBSGQ3DQOR4HY",
|
|
252
|
+
),
|
|
253
|
+
(
|
|
254
|
+
"280070",
|
|
255
|
+
"ISCC:FAAHAAABAIBQIBIGA4EASCQLBQGQ4DYQCEJBGFAVCYLRQGI2DMOB2HQ7",
|
|
256
|
+
),
|
|
257
|
+
(
|
|
258
|
+
"2c0017",
|
|
259
|
+
"ISCC:FQABOAABAIBQIBIGA4EASCQLBQGQ4DYQCEJBGFAVCYLRQGI2DMOB2HQ7",
|
|
260
|
+
),
|
|
261
|
+
(
|
|
262
|
+
"2efff170",
|
|
263
|
+
"ISCC:F377C4AAAEBAGBAFAYDQQCIKBMGA2DQPCAIREEYUCULBOGAZDINRYHI6D4",
|
|
264
|
+
),
|
|
265
|
+
];
|
|
266
|
+
for (header_hex, expected) in cases {
|
|
267
|
+
let mut raw = hx(header_hex);
|
|
268
|
+
raw.extend(0u8..32);
|
|
269
|
+
assert_eq!(decode_seq(&raw).unwrap(), vec![expected.to_string()]);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
#[test]
|
|
274
|
+
fn test_decode_seq_undefined_subtype() {
|
|
275
|
+
assert_eq!(
|
|
276
|
+
decode_seq(&hx("270000000000")).unwrap(),
|
|
277
|
+
vec!["ISCC:E4AAAAAAAA".to_string()]
|
|
278
|
+
);
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
#[test]
|
|
282
|
+
fn test_seq_interop_with_iscc_decompose() {
|
|
283
|
+
let value = encode_seq(&UNITS).unwrap();
|
|
284
|
+
let decomposed = iscc_lib::iscc_decompose(&format!("ISCC:{}", encode_base32(&value))).unwrap();
|
|
285
|
+
let expected: Vec<String> = UNITS
|
|
286
|
+
.iter()
|
|
287
|
+
.map(|u| u.strip_prefix("ISCC:").unwrap().to_string())
|
|
288
|
+
.collect();
|
|
289
|
+
assert_eq!(decomposed, expected);
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
#[test]
|
|
293
|
+
fn test_seq_repeated_keys_keep_order() {
|
|
294
|
+
let data_64 = gen_data_code_v0(b"a", 64).unwrap().iscc;
|
|
295
|
+
let data_256 = gen_data_code_v0(b"a", 256).unwrap().iscc;
|
|
296
|
+
let inst_64 = gen_instance_code_v0(b"a", 64).unwrap().iscc;
|
|
297
|
+
let inst_256 = gen_instance_code_v0(b"a", 256).unwrap().iscc;
|
|
298
|
+
let units = vec![data_256, inst_64, data_64, inst_256];
|
|
299
|
+
assert_eq!(
|
|
300
|
+
decode_seq(&encode_seq(&refs(&units)).unwrap()).unwrap(),
|
|
301
|
+
units
|
|
302
|
+
);
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
// ---- decode_seq negative cases ----
|
|
306
|
+
|
|
307
|
+
#[test]
|
|
308
|
+
fn test_decode_seq_empty() {
|
|
309
|
+
assert_err_contains(decode_seq(b""), "Empty");
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
#[test]
|
|
313
|
+
fn test_decode_seq_incomplete_header() {
|
|
314
|
+
assert_err_contains(decode_seq(&hx("28")), "Malformed ISCC-HEADER at offset 0");
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
#[test]
|
|
318
|
+
fn test_decode_seq_nonzero_header_padding() {
|
|
319
|
+
assert_eq!(
|
|
320
|
+
decode_seq(&hx("28000000000000")).unwrap(),
|
|
321
|
+
vec!["ISCC:FAAAAAAAAAAA".to_string()]
|
|
322
|
+
);
|
|
323
|
+
assert_err_contains(
|
|
324
|
+
decode_seq(&hx("28000100000000")),
|
|
325
|
+
"Non-canonical ISCC-HEADER at offset 0",
|
|
326
|
+
);
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
#[test]
|
|
330
|
+
fn test_decode_seq_nonzero_header_padding_second_unit() {
|
|
331
|
+
let raw = hx(&format!("{SINGLE_HEX}28000100000000"));
|
|
332
|
+
let offset = hx(SINGLE_HEX).len();
|
|
333
|
+
assert_err_contains(
|
|
334
|
+
decode_seq(&raw),
|
|
335
|
+
&format!("Non-canonical ISCC-HEADER at offset {offset}"),
|
|
336
|
+
);
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
#[test]
|
|
340
|
+
fn test_decode_seq_length_above_7() {
|
|
341
|
+
for ln in [8u32, 9] {
|
|
342
|
+
let mut raw = header(MainType::Content, ln);
|
|
343
|
+
raw.extend(vec![0u8; 4 * (ln as usize + 1)]);
|
|
344
|
+
assert_err_contains(decode_seq(&raw), &format!("Unsupported Length field {ln}"));
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
#[test]
|
|
349
|
+
fn test_decode_seq_truncated() {
|
|
350
|
+
let raw = hx(HEX);
|
|
351
|
+
assert_err_contains(
|
|
352
|
+
decode_seq(&raw[..raw.len() - 1]),
|
|
353
|
+
"Truncated ISCC-UNIT at offset 102",
|
|
354
|
+
);
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
#[test]
|
|
358
|
+
fn test_decode_seq_trailing_byte() {
|
|
359
|
+
let mut raw = hx(HEX);
|
|
360
|
+
raw.push(0);
|
|
361
|
+
assert_err_contains(decode_seq(&raw), "offset 136");
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
#[test]
|
|
365
|
+
fn test_decode_seq_rejects_maintype() {
|
|
366
|
+
let mut flake = header(MainType::Flake, 1);
|
|
367
|
+
flake.extend([0u8; 8]);
|
|
368
|
+
for raw in [hx(ISCC_CODE_HEX), hx(ISCC_ID_HEX), flake] {
|
|
369
|
+
assert_err_contains(decode_seq(&raw), "not permitted");
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
// ---- encode_seq negative cases ----
|
|
374
|
+
|
|
375
|
+
#[test]
|
|
376
|
+
fn test_encode_seq_empty() {
|
|
377
|
+
assert_err_contains(encode_seq(&[]), "at least one");
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
#[test]
|
|
381
|
+
fn test_encode_seq_empty_unit() {
|
|
382
|
+
for unit in ["", " "] {
|
|
383
|
+
assert_err_contains(encode_seq(&[unit]), "must not be empty");
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
#[test]
|
|
388
|
+
fn test_encode_seq_rejects_maintype() {
|
|
389
|
+
for hex_code in [ISCC_CODE_HEX, ISCC_ID_HEX] {
|
|
390
|
+
let unit = format!("ISCC:{}", encode_base32(&hx(hex_code)));
|
|
391
|
+
assert_err_contains(encode_seq(&[&unit]), "not permitted");
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
#[test]
|
|
396
|
+
fn test_encode_seq_rejects_flake() {
|
|
397
|
+
// gen_flake_code_v0 is not ported; build a 64-bit FLAKE unit directly.
|
|
398
|
+
let mut raw = header(MainType::Flake, 1);
|
|
399
|
+
raw.extend([0x12u8; 8]);
|
|
400
|
+
let flake = format!("ISCC:{}", encode_base32(&raw));
|
|
401
|
+
assert_err_contains(encode_seq(&[&flake]), "not permitted");
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
#[test]
|
|
405
|
+
fn test_encode_seq_rejects_multiformat() {
|
|
406
|
+
let raw = decode_base32(UNITS[0].strip_prefix("ISCC:").unwrap()).unwrap();
|
|
407
|
+
let mut prefixed = MC_PREFIX.to_vec();
|
|
408
|
+
prefixed.extend(&raw);
|
|
409
|
+
let base16 = format!("f{}", hex::encode(&prefixed));
|
|
410
|
+
let base32 = format!("b{}", encode_base32(&prefixed).to_lowercase());
|
|
411
|
+
for mf in [base16, base32] {
|
|
412
|
+
assert!(encode_seq(&[&mf]).is_err(), "{mf}");
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
#[test]
|
|
417
|
+
fn test_encode_seq_trailing_data() {
|
|
418
|
+
let unit = encode_base32(&[0u8; 8]);
|
|
419
|
+
assert_err_contains(encode_seq(&[&unit]), "Trailing data");
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
#[test]
|
|
423
|
+
fn test_encode_seq_multiple_units_in_one_string() {
|
|
424
|
+
assert_err_contains(encode_seq(&[TEXT]), "Trailing data");
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
// ---- read_unit ----
|
|
428
|
+
|
|
429
|
+
#[test]
|
|
430
|
+
fn test_read_unit_default_offset() {
|
|
431
|
+
let raw = hx(SINGLE_HEX);
|
|
432
|
+
assert_eq!(read_unit(&raw, 0).unwrap(), raw.len());
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
#[test]
|
|
436
|
+
fn test_read_unit_sequential_offsets() {
|
|
437
|
+
let raw = hx(HEX);
|
|
438
|
+
let mut offsets = vec![0usize];
|
|
439
|
+
while *offsets.last().unwrap() < raw.len() {
|
|
440
|
+
offsets.push(read_unit(&raw, *offsets.last().unwrap()).unwrap());
|
|
441
|
+
}
|
|
442
|
+
assert_eq!(offsets, vec![0, 34, 68, 102, 136]);
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
#[test]
|
|
446
|
+
fn test_read_unit_offsets_128() {
|
|
447
|
+
let raw = hx(HEX_128);
|
|
448
|
+
assert_eq!(read_unit(&raw, 0).unwrap(), 18);
|
|
449
|
+
assert_eq!(read_unit(&raw, 18).unwrap(), raw.len());
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
#[test]
|
|
453
|
+
fn test_read_unit_truncated_at_offset() {
|
|
454
|
+
let raw = hx(HEX_128);
|
|
455
|
+
assert_err_contains(
|
|
456
|
+
read_unit(&raw[..raw.len() - 1], 18),
|
|
457
|
+
"Truncated ISCC-UNIT at offset 18",
|
|
458
|
+
);
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
#[test]
|
|
462
|
+
fn test_read_unit_offset_out_of_range() {
|
|
463
|
+
// Rust slices panic where Python is lenient: these must be errors, not panics.
|
|
464
|
+
let raw = hx("300000000000");
|
|
465
|
+
for offset in [raw.len(), raw.len() + 1, 100, usize::MAX] {
|
|
466
|
+
assert_err_contains(
|
|
467
|
+
read_unit(&raw, offset),
|
|
468
|
+
&format!("Malformed ISCC-HEADER at offset {offset}"),
|
|
469
|
+
);
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
#[test]
|
|
474
|
+
fn test_seq_maintypes() {
|
|
475
|
+
assert_eq!(
|
|
476
|
+
SEQ_MAINTYPES,
|
|
477
|
+
&[
|
|
478
|
+
MainType::Meta,
|
|
479
|
+
MainType::Semantic,
|
|
480
|
+
MainType::Content,
|
|
481
|
+
MainType::Data,
|
|
482
|
+
MainType::Instance
|
|
483
|
+
]
|
|
484
|
+
);
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
// ---- IEP-0020 decoder vectors (spec "Verified when") ----
|
|
488
|
+
|
|
489
|
+
#[test]
|
|
490
|
+
fn test_iep0020_accepts_subtype_7() {
|
|
491
|
+
assert_eq!(
|
|
492
|
+
decode_seq(&hx("270000000000")).unwrap(),
|
|
493
|
+
vec!["ISCC:E4AAAAAAAA".to_string()]
|
|
494
|
+
);
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
#[test]
|
|
498
|
+
fn test_iep0020_accepts_data_version_1_then_0() {
|
|
499
|
+
assert_eq!(
|
|
500
|
+
decode_seq(&hx("301000000000300000000000")).unwrap(),
|
|
501
|
+
vec!["ISCC:GAIAAAAAAA".to_string(), "ISCC:GAAAAAAAAA".to_string()]
|
|
502
|
+
);
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
#[test]
|
|
506
|
+
fn test_iep0020_accepts_four_unit_vector() {
|
|
507
|
+
assert_eq!(decode_seq(&hx(HEX)).unwrap(), owned(&UNITS));
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
#[test]
|
|
511
|
+
fn test_iep0020_encode_multibase_like_lowercase() {
|
|
512
|
+
assert_eq!(
|
|
513
|
+
encode_seq(&["faaa-aaaaaaaa"]).unwrap(),
|
|
514
|
+
hx("28000000000000")
|
|
515
|
+
);
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
#[test]
|
|
519
|
+
fn test_iep0020_table2_rejects() {
|
|
520
|
+
let long_length = format!("200800{}", "00".repeat(36));
|
|
521
|
+
let cases: [(&str, &str); 7] = [
|
|
522
|
+
("", "Empty ISCC-SEQ"),
|
|
523
|
+
("28000100000000", "Non-canonical ISCC-HEADER at offset 0"),
|
|
524
|
+
(&long_length, "Unsupported Length field 8 at offset 0"),
|
|
525
|
+
("3000000000", "Truncated ISCC-UNIT at offset 0"),
|
|
526
|
+
("30000000000000", "Malformed ISCC-HEADER at offset 6"),
|
|
527
|
+
(ISCC_CODE_HEX, "MainType 5 is not permitted"),
|
|
528
|
+
("60100000000000000000", "MainType 6 is not permitted"),
|
|
529
|
+
];
|
|
530
|
+
for (input, needle) in cases {
|
|
531
|
+
assert_err_contains(decode_seq(&hx(input)), needle);
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
#[test]
|
|
536
|
+
fn test_roundtrip_conformance_decode_vectors() {
|
|
537
|
+
let data: serde_json::Value =
|
|
538
|
+
serde_json::from_str(include_str!("data.json")).expect("valid data.json");
|
|
539
|
+
let cases = data["decode_seq"].as_object().expect("decode_seq section");
|
|
540
|
+
assert_eq!(cases.len(), 4);
|
|
541
|
+
for (name, tc) in cases {
|
|
542
|
+
let input = tc["inputs"][0].as_str().unwrap();
|
|
543
|
+
let raw = hx(input.strip_prefix("bytes:").unwrap());
|
|
544
|
+
let units = decode_seq(&raw).unwrap();
|
|
545
|
+
assert_eq!(encode_seq(&refs(&units)).unwrap(), raw, "{name}");
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
// ---- Typed codec keeps rejecting what decode_seq accepts ----
|
|
550
|
+
|
|
551
|
+
#[test]
|
|
552
|
+
fn test_typed_codec_rejects_data_version_1() {
|
|
553
|
+
assert!(decode_seq(&hx("301000000000")).is_ok());
|
|
554
|
+
assert!(iscc_lib::iscc_decode("ISCC:GAIAAAAAAA").is_err());
|
|
555
|
+
assert!(iscc_lib::iscc_decompose("ISCC:GAIAAAAAAA").is_err());
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
#[test]
|
|
559
|
+
fn test_typed_codec_rejects_unknown_subtype() {
|
|
560
|
+
let mut raw = hx("280070"); // Content, SubType 8, Version 0, Length 7
|
|
561
|
+
raw.extend(0u8..32);
|
|
562
|
+
let units = decode_seq(&raw).unwrap();
|
|
563
|
+
assert!(iscc_lib::iscc_decode(&units[0]).is_err());
|
|
564
|
+
assert!(iscc_lib::iscc_decompose(&units[0]).is_err());
|
|
565
|
+
}
|