| 1 | package signjar |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | |
| 8 | "github.com/stretchr/testify/assert" |
| 9 | "github.com/stretchr/testify/require" |
| 10 | ) |
| 11 | |
| 12 | func TestParse(t *testing.T) { |
| 13 | t.Parallel() |
| 14 | t.Run("Full", func(t *testing.T) { |
| 15 | const manifest = `Manifest-Version: 1.0 |
| 16 | Built-By: nobody |
| 17 | Long-Header-Line: 0123456789abcdef0123456789abcdef0123456789abcdef |
| 18 | 0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef |
| 19 | 0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef |
| 20 | |
| 21 | Name: foo |
| 22 | Ham: spam |
| 23 | Eggs: bacon |
| 24 | |
| 25 | ` |
| 26 | main := http.Header{ |
| 27 | "Manifest-Version": []string{"1.0"}, |
| 28 | "Built-By": []string{"nobody"}, |
| 29 | "Long-Header-Line": []string{ |
| 30 | "0123456789abcdef0123456789abcdef" + |
| 31 | "0123456789abcdef0123456789abcdef0123456789abcdef" + |
| 32 | "0123456789abcdef0123456789abcdef0123456789abcdef" + |
| 33 | "0123456789abcdef0123456789abcdef0123456789abcdef"}, |
| 34 | } |
| 35 | file := http.Header{ |
| 36 | "Name": []string{"foo"}, |
| 37 | "Ham": []string{"spam"}, |
| 38 | "Eggs": []string{"bacon"}, |
| 39 | } |
| 40 | expected := &FilesMap{ |
| 41 | Main: main, |
| 42 | Files: map[string]http.Header{"foo": file}, |
| 43 | Order: []string{"foo"}, |
| 44 | } |
| 45 | parsed, err := ParseManifest([]byte(manifest)) |
| 46 | require.NoError(t, err) |
| 47 | assert.Equal(t, expected, parsed) |
| 48 | crlfManifest := []byte(strings.ReplaceAll(manifest, "\n", "\r\n")) |
| 49 | parsed, err = ParseManifest(crlfManifest) |
| 50 | require.NoError(t, err) |
| 51 | assert.Equal(t, expected, parsed) |
| 52 | }) |
| 53 | t.Run("TruncatedMain", func(t *testing.T) { |
| 54 | const manifest = "Manifest-Version: 1.0\n" |
| 55 | expected := &FilesMap{ |
| 56 | Main: http.Header{ |
| 57 | "Manifest-Version": []string{"1.0"}, |
| 58 | }, |
| 59 | Order: []string{}, |
| 60 | Files: map[string]http.Header{}, |
| 61 | } |
| 62 | _, err := ParseManifest([]byte(manifest)) |
| 63 | require.ErrorIs(t, err, ErrManifestLineEndings) |
| 64 | parsed, malformed, err := parseManifest([]byte(manifest)) |
| 65 | require.NoError(t, err) |
| 66 | assert.True(t, malformed) |
| 67 | assert.Equal(t, expected, parsed) |
| 68 | }) |
| 69 | t.Run("TruncatedFile", func(t *testing.T) { |
| 70 | const manifest = "Manifest-Version: 1.0\n\nName: foo\n" |
| 71 | file := http.Header{ |
| 72 | "Name": []string{"foo"}, |
| 73 | } |
| 74 | expected := &FilesMap{ |
| 75 | Main: http.Header{ |
| 76 | "Manifest-Version": []string{"1.0"}, |
| 77 | }, |
| 78 | Order: []string{"foo"}, |
| 79 | Files: map[string]http.Header{"foo": file}, |
| 80 | } |
| 81 | _, err := ParseManifest([]byte(manifest)) |
| 82 | require.ErrorIs(t, err, ErrManifestLineEndings) |
| 83 | parsed, malformed, err := parseManifest([]byte(manifest)) |
| 84 | require.NoError(t, err) |
| 85 | assert.True(t, malformed) |
| 86 | assert.Equal(t, expected, parsed) |
| 87 | }) |
| 88 | t.Run("TrailingWhitespace", func(t *testing.T) { |
| 89 | const manifest = "Manifest-Version: 1.0\n\nName: foo\n\n\n" |
| 90 | file := http.Header{ |
| 91 | "Name": []string{"foo"}, |
| 92 | } |
| 93 | expected := &FilesMap{ |
| 94 | Main: http.Header{ |
| 95 | "Manifest-Version": []string{"1.0"}, |
| 96 | }, |
| 97 | Order: []string{"foo"}, |
| 98 | Files: map[string]http.Header{"foo": file}, |
| 99 | } |
| 100 | _, err := ParseManifest([]byte(manifest)) |
| 101 | require.ErrorIs(t, err, ErrManifestLineEndings) |
| 102 | parsed, malformed, err := parseManifest([]byte(manifest)) |
| 103 | require.NoError(t, err) |
| 104 | assert.True(t, malformed) |
| 105 | assert.Equal(t, expected, parsed) |
| 106 | }) |
| 107 | t.Run("InvalidNoName", func(t *testing.T) { |
| 108 | const manifest = "Manifest-Version: 1.0\n\nFoo: bar\n\n" |
| 109 | _, err := ParseManifest([]byte(manifest)) |
| 110 | require.Error(t, err) |
| 111 | }) |
| 112 | } |
| 113 | |
| 114 | func TestDump(t *testing.T) { |
| 115 | manifest := &FilesMap{ |
| 116 | Main: http.Header{ |
| 117 | "Manifest-Version": []string{"1.0"}, |
| 118 | "D": []string{"D"}, |
| 119 | "C": []string{"C"}, |
| 120 | "B": []string{"B"}, |
| 121 | "A": []string{"A"}, |
| 122 | "Long-Header": []string{strings.Repeat("0123456789abcdef", 10)}, |
| 123 | }, |
| 124 | Files: map[string]http.Header{ |
| 125 | "foo": {"Name": []string{"foo"}, "Foo": []string{"bar"}}, |
| 126 | "bar": {"Name": []string{"bar"}, "Foo": []string{"bar"}}, |
| 127 | }, |
| 128 | Order: []string{"bar", "foo"}, |
| 129 | } |
| 130 | result := string(manifest.Dump()) |
| 131 | expected := `Manifest-Version: 1.0 |
| 132 | A: A |
| 133 | B: B |
| 134 | C: C |
| 135 | D: D |
| 136 | Long-Header: 0123456789abcdef0123456789abcdef0123456789abcdef012345678 |
| 137 | 9abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcd |
| 138 | ef0123456789abcdef0123456789abcdef |
| 139 | |
| 140 | Name: bar |
| 141 | Foo: bar |
| 142 | |
| 143 | Name: foo |
| 144 | Foo: bar |
| 145 | |
| 146 | ` |
| 147 | expected = strings.ReplaceAll(expected, "\n", "\r\n") |
| 148 | assert.Equal(t, expected, result) |
| 149 | } |