ash-core 2.3.6

DEPRECATED — Use `ashcore` instead. This crate is End of Life (EOL).
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
//! Cross-Platform Compatibility Tests for ASH Rust SDK
//!
//! Tests Unicode handling, number representation, string encoding,
//! and byte order consistency across platforms.

use ash_core::{
    ash_canonicalize_json, ash_canonicalize_query,
    ash_hash_body, ash_normalize_binding,
    ash_derive_client_secret, ash_build_proof,
};

// =========================================================================
// UNICODE HANDLING
// =========================================================================

mod unicode_handling {
    use super::*;

    #[test]
    fn test_utf8_basic_multilingual_plane() {
        // BMP characters (U+0000 to U+FFFF)
        let json = r#"{"latin":"Hello","greek":"Γεια","cyrillic":"Привет"}"#;
        let result = ash_canonicalize_json(json);
        assert!(result.is_ok());
    }

    #[test]
    fn test_utf8_supplementary_planes() {
        // SMP characters (U+10000 and above)
        let json = r#"{"emoji":"😀🎉","math":"𝕳𝖊𝖑𝖑𝖔"}"#;
        let result = ash_canonicalize_json(json);
        assert!(result.is_ok());
    }

    #[test]
    fn test_nfc_normalization_consistency() {
        // Same character, different representations
        let nfc = r#"{"name":"café"}"#;                    // é as single char (U+00E9)
        let nfd = r#"{"name":"cafe\u0301"}"#;              // e + combining acute (U+0065 U+0301)

        let result_nfc = ash_canonicalize_json(nfc).unwrap();
        let result_nfd = ash_canonicalize_json(nfd).unwrap();

        assert_eq!(result_nfc, result_nfd, "NFC and NFD should canonicalize to same output");
    }

    #[test]
    fn test_nfc_various_characters() {
        let test_cases = [
            ("ñ", "n\u{0303}"),       // n with tilde
            ("ö", "o\u{0308}"),       // o with diaeresis
            ("ç", "c\u{0327}"),       // c with cedilla
            ("å", "a\u{030A}"),       // a with ring above
        ];

        for (nfc, nfd) in test_cases {
            let json_nfc = format!(r#"{{"char":"{}"}}"#, nfc);
            let json_nfd = format!(r#"{{"char":"{}"}}"#, nfd);

            let result_nfc = ash_canonicalize_json(&json_nfc).unwrap();
            let result_nfd = ash_canonicalize_json(&json_nfd).unwrap();

            assert_eq!(result_nfc, result_nfd, "NFC/NFD mismatch for {}", nfc);
        }
    }

    #[test]
    fn test_emoji_handling() {
        let emojis = ["😀", "🎉", "💯", "🚀", "❤️", "👨‍👩‍👧‍👦"];

        for emoji in emojis {
            let json = format!(r#"{{"emoji":"{}"}}"#, emoji);
            let result = ash_canonicalize_json(&json);
            assert!(result.is_ok(), "Failed to canonicalize emoji: {}", emoji);
        }
    }

    #[test]
    fn test_emoji_zwj_sequences() {
        // Zero-width joiner sequences
        let zwj_emojis = [
            "👨‍👩‍👧",      // Family
            "👩‍💻",        // Woman technologist
            "🏳️‍🌈",       // Rainbow flag
        ];

        for emoji in zwj_emojis {
            let json = format!(r#"{{"emoji":"{}"}}"#, emoji);
            let result = ash_canonicalize_json(&json);
            assert!(result.is_ok(), "Failed ZWJ emoji: {}", emoji);
        }
    }

    #[test]
    fn test_rtl_text_handling() {
        // Right-to-left text
        let rtl_texts = [
            ("arabic", "العربية"),
            ("hebrew", "עברית"),
            ("persian", "فارسی"),
        ];

        for (lang, text) in rtl_texts {
            let json = format!(r#"{{"{}":{}}}"#, lang, serde_json::json!(text));
            let result = ash_canonicalize_json(&json);
            assert!(result.is_ok(), "Failed RTL text: {}", lang);
        }
    }

    #[test]
    fn test_bidi_text() {
        // Bidirectional text
        let json = r#"{"mixed":"Hello مرحبا World"}"#;
        let result = ash_canonicalize_json(json);
        assert!(result.is_ok());
    }

    #[test]
    fn test_cjk_characters() {
        let cjk = [
            ("chinese_simplified", "你好世界"),
            ("chinese_traditional", "你好世界"),
            ("japanese_hiragana", "こんにちは"),
            ("japanese_katakana", "コンニチハ"),
            ("japanese_kanji", "今日は"),
            ("korean", "안녕하세요"),
        ];

        for (name, text) in cjk {
            let json = format!(r#"{{"{}":{}}}"#, name, serde_json::json!(text));
            let result = ash_canonicalize_json(&json);
            assert!(result.is_ok(), "Failed CJK: {}", name);
        }
    }

    #[test]
    fn test_unicode_in_binding_path() {
        let result = ash_normalize_binding("GET", "/api/用户", "");
        assert!(result.is_ok());
    }

    #[test]
    fn test_unicode_in_query_value() {
        let result = ash_canonicalize_query("name=日本語");
        assert!(result.is_ok());
    }
}

// =========================================================================
// NUMBER REPRESENTATION
// =========================================================================

mod number_representation {
    use super::*;

    #[test]
    fn test_integer_zero() {
        let result = ash_canonicalize_json(r#"{"value":0}"#).unwrap();
        assert!(result.contains(":0}"));
    }

    #[test]
    fn test_negative_zero_normalized() {
        let result = ash_canonicalize_json(r#"{"value":-0}"#).unwrap();
        // -0 should become 0
        assert!(!result.contains("-0"));
        assert!(result.contains(":0}"));
    }

    #[test]
    fn test_positive_integers() {
        let integers = [1, 42, 100, 999, 10000, 1000000];
        for i in integers {
            let json = format!(r#"{{"value":{}}}"#, i);
            let result = ash_canonicalize_json(&json).unwrap();
            assert!(result.contains(&format!(":{}", i)));
        }
    }

    #[test]
    fn test_negative_integers() {
        let integers = [-1, -42, -100, -999, -10000];
        for i in integers {
            let json = format!(r#"{{"value":{}}}"#, i);
            let result = ash_canonicalize_json(&json).unwrap();
            assert!(result.contains(&format!(":{}", i)));
        }
    }

    #[test]
    fn test_large_integers() {
        // Safe integer range for JavaScript: -(2^53 - 1) to 2^53 - 1
        let large = [
            9007199254740991i64,   // MAX_SAFE_INTEGER
            -9007199254740991i64,  // MIN_SAFE_INTEGER
        ];

        for n in large {
            let json = format!(r#"{{"value":{}}}"#, n);
            let result = ash_canonicalize_json(&json);
            assert!(result.is_ok(), "Failed for large integer: {}", n);
        }
    }

    #[test]
    fn test_float_basic() {
        let result = ash_canonicalize_json(r#"{"value":3.14}"#).unwrap();
        assert!(result.contains("3.14"));
    }

    #[test]
    fn test_whole_float_becomes_integer() {
        let result = ash_canonicalize_json(r#"{"value":3.0}"#).unwrap();
        // 3.0 should become 3
        assert!(result.contains(":3}") || result.contains(":3,"));
    }

    #[test]
    fn test_float_precision() {
        let floats = [
            ("0.1", "0.1"),
            ("0.01", "0.01"),
            ("0.001", "0.001"),
            ("1.5", "1.5"),
            ("3.14159", "3.14159"),
        ];

        for (input, expected) in floats {
            let json = format!(r#"{{"value":{}}}"#, input);
            let result = ash_canonicalize_json(&json).unwrap();
            assert!(result.contains(expected), "Float {} not preserved", input);
        }
    }

    #[test]
    fn test_scientific_notation_normalized() {
        let json = r#"{"value":1e10}"#;
        let result = ash_canonicalize_json(json).unwrap();
        // Should be normalized to decimal form
        assert!(result.contains("10000000000") || result.contains("1e10") || result.contains("1E10"));
    }

    #[test]
    fn test_very_small_numbers() {
        let json = r#"{"value":0.0000001}"#;
        let result = ash_canonicalize_json(json);
        assert!(result.is_ok());
    }
}

// =========================================================================
// STRING ENCODING
// =========================================================================

mod string_encoding {
    use super::*;

    #[test]
    fn test_escape_backslash() {
        let json = r#"{"path":"C:\\Users\\file"}"#;
        let result = ash_canonicalize_json(json).unwrap();
        assert!(result.contains("\\\\"));
    }

    #[test]
    fn test_escape_quote() {
        let json = r#"{"text":"say \"hello\""}"#;
        let result = ash_canonicalize_json(json).unwrap();
        assert!(result.contains("\\\""));
    }

    #[test]
    fn test_escape_newline() {
        let json = "{\"text\":\"line1\\nline2\"}";
        let result = ash_canonicalize_json(json).unwrap();
        assert!(result.contains("\\n"));
    }

    #[test]
    fn test_escape_tab() {
        let json = "{\"text\":\"col1\\tcol2\"}";
        let result = ash_canonicalize_json(json).unwrap();
        assert!(result.contains("\\t"));
    }

    #[test]
    fn test_escape_carriage_return() {
        let json = "{\"text\":\"line1\\rline2\"}";
        let result = ash_canonicalize_json(json).unwrap();
        assert!(result.contains("\\r"));
    }

    #[test]
    fn test_escape_form_feed() {
        let json = "{\"text\":\"page1\\fpage2\"}";
        let result = ash_canonicalize_json(json).unwrap();
        assert!(result.contains("\\f"));
    }

    #[test]
    fn test_escape_backspace() {
        let json = "{\"text\":\"back\\bspace\"}";
        let result = ash_canonicalize_json(json).unwrap();
        assert!(result.contains("\\b"));
    }

    #[test]
    fn test_control_characters_escaped() {
        // Control characters U+0000 to U+001F should be escaped
        for c in 0u8..32 {
            let json = format!(r#"{{"text":"\u{:04x}"}}"#, c);
            let result = ash_canonicalize_json(&json);
            if let Ok(canonical) = result {
                // Should contain escape sequence
                assert!(
                    canonical.contains("\\u") ||
                    canonical.contains("\\n") ||
                    canonical.contains("\\r") ||
                    canonical.contains("\\t") ||
                    canonical.contains("\\b") ||
                    canonical.contains("\\f"),
                    "Control char U+{:04X} not escaped", c
                );
            }
        }
    }

    #[test]
    fn test_solidus_not_escaped() {
        // Forward slash (solidus) should NOT be escaped
        let json = r#"{"url":"https://example.com/path"}"#;
        let result = ash_canonicalize_json(json).unwrap();
        // Should NOT contain escaped solidus
        assert!(!result.contains("\\/"));
        assert!(result.contains("/"));
    }
}

// =========================================================================
// BYTE ORDER AND HEX ENCODING
// =========================================================================

mod byte_order {
    use super::*;

    #[test]
    fn test_hash_lowercase_hex() {
        let hash = ash_hash_body("test");
        // Should be all lowercase
        assert!(hash.chars().all(|c| !c.is_uppercase()));
        // Should be valid hex
        assert!(hash.chars().all(|c| c.is_ascii_hexdigit()));
    }

    #[test]
    fn test_hash_consistent_length() {
        let long_body = "x".repeat(1000);
        let bodies = ["", "a", "ab", "abc", "test", long_body.as_str()];
        for body in bodies {
            let hash = ash_hash_body(body);
            assert_eq!(hash.len(), 64, "Hash length should always be 64");
        }
    }

    #[test]
    fn test_proof_lowercase_hex() {
        let nonce = "a".repeat(64);
        let secret = ash_derive_client_secret(&nonce, "ctx", "POST|/|").unwrap();
        let proof = ash_build_proof(&secret, "1700000000", "POST|/|", &"b".repeat(64)).unwrap();

        // Should be all lowercase
        assert!(proof.chars().all(|c| !c.is_uppercase()));
        // Should be valid hex
        assert!(proof.chars().all(|c| c.is_ascii_hexdigit()));
        assert_eq!(proof.len(), 64);
    }

    #[test]
    fn test_nonce_case_insensitive_input() {
        let nonce_lower = "a".repeat(64);
        let nonce_upper = "A".repeat(64);
        let nonce_mixed = "aA".repeat(32);

        let secret_lower = ash_derive_client_secret(&nonce_lower, "ctx", "GET|/|");
        let secret_upper = ash_derive_client_secret(&nonce_upper, "ctx", "GET|/|");
        let secret_mixed = ash_derive_client_secret(&nonce_mixed, "ctx", "GET|/|");

        // All should succeed
        assert!(secret_lower.is_ok());
        assert!(secret_upper.is_ok());
        assert!(secret_mixed.is_ok());
    }

    #[test]
    fn test_deterministic_hash() {
        let body = "test content";
        let hash1 = ash_hash_body(body);
        let hash2 = ash_hash_body(body);
        assert_eq!(hash1, hash2, "Same input should produce same hash");
    }

    #[test]
    fn test_known_hash_value() {
        // Known SHA-256 hash of "test"
        let hash = ash_hash_body("test");
        assert_eq!(hash, "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08");
    }
}

// =========================================================================
// KEY SORTING
// =========================================================================

mod key_sorting {
    use super::*;

    #[test]
    fn test_alphabetical_key_sorting() {
        let json = r#"{"z":1,"a":2,"m":3}"#;
        let result = ash_canonicalize_json(json).unwrap();
        assert_eq!(result, r#"{"a":2,"m":3,"z":1}"#);
    }

    #[test]
    fn test_numeric_string_keys_lexicographic() {
        let json = r#"{"10":1,"2":2,"1":3}"#;
        let result = ash_canonicalize_json(json).unwrap();
        // Lexicographic order: "1" < "10" < "2"
        assert_eq!(result, r#"{"1":3,"10":1,"2":2}"#);
    }

    #[test]
    fn test_case_sensitive_sorting() {
        let json = r#"{"a":1,"A":2,"b":3,"B":4}"#;
        let result = ash_canonicalize_json(json).unwrap();
        // ASCII order: A (65) < B (66) < a (97) < b (98)
        assert_eq!(result, r#"{"A":2,"B":4,"a":1,"b":3}"#);
    }

    #[test]
    fn test_unicode_key_sorting() {
        let json = r#"{"é":1,"e":2,"ê":3}"#;
        let result = ash_canonicalize_json(json).unwrap();
        // Should be sorted by UTF-8 byte order
        assert!(!result.is_empty());  // Order depends on NFC normalization
    }

    #[test]
    fn test_nested_object_key_sorting() {
        let json = r#"{"outer":{"z":1,"a":2}}"#;
        let result = ash_canonicalize_json(json).unwrap();
        assert_eq!(result, r#"{"outer":{"a":2,"z":1}}"#);
    }

    #[test]
    fn test_deeply_nested_key_sorting() {
        let json = r#"{"l1":{"l2":{"z":1,"a":2}}}"#;
        let result = ash_canonicalize_json(json).unwrap();
        assert_eq!(result, r#"{"l1":{"l2":{"a":2,"z":1}}}"#);
    }
}

// =========================================================================
// ARRAY ORDER PRESERVATION
// =========================================================================

mod array_order {
    use super::*;

    #[test]
    fn test_array_order_preserved() {
        let json = r#"{"arr":[3,1,4,1,5,9,2,6]}"#;
        let result = ash_canonicalize_json(json).unwrap();
        assert_eq!(result, r#"{"arr":[3,1,4,1,5,9,2,6]}"#);
    }

    #[test]
    fn test_array_of_objects_preserved() {
        let json = r#"{"items":[{"id":2},{"id":1},{"id":3}]}"#;
        let result = ash_canonicalize_json(json).unwrap();
        // Order should be preserved, but keys within objects sorted
        let parsed: serde_json::Value = serde_json::from_str(&result).unwrap();
        let items = parsed["items"].as_array().unwrap();
        assert_eq!(items[0]["id"], 2);
        assert_eq!(items[1]["id"], 1);
        assert_eq!(items[2]["id"], 3);
    }

    #[test]
    fn test_nested_arrays_preserved() {
        let json = r#"{"matrix":[[3,1],[4,1],[5,9]]}"#;
        let result = ash_canonicalize_json(json).unwrap();
        assert_eq!(result, r#"{"matrix":[[3,1],[4,1],[5,9]]}"#);
    }
}