1616package intoto
1717
1818import (
19+ "bytes"
20+ "context"
1921 "crypto"
2022 "crypto/ecdsa"
2123 "crypto/elliptic"
@@ -33,13 +35,16 @@ import (
3335 "strings"
3436 "testing"
3537
38+ "github.com/go-openapi/runtime"
3639 "github.com/go-openapi/strfmt"
3740 "github.com/go-openapi/swag"
3841 "github.com/google/go-cmp/cmp"
42+ "github.com/google/go-cmp/cmp/cmpopts"
3943 "github.com/in-toto/in-toto-golang/in_toto"
4044 slsa "github.com/in-toto/in-toto-golang/in_toto/slsa_provenance/v0.2"
4145 "github.com/secure-systems-lab/go-securesystemslib/dsse"
4246 "github.com/sigstore/rekor/pkg/generated/models"
47+ "github.com/sigstore/rekor/pkg/types"
4348 "github.com/sigstore/sigstore/pkg/signature"
4449 "go.uber.org/goleak"
4550)
@@ -297,6 +302,35 @@ func TestV002Entry_Unmarshal(t *testing.T) {
297302 if ! reflect .DeepEqual (got , want ) {
298303 t .Errorf ("V002Entry.IndexKeys() = %v, want %v" , got , want )
299304 }
305+ payloadBytes , _ := v .env .DecodeB64Payload ()
306+ payloadSha := sha256 .Sum256 (payloadBytes )
307+ payloadHash := hex .EncodeToString (payloadSha [:])
308+
309+ canonicalBytes , err := v .Canonicalize (context .Background ())
310+ if err != nil {
311+ t .Errorf ("error canonicalizing entry: %v" , err )
312+ }
313+
314+ pe , err := models .UnmarshalProposedEntry (bytes .NewReader (canonicalBytes ), runtime .JSONConsumer ())
315+ if err != nil {
316+ t .Errorf ("unexpected err from Unmarshalling canonicalized entry for '%v': %v" , tt .name , err )
317+ }
318+ canonicalEntry , err := types .UnmarshalEntry (pe )
319+ if err != nil {
320+ t .Errorf ("unexpected err from type-specific unmarshalling for '%v': %v" , tt .name , err )
321+ }
322+ canonicalV002 := canonicalEntry .(* V002Entry )
323+ fmt .Printf ("%v" , canonicalV002 .IntotoObj .Content )
324+ if * canonicalV002 .IntotoObj .Content .Hash .Value != * tt .it .Content .Hash .Value {
325+ t .Errorf ("envelope hashes do not match post canonicalization: %v %v" , * canonicalV002 .IntotoObj .Content .Hash .Value , * tt .it .Content .Hash .Value )
326+ }
327+ if canonicalV002 .AttestationKey () != "" && * canonicalV002 .IntotoObj .Content .PayloadHash .Value != payloadHash {
328+ t .Errorf ("payload hashes do not match post canonicalization: %v %v" , canonicalV002 .IntotoObj .Content .PayloadHash .Value , payloadHash )
329+ }
330+ canonicalIndexKeys , _ := canonicalV002 .IndexKeys ()
331+ if ! cmp .Equal (got , canonicalIndexKeys , cmpopts .SortSlices (func (x , y string ) bool { return x < y })) {
332+ t .Errorf ("index keys from hydrated object do not match those generated from canonicalized (and re-hydrated) object: %v %v" , got , canonicalIndexKeys )
333+ }
300334
301335 return nil
302336 }
0 commit comments