Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ramonpetgrave64 committed Jan 10, 2024
1 parent 74b18aa commit bcfd751
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions verifiers/internal/gha/npm_sigstore_tuf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@ const (
testTargetKeyMaterial = "MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE1Olb3zMAFFxXKHiIkQO5cJ3Yhl5i6UPp+IhuteBJbuHcA5UogKo0EWtlWwW6KSaKoTNEYL7JlCQiVnkhBktUgg=="
)

// mockSigstoreTufClient a mock implementation of SigstoreTufClient
type mockSigstoreTufClient struct {
SigstoreTufClient
localPath string
}

// GetTarget mock implementation of GetTarget for the mockSigstoreTufClient
func (client mockSigstoreTufClient) GetTarget(targetPath string) ([]byte, error) {
content, err := os.ReadFile(targetPath)
if err != nil {
Expand All @@ -27,18 +30,13 @@ func (client mockSigstoreTufClient) GetTarget(targetPath string) ([]byte, error)
return content, nil
}

func mustReadLocalFile(t *testing.T, filePath string) []byte {
content, err := os.ReadFile(filePath)
assert.NoErrorf(t, err, "reading local file: %s", err)
return content
}

// TestGetTarget ensures we can parse the target file
func TestGetNpmjsKeysTarget(t *testing.T) {
t.Run("parsing local registry.npmjs.org_keys.json", func(t *testing.T) {
content := mustReadLocalFile(t, testTargetLocalFilePath)
content, err := os.ReadFile(testTargetLocalFilePath)
assert.NoErrorf(t, err, "reading local file: %s", err)
var expectedKeys NpmjsKeysTarget
err := json.Unmarshal(content, &expectedKeys)
err = json.Unmarshal(content, &expectedKeys)
assert.NoErrorf(t, err, "parsing mock file: %s", err)

mockClient := mockSigstoreTufClient{localPath: testTargetLocalFilePath}
Expand All @@ -48,13 +46,14 @@ func TestGetNpmjsKeysTarget(t *testing.T) {
})

t.Run("parsing non-existant registry.npmjs.org_keys.json", func(t *testing.T) {
nonExistantPath := "./testdatamy-fake-path"
nonExistantPath := "./testdata/my-fake-path"
mockClient := mockSigstoreTufClient{localPath: nonExistantPath}
_, err := GetNpmjsKeysTarget(mockClient, nonExistantPath)
assert.Error(t, err)
})
}

// TestGetAttestationKeyMaterialByKeyId ensure that we find the "npm:attestations" key material, given keyid
func TestGetAttestationKeyMaterialByKeyId(t *testing.T) {
tests := []struct {
name string
Expand All @@ -72,7 +71,7 @@ func TestGetAttestationKeyMaterialByKeyId(t *testing.T) {
expectError: false,
},
{
name: "missing attestation keyusage",
name: "missing the 'npm:attestations' keyusage",
localPath: "./testdata/wrong_keyusage_registry.npmjs.org_keys.json",
keyId: testTargetKeyId,
expectedKeyMaterial: testTargetKeyMaterial,
Expand Down

0 comments on commit bcfd751

Please sign in to comment.