Skip to content

Commit

Permalink
add test case for b64
Browse files Browse the repository at this point in the history
  • Loading branch information
shogo82148 committed Nov 7, 2022
1 parent 8539cb9 commit ddf9be7
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 55 deletions.
62 changes: 40 additions & 22 deletions jws/fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,18 @@ func FuzzJWS(f *testing.F) {
`}`,
)

f.Add(`{`+
`"payload":`+
`"eyJpc3MiOiJqb2UiLA0KICJleHAiOjEzMDA4MTkzODAsDQogImh0dHA6Ly9leGF`+
`tcGxlLmNvbS9pc19yb290Ijp0cnVlfQ",`+
`"protected":"eyJhbGciOiJFUzI1NiJ9",`+
`"header":`+
`{"kid":"e9bc097a-ce51-4036-9562-d2ade882db0d"},`+
`"signature":`+
`"DtEhU3ljbEg8L38VWAfUAqOyKAM6-Xx-F4GawxaepmXFCgfTjDxw5djxLa8IS`+
`lSApmWQxfKTUJqPP3-Kg6NU1Q"`+
`}`,
f.Add(
`{`+
`"payload":`+
`"eyJpc3MiOiJqb2UiLA0KICJleHAiOjEzMDA4MTkzODAsDQogImh0dHA6Ly9leGF`+
`tcGxlLmNvbS9pc19yb290Ijp0cnVlfQ",`+
`"protected":"eyJhbGciOiJFUzI1NiJ9",`+
`"header":`+
`{"kid":"e9bc097a-ce51-4036-9562-d2ade882db0d"},`+
`"signature":`+
`"DtEhU3ljbEg8L38VWAfUAqOyKAM6-Xx-F4GawxaepmXFCgfTjDxw5djxLa8IS`+
`lSApmWQxfKTUJqPP3-Kg6NU1Q"`+
`}`,
`{"kty":"EC",`+
`"crv":"P-256",`+
`"x":"f83OJ3D2xF1Bg8vub9tLe1gHMzV76e8Tus9uPHvRVEU",`+
Expand All @@ -119,17 +120,18 @@ func FuzzJWS(f *testing.F) {
`}`,
)

f.Add(`{`+
`"payload":`+
`"eyJpc3MiOiJqb2UiLA0KICJleHAiOjEzMDA4MTkzODAsDQogImh0dHA6Ly9leGF`+
`tcGxlLmNvbS9pc19yb290Ijp0cnVlfQ",`+
`"protected":"eyJhbGciOiJFUzI1NiJ9",`+
`"header":`+
`{"kid":"e9bc097a-ce51-4036-9562-d2ade882db0d"},`+
`"signature":`+
`"DtEhU3ljbEg8L38VWAfUAqOyKAM6-Xx-F4GawxaepmXFCgfTjDxw5djxLa8IS`+
`lSApmWQxfKTUJqPP3-Kg6NU1Q"`+
`}`,
f.Add(
`{`+
`"payload":`+
`"eyJpc3MiOiJqb2UiLA0KICJleHAiOjEzMDA4MTkzODAsDQogImh0dHA6Ly9leGF`+
`tcGxlLmNvbS9pc19yb290Ijp0cnVlfQ",`+
`"protected":"eyJhbGciOiJFUzI1NiJ9",`+
`"header":`+
`{"kid":"e9bc097a-ce51-4036-9562-d2ade882db0d"},`+
`"signature":`+
`"DtEhU3ljbEg8L38VWAfUAqOyKAM6-Xx-F4GawxaepmXFCgfTjDxw5djxLa8IS`+
`lSApmWQxfKTUJqPP3-Kg6NU1Q"`+
`}`,
`{"kty":"EC",`+
`"crv":"P-256",`+
`"x":"f83OJ3D2xF1Bg8vub9tLe1gHMzV76e8Tus9uPHvRVEU",`+
Expand All @@ -138,6 +140,22 @@ func FuzzJWS(f *testing.F) {
`}`,
)

f.Add(
`{`+
`"protected":`+
`"eyJhbGciOiJIUzI1NiIsImI2NCI6ZmFsc2UsImNyaXQiOlsiYjY0Il19",`+
`"payload":`+
`"$.02",`+
`"signature":`+
`"A5dxf2s96_n5FLueVuW1Z_vh161FwXZC4YLPff6dmDY"`+
`}`,
`{`+
`"kty":"oct",`+
`"k":"AyM1SysPpbyDfgZld3umj1qzKObwVMkoqQ-EstJQLr_T-1qS0gZH75`+
`aKtMN3Yj0iPS4hcgUuTwjAzZr1Z9CAow"`+
`}`,
)

f.Fuzz(func(t *testing.T, raw, rawKey string) {
var msg1 Message
if err := msg1.UnmarshalJSON([]byte(raw)); err != nil {
Expand Down
69 changes: 36 additions & 33 deletions jws/jws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -585,39 +585,42 @@ func TestMarshalJSON(t *testing.T) {
})

// test for b64 header parameter.
// t.Run("RFC 7797 Section 4.2. Example with Header Parameters", func(t *testing.T) {
// raw := `{` +
// `"protected":` +
// `"eyJhbGciOiJIUzI1NiIsImI2NCI6ZmFsc2UsImNyaXQiOlsiYjY0Il19",` +
// `"payload":` +
// `"$.02",` +
// `"signature":` +
// `"A5dxf2s96_n5FLueVuW1Z_vh161FwXZC4YLPff6dmDY"` +
// `}`
// var msg Message
// if err := msg.UnmarshalJSON([]byte(raw)); err != nil {
// t.Fatal(err)
// }
// _, payload, err := msg.Verify(FindKeyFunc(func(protected, header *Header) (sig.SigningKey, error) {
// rawKey := `{` +
// `"kty":"oct",` +
// `"k":"AyM1SysPpbyDfgZld3umj1qzKObwVMkoqQ-EstJQLr_T-1qS0gZH75` +
// `aKtMN3Yj0iPS4hcgUuTwjAzZr1Z9CAow"` +
// `}`
// key, err := jwk.ParseKey([]byte(rawKey))
// if err != nil {
// return nil, err
// }
// return protected.Algorithm().New().NewSigningKey(key), nil
// }))
// if err != nil {
// t.Fatal(err)
// }
// want := []byte(`$.02`)
// if !bytes.Equal(payload, want) {
// t.Errorf("unexpected payload: want %q, got %q", string(want), string(payload))
// }
// })
t.Run("RFC 7797 Section 4.2. Example with Header Parameters", func(t *testing.T) {
msg := NewRawMessage([]byte("$.02"))
header := NewHeader()
header.SetAlgorithm(jwa.HS256)
header.SetBase64(false)

rawKey := `{` +
`"kty":"oct",` +
`"k":"AyM1SysPpbyDfgZld3umj1qzKObwVMkoqQ-EstJQLr_T-1qS0gZH75` +
`aKtMN3Yj0iPS4hcgUuTwjAzZr1Z9CAow"` +
`}`
key, err := jwk.ParseKey([]byte(rawKey))
if err != nil {
t.Fatal(err)
}

if err := msg.Sign(header, nil, jwa.HS256.New().NewSigningKey(key)); err != nil {
t.Fatal(err)
}

got, err := msg.MarshalJSON()
if err != nil {
t.Fatal(err)
}
want := `{` +
`"payload":` +
`"$.02",` +
`"protected":` +
`"eyJhbGciOiJIUzI1NiIsImI2NCI6ZmFsc2UsImNyaXQiOlsiYjY0Il19",` +
`"signature":` +
`"A5dxf2s96_n5FLueVuW1Z_vh161FwXZC4YLPff6dmDY"` +
`}`
if string(got) != want {
t.Errorf("want %s, got %s", want, got)
}
})
}

func TestSign(t *testing.T) {
Expand Down

0 comments on commit ddf9be7

Please sign in to comment.