avm99963 | e13bace | 2020-12-25 01:35:25 +0100 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "crypto/sha256" |
| 6 | "encoding/hex" |
| 7 | "io" |
| 8 | "os" |
| 9 | "testing" |
| 10 | ) |
| 11 | |
| 12 | type Manifest struct { |
| 13 | name string |
| 14 | templateFile string |
| 15 | expectedFile string |
| 16 | dependencies []string |
| 17 | } |
| 18 | |
| 19 | func TestManifestConversions(t *testing.T) { |
| 20 | testManifests := []Manifest{ |
| 21 | { |
| 22 | name: "ManifestSmall", |
| 23 | templateFile: "testdata/manifest_small1.gjson", |
| 24 | expectedFile: "testdata/manifest_small1_expected.json", |
| 25 | dependencies: []string{"AAA", "BBB", "D"}, |
| 26 | }, |
| 27 | { |
| 28 | name: "ManifestFrozenChromium", |
| 29 | templateFile: "testdata/manifest_frozen.gjson", |
| 30 | expectedFile: "testdata/manifest_frozen_chromium_expected.json", |
| 31 | dependencies: []string{"CHROMIUM"}, |
| 32 | }, |
| 33 | { |
| 34 | name: "ManifestFrozenGecko", |
| 35 | templateFile: "testdata/manifest_frozen.gjson", |
| 36 | expectedFile: "testdata/manifest_frozen_gecko_expected.json", |
| 37 | dependencies: []string{"GECKO"}, |
| 38 | }, |
| 39 | } |
| 40 | |
| 41 | for _, m := range testManifests { |
| 42 | t.Run(m.name, func(t *testing.T) { |
| 43 | template, err := os.Open(m.templateFile) |
| 44 | if err != nil { |
| 45 | t.Fatalf("Can't open test file: %v", err) |
| 46 | } |
| 47 | defer template.Close() |
| 48 | |
| 49 | expected, err := os.Open(m.expectedFile) |
| 50 | if err != nil { |
| 51 | t.Fatalf("Can't open expected file: %v", err) |
| 52 | } |
| 53 | defer expected.Close() |
| 54 | |
| 55 | dest := bytes.NewBufferString("") |
| 56 | |
| 57 | WriteManifest(template, dest, m.dependencies) |
| 58 | if err != nil { |
| 59 | t.Fatal(err) |
| 60 | } |
| 61 | |
| 62 | h1 := sha256.New() |
| 63 | if _, err := io.Copy(h1, dest); err != nil { |
| 64 | t.Fatalf("Can't prepare sha256 sum of the result: %v", err) |
| 65 | } |
| 66 | |
| 67 | h2 := sha256.New() |
| 68 | if _, err := io.Copy(h2, expected); err != nil { |
| 69 | t.Fatalf("Can't prepare sha256 sum of the expected file: %v", err) |
| 70 | } |
| 71 | |
| 72 | sum1 := h1.Sum(nil) |
| 73 | sum2 := h2.Sum(nil) |
| 74 | if bytes.Compare(sum1, sum2) != 0 { |
| 75 | t.Fatalf("The expected manifest file (sha256: %v) differs from what we got (sha256: %v).", hex.EncodeToString(sum2), hex.EncodeToString(sum1)) |
| 76 | } |
| 77 | }) |
| 78 | } |
| 79 | } |