alts: support altsCallCredentials in GoogleDefaultChannelCredentials … · grpc/grpc-java@ba8ab79
1+/*
2+ * Copyright 2024 The gRPC Authors
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 io.grpc.alts;
18+19+import static org.mockito.Mockito.any;
20+import static org.mockito.Mockito.never;
21+import static org.mockito.Mockito.times;
22+import static org.mockito.Mockito.verify;
23+24+import io.grpc.Attributes;
25+import io.grpc.CallCredentials;
26+import io.grpc.CallCredentials.RequestInfo;
27+import io.grpc.MethodDescriptor;
28+import io.grpc.SecurityLevel;
29+import io.grpc.alts.internal.AltsInternalContext;
30+import io.grpc.alts.internal.AltsProtocolNegotiator;
31+import io.grpc.testing.TestMethodDescriptors;
32+import org.junit.Rule;
33+import org.junit.Test;
34+import org.junit.runner.RunWith;
35+import org.junit.runners.JUnit4;
36+import org.mockito.Mock;
37+import org.mockito.junit.MockitoJUnit;
38+import org.mockito.junit.MockitoRule;
39+40+/** Unit tests for {@link DualCallCredentials}. */
41+@RunWith(JUnit4.class)
42+public class DualCallCredentialsTest {
43+44+@Rule public final MockitoRule mocks = MockitoJUnit.rule();
45+46+@Mock CallCredentials tlsCallCredentials;
47+48+@Mock CallCredentials altsCallCredentials;
49+50+private static final String AUTHORITY = "testauthority";
51+private static final SecurityLevel SECURITY_LEVEL = SecurityLevel.PRIVACY_AND_INTEGRITY;
52+53+@Test
54+public void invokeTlsCallCredentials() {
55+DualCallCredentials callCredentials =
56+new DualCallCredentials(tlsCallCredentials, altsCallCredentials);
57+RequestInfo requestInfo = new RequestInfoImpl(false);
58+callCredentials.applyRequestMetadata(requestInfo, null, null);
59+60+verify(altsCallCredentials, never()).applyRequestMetadata(any(), any(), any());
61+verify(tlsCallCredentials, times(1)).applyRequestMetadata(requestInfo, null, null);
62+ }
63+64+@Test
65+public void invokeAltsCallCredentials() {
66+DualCallCredentials callCredentials =
67+new DualCallCredentials(tlsCallCredentials, altsCallCredentials);
68+RequestInfo requestInfo = new RequestInfoImpl(true);
69+callCredentials.applyRequestMetadata(requestInfo, null, null);
70+71+verify(altsCallCredentials, times(1)).applyRequestMetadata(requestInfo, null, null);
72+verify(tlsCallCredentials, never()).applyRequestMetadata(any(), any(), any());
73+ }
74+75+private static final class RequestInfoImpl extends CallCredentials.RequestInfo {
76+private Attributes attrs;
77+78+RequestInfoImpl(boolean hasAltsContext) {
79+attrs =
80+hasAltsContext
81+ ? Attributes.newBuilder()
82+ .set(
83+AltsProtocolNegotiator.AUTH_CONTEXT_KEY,
84+AltsInternalContext.getDefaultInstance())
85+ .build()
86+ : Attributes.EMPTY;
87+ }
88+89+@Override
90+public MethodDescriptor<?, ?> getMethodDescriptor() {
91+return TestMethodDescriptors.voidMethod();
92+ }
93+94+@Override
95+public SecurityLevel getSecurityLevel() {
96+return SECURITY_LEVEL;
97+ }
98+99+@Override
100+public String getAuthority() {
101+return AUTHORITY;
102+ }
103+104+@Override
105+public Attributes getTransportAttrs() {
106+return attrs;
107+ }
108+ }
109+}