• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2020 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 ////////////////////////////////////////////////////////////////////////////////
16 
17 package com.google.crypto.tink.jwt;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 import static org.junit.Assert.assertThrows;
21 
22 import com.google.gson.JsonArray;
23 import com.google.gson.JsonObject;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.junit.runners.JUnit4;
27 
28 /** Unit tests for JsonUtil */
29 @RunWith(JUnit4.class)
30 public final class JsonUtilTest {
31 
32   @Test
validateValidString_success()33   public void validateValidString_success() throws Exception {
34     assertThat(JsonUtil.isValidString("")).isTrue();
35     assertThat(JsonUtil.isValidString("foo")).isTrue();
36     assertThat(JsonUtil.isValidString("*\uD834\uDD1E*")).isTrue();
37     assertThat(JsonUtil.isValidString("\uD834\uDD1E*")).isTrue();
38     assertThat(JsonUtil.isValidString("*\uD834\uDD1E")).isTrue();
39     assertThat(JsonUtil.isValidString("\uD834\uDD1E")).isTrue();
40   }
41 
42   @Test
validateInvalidString_throws()43   public void validateInvalidString_throws() throws Exception {
44     assertThat(JsonUtil.isValidString("*\uD834")).isFalse();
45     assertThat(JsonUtil.isValidString("\uD834*")).isFalse();
46     assertThat(JsonUtil.isValidString("\uD834")).isFalse();
47     assertThat(JsonUtil.isValidString("*\uD834*")).isFalse();
48     assertThat(JsonUtil.isValidString("\uDD1E")).isFalse();
49     assertThat(JsonUtil.isValidString("*\uDD1E")).isFalse();
50     assertThat(JsonUtil.isValidString("\uDD1E*")).isFalse();
51     assertThat(JsonUtil.isValidString("*\uDD1E*")).isFalse();
52   }
53   ;
54 
55   @Test
parseJson_success()56   public void parseJson_success() throws Exception {
57     JsonObject header = JsonUtil.parseJson("{\"bool\":false}");
58     assertThat(header.get("bool").getAsBoolean()).isFalse();
59   }
60 
61   @Test
parseJsonArray_success()62   public void parseJsonArray_success() throws Exception {
63     JsonArray array = JsonUtil.parseJsonArray("[1, \"foo\"]");
64     assertThat(array.get(0).getAsInt()).isEqualTo(1);
65     assertThat(array.get(1).getAsString()).isEqualTo("foo");
66   }
67 
68   @Test
parseRecursiveJsonString_success()69   public void parseRecursiveJsonString_success() throws Exception {
70     StringBuilder sb = new StringBuilder();
71     for (int i = 0; i < 10000; i++) {
72       sb.append("{\"a\":");
73     }
74     sb.append("1");
75     for (int i = 0; i < 10000; i++) {
76       sb.append("}");
77     }
78     assertThrows(JwtInvalidException.class, () -> JsonUtil.parseJson(sb.toString()));
79   }
80 
81   @Test
parseJsonWithoutQuotes_fail()82   public void parseJsonWithoutQuotes_fail() throws Exception {
83     assertThrows(JwtInvalidException.class, () -> JsonUtil.parseJson("{bool:false}"));
84   }
85 
86   @Test
parseJsonObjectWithoutDuplicateKey_fail()87   public void parseJsonObjectWithoutDuplicateKey_fail() throws Exception {
88     assertThrows(JwtInvalidException.class, () -> JsonUtil.parseJson("{\"a\":1,\"a\":2}"));
89   }
90 
91   @Test
parseJsonArrayWithoutComments_fail()92   public void parseJsonArrayWithoutComments_fail() throws Exception {
93     assertThrows(JwtInvalidException.class, () -> JsonUtil.parseJson("[1, \"foo\" /* comment */]"));
94   }
95 
96   @Test
parseJsonWithoutComments_fail()97   public void parseJsonWithoutComments_fail() throws Exception {
98     assertThrows(
99         JwtInvalidException.class, () -> JsonUtil.parseJson("{\"bool\":false /* comment */}"));
100   }
101 }
102