|
| 1 | +package image |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/opencontainers/go-digest" |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | +) |
| 10 | + |
| 11 | +func TestValidateBlobAgainstDigest(t *testing.T) { |
| 12 | + testBlob := []byte("test data") |
| 13 | + |
| 14 | + tests := []struct { |
| 15 | + name string |
| 16 | + blob []byte |
| 17 | + expectedDigest digest.Digest |
| 18 | + expectError bool |
| 19 | + errorContains string |
| 20 | + }{ |
| 21 | + { |
| 22 | + name: "empty digest", |
| 23 | + blob: testBlob, |
| 24 | + expectedDigest: "", |
| 25 | + expectError: true, |
| 26 | + errorContains: "expected digest is empty", |
| 27 | + }, |
| 28 | + { |
| 29 | + name: "invalid digest format - no algorithm", |
| 30 | + blob: testBlob, |
| 31 | + expectedDigest: "invalidsyntax", |
| 32 | + expectError: true, |
| 33 | + errorContains: "invalid digest format", |
| 34 | + }, |
| 35 | + { |
| 36 | + name: "invalid digest format - sha256 prefix w/ malformed hex", |
| 37 | + blob: testBlob, |
| 38 | + expectedDigest: "sha256:notahexstring!@#", |
| 39 | + expectError: true, |
| 40 | + errorContains: "invalid digest format", |
| 41 | + }, |
| 42 | + { |
| 43 | + name: "invalid digest format - sha512 prefix w/ malformed hex", |
| 44 | + blob: testBlob, |
| 45 | + expectedDigest: "sha512:notahexstring!@#", |
| 46 | + expectError: true, |
| 47 | + errorContains: "invalid digest format", |
| 48 | + }, |
| 49 | + { |
| 50 | + name: "invalid digest format - unknown algorithm", |
| 51 | + blob: testBlob, |
| 52 | + expectedDigest: "unknown-algo:0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef", |
| 53 | + expectError: true, |
| 54 | + errorContains: "invalid digest format", |
| 55 | + }, |
| 56 | + { |
| 57 | + name: "digest mismatch", |
| 58 | + blob: testBlob, |
| 59 | + expectedDigest: digest.SHA256.FromBytes([]byte("different data")), |
| 60 | + expectError: true, |
| 61 | + errorContains: "blob digest", |
| 62 | + }, |
| 63 | + { |
| 64 | + name: "empty blob with matching digest", |
| 65 | + blob: []byte{}, |
| 66 | + expectedDigest: digest.SHA256.FromBytes([]byte{}), |
| 67 | + expectError: false, |
| 68 | + }, |
| 69 | + { |
| 70 | + name: "unavailable algorithm - blake2b", |
| 71 | + blob: testBlob, |
| 72 | + expectedDigest: "blake2b:0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef", |
| 73 | + expectError: true, |
| 74 | + errorContains: "invalid digest format", |
| 75 | + }, |
| 76 | + { |
| 77 | + name: "sha256 digest success", |
| 78 | + blob: testBlob, |
| 79 | + expectedDigest: digest.SHA256.FromBytes(testBlob), |
| 80 | + expectError: false, |
| 81 | + }, |
| 82 | + { |
| 83 | + name: "sha512 digest success", |
| 84 | + blob: testBlob, |
| 85 | + expectedDigest: digest.SHA512.FromBytes(testBlob), |
| 86 | + expectError: false, |
| 87 | + }, |
| 88 | + } |
| 89 | + |
| 90 | + for _, tt := range tests { |
| 91 | + t.Run(tt.name, func(t *testing.T) { |
| 92 | + err := validateBlobAgainstDigest(tt.blob, tt.expectedDigest) |
| 93 | + if tt.expectError { |
| 94 | + require.Error(t, err) |
| 95 | + assert.Contains(t, err.Error(), tt.errorContains) |
| 96 | + } else { |
| 97 | + assert.NoError(t, err) |
| 98 | + } |
| 99 | + }) |
| 100 | + } |
| 101 | +} |
0 commit comments