From ddf9be7a4281c241f3dc83def325fafcab20145c Mon Sep 17 00:00:00 2001 From: Ichinose Shogo Date: Mon, 7 Nov 2022 16:05:02 +0900 Subject: [PATCH] add test case for b64 --- jws/fuzz_test.go | 62 ++++++++++++++++++++++++++++--------------- jws/jws_test.go | 69 +++++++++++++++++++++++++----------------------- 2 files changed, 76 insertions(+), 55 deletions(-) diff --git a/jws/fuzz_test.go b/jws/fuzz_test.go index 5eece53..987f5f1 100644 --- a/jws/fuzz_test.go +++ b/jws/fuzz_test.go @@ -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",`+ @@ -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",`+ @@ -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 { diff --git a/jws/jws_test.go b/jws/jws_test.go index a2610f0..3733acf 100644 --- a/jws/jws_test.go +++ b/jws/jws_test.go @@ -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) {