From c4a3b6dea19c5c756b60be15d15244ea3e6ea6c0 Mon Sep 17 00:00:00 2001 From: Kendall Weihe Date: Wed, 3 Apr 2024 09:27:15 -0400 Subject: [PATCH 1/7] Add reusable test vector code --- dids/didjwk/didjwk_test.go | 22 ++++++++++++++++++++++ web5-spec-test-vectors.go | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 web5-spec-test-vectors.go diff --git a/dids/didjwk/didjwk_test.go b/dids/didjwk/didjwk_test.go index 802e623..aceb45b 100644 --- a/dids/didjwk/didjwk_test.go +++ b/dids/didjwk/didjwk_test.go @@ -4,6 +4,7 @@ import ( "testing" "github.com/alecthomas/assert/v2" + web5go "github.com/tbd54566975/web5-go" "github.com/tbd54566975/web5-go/dids/didcore" "github.com/tbd54566975/web5-go/dids/didjwk" "github.com/tbd54566975/web5-go/jwk" @@ -34,3 +35,24 @@ func TestResolveDIDJWK(t *testing.T) { assert.Equal(t, 1, len(result.Document.Authentication)) assert.Equal(t, 1, len(result.Document.AssertionMethod)) } + +func TestVector_Resolve(t *testing.T) { + testVectors, err := + web5go.ReadTestVector[string, didcore.ResolutionResult]("../../web5-spec/test-vectors/did_jwk/resolve.json") + assert.NoError(t, err) + + resolver := &didjwk.Resolver{} + + for _, vector := range testVectors.Vectors { + t.Run(vector.Description, func(t *testing.T) { + result, err := resolver.Resolve(vector.Input) + + if vector.Errors { + assert.Error(t, err) + } else { + assert.NoError(t, err) + assert.Equal(t, vector.Output, result) + } + }) + } +} diff --git a/web5-spec-test-vectors.go b/web5-spec-test-vectors.go new file mode 100644 index 0000000..89df3a2 --- /dev/null +++ b/web5-spec-test-vectors.go @@ -0,0 +1,33 @@ +package web5go + +import ( + "encoding/json" + "os" +) + +type TestVectors[T, U any] struct { + Description string `json:"description"` + Vectors []TestVector[T, U] `json:"vectors"` +} + +type TestVector[T, U any] struct { + Description string `json:"description"` + Input T `json:"input"` + Output U `json:"output"` + Errors bool `json:"errors"` +} + +func ReadTestVector[T, U any](path string) (TestVectors[T, U], error) { + data, err := os.ReadFile(path) + if err != nil { + return TestVectors[T, U]{}, err + } + + var testVectors TestVectors[T, U] + err = json.Unmarshal(data, &testVectors) + if err != nil { + return TestVectors[T, U]{}, err + } + + return testVectors, nil +} From 26ff1c4e27832d4474da4f09f8d90743982e5da8 Mon Sep 17 00:00:00 2001 From: Kendall Weihe Date: Wed, 3 Apr 2024 09:31:33 -0400 Subject: [PATCH 2/7] Add doc comments, add print descriptions --- dids/didjwk/didjwk_test.go | 3 +++ web5-spec-test-vectors.go | 3 +++ 2 files changed, 6 insertions(+) diff --git a/dids/didjwk/didjwk_test.go b/dids/didjwk/didjwk_test.go index aceb45b..a876f71 100644 --- a/dids/didjwk/didjwk_test.go +++ b/dids/didjwk/didjwk_test.go @@ -1,6 +1,7 @@ package didjwk_test import ( + "fmt" "testing" "github.com/alecthomas/assert/v2" @@ -40,11 +41,13 @@ func TestVector_Resolve(t *testing.T) { testVectors, err := web5go.ReadTestVector[string, didcore.ResolutionResult]("../../web5-spec/test-vectors/did_jwk/resolve.json") assert.NoError(t, err) + fmt.Println("Running test vectors: ", testVectors.Description) resolver := &didjwk.Resolver{} for _, vector := range testVectors.Vectors { t.Run(vector.Description, func(t *testing.T) { + fmt.Println("Running test vector: ", vector.Description) result, err := resolver.Resolve(vector.Input) if vector.Errors { diff --git a/web5-spec-test-vectors.go b/web5-spec-test-vectors.go index 89df3a2..f4afacd 100644 --- a/web5-spec-test-vectors.go +++ b/web5-spec-test-vectors.go @@ -5,11 +5,13 @@ import ( "os" ) +// TestVectors are JSON files which are tested against to ensure interop with the specification type TestVectors[T, U any] struct { Description string `json:"description"` Vectors []TestVector[T, U] `json:"vectors"` } +// TestVector is an individual test vector case type TestVector[T, U any] struct { Description string `json:"description"` Input T `json:"input"` @@ -17,6 +19,7 @@ type TestVector[T, U any] struct { Errors bool `json:"errors"` } +// ReadTestVector is for reading the vector at the given path func ReadTestVector[T, U any](path string) (TestVectors[T, U], error) { data, err := os.ReadFile(path) if err != nil { From 2861b54537bf86f269aaa212c550a724f3012718 Mon Sep 17 00:00:00 2001 From: Kendall Weihe Date: Wed, 3 Apr 2024 09:45:25 -0400 Subject: [PATCH 3/7] Add test vector for vcjwt decode --- vc/vcjwt.go | 4 ++++ vc/vcjwt_test.go | 23 +++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/vc/vcjwt.go b/vc/vcjwt.go index 57d3ce7..141c981 100644 --- a/vc/vcjwt.go +++ b/vc/vcjwt.go @@ -55,6 +55,10 @@ func Decode[T CredentialSubject](vcJWT string) (DecodedVCJWT[T], error) { return DecodedVCJWT[T]{}, fmt.Errorf("failed to decode vc claim: %w", err) } + if vc.Type == nil { + return DecodedVCJWT[T]{}, errors.New("vc-jwt missing vc type") + } + // the following conditionals are included to conform with the jwt decoding section // of the specification defined here: https://www.w3.org/TR/vc-data-model/#jwt-decoding if decoded.Claims.Issuer != "" { diff --git a/vc/vcjwt_test.go b/vc/vcjwt_test.go index 58ef3e7..7ebc1dc 100644 --- a/vc/vcjwt_test.go +++ b/vc/vcjwt_test.go @@ -1,10 +1,12 @@ package vc_test import ( + "fmt" "testing" "time" "github.com/alecthomas/assert/v2" + web5go "github.com/tbd54566975/web5-go" "github.com/tbd54566975/web5-go/dids/didjwk" "github.com/tbd54566975/web5-go/jwt" "github.com/tbd54566975/web5-go/vc" @@ -164,3 +166,24 @@ func TestVerify(t *testing.T) { }) } } + +func TestVector_Decode(t *testing.T) { + testVectors, err := + web5go.ReadTestVector[string, any]("../web5-spec/test-vectors/vc_jwt/decode.json") + assert.NoError(t, err) + fmt.Println("Running test vectors: ", testVectors.Description) + + for _, vector := range testVectors.Vectors { + t.Run(vector.Description, func(t *testing.T) { + fmt.Println("Running test vector: ", vector.Description) + + _, err := vc.Decode[vc.Claims](vector.Input) + + if vector.Errors { + assert.Error(t, err) + } else { + assert.NoError(t, err) + } + }) + } +} From e77f69bb5416a24a51a3793782ff504016ce96e9 Mon Sep 17 00:00:00 2001 From: Kendall Weihe Date: Wed, 3 Apr 2024 09:46:21 -0400 Subject: [PATCH 4/7] Remove failing didjwk vector test --- dids/didjwk/didjwk_test.go | 25 ------------------------- 1 file changed, 25 deletions(-) diff --git a/dids/didjwk/didjwk_test.go b/dids/didjwk/didjwk_test.go index a876f71..802e623 100644 --- a/dids/didjwk/didjwk_test.go +++ b/dids/didjwk/didjwk_test.go @@ -1,11 +1,9 @@ package didjwk_test import ( - "fmt" "testing" "github.com/alecthomas/assert/v2" - web5go "github.com/tbd54566975/web5-go" "github.com/tbd54566975/web5-go/dids/didcore" "github.com/tbd54566975/web5-go/dids/didjwk" "github.com/tbd54566975/web5-go/jwk" @@ -36,26 +34,3 @@ func TestResolveDIDJWK(t *testing.T) { assert.Equal(t, 1, len(result.Document.Authentication)) assert.Equal(t, 1, len(result.Document.AssertionMethod)) } - -func TestVector_Resolve(t *testing.T) { - testVectors, err := - web5go.ReadTestVector[string, didcore.ResolutionResult]("../../web5-spec/test-vectors/did_jwk/resolve.json") - assert.NoError(t, err) - fmt.Println("Running test vectors: ", testVectors.Description) - - resolver := &didjwk.Resolver{} - - for _, vector := range testVectors.Vectors { - t.Run(vector.Description, func(t *testing.T) { - fmt.Println("Running test vector: ", vector.Description) - result, err := resolver.Resolve(vector.Input) - - if vector.Errors { - assert.Error(t, err) - } else { - assert.NoError(t, err) - assert.Equal(t, vector.Output, result) - } - }) - } -} From fc153eb0ff76c9c9e471636f304c5211485fa54f Mon Sep 17 00:00:00 2001 From: Kendall Weihe Date: Wed, 3 Apr 2024 09:50:48 -0400 Subject: [PATCH 5/7] Fix decode test --- vc/vcjwt_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/vc/vcjwt_test.go b/vc/vcjwt_test.go index 7ebc1dc..4fd53bf 100644 --- a/vc/vcjwt_test.go +++ b/vc/vcjwt_test.go @@ -84,6 +84,7 @@ func TestDecode_SetClaims(t *testing.T) { Misc: map[string]any{ "vc": vc.DataModel[vc.Claims]{ CredentialSubject: subjectClaims, + Type: []string{"Something"}, }, }, } From e8327287725a77a75438ff58d3c235c2f6476685 Mon Sep 17 00:00:00 2001 From: Kendall Weihe Date: Wed, 3 Apr 2024 10:02:45 -0400 Subject: [PATCH 6/7] Change package name to web5, change function name to load --- vc/vcjwt_test.go | 4 ++-- web5-spec-test-vectors.go | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/vc/vcjwt_test.go b/vc/vcjwt_test.go index 4fd53bf..917f451 100644 --- a/vc/vcjwt_test.go +++ b/vc/vcjwt_test.go @@ -6,7 +6,7 @@ import ( "time" "github.com/alecthomas/assert/v2" - web5go "github.com/tbd54566975/web5-go" + "github.com/tbd54566975/web5-go" "github.com/tbd54566975/web5-go/dids/didjwk" "github.com/tbd54566975/web5-go/jwt" "github.com/tbd54566975/web5-go/vc" @@ -170,7 +170,7 @@ func TestVerify(t *testing.T) { func TestVector_Decode(t *testing.T) { testVectors, err := - web5go.ReadTestVector[string, any]("../web5-spec/test-vectors/vc_jwt/decode.json") + web5.LoadTestVectors[string, any]("../web5-spec/test-vectors/vc_jwt/decode.json") assert.NoError(t, err) fmt.Println("Running test vectors: ", testVectors.Description) diff --git a/web5-spec-test-vectors.go b/web5-spec-test-vectors.go index f4afacd..29e1fd6 100644 --- a/web5-spec-test-vectors.go +++ b/web5-spec-test-vectors.go @@ -1,4 +1,4 @@ -package web5go +package web5 import ( "encoding/json" @@ -19,8 +19,8 @@ type TestVector[T, U any] struct { Errors bool `json:"errors"` } -// ReadTestVector is for reading the vector at the given path -func ReadTestVector[T, U any](path string) (TestVectors[T, U], error) { +// LoadTestVectors is for reading the vector at the given path +func LoadTestVectors[T, U any](path string) (TestVectors[T, U], error) { data, err := os.ReadFile(path) if err != nil { return TestVectors[T, U]{}, err From d5fc9d0b69721cb5090b20d11286af0c212b1005 Mon Sep 17 00:00:00 2001 From: Kendall Weihe Date: Wed, 3 Apr 2024 10:09:54 -0400 Subject: [PATCH 7/7] Rename file to match go convention --- web5-spec-test-vectors.go => testvectors.go | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename web5-spec-test-vectors.go => testvectors.go (100%) diff --git a/web5-spec-test-vectors.go b/testvectors.go similarity index 100% rename from web5-spec-test-vectors.go rename to testvectors.go