TestNameDecoder verifies that a NameDecoder passed via ReaderOptions may rewrite Name and Comment before UTF-8 detection runs.
(t *testing.T)
| 1918 | // TestNameDecoder verifies that a NameDecoder passed via ReaderOptions may |
| 1919 | // rewrite Name and Comment before UTF-8 detection runs. |
| 1920 | func TestNameDecoder(t *testing.T) { |
| 1921 | rawName := "\x93\xfa\x96{\x8c\xea.txt" |
| 1922 | rawComment := "\x83R\x83\x81\x83\x93\x83g" |
| 1923 | decodedName := "日本語.txt" |
| 1924 | decodedComment := "コメント" |
| 1925 | |
| 1926 | var buf bytes.Buffer |
| 1927 | zw := NewWriter(&buf) |
| 1928 | if _, err := zw.CreateHeader(&FileHeader{ |
| 1929 | Name: rawName, |
| 1930 | Comment: rawComment, |
| 1931 | NonUTF8: true, |
| 1932 | Method: Store, |
| 1933 | }); err != nil { |
| 1934 | t.Fatal(err) |
| 1935 | } |
| 1936 | if err := zw.Close(); err != nil { |
| 1937 | t.Fatal(err) |
| 1938 | } |
| 1939 | |
| 1940 | var called int |
| 1941 | decoder := func(f *FileHeader) error { |
| 1942 | called++ |
| 1943 | if f.Name != rawName { |
| 1944 | t.Errorf("decoder got Name=%q, want %q", f.Name, rawName) |
| 1945 | } |
| 1946 | if f.Comment != rawComment { |
| 1947 | t.Errorf("decoder got Comment=%q, want %q", f.Comment, rawComment) |
| 1948 | } |
| 1949 | f.Name = decodedName |
| 1950 | f.Comment = decodedComment |
| 1951 | // Mark the entry as UTF-8 now that the decoder has rewritten it. |
| 1952 | f.Flags |= 0x800 |
| 1953 | return nil |
| 1954 | } |
| 1955 | zr, err := NewReaderWithOptions(bytes.NewReader(buf.Bytes()), int64(buf.Len()), ReaderOptions{NameDecoder: decoder}) |
| 1956 | if err != nil { |
| 1957 | t.Fatal(err) |
| 1958 | } |
| 1959 | if called != 1 { |
| 1960 | t.Fatalf("NameDecoder called %d times, want 1", called) |
| 1961 | } |
| 1962 | if len(zr.File) != 1 { |
| 1963 | t.Fatalf("File count = %d, want 1", len(zr.File)) |
| 1964 | } |
| 1965 | got := zr.File[0] |
| 1966 | if got.Name != decodedName { |
| 1967 | t.Errorf("Name = %q, want %q", got.Name, decodedName) |
| 1968 | } |
| 1969 | if got.Comment != decodedComment { |
| 1970 | t.Errorf("Comment = %q, want %q", got.Comment, decodedComment) |
| 1971 | } |
| 1972 | if got.NonUTF8 { |
| 1973 | t.Errorf("NonUTF8 = true, want false after decoder rewrite to UTF-8") |
| 1974 | } |
| 1975 | } |
| 1976 | |
| 1977 | // TestNameDecoderDefault verifies that NewReader preserves the existing |
nothing calls this directly
no test coverage detected
searching dependent graphs…