| 1 | package sftpfs |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | func TestDetectKind(t *testing.T) { |
| 9 | cases := []struct { |
| 10 | name string |
| 11 | in []byte |
| 12 | want Kind |
| 13 | }{ |
| 14 | {"ascii", []byte("hello world\n"), KindText}, |
| 15 | {"utf8", []byte("héllo 世界"), KindText}, |
| 16 | {"empty", []byte{}, KindText}, |
| 17 | {"nul", []byte("abc\x00def"), KindBinary}, |
| 18 | {"invalid-utf8", []byte{0xff, 0xfe, 0xfd, 0xfc}, KindBinary}, |
| 19 | {"long-text", []byte(strings.Repeat("a", 20000)), KindText}, |
| 20 | } |
| 21 | for _, c := range cases { |
| 22 | if got := DetectKind(c.in); got != c.want { |
| 23 | t.Errorf("DetectKind(%s) = %v, want %v", c.name, got, c.want) |
| 24 | } |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | func TestDetectKindTruncatedRune(t *testing.T) { |
| 29 | // A multi-byte rune split exactly at the sniff boundary must not be |
| 30 | // misread as binary. |
| 31 | body := append([]byte(strings.Repeat("a", sniffLen-1)), []byte("世")[0]) |
| 32 | if got := DetectKind(body); got != KindText { |
| 33 | t.Errorf("truncated trailing rune classified as %v, want text", got) |
| 34 | } |
| 35 | } |
| 36 |