| 1 | package update |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | func TestWindowsPayloadManifestRoundTripBindsVersionAndExactMembers(t *testing.T) { |
| 9 | hashes := make(map[string]string, len(windowsPayloadFileNames)) |
| 10 | for _, name := range windowsPayloadFileNames { |
| 11 | hashes[name] = WindowsPayloadSHA256([]byte(name)) |
| 12 | } |
| 13 | b, err := EncodeWindowsPayloadManifest("v2.0.0", hashes) |
| 14 | if err != nil { |
| 15 | t.Fatal(err) |
| 16 | } |
| 17 | decoded, err := DecodeWindowsPayloadManifest(b, "v2.0.0") |
| 18 | if err != nil { |
| 19 | t.Fatal(err) |
| 20 | } |
| 21 | if len(decoded) != len(hashes) { |
| 22 | t.Fatalf("decoded members = %d, want %d", len(decoded), len(hashes)) |
| 23 | } |
| 24 | if _, err := DecodeWindowsPayloadManifest(b, "v2.0.1"); err == nil { |
| 25 | t.Fatal("payload manifest authorized a different release version") |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | func TestWindowsPayloadManifestRejectsUnknownOrMissingMembers(t *testing.T) { |
| 30 | hashes := make(map[string]string, len(windowsPayloadFileNames)) |
| 31 | for _, name := range windowsPayloadFileNames { |
| 32 | hashes[name] = strings.Repeat("a", 64) |
| 33 | } |
| 34 | delete(hashes, windowsPayloadFileNames[0]) |
| 35 | if _, err := EncodeWindowsPayloadManifest("v2", hashes); err == nil { |
| 36 | t.Fatal("incomplete payload manifest encoded successfully") |
| 37 | } |
| 38 | hashes[windowsPayloadFileNames[0]] = strings.Repeat("a", 64) |
| 39 | hashes["other.exe"] = strings.Repeat("b", 64) |
| 40 | if _, err := EncodeWindowsPayloadManifest("v2", hashes); err == nil { |
| 41 | t.Fatal("payload manifest with an extra member encoded successfully") |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | func TestWindowsPayloadManifestRejectsDuplicateAndTrailingData(t *testing.T) { |
| 46 | hashes := make(map[string]string, len(windowsPayloadFileNames)) |
| 47 | for _, name := range windowsPayloadFileNames { |
| 48 | hashes[name] = strings.Repeat("a", 64) |
| 49 | } |
| 50 | b, err := EncodeWindowsPayloadManifest("v2", hashes) |
| 51 | if err != nil { |
| 52 | t.Fatal(err) |
| 53 | } |
| 54 | duplicate := strings.Replace( |
| 55 | string(b), |
| 56 | `"files": [`, |
| 57 | `"files": [{"name":"reasonix-desktop.exe","sha256":"`+strings.Repeat("a", 64)+`"},`, |
| 58 | 1, |
| 59 | ) |
| 60 | if _, err := DecodeWindowsPayloadManifest([]byte(duplicate), "v2"); err == nil { |
| 61 | t.Fatal("duplicate payload member was accepted") |
| 62 | } |
| 63 | if _, err := DecodeWindowsPayloadManifest(append(b, []byte(`{}`)...), "v2"); err == nil { |
| 64 | t.Fatal("trailing JSON value was accepted") |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | func TestWindowsPayloadManifestRejectsNonCanonicalIdentity(t *testing.T) { |
| 69 | hashes := make(map[string]string, len(windowsPayloadFileNames)) |
| 70 | for _, name := range windowsPayloadFileNames { |
| 71 | hashes[name] = strings.Repeat("a", 64) |
| 72 | } |
| 73 | b, err := EncodeWindowsPayloadManifest("v2", hashes) |
| 74 | if err != nil { |
| 75 | t.Fatal(err) |
| 76 | } |
| 77 | for _, changed := range [][]byte{ |
| 78 | []byte(strings.Replace(string(b), `"version": "v2"`, `"version": " v2 "`, 1)), |
| 79 | []byte(strings.Replace(string(b), `"name": "reasonix-desktop.exe"`, `"name": "Reasonix-Desktop.exe"`, 1)), |
| 80 | []byte(strings.Replace(string(b), strings.Repeat("a", 64), strings.Repeat("A", 64), 1)), |
| 81 | } { |
| 82 | if _, err := DecodeWindowsPayloadManifest(changed, "v2"); err == nil { |
| 83 | t.Fatalf("non-canonical payload manifest was accepted: %s", changed) |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 |