| 1 | package builtin |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "encoding/binary" |
| 7 | "encoding/json" |
| 8 | "os" |
| 9 | "path/filepath" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | "unicode/utf16" |
| 13 | ) |
| 14 | |
| 15 | // TestReadFileBOMlessUTF16LE proves a Windows-style UTF-16 source file with no |
| 16 | // BOM is decoded rather than rejected as binary on the NUL-byte check. |
| 17 | func TestReadFileBOMlessUTF16LE(t *testing.T) { |
| 18 | dir := t.TempDir() |
| 19 | path := filepath.Join(dir, "CameraManagerLogLoader.cpp") |
| 20 | src := "// Created by 69431 on 2024/12/31\n#include \"alson/CameraManagerLogLoader.h\"\n" |
| 21 | var b bytes.Buffer |
| 22 | for _, u := range utf16.Encode([]rune(src)) { |
| 23 | _ = binary.Write(&b, binary.LittleEndian, u) |
| 24 | } |
| 25 | if err := os.WriteFile(path, b.Bytes(), 0o644); err != nil { |
| 26 | t.Fatal(err) |
| 27 | } |
| 28 | args, _ := json.Marshal(map[string]any{"path": path}) |
| 29 | out, err := readFile{}.Execute(context.Background(), args) |
| 30 | if err != nil { |
| 31 | t.Fatalf("read rejected BOM-less UTF-16: %v", err) |
| 32 | } |
| 33 | if !strings.Contains(out, "Created by 69431") || !strings.Contains(out, "#include") { |
| 34 | t.Fatalf("UTF-16 content not decoded:\n%s", out) |
| 35 | } |
| 36 | if strings.Contains(out, "\x00") { |
| 37 | t.Fatal("NUL bytes leaked into decoded output") |
| 38 | } |
| 39 | } |
| 40 |