File: sigstore_test.go

package info (click to toggle)
golang-github-containers-image 5.28.0-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 5,104 kB
  • sloc: sh: 194; makefile: 73
file content (71 lines) | stat: -rw-r--r-- 2,373 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package signature

import (
	"encoding/json"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestSigstoreFromComponents(t *testing.T) {
	const mimeType = "mime-type"
	payload := []byte("payload")
	annotations := map[string]string{"a": "b", "c": "d"}

	sig := SigstoreFromComponents(mimeType, payload, annotations)
	assert.Equal(t, Sigstore{
		untrustedMIMEType:    mimeType,
		untrustedPayload:     payload,
		untrustedAnnotations: annotations,
	}, sig)
}

func TestSigstoreFromBlobChunk(t *testing.T) {
	// Success
	json := []byte(`{"mimeType":"mime-type","payload":"cGF5bG9hZA==", "annotations":{"a":"b","c":"d"}}`)
	res, err := sigstoreFromBlobChunk(json)
	require.NoError(t, err)
	assert.Equal(t, "mime-type", res.UntrustedMIMEType())
	assert.Equal(t, []byte("payload"), res.UntrustedPayload())
	assert.Equal(t, map[string]string{"a": "b", "c": "d"}, res.UntrustedAnnotations())

	// Invalid JSON
	_, err = sigstoreFromBlobChunk([]byte("&"))
	assert.Error(t, err)
}

func TestSigstoreFormatID(t *testing.T) {
	sig := SigstoreFromComponents("mime-type", []byte("payload"),
		map[string]string{"a": "b", "c": "d"})
	assert.Equal(t, SigstoreFormat, sig.FormatID())
}

func TestSigstoreBlobChunk(t *testing.T) {
	sig := SigstoreFromComponents("mime-type", []byte("payload"),
		map[string]string{"a": "b", "c": "d"})
	res, err := sig.blobChunk()
	require.NoError(t, err)

	expectedJSON := []byte(`{"mimeType":"mime-type","payload":"cGF5bG9hZA==", "annotations":{"a":"b","c":"d"}}`)
	// Don’t directly compare the JSON representation so that we don’t test for formatting differences, just verify that it contains exactly the expected data.
	var raw, expectedRaw map[string]any
	err = json.Unmarshal(res, &raw)
	require.NoError(t, err)
	err = json.Unmarshal(expectedJSON, &expectedRaw)
	require.NoError(t, err)
	assert.Equal(t, expectedRaw, raw)
}

func TestSigstoreUntrustedPayload(t *testing.T) {
	var payload = []byte("payload")
	sig := SigstoreFromComponents("mime-type", payload,
		map[string]string{"a": "b", "c": "d"})
	assert.Equal(t, payload, sig.UntrustedPayload())
}

func TestSigstoreUntrustedAnnotations(t *testing.T) {
	annotations := map[string]string{"a": "b", "c": "d"}
	sig := SigstoreFromComponents("mime-type", []byte("payload"), annotations)
	assert.Equal(t, annotations, sig.UntrustedAnnotations())
}