|
| 1 | +package supporteddigests |
| 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 TestGet(t *testing.T) { |
| 12 | + // Test that Get returns the default algorithm (SHA256) |
| 13 | + algorithm := Get() |
| 14 | + assert.Equal(t, digest.Canonical, algorithm) |
| 15 | + assert.Equal(t, "sha256", algorithm.String()) |
| 16 | +} |
| 17 | + |
| 18 | +func TestSet(t *testing.T) { |
| 19 | + tests := []struct { |
| 20 | + name string |
| 21 | + algorithm digest.Algorithm |
| 22 | + expectError bool |
| 23 | + expected digest.Algorithm |
| 24 | + }{ |
| 25 | + { |
| 26 | + name: "Set SHA256", |
| 27 | + algorithm: digest.SHA256, |
| 28 | + expectError: false, |
| 29 | + expected: digest.SHA256, |
| 30 | + }, |
| 31 | + { |
| 32 | + name: "Set SHA512", |
| 33 | + algorithm: digest.SHA512, |
| 34 | + expectError: false, |
| 35 | + expected: digest.SHA512, |
| 36 | + }, |
| 37 | + { |
| 38 | + name: "Set empty string (should default to SHA256)", |
| 39 | + algorithm: "", |
| 40 | + expectError: false, |
| 41 | + expected: digest.Canonical, // SHA256 |
| 42 | + }, |
| 43 | + { |
| 44 | + name: "Set unsupported algorithm SHA384", |
| 45 | + algorithm: digest.SHA384, |
| 46 | + expectError: true, |
| 47 | + expected: digest.Canonical, // Should remain unchanged (default) |
| 48 | + }, |
| 49 | + { |
| 50 | + name: "Set unsupported algorithm MD5", |
| 51 | + algorithm: digest.Digest("md5:invalid").Algorithm(), |
| 52 | + expectError: true, |
| 53 | + expected: digest.Canonical, // Should remain unchanged (default) |
| 54 | + }, |
| 55 | + } |
| 56 | + |
| 57 | + for _, tt := range tests { |
| 58 | + t.Run(tt.name, func(t *testing.T) { |
| 59 | + err := Set(tt.algorithm) |
| 60 | + if tt.expectError { |
| 61 | + assert.Error(t, err) |
| 62 | + assert.Contains(t, err.Error(), "unsupported digest algorithm") |
| 63 | + // Verify algorithm wasn't changed |
| 64 | + assert.Equal(t, tt.expected, Get()) |
| 65 | + } else { |
| 66 | + assert.NoError(t, err) |
| 67 | + assert.Equal(t, tt.expected, Get()) |
| 68 | + } |
| 69 | + }) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +func TestAlgorithmPersistence(t *testing.T) { |
| 74 | + // Test that algorithm changes persist across multiple calls |
| 75 | + err := Set(digest.SHA512) |
| 76 | + require.NoError(t, err) |
| 77 | + assert.Equal(t, digest.SHA512, Get()) |
| 78 | + |
| 79 | + // Verify it's still SHA512 after another call |
| 80 | + assert.Equal(t, digest.SHA512, Get()) |
| 81 | + |
| 82 | + // Change to SHA256 |
| 83 | + err = Set(digest.SHA256) |
| 84 | + require.NoError(t, err) |
| 85 | + assert.Equal(t, digest.SHA256, Get()) |
| 86 | + |
| 87 | + // Verify it's still SHA256 after another call |
| 88 | + assert.Equal(t, digest.SHA256, Get()) |
| 89 | +} |
| 90 | + |
| 91 | +func TestAlgorithmStringRepresentation(t *testing.T) { |
| 92 | + // Test SHA256 string representation |
| 93 | + err := Set(digest.SHA256) |
| 94 | + require.NoError(t, err) |
| 95 | + assert.Equal(t, "sha256", Get().String()) |
| 96 | + |
| 97 | + // Test SHA512 string representation |
| 98 | + err = Set(digest.SHA512) |
| 99 | + require.NoError(t, err) |
| 100 | + assert.Equal(t, "sha512", Get().String()) |
| 101 | +} |
0 commit comments