alts: Add ClientCall support to AltsContextUtil · grpc/grpc-java@b1bc0a9
@@ -24,6 +24,7 @@
2424import static org.mockito.Mockito.when;
25252626import io.grpc.Attributes;
27+import io.grpc.ClientCall;
2728import io.grpc.ServerCall;
2829import io.grpc.alts.AltsContext.SecurityLevel;
2930import io.grpc.alts.internal.AltsInternalContext;
@@ -37,27 +38,38 @@
3738/** Unit tests for {@link AltsContextUtil}. */
3839@RunWith(JUnit4.class)
3940public class AltsContextUtilTest {
40-41-private final ServerCall<?,?> call = mock(ServerCall.class);
42-4341@Test
4442public void check_noAttributeValue() {
45-when(call.getAttributes()).thenReturn(Attributes.newBuilder().build());
43+assertFalse(AltsContextUtil.check(Attributes.newBuilder().build()));
44+ }
464547-assertFalse(AltsContextUtil.check(call));
46+@Test
47+public void check_unexpectedAttributeValueType() {
48+assertFalse(AltsContextUtil.check(Attributes.newBuilder()
49+ .set(AltsProtocolNegotiator.AUTH_CONTEXT_KEY, new Object())
50+ .build()));
4851 }
49525053@Test
51-public void contains_unexpectedAttributeValueType() {
54+public void check_altsInternalContext() {
55+assertTrue(AltsContextUtil.check(Attributes.newBuilder()
56+ .set(AltsProtocolNegotiator.AUTH_CONTEXT_KEY, AltsInternalContext.getDefaultInstance())
57+ .build()));
58+ }
59+60+@Test
61+public void checkServer_altsInternalContext() {
62+ServerCall<?,?> call = mock(ServerCall.class);
5263when(call.getAttributes()).thenReturn(Attributes.newBuilder()
53- .set(AltsProtocolNegotiator.AUTH_CONTEXT_KEY, new Object())
64+ .set(AltsProtocolNegotiator.AUTH_CONTEXT_KEY, AltsInternalContext.getDefaultInstance())
5465 .build());
556656-assertFalse(AltsContextUtil.check(call));
67+assertTrue(AltsContextUtil.check(call));
5768 }
58695970@Test
60-public void contains_altsInternalContext() {
71+public void checkClient_altsInternalContext() {
72+ClientCall<?,?> call = mock(ClientCall.class);
6173when(call.getAttributes()).thenReturn(Attributes.newBuilder()
6274 .set(AltsProtocolNegotiator.AUTH_CONTEXT_KEY, AltsInternalContext.getDefaultInstance())
6375 .build());
@@ -66,26 +78,57 @@ public void contains_altsInternalContext() {
6678 }
67796880@Test
69-public void from_altsInternalContext() {
81+public void createFrom_altsInternalContext() {
7082HandshakerResult handshakerResult =
7183HandshakerResult.newBuilder()
7284 .setPeerIdentity(Identity.newBuilder().setServiceAccount("remote@peer"))
7385 .setLocalIdentity(Identity.newBuilder().setServiceAccount("local@peer"))
7486 .build();
75-when(call.getAttributes()).thenReturn(Attributes.newBuilder()
76- .set(AltsProtocolNegotiator.AUTH_CONTEXT_KEY, new AltsInternalContext(handshakerResult))
77- .build());
788779-AltsContext context = AltsContextUtil.createFrom(call);
88+AltsContext context = AltsContextUtil.createFrom(Attributes.newBuilder()
89+ .set(AltsProtocolNegotiator.AUTH_CONTEXT_KEY, new AltsInternalContext(handshakerResult))
90+ .build());
8091assertEquals("remote@peer", context.getPeerServiceAccount());
8192assertEquals("local@peer", context.getLocalServiceAccount());
8293assertEquals(SecurityLevel.INTEGRITY_AND_PRIVACY, context.getSecurityLevel());
8394 }
84958596@Test(expected = IllegalArgumentException.class)
86-public void from_noAttributeValue() {
87-when(call.getAttributes()).thenReturn(Attributes.newBuilder().build());
97+public void createFrom_noAttributeValue() {
98+AltsContextUtil.createFrom(Attributes.newBuilder().build());
99+ }
8810089-AltsContextUtil.createFrom(call);
101+@Test
102+public void createFromServer_altsInternalContext() {
103+HandshakerResult handshakerResult =
104+HandshakerResult.newBuilder()
105+ .setPeerIdentity(Identity.newBuilder().setServiceAccount("remote@peer"))
106+ .setLocalIdentity(Identity.newBuilder().setServiceAccount("local@peer"))
107+ .build();
108+109+ServerCall<?,?> call = mock(ServerCall.class);
110+when(call.getAttributes()).thenReturn(Attributes.newBuilder()
111+ .set(AltsProtocolNegotiator.AUTH_CONTEXT_KEY, new AltsInternalContext(handshakerResult))
112+ .build());
113+114+AltsContext context = AltsContextUtil.createFrom(call);
115+assertEquals("remote@peer", context.getPeerServiceAccount());
116+ }
117+118+@Test
119+public void createFromClient_altsInternalContext() {
120+HandshakerResult handshakerResult =
121+HandshakerResult.newBuilder()
122+ .setPeerIdentity(Identity.newBuilder().setServiceAccount("remote@peer"))
123+ .setLocalIdentity(Identity.newBuilder().setServiceAccount("local@peer"))
124+ .build();
125+126+ClientCall<?,?> call = mock(ClientCall.class);
127+when(call.getAttributes()).thenReturn(Attributes.newBuilder()
128+ .set(AltsProtocolNegotiator.AUTH_CONTEXT_KEY, new AltsInternalContext(handshakerResult))
129+ .build());
130+131+AltsContext context = AltsContextUtil.createFrom(call);
132+assertEquals("remote@peer", context.getPeerServiceAccount());
90133 }
91134}