|
1 | 1 | package acquisition |
2 | 2 |
|
3 | | -import "testing" |
| 3 | +import ( |
| 4 | + "archive/zip" |
| 5 | + "bytes" |
| 6 | + "crypto/sha256" |
| 7 | + "encoding/csv" |
| 8 | + "encoding/hex" |
| 9 | + "io" |
| 10 | + "strings" |
| 11 | + "testing" |
| 12 | +) |
| 13 | + |
| 14 | +func TestCreateHashListTracksPlaintextZipEntries(t *testing.T) { |
| 15 | + var archive bytes.Buffer |
| 16 | + ezw := &EncryptedZipWriter{ |
| 17 | + zipWriter: zip.NewWriter(&archive), |
| 18 | + } |
| 19 | + |
| 20 | + if err := ezw.CreateFileFromString("first.txt", "first content"); err != nil { |
| 21 | + t.Fatalf("CreateFileFromString() error = %v", err) |
| 22 | + } |
| 23 | + |
| 24 | + writer, err := ezw.CreateFile("stream.bin") |
| 25 | + if err != nil { |
| 26 | + t.Fatalf("CreateFile() error = %v", err) |
| 27 | + } |
| 28 | + if _, err := writer.Write([]byte("streamed ")); err != nil { |
| 29 | + t.Fatalf("Write() error = %v", err) |
| 30 | + } |
| 31 | + if _, err := writer.Write([]byte("content")); err != nil { |
| 32 | + t.Fatalf("Write() error = %v", err) |
| 33 | + } |
| 34 | + |
| 35 | + if err := ezw.CreateHashList(); err != nil { |
| 36 | + t.Fatalf("CreateHashList() error = %v", err) |
| 37 | + } |
| 38 | + if err := ezw.zipWriter.Close(); err != nil { |
| 39 | + t.Fatalf("zip Close() error = %v", err) |
| 40 | + } |
| 41 | + |
| 42 | + reader, err := zip.NewReader(bytes.NewReader(archive.Bytes()), int64(archive.Len())) |
| 43 | + if err != nil { |
| 44 | + t.Fatalf("zip.NewReader() error = %v", err) |
| 45 | + } |
| 46 | + |
| 47 | + files := make(map[string]string) |
| 48 | + for _, file := range reader.File { |
| 49 | + readCloser, err := file.Open() |
| 50 | + if err != nil { |
| 51 | + t.Fatalf("Open(%q) error = %v", file.Name, err) |
| 52 | + } |
| 53 | + content, err := io.ReadAll(readCloser) |
| 54 | + readCloser.Close() |
| 55 | + if err != nil { |
| 56 | + t.Fatalf("ReadAll(%q) error = %v", file.Name, err) |
| 57 | + } |
| 58 | + files[file.Name] = string(content) |
| 59 | + } |
| 60 | + |
| 61 | + records, err := csv.NewReader(strings.NewReader(files["hashes.csv"])).ReadAll() |
| 62 | + if err != nil { |
| 63 | + t.Fatalf("ReadAll(hashes.csv) error = %v", err) |
| 64 | + } |
| 65 | + |
| 66 | + gotHashes := make(map[string]string) |
| 67 | + for _, record := range records { |
| 68 | + if len(record) != 2 { |
| 69 | + t.Fatalf("hash record has %d fields, want 2: %#v", len(record), record) |
| 70 | + } |
| 71 | + gotHashes[record[0]] = record[1] |
| 72 | + } |
| 73 | + |
| 74 | + wantHashes := map[string]string{ |
| 75 | + "first.txt": sha256Hex("first content"), |
| 76 | + "stream.bin": sha256Hex("streamed content"), |
| 77 | + } |
| 78 | + if len(gotHashes) != len(wantHashes) { |
| 79 | + t.Fatalf("got %d hash records, want %d: %#v", len(gotHashes), len(wantHashes), gotHashes) |
| 80 | + } |
| 81 | + for name, wantHash := range wantHashes { |
| 82 | + if gotHashes[name] != wantHash { |
| 83 | + t.Fatalf("hash for %q = %q, want %q", name, gotHashes[name], wantHash) |
| 84 | + } |
| 85 | + } |
| 86 | + if _, ok := gotHashes["hashes.csv"]; ok { |
| 87 | + t.Fatal("hashes.csv should not include a hash record for itself") |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +func sha256Hex(content string) string { |
| 92 | + sum := sha256.Sum256([]byte(content)) |
| 93 | + return hex.EncodeToString(sum[:]) |
| 94 | +} |
4 | 95 |
|
5 | 96 | func TestValidateZipEntryName(t *testing.T) { |
6 | 97 | tests := []struct { |
|
0 commit comments