Skip to content

Commit

Permalink
JsonParser to not allow duplicate elements
Browse files Browse the repository at this point in the history
  • Loading branch information
erm-g committed Sep 20, 2024
1 parent 0312dc1 commit 775de8f
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
3 changes: 3 additions & 0 deletions core/src/main/java/io/grpc/internal/JsonParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ private static Object parseRecursive(JsonReader jr) throws IOException {
Map<String, Object> obj = new LinkedHashMap<>();
while (jr.hasNext()) {
String name = jr.nextName();
if (obj.containsKey(name)) {
throw new IllegalArgumentException("Duplicate key found: " + name);
}
Object value = parseRecursive(jr);
obj.put(name, value);
}
Expand Down
7 changes: 7 additions & 0 deletions core/src/test/java/io/grpc/internal/JsonParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,11 @@ public void objectStringName() throws IOException {

assertEquals(expected, JsonParser.parse("{\"hi\": 2}"));
}

@Test
public void duplicate() throws IOException {
thrown.expect(IllegalArgumentException.class);

JsonParser.parse("{\"hi\": 2, \"hi\": 3}");
}
}
9 changes: 7 additions & 2 deletions core/src/test/java/io/grpc/internal/SpiffeUtil2Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public class SpiffeUtil2Test {
private static final String SPIFFE_TRUST_BUNDLE_MALFORMED = "spiffebundle_malformed.json";
private static final String SPIFFE_TRUST_BUNDLE_WRONG_ELEMENTS =
"spiffebundle_wrong_elements.json";
private static final String SPIFFE_TRUST_BUNDLE_DUPLICATES = "spiffebundle_duplicates.json";
private static final String SPIFFE_TRUST_BUNDLE_WITH_WRONG_ROOT =
"spiffebundle_wrong_root.json";

Expand Down Expand Up @@ -117,7 +118,7 @@ public void loadTrustBundleFromFileSuccessTest() throws IOException, Certificate
}

@Test
public void loadTrustBundleFromFileFailureTest() throws IOException, CertificateParsingException {
public void loadTrustBundleFromFileFailureTest() throws IOException {
NullPointerException npe = assertThrows(NullPointerException.class, () -> SpiffeUtil2.
loadTrustBundleFromFile(getClass().getClassLoader().getResource(TEST_DIRECTORY_PREFIX
+ SPIFFE_TRUST_BUNDLE_WITH_WRONG_ROOT).getPath()));
Expand All @@ -126,6 +127,10 @@ public void loadTrustBundleFromFileFailureTest() throws IOException, Certificate
loadTrustBundleFromFile(getClass().getClassLoader().getResource(TEST_DIRECTORY_PREFIX
+ SPIFFE_TRUST_BUNDLE_MALFORMED).getPath()));
assertTrue(iae.getMessage().contains("SPIFFE Trust Bundle should be a JSON object."));
iae = assertThrows(IllegalArgumentException.class, () -> SpiffeUtil2.
loadTrustBundleFromFile(getClass().getClassLoader().getResource(TEST_DIRECTORY_PREFIX
+ SPIFFE_TRUST_BUNDLE_DUPLICATES).getPath()));
assertTrue(iae.getMessage().contains("Duplicate key found: google.com"));
SpiffeBundle tb = SpiffeUtil2.loadTrustBundleFromFile(getClass().getClassLoader()
.getResource(TEST_DIRECTORY_PREFIX + SPIFFE_TRUST_BUNDLE_WRONG_ELEMENTS).getPath());
assertEquals(4, tb.getBundleMap().size());
Expand All @@ -135,7 +140,7 @@ public void loadTrustBundleFromFileFailureTest() throws IOException, Certificate
}

@Test
public void loadTrustBundleFromFileParameterValidityTest() throws IOException, CertificateParsingException {
public void loadTrustBundleFromFileParameterValidityTest() {
NullPointerException npe = assertThrows(NullPointerException.class, () -> SpiffeUtil2
.loadTrustBundleFromFile(null));
assertEquals("trustBundleFile", npe.getMessage());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"trust_domains": {
"google.com": {
"sequence_number": 123,
"keys": [
{
"x5c": "VALUE_DOESN'T_MATTER"
}
]
},
"google.com": {
"sequence_number": 123,
"keys": [
{
"use": "x509-svid",
"kid": "some_value",
"x5c": "VALUE_DOESN'T_MATTER"
}
]
},
"test.google.com.au": {},
"example.com": {
"sequence_number": 12035488,
"keys": [
{
"use": "Z_x509-svid",
"kid": "",
"x5c": "VALUE_DOESN'T_MATTER"
}
]
}
}
}

0 comments on commit 775de8f

Please sign in to comment.