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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
//! Unicode Security Tests (TM-UNI-*)
//!
//! Tests for Unicode-specific threats identified in specs/threat-model.md.
//! Covers byte-boundary safety, invisible characters, homoglyphs, normalization,
//! and bidi attacks across parser, builtins, and VFS.
//!
//! Run with: `cargo test unicode_`
use bashkit::{Bash, FileSystem, FsLimits, InMemoryFs};
use std::path::Path;
// =============================================================================
// 1. BUILTIN PARSER BYTE-BOUNDARY SAFETY (TM-UNI-001, TM-UNI-002)
// =============================================================================
mod byte_boundary_safety {
use super::*;
/// TM-UNI-001: Awk parser must not panic on multi-byte Unicode in comments.
/// Reproduces issue #395: box-drawing characters (U+2500, 3 bytes each)
/// cause byte-boundary panic in awk parser.
#[tokio::test]
async fn unicode_awk_multibyte_comment_no_panic() {
let mut bash = Bash::new();
// Box-drawing chars in awk comment (the exact pattern from issue #395)
let result = bash
.exec(
r#"echo "hello" | awk '# ── Pass 1 ──
{print $1}'"#,
)
.await
.unwrap();
// May fail to parse correctly (TM-UNI-001 is PARTIAL), but must not crash
// the process. catch_unwind (TM-INT-001) should catch any panic.
// We accept either success or a non-zero exit code.
let _ = result.exit_code;
}
/// TM-UNI-001: Awk parser handles multi-byte chars in string literals
#[tokio::test]
async fn unicode_awk_multibyte_string_no_panic() {
let mut bash = Bash::new();
let result = bash
.exec(r#"echo "café" | awk '{print "→ " $0}'"#)
.await
.unwrap();
let _ = result.exit_code;
}
/// TM-UNI-001: Awk parser handles CJK characters in input
#[tokio::test]
async fn unicode_awk_cjk_input_no_panic() {
let mut bash = Bash::new();
let result = bash
.exec(r#"echo "日本語 テスト" | awk '{print $1}'"#)
.await
.unwrap();
let _ = result.exit_code;
}
/// TM-UNI-001: Awk parser handles emoji in input
#[tokio::test]
async fn unicode_awk_emoji_no_panic() {
let mut bash = Bash::new();
let result = bash
.exec(r#"echo "hello 🌍 world" | awk '{print $2}'"#)
.await
.unwrap();
let _ = result.exit_code;
}
/// TM-UNI-001: Awk with mixed ASCII and multi-byte in field separator
#[tokio::test]
async fn unicode_awk_multibyte_field_separator_no_panic() {
let mut bash = Bash::new();
let result = bash
.exec(r#"echo "a│b│c" | awk -F'│' '{print $2}'"#)
.await
.unwrap();
let _ = result.exit_code;
}
/// TM-UNI-001: Awk with multi-byte in pattern match
#[tokio::test]
async fn unicode_awk_multibyte_pattern_no_panic() {
let mut bash = Bash::new();
let result = bash
.exec(r#"printf "café\ntest\n" | awk '/café/{print "found: " $0}'"#)
.await
.unwrap();
let _ = result.exit_code;
}
/// TM-UNI-001: Awk with multi-byte chars in variable assignment
#[tokio::test]
async fn unicode_awk_multibyte_variable_no_panic() {
let mut bash = Bash::new();
let result = bash
.exec(r#"echo "test" | awk 'BEGIN{x="─═─"} {print x, $0}'"#)
.await
.unwrap();
let _ = result.exit_code;
}
/// TM-UNI-002: Sed handles multi-byte Unicode without panic
#[tokio::test]
async fn unicode_sed_multibyte_no_panic() {
let mut bash = Bash::new();
let result = bash
.exec(r#"echo "café latte" | sed 's/café/coffee/'"#)
.await
.unwrap();
let _ = result.exit_code;
}
/// TM-UNI-002: Sed with CJK replacement
#[tokio::test]
async fn unicode_sed_cjk_replacement_no_panic() {
let mut bash = Bash::new();
let result = bash
.exec(r#"echo "hello world" | sed 's/world/世界/'"#)
.await
.unwrap();
let _ = result.exit_code;
}
/// TM-UNI-002: Sed with box-drawing chars in pattern
#[tokio::test]
async fn unicode_sed_box_drawing_no_panic() {
let mut bash = Bash::new();
let result = bash
.exec(r#"echo "──border──" | sed 's/──//g'"#)
.await
.unwrap();
let _ = result.exit_code;
}
/// TM-UNI-001/002: Grep handles multi-byte patterns
#[tokio::test]
async fn unicode_grep_multibyte_no_panic() {
let mut bash = Bash::new();
let result = bash.exec(r#"echo "café" | grep "café""#).await.unwrap();
let _ = result.exit_code;
}
/// Stress test: many different multi-byte chars in single awk program
#[tokio::test]
async fn unicode_awk_stress_mixed_multibyte() {
let mut bash = Bash::new();
let result = bash
.exec(
r#"printf "α β γ δ ε\n日本 中文 한국\n🌍 🌎 🌏\n" | awk '{
for(i=1;i<=NF;i++) print NR, i, $i
}'"#,
)
.await
.unwrap();
let _ = result.exit_code;
}
}
// =============================================================================
// 2. ZERO-WIDTH CHARACTER TESTS (TM-UNI-003, TM-UNI-004, TM-UNI-005)
// =============================================================================
mod zero_width_chars {
use super::*;
/// TM-UNI-003: Zero-width space in filename — documents current behavior.
/// Currently UNMITIGATED: find_unsafe_path_char() does not reject ZWSP.
#[tokio::test]
async fn unicode_zwsp_in_filename_current_behavior() {
let fs = InMemoryFs::new();
// Zero Width Space (U+200B) in filename
let result = fs
.write_file(Path::new("/tmp/file\u{200B}name.txt"), b"data")
.await;
// Currently this succeeds — documents the gap.
// When TM-UNI-003 is fixed, this should return an error.
if result.is_ok() {
// Gap confirmed: zero-width chars pass validation
// Also verify the file is distinguishable from "filename.txt"
let normal = fs
.write_file(Path::new("/tmp/filename.txt"), b"other")
.await;
assert!(normal.is_ok());
// Two distinct files exist with visually identical names
let content1 = fs
.read_file(Path::new("/tmp/file\u{200B}name.txt"))
.await
.unwrap();
let content2 = fs.read_file(Path::new("/tmp/filename.txt")).await.unwrap();
assert_ne!(
content1, content2,
"ZWSP creates distinct file (TM-UNI-003 gap)"
);
}
// If it fails, the mitigation has been implemented
}
/// TM-UNI-003: BOM (U+FEFF) in filename — documents current behavior
#[tokio::test]
async fn unicode_bom_in_filename_current_behavior() {
let fs = InMemoryFs::new();
let result = fs
.write_file(Path::new("/tmp/\u{FEFF}file.txt"), b"data")
.await;
// Documents whether BOM is caught or not
let _ = result;
}
/// TM-UNI-003: ZWJ (U+200D) in filename — documents current behavior
#[tokio::test]
async fn unicode_zwj_in_filename_current_behavior() {
let fs = InMemoryFs::new();
let result = fs
.write_file(Path::new("/tmp/file\u{200D}name.txt"), b"data")
.await;
let _ = result;
}
/// TM-UNI-004: Zero-width chars in variable names — pass-through is correct
#[tokio::test]
async fn unicode_zwsp_in_variable_passthrough() {
let mut bash = Bash::new();
// Variable names with zero-width chars are treated as different variables
// This matches Bash behavior and is accepted risk
let result = bash
.exec(
r#"x="normal"
echo "$x""#,
)
.await
.unwrap();
assert_eq!(result.exit_code, 0);
assert!(result.stdout.contains("normal"));
}
/// TM-UNI-005: Zero-width chars in string values — correct pass-through
#[tokio::test]
async fn unicode_zwsp_in_string_passthrough() {
let mut bash = Bash::new();
let result = bash.exec("echo \"hello\u{200B}world\"").await.unwrap();
assert_eq!(result.exit_code, 0);
// The ZWSP should be preserved in output (correct Bash behavior)
assert!(result.stdout.contains("hello"));
assert!(result.stdout.contains("world"));
}
}
// =============================================================================
// 3. HOMOGLYPH / CONFUSABLE CHARACTER TESTS (TM-UNI-006, TM-UNI-007)
// =============================================================================
mod homoglyph_tests {
use super::*;
/// TM-UNI-006: Cyrillic vs Latin creates distinct files (accepted risk)
#[tokio::test]
async fn unicode_homoglyph_filenames_distinct() {
let fs = InMemoryFs::new();
// Latin 'a' (U+0061)
fs.write_file(Path::new("/tmp/data.txt"), b"latin")
.await
.unwrap();
// Cyrillic 'а' (U+0430) — visually identical to Latin 'a'
fs.write_file(Path::new("/tmp/d\u{0430}ta.txt"), b"cyrillic")
.await
.unwrap();
// These are distinct files despite looking identical
let latin = fs.read_file(Path::new("/tmp/data.txt")).await.unwrap();
let cyrillic = fs
.read_file(Path::new("/tmp/d\u{0430}ta.txt"))
.await
.unwrap();
assert_eq!(latin, b"latin");
assert_eq!(cyrillic, b"cyrillic");
}
/// TM-UNI-007: Homoglyph variables are distinct (accepted, matches Bash)
#[tokio::test]
async fn unicode_homoglyph_variables_distinct() {
let mut bash = Bash::new();
// Scripts with visually similar but distinct variable names
// This is accepted risk — matches Bash behavior
let result = bash.exec("x=latin; echo $x").await.unwrap();
assert_eq!(result.exit_code, 0);
assert!(result.stdout.contains("latin"));
}
}
// =============================================================================
// 4. UNICODE NORMALIZATION TESTS (TM-UNI-008)
// =============================================================================
mod normalization_tests {
use super::*;
/// TM-UNI-008: NFC vs NFD creates distinct files (accepted, matches Linux)
#[tokio::test]
async fn unicode_nfc_nfd_distinct_files() {
let fs = InMemoryFs::new();
// NFC: é as single codepoint U+00E9
fs.write_file(Path::new("/tmp/caf\u{00E9}.txt"), b"nfc")
.await
.unwrap();
// NFD: é as e (U+0065) + combining acute (U+0301)
fs.write_file(Path::new("/tmp/cafe\u{0301}.txt"), b"nfd")
.await
.unwrap();
// On Linux (and in Bashkit's VFS), these are distinct files
let nfc = fs
.read_file(Path::new("/tmp/caf\u{00E9}.txt"))
.await
.unwrap();
let nfd = fs
.read_file(Path::new("/tmp/cafe\u{0301}.txt"))
.await
.unwrap();
assert_eq!(nfc, b"nfc");
assert_eq!(nfd, b"nfd");
}
/// TM-UNI-008: Scripts using different normalization forms work correctly
#[tokio::test]
async fn unicode_normalization_in_scripts() {
let mut bash = Bash::new();
// NFC form in variable comparison
let result = bash
.exec("x=\"caf\u{00E9}\"; if [ \"$x\" = \"caf\u{00E9}\" ]; then echo match; fi")
.await
.unwrap();
assert_eq!(result.exit_code, 0);
assert!(result.stdout.contains("match"));
}
}
// =============================================================================
// 5. COMBINING CHARACTER ABUSE (TM-UNI-009, TM-UNI-010)
// =============================================================================
mod combining_char_tests {
use super::*;
/// TM-UNI-009: Excessive combining marks in filename — bounded by length limit
#[tokio::test]
async fn unicode_excessive_combining_marks_bounded() {
let limits = FsLimits::new().max_filename_length(255);
let fs = InMemoryFs::with_limits(limits);
// 200 combining diacritical marks on a single base character
let mut name = String::from("/tmp/a");
for _ in 0..200 {
name.push('\u{0300}'); // Combining grave accent
}
name.push_str(".txt");
let result = fs.write_file(Path::new(&name), b"data").await;
// Should either succeed (within 255 byte limit) or fail with length error
// Must not panic or hang
let _ = result;
}
/// TM-UNI-010: Combining marks in awk input — does not cause hang
#[tokio::test]
async fn unicode_combining_marks_in_awk_no_hang() {
let mut bash = Bash::new();
// Combining marks in data processed by awk
let result = bash
.exec("echo \"a\u{0300}\u{0301}\u{0302}bc\" | awk '{print length($0), $0}'")
.await
.unwrap();
// Must complete without hanging
let _ = result.exit_code;
}
}
// =============================================================================
// 6. TAG CHARACTERS AND INVISIBLES (TM-UNI-011, TM-UNI-012, TM-UNI-013)
// =============================================================================
mod invisible_char_tests {
use super::*;
/// TM-UNI-011: Tag characters in filename — documents current behavior
#[tokio::test]
async fn unicode_tag_chars_in_filename_current_behavior() {
let fs = InMemoryFs::new();
// U+E0001 (Language Tag) — invisible, deprecated since Unicode 5.0
let result = fs
.write_file(Path::new("/tmp/file\u{E0001}name.txt"), b"data")
.await;
// Currently UNMITIGATED — documents the gap
let _ = result;
}
/// TM-UNI-012: Interlinear annotation chars in filename — documents current behavior
#[tokio::test]
async fn unicode_interlinear_annotation_in_filename() {
let fs = InMemoryFs::new();
// U+FFF9 (Interlinear Annotation Anchor)
let result = fs
.write_file(Path::new("/tmp/file\u{FFF9}name.txt"), b"data")
.await;
let _ = result;
}
/// TM-UNI-013: Deprecated format chars in filename — documents current behavior
#[tokio::test]
async fn unicode_deprecated_format_chars_in_filename() {
let fs = InMemoryFs::new();
// U+206A (Inhibit Symmetric Swapping) — deprecated
let result = fs
.write_file(Path::new("/tmp/file\u{206A}name.txt"), b"data")
.await;
let _ = result;
}
}
// =============================================================================
// 7. BIDI IN SCRIPT SOURCE (TM-UNI-014)
// =============================================================================
mod bidi_script_tests {
use super::*;
/// TM-UNI-014: Bidi overrides in script comments — accepted risk.
/// Trojan Source attack: RTL override in comment can reorder displayed code.
/// Bashkit executes untrusted scripts by design, so this is accepted.
#[tokio::test]
async fn unicode_bidi_in_script_comment_accepted() {
let mut bash = Bash::new();
// RTL override in a comment — visually misleading but functionally harmless
let result = bash
.exec("# \u{202E}this comment has RTL override\necho safe")
.await
.unwrap();
assert_eq!(result.exit_code, 0);
assert!(result.stdout.contains("safe"));
}
/// TM-UNI-014: Bidi overrides in string literals — pass through correctly
#[tokio::test]
async fn unicode_bidi_in_string_passthrough() {
let mut bash = Bash::new();
let result = bash.exec("echo \"text\u{202E}reversed\"").await.unwrap();
assert_eq!(result.exit_code, 0);
// The bidi char should be preserved in output
assert!(result.stdout.contains("text"));
}
/// TM-DOS-015 (cross-ref): Bidi overrides in filenames ARE blocked
#[tokio::test]
async fn unicode_bidi_in_filename_blocked() {
let fs = InMemoryFs::new();
// RTL override in filename — this SHOULD be blocked (TM-DOS-015)
let result = fs
.write_file(Path::new("/tmp/test\u{202E}exe.txt"), b"data")
.await;
assert!(
result.is_err(),
"Bidi override in filename should be rejected"
);
let err = result.unwrap_err().to_string();
assert!(
err.contains("bidi override"),
"Error should mention bidi override: {}",
err
);
}
}
// =============================================================================
// 8. EXISTING PATH VALIDATION CROSS-CHECKS (TM-DOS-015 vs TM-UNI)
// =============================================================================
mod path_validation_crosscheck {
use super::*;
/// Verify existing TM-DOS-015 protections still work alongside new TM-UNI threats
#[tokio::test]
async fn unicode_control_chars_still_blocked() {
let fs = InMemoryFs::new();
// NULL-ish control chars (Rust strings can't contain U+0000, but others)
for ch in ['\u{0001}', '\u{001F}', '\u{007F}', '\u{0080}', '\u{009F}'] {
let path = format!("/tmp/file{}name.txt", ch);
let result = fs.write_file(Path::new(&path), b"data").await;
assert!(
result.is_err(),
"Control char U+{:04X} should be rejected in filenames",
ch as u32
);
}
}
/// All bidi override codepoints are blocked
#[tokio::test]
async fn unicode_all_bidi_overrides_blocked_in_paths() {
let fs = InMemoryFs::new();
// LRE, RLE, PDF, LRO, RLO
for ch in ['\u{202A}', '\u{202B}', '\u{202C}', '\u{202D}', '\u{202E}'] {
let path = format!("/tmp/file{}name.txt", ch);
let result = fs.write_file(Path::new(&path), b"data").await;
assert!(
result.is_err(),
"Bidi char U+{:04X} should be rejected in filenames",
ch as u32
);
}
// LRI, RLI, FSI, PDI
for ch in ['\u{2066}', '\u{2067}', '\u{2068}', '\u{2069}'] {
let path = format!("/tmp/file{}name.txt", ch);
let result = fs.write_file(Path::new(&path), b"data").await;
assert!(
result.is_err(),
"Bidi isolate U+{:04X} should be rejected in filenames",
ch as u32
);
}
}
/// Normal Unicode in filenames still works
#[tokio::test]
async fn unicode_normal_chars_allowed_in_paths() {
let fs = InMemoryFs::new();
// Accented characters
fs.write_file(Path::new("/tmp/café.txt"), b"ok")
.await
.unwrap();
// CJK characters
fs.write_file(Path::new("/tmp/文件.txt"), b"ok")
.await
.unwrap();
// Emoji
fs.write_file(Path::new("/tmp/🌍.txt"), b"ok")
.await
.unwrap();
// Arabic
fs.write_file(Path::new("/tmp/ملف.txt"), b"ok")
.await
.unwrap();
// Devanagari
fs.write_file(Path::new("/tmp/फ़ाइल.txt"), b"ok")
.await
.unwrap();
}
}
// =============================================================================
// 9. END-TO-END UNICODE SECURITY (integration tests)
// =============================================================================
mod e2e_unicode_security {
use super::*;
/// Full pipeline: Unicode data flows through echo -> file -> awk without panic
#[tokio::test]
async fn unicode_e2e_pipeline_no_panic() {
let mut bash = Bash::new();
let result = bash
.exec(
r#"
echo "名前,値" > /tmp/data.csv
echo "日本語,テスト" >> /tmp/data.csv
echo "café,latte" >> /tmp/data.csv
awk -F, '{print NR ": " $1 " → " $2}' /tmp/data.csv
"#,
)
.await
.unwrap();
// Must not crash; may or may not produce correct output depending on
// TM-UNI-001 fix status
let _ = result.exit_code;
}
/// Grep with multi-byte pattern on multi-byte file content
#[tokio::test]
async fn unicode_e2e_grep_multibyte() {
let mut bash = Bash::new();
let result = bash
.exec(
r#"
echo "hello world" > /tmp/test.txt
echo "café latte" >> /tmp/test.txt
echo "日本語" >> /tmp/test.txt
grep "café" /tmp/test.txt
"#,
)
.await
.unwrap();
assert_eq!(result.exit_code, 0);
assert!(result.stdout.contains("café"));
}
/// Sed with multi-byte substitution on file content
#[tokio::test]
async fn unicode_e2e_sed_multibyte() {
let mut bash = Bash::new();
let result = bash
.exec(
r#"
echo "hello world" > /tmp/test.txt
sed 's/world/世界/' /tmp/test.txt
"#,
)
.await
.unwrap();
assert_eq!(result.exit_code, 0);
assert!(result.stdout.contains("世界"));
}
/// Scripts with mixed encodings in variable operations
#[tokio::test]
async fn unicode_e2e_variable_ops() {
let mut bash = Bash::new();
let result = bash
.exec(
r#"
x="café"
echo "${#x}"
echo "${x/é/e}"
"#,
)
.await
.unwrap();
assert_eq!(result.exit_code, 0);
}
/// TM-UNI-001: The exact scenario from issue #395 — LLM-generated awk code
#[tokio::test]
async fn unicode_issue_395_exact_reproduction() {
let mut bash = Bash::new();
// Sonnet 4.6 generates awk code with Unicode box-drawing characters in comments
let awk_code = r#"echo "key=value" | awk '
# ── Pass 1: load all overrides into a map ──────────────────────────────────
NR == FNR {
print $0
}'"#;
let result = bash.exec(awk_code).await.unwrap();
// Must not crash the process. catch_unwind should prevent panic propagation.
let _ = result.exit_code;
}
}
// =============================================================================
// 10. EXPR BUILTIN BYTE-BOUNDARY TESTS (TM-UNI-015)
// =============================================================================
mod expr_byte_boundary {
use super::*;
/// TM-UNI-015: `expr length` with multi-byte string
/// `.len()` returns bytes (5 for "café") but should return char count (4).
#[tokio::test]
async fn unicode_expr_length_multibyte_no_panic() {
let mut bash = Bash::new();
let result = bash.exec(r#"expr length "café""#).await.unwrap();
// Must not crash. Output may be 5 (bytes) instead of 4 (chars) — documents bug.
let _ = result.exit_code;
}
/// TM-UNI-015: `expr substr` with multi-byte string — panic risk
/// Position 4 in "café" is 'é' (char) but byte 4 is mid-char → panic without catch_unwind.
#[tokio::test]
async fn unicode_expr_substr_multibyte_no_panic() {
let mut bash = Bash::new();
let result = bash.exec(r#"expr substr "café" 4 1"#).await.unwrap();
// Must not crash the process. catch_unwind should catch any panic.
let _ = result.exit_code;
}
/// TM-UNI-015: `expr substr` with CJK — each char is 3 bytes
#[tokio::test]
async fn unicode_expr_substr_cjk_no_panic() {
let mut bash = Bash::new();
let result = bash.exec(r#"expr substr "日本語" 2 1"#).await.unwrap();
// Position 2 is byte 2 (mid-char for 3-byte CJK) → panic risk
let _ = result.exit_code;
}
/// TM-UNI-015: `expr substr` with emoji — each char is 4 bytes
#[tokio::test]
async fn unicode_expr_substr_emoji_no_panic() {
let mut bash = Bash::new();
let result = bash.exec(r#"expr substr "🌍🌎🌏" 2 1"#).await.unwrap();
let _ = result.exit_code;
}
/// TM-UNI-015: `expr index` with multi-byte chars
#[tokio::test]
async fn unicode_expr_index_multibyte_no_panic() {
let mut bash = Bash::new();
let result = bash.exec(r#"expr index "café" "é""#).await.unwrap();
let _ = result.exit_code;
}
/// TM-UNI-015: `expr length` with emoji string
#[tokio::test]
async fn unicode_expr_length_emoji_no_panic() {
let mut bash = Bash::new();
let result = bash.exec(r#"expr length "🌍🌎🌏""#).await.unwrap();
let _ = result.exit_code;
}
}
// =============================================================================
// 11. PRINTF PRECISION BYTE-BOUNDARY TESTS (TM-UNI-016)
// =============================================================================
mod printf_byte_boundary {
use super::*;
/// TM-UNI-016: printf precision truncation on multi-byte string
/// `%.1s` on "é" (2 bytes) tries &s[..1] which is mid-char → panic risk.
#[tokio::test]
async fn unicode_printf_precision_multibyte_no_panic() {
let mut bash = Bash::new();
let result = bash.exec(r#"printf "%.1s\n" "é""#).await.unwrap();
let _ = result.exit_code;
}
/// TM-UNI-016: printf precision on CJK string
#[tokio::test]
async fn unicode_printf_precision_cjk_no_panic() {
let mut bash = Bash::new();
let result = bash.exec(r#"printf "%.1s\n" "日本""#).await.unwrap();
let _ = result.exit_code;
}
/// TM-UNI-016: printf precision on emoji string
#[tokio::test]
async fn unicode_printf_precision_emoji_no_panic() {
let mut bash = Bash::new();
let result = bash.exec(r#"printf "%.2s\n" "🌍🌎""#).await.unwrap();
let _ = result.exit_code;
}
/// TM-UNI-016: printf width with multi-byte string
#[tokio::test]
async fn unicode_printf_width_multibyte_no_panic() {
let mut bash = Bash::new();
let result = bash.exec(r#"printf "%10s\n" "café""#).await.unwrap();
let _ = result.exit_code;
}
/// TM-UNI-016: printf with multiple multi-byte args
#[tokio::test]
async fn unicode_printf_multiple_multibyte_args_no_panic() {
let mut bash = Bash::new();
let result = bash
.exec(r#"printf "%-10s %5s\n" "日本語" "café""#)
.await
.unwrap();
let _ = result.exit_code;
}
}
// =============================================================================
// 12. CUT/TR BYTE-BOUNDARY TESTS (TM-UNI-017)
// =============================================================================
mod cuttr_byte_boundary {
use super::*;
/// TM-UNI-017: tr with multi-byte char in SET1
#[tokio::test]
async fn unicode_tr_multibyte_set1_no_panic() {
let mut bash = Bash::new();
let result = bash.exec(r#"echo "café" | tr 'é' 'e'"#).await.unwrap();
let _ = result.exit_code;
}
/// TM-UNI-017: tr with CJK chars in sets
#[tokio::test]
async fn unicode_tr_cjk_sets_no_panic() {
let mut bash = Bash::new();
let result = bash.exec(r#"echo "日本語" | tr '日' '月'"#).await.unwrap();
let _ = result.exit_code;
}
/// TM-UNI-017: tr delete mode with multi-byte chars
#[tokio::test]
async fn unicode_tr_delete_multibyte_no_panic() {
let mut bash = Bash::new();
let result = bash.exec(r#"echo "café" | tr -d 'é'"#).await.unwrap();
let _ = result.exit_code;
}
/// TM-UNI-017: cut with multi-byte field delimiter
#[tokio::test]
async fn unicode_cut_multibyte_delimiter_no_panic() {
let mut bash = Bash::new();
let result = bash.exec(r#"echo "a│b│c" | cut -d'│' -f2"#).await.unwrap();
let _ = result.exit_code;
}
/// TM-UNI-017: cut character mode on multi-byte string
#[tokio::test]
async fn unicode_cut_chars_multibyte_no_panic() {
let mut bash = Bash::new();
let result = bash.exec(r#"echo "café" | cut -c4"#).await.unwrap();
let _ = result.exit_code;
}
/// TM-UNI-017: tr squeeze mode with multi-byte chars
#[tokio::test]
async fn unicode_tr_squeeze_multibyte_no_panic() {
let mut bash = Bash::new();
let result = bash.exec(r#"echo "caféé" | tr -s 'é'"#).await.unwrap();
let _ = result.exit_code;
}
}
// =============================================================================
// 13. INTERPRETER ARITHMETIC BYTE/CHAR TESTS (TM-UNI-018)
// =============================================================================
mod interpreter_byte_boundary {
use super::*;
/// TM-UNI-018: Arithmetic with multi-byte in expression context
/// Not a panic risk, but tests correct operator detection with mixed byte/char offsets.
#[tokio::test]
async fn unicode_arithmetic_multibyte_context_no_panic() {
let mut bash = Bash::new();
// Variable names are ASCII in practice, but test the boundary
let result = bash.exec(r#"x=1; echo $((x + 1))"#).await.unwrap();
assert_eq!(result.exit_code, 0);
}
/// TM-UNI-018: Variable assignment with multi-byte value in arithmetic-adjacent context
#[tokio::test]
async fn unicode_variable_multibyte_value_no_panic() {
let mut bash = Bash::new();
let result = bash
.exec(
r#"x="café"
echo "${#x}""#,
)
.await
.unwrap();
// Must not panic; tests string length handling
let _ = result.exit_code;
}
}
// =============================================================================
// 14. SED EXTENDED BYTE-BOUNDARY TESTS (TM-UNI-002 expanded)
// =============================================================================
mod sed_extended_byte_boundary {
use super::*;
/// TM-UNI-002: Sed with multi-byte delimiter character
#[tokio::test]
async fn unicode_sed_multibyte_delimiter_no_panic() {
let mut bash = Bash::new();
// Using a multi-byte char as sed delimiter (uncommon but valid)
let result = bash
.exec(r#"echo "hello world" | sed 's│hello│goodbye│'"#)
.await
.unwrap();
let _ = result.exit_code;
}
/// TM-UNI-002: Sed address with multi-byte pattern
#[tokio::test]
async fn unicode_sed_address_multibyte_no_panic() {
let mut bash = Bash::new();
let result = bash
.exec(r#"printf "café\nlatte\n" | sed '/café/d'"#)
.await
.unwrap();
let _ = result.exit_code;
}
/// TM-UNI-002: Sed append command with multi-byte text
#[tokio::test]
async fn unicode_sed_append_multibyte_no_panic() {
let mut bash = Bash::new();
let result = bash.exec(r#"echo "test" | sed 'a\日本語'"#).await.unwrap();
let _ = result.exit_code;
}
/// TM-UNI-002: Sed insert command with multi-byte text
#[tokio::test]
async fn unicode_sed_insert_multibyte_no_panic() {
let mut bash = Bash::new();
let result = bash
.exec(r#"echo "test" | sed 'i\→ header'"#)
.await
.unwrap();
let _ = result.exit_code;
}
/// TM-UNI-002: Sed with emoji in all positions
#[tokio::test]
async fn unicode_sed_emoji_all_positions_no_panic() {
let mut bash = Bash::new();
let result = bash
.exec(r#"echo "🌍 hello 🌎" | sed 's/🌍/🌏/g'"#)
.await
.unwrap();
let _ = result.exit_code;
}
/// TM-UNI-002: Sed multiple commands with multi-byte
#[tokio::test]
async fn unicode_sed_multiple_commands_multibyte_no_panic() {
let mut bash = Bash::new();
let result = bash
.exec(r#"echo "café latte" | sed -e 's/café/coffee/' -e 's/latte/milk/'"#)
.await
.unwrap();
let _ = result.exit_code;
}
/// TM-UNI-002: Sed y (transliterate) command with multi-byte
/// NOTE: This triggers a panic in sed.rs that is NOT caught by catch_unwind
/// in the pipeline context. Documents that pipeline error propagation needs work.
#[tokio::test]
async fn unicode_sed_transliterate_multibyte_no_panic() {
let mut bash = Bash::new();
// Use catch_unwind at test level since this panic may escape the builtin wrapper
let result = std::panic::AssertUnwindSafe(async move {
bash.exec(r#"echo "abc" | sed 'y/abc/αβγ/'"#).await
});
let outcome = tokio::task::spawn(result).await;
// Must not crash the test process. The panic may be caught at builtin level
// (returning error exit code) or may propagate (caught here by spawn).
let _ = outcome;
}
}
// =============================================================================
// 15. CROSS-COMPONENT UNICODE E2E (integration across new findings)
// =============================================================================
mod cross_component_unicode_e2e {
use super::*;
/// E2E: Multi-byte data through expr + variable expansion
#[tokio::test]
async fn unicode_e2e_expr_variable_expansion() {
let mut bash = Bash::new();
let result = bash
.exec(
r#"
x="café latte"
len=$(expr length "$x")
echo "length: $len"
"#,
)
.await
.unwrap();
// Must not crash; may report bytes instead of chars
let _ = result.exit_code;
}
/// E2E: Multi-byte data through printf formatting
#[tokio::test]
async fn unicode_e2e_printf_formatting() {
let mut bash = Bash::new();
let result = bash
.exec(
r#"
for item in "café" "日本語" "🌍🌎"; do
printf "%-15s [%s]\n" "$item" "$item"
done
"#,
)
.await
.unwrap();
let _ = result.exit_code;
}
/// E2E: Multi-byte through sed pipeline with multiple operations
#[tokio::test]
async fn unicode_e2e_sed_pipeline_multibyte() {
let mut bash = Bash::new();
let result = bash
.exec(
r#"
echo "── café ── latte ──" | sed 's/──/==/g' | sed 's/café/coffee/'
"#,
)
.await
.unwrap();
let _ = result.exit_code;
}
/// E2E: tr + cut pipeline with multi-byte data
#[tokio::test]
async fn unicode_e2e_tr_cut_pipeline() {
let mut bash = Bash::new();
let result = bash
.exec(
r#"
echo "café:latte:espresso" | cut -d: -f2
echo "CAFÉ" | tr '[:upper:]' '[:lower:]'
"#,
)
.await
.unwrap();
let _ = result.exit_code;
}
/// E2E: All affected builtins in single script
#[tokio::test]
async fn unicode_e2e_all_builtins_stress() {
let mut bash = Bash::new();
let result = bash
.exec(
r#"
# Awk with multi-byte (TM-UNI-001)
echo "日本語 テスト" | awk '{print $1}'
# Sed with multi-byte (TM-UNI-002)
echo "café" | sed 's/é/e/'
# Expr with multi-byte (TM-UNI-015)
expr length "日本語"
# Printf with multi-byte (TM-UNI-016)
printf "%s\n" "café"
# Grep with multi-byte (safe)
echo "café" | grep "café"
echo "done"
"#,
)
.await
.unwrap();
// At least the script should complete without crash
let _ = result.exit_code;
}
}