| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
package jose |
| 20 |
|
| 21 |
import ( |
| 22 |
"testing" |
| 23 |
|
| 24 |
"gopkg.in/square/go-jose.v1/json" |
| 25 |
) |
| 26 |
|
| 27 |
type CaseSensitive struct { |
| 28 |
A int `json:"Test"` |
| 29 |
B int `json:"test"` |
| 30 |
C int `json:"TEST"` |
| 31 |
} |
| 32 |
|
| 33 |
type UnicodeTest struct { |
| 34 |
Sig string `json:"sig"` |
| 35 |
} |
| 36 |
|
| 37 |
func TestUnicodeComparison(t *testing.T) { |
| 38 |
|
| 39 |
raw := []byte(`{"\u0073ig":"foo"}`) |
| 40 |
var ut UnicodeTest |
| 41 |
err := json.Unmarshal(raw, &ut) |
| 42 |
if err != nil { |
| 43 |
t.Error(err) |
| 44 |
} |
| 45 |
|
| 46 |
if ut.Sig != "foo" { |
| 47 |
t.Error("strings 'sig' and '\\u0073ig' should be equal") |
| 48 |
} |
| 49 |
|
| 50 |
raw = []byte(`{"si\u0047":"bar"}`) |
| 51 |
var ut2 UnicodeTest |
| 52 |
err = json.Unmarshal(raw, &ut2) |
| 53 |
if err != nil { |
| 54 |
t.Error(err) |
| 55 |
} |
| 56 |
|
| 57 |
if ut2.Sig != "" { |
| 58 |
t.Error("strings 'sig' and 'si\\u0047' should not be equal") |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
func TestCaseSensitiveJSON(t *testing.T) { |
| 63 |
raw := []byte(`{"test":42}`) |
| 64 |
var cs CaseSensitive |
| 65 |
err := json.Unmarshal(raw, &cs) |
| 66 |
if err != nil { |
| 67 |
t.Error(err) |
| 68 |
} |
| 69 |
|
| 70 |
if cs.A != 0 || cs.B != 42 || cs.C != 0 { |
| 71 |
t.Errorf("parsing JSON should be case-sensitive (got %v)", cs) |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
func TestErrorOnTrailingCharacters(t *testing.T) { |
| 76 |
raw := []byte(`{"test":42}asdf`) |
| 77 |
var m map[string]interface{} |
| 78 |
err := json.Unmarshal(raw, &m) |
| 79 |
if err == nil { |
| 80 |
t.Error("json.Unmarshal should fail if string has trailing chars") |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
func TestRejectDuplicateKeysObject(t *testing.T) { |
| 85 |
raw := []byte(`{"test":42,"test":43}`) |
| 86 |
var cs CaseSensitive |
| 87 |
err := json.Unmarshal(raw, &cs) |
| 88 |
if err == nil { |
| 89 |
t.Error("should reject JSON with duplicate keys, but didn't") |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
func TestRejectDuplicateKeysInterface(t *testing.T) { |
| 94 |
raw := []byte(`{"test":42,"test":43}`) |
| 95 |
var m interface{} |
| 96 |
err := json.Unmarshal(raw, &m) |
| 97 |
if err == nil { |
| 98 |
t.Error("should reject JSON with duplicate keys, but didn't") |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
func TestParseCaseSensitiveJWE(t *testing.T) { |
| 103 |
invalidJWE := `{"protected":"eyJlbmMiOiJYWVoiLCJBTEciOiJYWVoifQo","encrypted_key":"QUJD","iv":"QUJD","ciphertext":"QUJD","tag":"QUJD"}` |
| 104 |
_, err := ParseEncrypted(invalidJWE) |
| 105 |
if err == nil { |
| 106 |
t.Error("Able to parse message with case-invalid headers", invalidJWE) |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
func TestParseCaseSensitiveJWS(t *testing.T) { |
| 111 |
invalidJWS := `{"PAYLOAD":"CUJD","signatures":[{"protected":"e30","signature":"CUJD"}]}` |
| 112 |
_, err := ParseSigned(invalidJWS) |
| 113 |
if err == nil { |
| 114 |
t.Error("Able to parse message with case-invalid headers", invalidJWS) |
| 115 |
} |
| 116 |
} |