Debian Codegraph @

src/test/java/org/jose4j/json/JsonUtilTest.java

raw
1 /*
2 * Copyright 2012-2017 Brian Campbell
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package org.jose4j.json;
18
19 import org.jose4j.lang.ExceptionHelp;
20 import org.junit.Assert;
21 import org.jose4j.lang.JoseException;
22 import org.junit.Test;
23 import org.slf4j.LoggerFactory;
24
25 import java.util.Collections;
26 import java.util.HashMap;
27 import java.util.Map;
28 import java.util.Random;
29
30 import static org.junit.Assert.fail;
31
32 /**
33 */
34 public class JsonUtilTest
35 {
36 @Test
37 public void needsEsc() throws Exception
38 {
39 // char array serialization wasn't escaping special characters like quote and backslash
40 // and serialization of an unrecognized object was just getting the toString of it directly
41 // both of which could result in invalid JSON. this tests that has been fixed
42 Map<String, Object> map = new HashMap<>();
43 map.put("char array", new char[]{'a', '\\', '\"'});
44 map.put("some object", new Object());
45 map.put("nested", Collections.singletonMap("chars", "\"meh".toCharArray()));
46 map.put("nested also", Collections.singletonMap("obj", new Random()));
47
48 String s = JsonUtil.toJson(map);
49 System.out.println(s);
50 Map<String, Object> parsedMap = JsonUtil.parseJson(s);
51 }
52
53 @Test
54 public void testParseJson1() throws JoseException
55 {
56 String basic = "{\"key\":\"value\"}";
57 Map<String,Object> map = JsonUtil.parseJson(basic);
58 Assert.assertEquals(1, map.size());
59 Assert.assertEquals("value", map.get("key"));
60 }
61
62 @Test
63 public void testParseJsonDisallowDupes()
64 {
65 String basic = "{\"key\":\"value\",\"key\":\"value2\"}";
66
67 try
68 {
69 Map<String,?> map = JsonUtil.parseJson(basic);
70 fail("parsing of " + basic + " should fail because the same member name occurs multiple times but returned: " + map);
71 }
72 catch (JoseException e)
73 {
74 // expected
75 }
76 }
77
78 @Test
79 public void testParseJsonDisallowDupesMoreComplex()
80 {
81 String json = "{\n" +
82 " \"keys\": [\n" +
83 " {\n" +
84 " \"kty\": \"EC\",\n" +
85 " \"kid\": \"20b05\",\n" +
86 " \"use\": \"sig\",\n" +
87 " \"x\": \"baLYE[omitted]DLSIor7\",\n" +
88 " \"y\": \"Xh2Q4[omitted]AB3GKQ1\",\n" +
89 " \"crv\": \"P-384\"\n" +
90 " },\n" +
91 " {\n" +
92 " \"kty\": \"EC\",\n" +
93 " \"kid\": \"20b04\",\n" +
94 " \"use\": \"sig\",\n" +
95 " \"x\": \"-Pfjrs_rpNIu4XPMHOhW4DvhZ9sdEKgT8zINkLM6Yvg\",\n" +
96 " \"y\": \"1FXTX9JGWH4kG0KxUIQDqOIxC2R8w5sLHHYr6sjcTK4\",\n" +
97 " \"y\": \"1234567890abcdefghijklmnopqrstuvwxyzABCDEFG\",\n" + // duplicate y
98 " \"crv\": \"P-256\"\n" +
99 " }\n" +
100 " ]\n" +
101 "}";
102
103 try
104 {
105 Map<String,?> map = JsonUtil.parseJson(json);
106 fail("parsing of " + json + " should fail because the same member name occurs multiple times but returned: " + map);
107 }
108 catch (JoseException e)
109 {
110 // expected
111 }
112 }
113
114 @Test
115 public void testBiggerThanLong() throws Exception
116 {
117 String json = "{\"key\":\"value\",\"number\":90210, \"big number\":99990193716474719874987981237498321343555513331108571735145}";
118 Map<String, Object> parsed = JsonUtil.parseJson(json);
119 Assert.assertEquals(3, parsed.size());
120 }
121
122 @Test
123 public void testNonObjectErrorMessage()
124 {
125 String[] jsons = new String[] {"[\"key\",\"value\"]", "false", "\"really?\"", "1001", "null"};
126 for (String json : jsons)
127 {
128 try
129 {
130 Map<String, Object> parsed = JsonUtil.parseJson(json);
131 fail("shouldn't work but " + parsed);
132 }
133 catch (JoseException e)
134 {
135 LoggerFactory.getLogger(this.getClass()).debug("Expected: " + e);
136 }
137 }
138 }
139
140 //todo some general JSON tests?
141 // todo disallow extra trailing data (and leading?)
142 }