1// +build !std_json
2
3/*-
4 * Copyright 2014 Square Inc.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 *     http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19package jose
20
21import (
22	"testing"
23
24	"gopkg.in/square/go-jose.v1/json"
25)
26
27type CaseSensitive struct {
28	A int `json:"Test"`
29	B int `json:"test"`
30	C int `json:"TEST"`
31}
32
33type UnicodeTest struct {
34	Sig string `json:"sig"`
35}
36
37func TestUnicodeComparison(t *testing.T) {
38	// Some tests from RFC 7515, Section 10.13
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
62func 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
75func 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
84func 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
93func 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
102func 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
110func 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}
117