|
| 1 | +// Copyright 2023 SLSA Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package verify |
| 16 | + |
| 17 | +import ( |
| 18 | + "os" |
| 19 | + "path/filepath" |
| 20 | + "strings" |
| 21 | + "testing" |
| 22 | +) |
| 23 | + |
| 24 | +func TestParseDigest(t *testing.T) { |
| 25 | + tests := []struct { |
| 26 | + name string |
| 27 | + input string |
| 28 | + want string |
| 29 | + wantErr bool |
| 30 | + }{ |
| 31 | + { |
| 32 | + name: "valid sha256", |
| 33 | + input: "sha256:" + strings.Repeat("a", 64), |
| 34 | + want: strings.Repeat("a", 64), |
| 35 | + wantErr: false, |
| 36 | + }, |
| 37 | + { |
| 38 | + name: "valid sha512", |
| 39 | + input: "sha512:" + strings.Repeat("b", 128), |
| 40 | + want: strings.Repeat("b", 128), |
| 41 | + wantErr: false, |
| 42 | + }, |
| 43 | + { |
| 44 | + name: "valid sha256 with mixed case hex", |
| 45 | + input: "sha256:" + strings.Repeat("aB", 32), |
| 46 | + want: strings.Repeat("aB", 32), |
| 47 | + wantErr: false, |
| 48 | + }, |
| 49 | + { |
| 50 | + name: "sha256 wrong length - too short", |
| 51 | + input: "sha256:abc", |
| 52 | + want: "", |
| 53 | + wantErr: true, |
| 54 | + }, |
| 55 | + { |
| 56 | + name: "sha256 wrong length - too long", |
| 57 | + input: "sha256:" + strings.Repeat("a", 65), |
| 58 | + want: "", |
| 59 | + wantErr: true, |
| 60 | + }, |
| 61 | + { |
| 62 | + name: "sha512 wrong length", |
| 63 | + input: "sha512:" + strings.Repeat("a", 64), |
| 64 | + want: "", |
| 65 | + wantErr: true, |
| 66 | + }, |
| 67 | + { |
| 68 | + name: "invalid hex characters", |
| 69 | + input: "sha256:" + strings.Repeat("g", 64), |
| 70 | + want: "", |
| 71 | + wantErr: true, |
| 72 | + }, |
| 73 | + { |
| 74 | + name: "unsupported algorithm sha384", |
| 75 | + input: "sha384:" + strings.Repeat("a", 96), |
| 76 | + want: "", |
| 77 | + wantErr: true, |
| 78 | + }, |
| 79 | + { |
| 80 | + name: "unsupported algorithm md5", |
| 81 | + input: "md5:" + strings.Repeat("a", 32), |
| 82 | + want: "", |
| 83 | + wantErr: true, |
| 84 | + }, |
| 85 | + { |
| 86 | + name: "missing colon", |
| 87 | + input: "sha256" + strings.Repeat("a", 64), |
| 88 | + want: "", |
| 89 | + wantErr: true, |
| 90 | + }, |
| 91 | + { |
| 92 | + name: "empty hash value", |
| 93 | + input: "sha256:", |
| 94 | + want: "", |
| 95 | + wantErr: true, |
| 96 | + }, |
| 97 | + } |
| 98 | + |
| 99 | + for _, tt := range tests { |
| 100 | + t.Run(tt.name, func(t *testing.T) { |
| 101 | + got, err := parseDigest(tt.input) |
| 102 | + if (err != nil) != tt.wantErr { |
| 103 | + t.Errorf("parseDigest() error = %v, wantErr %v", err, tt.wantErr) |
| 104 | + return |
| 105 | + } |
| 106 | + if got != tt.want { |
| 107 | + t.Errorf("parseDigest() = %v, want %v", got, tt.want) |
| 108 | + } |
| 109 | + }) |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +func TestComputeArtifactHash(t *testing.T) { |
| 114 | + // Create a temporary file for testing file hash computation |
| 115 | + tmpDir := t.TempDir() |
| 116 | + tmpFile := filepath.Join(tmpDir, "test.txt") |
| 117 | + content := []byte("hello world") |
| 118 | + if err := os.WriteFile(tmpFile, content, 0o600); err != nil { |
| 119 | + t.Fatalf("failed to create temp file: %v", err) |
| 120 | + } |
| 121 | + // SHA256 of "hello world" is b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9 |
| 122 | + |
| 123 | + tests := []struct { |
| 124 | + name string |
| 125 | + input string |
| 126 | + want string |
| 127 | + wantErr bool |
| 128 | + }{ |
| 129 | + { |
| 130 | + name: "sha256 digest", |
| 131 | + input: "sha256:" + strings.Repeat("a", 64), |
| 132 | + want: strings.Repeat("a", 64), |
| 133 | + wantErr: false, |
| 134 | + }, |
| 135 | + { |
| 136 | + name: "sha512 digest", |
| 137 | + input: "sha512:" + strings.Repeat("b", 128), |
| 138 | + want: strings.Repeat("b", 128), |
| 139 | + wantErr: false, |
| 140 | + }, |
| 141 | + { |
| 142 | + name: "file path", |
| 143 | + input: tmpFile, |
| 144 | + want: "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9", |
| 145 | + wantErr: false, |
| 146 | + }, |
| 147 | + { |
| 148 | + name: "invalid digest format", |
| 149 | + input: "sha256:invalid", |
| 150 | + want: "", |
| 151 | + wantErr: true, |
| 152 | + }, |
| 153 | + { |
| 154 | + name: "non-existent file", |
| 155 | + input: "/non/existent/file.txt", |
| 156 | + want: "", |
| 157 | + wantErr: true, |
| 158 | + }, |
| 159 | + } |
| 160 | + |
| 161 | + for _, tt := range tests { |
| 162 | + t.Run(tt.name, func(t *testing.T) { |
| 163 | + got, err := computeArtifactHash(tt.input) |
| 164 | + if (err != nil) != tt.wantErr { |
| 165 | + t.Errorf("computeArtifactHash() error = %v, wantErr %v", err, tt.wantErr) |
| 166 | + return |
| 167 | + } |
| 168 | + if got != tt.want { |
| 169 | + t.Errorf("computeArtifactHash() = %v, want %v", got, tt.want) |
| 170 | + } |
| 171 | + }) |
| 172 | + } |
| 173 | +} |
0 commit comments