Skip to content

Commit 355301e

Browse files
test: enhance DeviceCodeOAuthFlowTest with immutability and equality edge cases
- Add Map.copyOf() defensive copy for scopes in DeviceCodeOAuthFlow record - Add testScopesImmutability to verify defensive copying behavior - Expand testEqualityAndHashCode with null refresh, different field values, null comparison, and type mismatch checks Fixes #607
1 parent e751b3f commit 355301e

2 files changed

Lines changed: 23 additions & 4 deletions

File tree

spec/src/main/java/io/a2a/spec/DeviceCodeOAuthFlow.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,6 @@ public record DeviceCodeOAuthFlow(String deviceAuthorizationUrl, String tokenUrl
3333
Assert.checkNotNullParam("deviceAuthorizationUrl", deviceAuthorizationUrl);
3434
Assert.checkNotNullParam("tokenUrl", tokenUrl);
3535
Assert.checkNotNullParam("scopes", scopes);
36+
scopes = Map.copyOf(scopes);
3637
}
3738
}

spec/src/test/java/io/a2a/spec/DeviceCodeOAuthFlowTest.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,31 @@ void testConstruction_nullScopes_throwsException() {
7777
void testEqualityAndHashCode() {
7878
DeviceCodeOAuthFlow flow1 = new DeviceCodeOAuthFlow(DEVICE_AUTH_URL, TOKEN_URL, REFRESH_URL, SCOPES);
7979
DeviceCodeOAuthFlow flow2 = new DeviceCodeOAuthFlow(DEVICE_AUTH_URL, TOKEN_URL, REFRESH_URL, SCOPES);
80-
DeviceCodeOAuthFlow flow3WithNullRefresh = new DeviceCodeOAuthFlow(DEVICE_AUTH_URL, TOKEN_URL, null, SCOPES);
80+
DeviceCodeOAuthFlow flow3 = new DeviceCodeOAuthFlow(DEVICE_AUTH_URL, TOKEN_URL, null, SCOPES);
81+
DeviceCodeOAuthFlow flow4 = new DeviceCodeOAuthFlow(DEVICE_AUTH_URL, TOKEN_URL, null, SCOPES);
8182

82-
// Test for equality with same values
83+
// Test for equality and hashCode consistency
8384
assertEquals(flow1, flow2);
8485
assertEquals(flow1.hashCode(), flow2.hashCode());
86+
assertEquals(flow3, flow4);
87+
assertEquals(flow3.hashCode(), flow4.hashCode());
88+
89+
// Test for inequality with different field values
90+
assertNotEquals(flow1, flow3);
91+
assertNotEquals(flow1, new DeviceCodeOAuthFlow("https://other.com", TOKEN_URL, REFRESH_URL, SCOPES));
92+
assertNotEquals(flow1, null);
93+
assertNotEquals(flow1, "not a flow");
94+
}
8595

86-
// Test for inequality with different values
87-
assertNotEquals(flow1, flow3WithNullRefresh);
96+
@Test
97+
void testScopesImmutability() {
98+
Map<String, String> mutableScopes = new java.util.HashMap<>();
99+
mutableScopes.put("read", "Read access");
100+
DeviceCodeOAuthFlow flow = new DeviceCodeOAuthFlow(DEVICE_AUTH_URL, TOKEN_URL, REFRESH_URL, mutableScopes);
101+
102+
// Modifying the original map should not affect the record
103+
mutableScopes.put("write", "Write access");
104+
assertNotEquals(mutableScopes.size(), flow.scopes().size(),
105+
"Record should be immutable and perform a defensive copy of the scopes map");
88106
}
89107
}

0 commit comments

Comments
 (0)