|
| 1 | +package blog |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +func TestLogLineChecksum(t *testing.T) { |
| 9 | + t.Parallel() |
| 10 | + |
| 11 | + testCases := []struct { |
| 12 | + name string |
| 13 | + line string |
| 14 | + want string |
| 15 | + }{ |
| 16 | + { |
| 17 | + name: "empty", |
| 18 | + line: "", |
| 19 | + // CRC32 of the empty string is 0. |
| 20 | + want: "AAAAAA", |
| 21 | + }, |
| 22 | + { |
| 23 | + name: "simple", |
| 24 | + line: "hello, world", |
| 25 | + // Deterministic base64url(CRC32("hello, world")) |
| 26 | + want: "OnKr_w", |
| 27 | + }, |
| 28 | + } |
| 29 | + |
| 30 | + for _, tc := range testCases { |
| 31 | + t.Run(tc.name, func(t *testing.T) { |
| 32 | + t.Parallel() |
| 33 | + got := LogLineChecksum(tc.line) |
| 34 | + if got != tc.want { |
| 35 | + t.Errorf("LogLineChecksum(%q) = %q, want %q", tc.line, got, tc.want) |
| 36 | + } |
| 37 | + // Checksum should be deterministic across repeated calls. |
| 38 | + if again := LogLineChecksum(tc.line); again != got { |
| 39 | + t.Errorf("LogLineChecksum(%q) not deterministic: got %q then %q", tc.line, got, again) |
| 40 | + } |
| 41 | + }) |
| 42 | + } |
| 43 | + |
| 44 | + // Different inputs produce different checksums. |
| 45 | + if LogLineChecksum("foo") == LogLineChecksum("bar") { |
| 46 | + t.Errorf("LogLineChecksum(%q) and LogLineChecksum(%q) should differ", "foo", "bar") |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +func TestChecksumWriter(t *testing.T) { |
| 51 | + t.Parallel() |
| 52 | + |
| 53 | + testCases := []struct { |
| 54 | + name string |
| 55 | + in string |
| 56 | + want string |
| 57 | + }{ |
| 58 | + { |
| 59 | + name: "empty", |
| 60 | + in: "", |
| 61 | + want: "AAAAAA ", |
| 62 | + }, |
| 63 | + { |
| 64 | + name: "simple", |
| 65 | + in: "hello, world", |
| 66 | + want: "OnKr_w hello, world", |
| 67 | + }, |
| 68 | + { |
| 69 | + name: "complex", |
| 70 | + in: `level=INFO msg="he said \"hi\"\nthen" trail=actual |
| 71 | +`, |
| 72 | + want: `R3t8pA level=INFO msg="he said \"hi\"\nthen" trail=actual |
| 73 | +`, |
| 74 | + }, |
| 75 | + } |
| 76 | + |
| 77 | + for _, tc := range testCases { |
| 78 | + t.Run(tc.name, func(t *testing.T) { |
| 79 | + t.Parallel() |
| 80 | + |
| 81 | + var buf bytes.Buffer |
| 82 | + w := newChecksumWriter(&buf) |
| 83 | + n, err := w.Write([]byte(tc.in)) |
| 84 | + if err != nil { |
| 85 | + t.Fatalf("checksumWriter.Write returned error: %s", err) |
| 86 | + } |
| 87 | + if n != len(tc.in) { |
| 88 | + t.Errorf("checksumWriter.Write returned n=%d, want %d", n, len(tc.in)) |
| 89 | + } |
| 90 | + if got := buf.String(); got != tc.want { |
| 91 | + t.Errorf("checksumWriter wrote %q, want %q", got, tc.want) |
| 92 | + } |
| 93 | + }) |
| 94 | + } |
| 95 | + |
| 96 | + // Each call to Write produces its own checksum-prefixed line. |
| 97 | + t.Run("multiple writes", func(t *testing.T) { |
| 98 | + t.Parallel() |
| 99 | + |
| 100 | + var buf bytes.Buffer |
| 101 | + w := newChecksumWriter(&buf) |
| 102 | + for _, line := range []string{"foo", "bar"} { |
| 103 | + n, err := w.Write([]byte(line)) |
| 104 | + if err != nil { |
| 105 | + t.Fatalf("checksumWriter.Write(%q) returned error: %s", line, err) |
| 106 | + } |
| 107 | + if n != len(line) { |
| 108 | + t.Errorf("checksumWriter.Write(%q) returned n=%d, want %d", line, n, len(line)) |
| 109 | + } |
| 110 | + } |
| 111 | + want := LogLineChecksum("foo") + " foo" + LogLineChecksum("bar") + " bar" |
| 112 | + if got := buf.String(); got != want { |
| 113 | + t.Errorf("checksumWriter wrote %q, want %q", got, want) |
| 114 | + } |
| 115 | + }) |
| 116 | +} |
0 commit comments