• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2022 Google LLC
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  *      https://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 com.google.cloud.confidentialcomputing.v1;
18 
19 import static com.google.cloud.confidentialcomputing.v1.ConfidentialComputingClient.ListLocationsPagedResponse;
20 
21 import com.google.api.gax.core.NoCredentialsProvider;
22 import com.google.api.gax.grpc.GaxGrpcProperties;
23 import com.google.api.gax.grpc.testing.LocalChannelProvider;
24 import com.google.api.gax.grpc.testing.MockGrpcService;
25 import com.google.api.gax.grpc.testing.MockServiceHelper;
26 import com.google.api.gax.rpc.ApiClientHeaderProvider;
27 import com.google.api.gax.rpc.InvalidArgumentException;
28 import com.google.cloud.location.GetLocationRequest;
29 import com.google.cloud.location.ListLocationsRequest;
30 import com.google.cloud.location.ListLocationsResponse;
31 import com.google.cloud.location.Location;
32 import com.google.common.collect.Lists;
33 import com.google.protobuf.AbstractMessage;
34 import com.google.protobuf.Any;
35 import com.google.protobuf.Timestamp;
36 import io.grpc.StatusRuntimeException;
37 import java.io.IOException;
38 import java.util.Arrays;
39 import java.util.HashMap;
40 import java.util.List;
41 import java.util.UUID;
42 import javax.annotation.Generated;
43 import org.junit.After;
44 import org.junit.AfterClass;
45 import org.junit.Assert;
46 import org.junit.Before;
47 import org.junit.BeforeClass;
48 import org.junit.Test;
49 
50 @Generated("by gapic-generator-java")
51 public class ConfidentialComputingClientTest {
52   private static MockConfidentialComputing mockConfidentialComputing;
53   private static MockLocations mockLocations;
54   private static MockServiceHelper mockServiceHelper;
55   private LocalChannelProvider channelProvider;
56   private ConfidentialComputingClient client;
57 
58   @BeforeClass
startStaticServer()59   public static void startStaticServer() {
60     mockConfidentialComputing = new MockConfidentialComputing();
61     mockLocations = new MockLocations();
62     mockServiceHelper =
63         new MockServiceHelper(
64             UUID.randomUUID().toString(),
65             Arrays.<MockGrpcService>asList(mockConfidentialComputing, mockLocations));
66     mockServiceHelper.start();
67   }
68 
69   @AfterClass
stopServer()70   public static void stopServer() {
71     mockServiceHelper.stop();
72   }
73 
74   @Before
setUp()75   public void setUp() throws IOException {
76     mockServiceHelper.reset();
77     channelProvider = mockServiceHelper.createChannelProvider();
78     ConfidentialComputingSettings settings =
79         ConfidentialComputingSettings.newBuilder()
80             .setTransportChannelProvider(channelProvider)
81             .setCredentialsProvider(NoCredentialsProvider.create())
82             .build();
83     client = ConfidentialComputingClient.create(settings);
84   }
85 
86   @After
tearDown()87   public void tearDown() throws Exception {
88     client.close();
89   }
90 
91   @Test
createChallengeTest()92   public void createChallengeTest() throws Exception {
93     Challenge expectedResponse =
94         Challenge.newBuilder()
95             .setName(ChallengeName.of("[PROJECT]", "[LOCATION]", "[UUID]").toString())
96             .setCreateTime(Timestamp.newBuilder().build())
97             .setExpireTime(Timestamp.newBuilder().build())
98             .setUsed(true)
99             .setTpmNonce("tpmNonce-180239106")
100             .build();
101     mockConfidentialComputing.addResponse(expectedResponse);
102 
103     LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]");
104     Challenge challenge = Challenge.newBuilder().build();
105 
106     Challenge actualResponse = client.createChallenge(parent, challenge);
107     Assert.assertEquals(expectedResponse, actualResponse);
108 
109     List<AbstractMessage> actualRequests = mockConfidentialComputing.getRequests();
110     Assert.assertEquals(1, actualRequests.size());
111     CreateChallengeRequest actualRequest = ((CreateChallengeRequest) actualRequests.get(0));
112 
113     Assert.assertEquals(parent.toString(), actualRequest.getParent());
114     Assert.assertEquals(challenge, actualRequest.getChallenge());
115     Assert.assertTrue(
116         channelProvider.isHeaderSent(
117             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
118             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
119   }
120 
121   @Test
createChallengeExceptionTest()122   public void createChallengeExceptionTest() throws Exception {
123     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
124     mockConfidentialComputing.addException(exception);
125 
126     try {
127       LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]");
128       Challenge challenge = Challenge.newBuilder().build();
129       client.createChallenge(parent, challenge);
130       Assert.fail("No exception raised");
131     } catch (InvalidArgumentException e) {
132       // Expected exception.
133     }
134   }
135 
136   @Test
createChallengeTest2()137   public void createChallengeTest2() throws Exception {
138     Challenge expectedResponse =
139         Challenge.newBuilder()
140             .setName(ChallengeName.of("[PROJECT]", "[LOCATION]", "[UUID]").toString())
141             .setCreateTime(Timestamp.newBuilder().build())
142             .setExpireTime(Timestamp.newBuilder().build())
143             .setUsed(true)
144             .setTpmNonce("tpmNonce-180239106")
145             .build();
146     mockConfidentialComputing.addResponse(expectedResponse);
147 
148     String parent = "parent-995424086";
149     Challenge challenge = Challenge.newBuilder().build();
150 
151     Challenge actualResponse = client.createChallenge(parent, challenge);
152     Assert.assertEquals(expectedResponse, actualResponse);
153 
154     List<AbstractMessage> actualRequests = mockConfidentialComputing.getRequests();
155     Assert.assertEquals(1, actualRequests.size());
156     CreateChallengeRequest actualRequest = ((CreateChallengeRequest) actualRequests.get(0));
157 
158     Assert.assertEquals(parent, actualRequest.getParent());
159     Assert.assertEquals(challenge, actualRequest.getChallenge());
160     Assert.assertTrue(
161         channelProvider.isHeaderSent(
162             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
163             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
164   }
165 
166   @Test
createChallengeExceptionTest2()167   public void createChallengeExceptionTest2() throws Exception {
168     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
169     mockConfidentialComputing.addException(exception);
170 
171     try {
172       String parent = "parent-995424086";
173       Challenge challenge = Challenge.newBuilder().build();
174       client.createChallenge(parent, challenge);
175       Assert.fail("No exception raised");
176     } catch (InvalidArgumentException e) {
177       // Expected exception.
178     }
179   }
180 
181   @Test
verifyAttestationTest()182   public void verifyAttestationTest() throws Exception {
183     VerifyAttestationResponse expectedResponse =
184         VerifyAttestationResponse.newBuilder()
185             .setOidcClaimsToken("oidcClaimsToken-566980887")
186             .build();
187     mockConfidentialComputing.addResponse(expectedResponse);
188 
189     VerifyAttestationRequest request =
190         VerifyAttestationRequest.newBuilder()
191             .setChallenge(ChallengeName.of("[PROJECT]", "[LOCATION]", "[UUID]").toString())
192             .setGcpCredentials(GcpCredentials.newBuilder().build())
193             .setTpmAttestation(TpmAttestation.newBuilder().build())
194             .build();
195 
196     VerifyAttestationResponse actualResponse = client.verifyAttestation(request);
197     Assert.assertEquals(expectedResponse, actualResponse);
198 
199     List<AbstractMessage> actualRequests = mockConfidentialComputing.getRequests();
200     Assert.assertEquals(1, actualRequests.size());
201     VerifyAttestationRequest actualRequest = ((VerifyAttestationRequest) actualRequests.get(0));
202 
203     Assert.assertEquals(request.getChallenge(), actualRequest.getChallenge());
204     Assert.assertEquals(request.getGcpCredentials(), actualRequest.getGcpCredentials());
205     Assert.assertEquals(request.getTpmAttestation(), actualRequest.getTpmAttestation());
206     Assert.assertTrue(
207         channelProvider.isHeaderSent(
208             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
209             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
210   }
211 
212   @Test
verifyAttestationExceptionTest()213   public void verifyAttestationExceptionTest() throws Exception {
214     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
215     mockConfidentialComputing.addException(exception);
216 
217     try {
218       VerifyAttestationRequest request =
219           VerifyAttestationRequest.newBuilder()
220               .setChallenge(ChallengeName.of("[PROJECT]", "[LOCATION]", "[UUID]").toString())
221               .setGcpCredentials(GcpCredentials.newBuilder().build())
222               .setTpmAttestation(TpmAttestation.newBuilder().build())
223               .build();
224       client.verifyAttestation(request);
225       Assert.fail("No exception raised");
226     } catch (InvalidArgumentException e) {
227       // Expected exception.
228     }
229   }
230 
231   @Test
listLocationsTest()232   public void listLocationsTest() throws Exception {
233     Location responsesElement = Location.newBuilder().build();
234     ListLocationsResponse expectedResponse =
235         ListLocationsResponse.newBuilder()
236             .setNextPageToken("")
237             .addAllLocations(Arrays.asList(responsesElement))
238             .build();
239     mockLocations.addResponse(expectedResponse);
240 
241     ListLocationsRequest request =
242         ListLocationsRequest.newBuilder()
243             .setName("name3373707")
244             .setFilter("filter-1274492040")
245             .setPageSize(883849137)
246             .setPageToken("pageToken873572522")
247             .build();
248 
249     ListLocationsPagedResponse pagedListResponse = client.listLocations(request);
250 
251     List<Location> resources = Lists.newArrayList(pagedListResponse.iterateAll());
252 
253     Assert.assertEquals(1, resources.size());
254     Assert.assertEquals(expectedResponse.getLocationsList().get(0), resources.get(0));
255 
256     List<AbstractMessage> actualRequests = mockLocations.getRequests();
257     Assert.assertEquals(1, actualRequests.size());
258     ListLocationsRequest actualRequest = ((ListLocationsRequest) actualRequests.get(0));
259 
260     Assert.assertEquals(request.getName(), actualRequest.getName());
261     Assert.assertEquals(request.getFilter(), actualRequest.getFilter());
262     Assert.assertEquals(request.getPageSize(), actualRequest.getPageSize());
263     Assert.assertEquals(request.getPageToken(), actualRequest.getPageToken());
264     Assert.assertTrue(
265         channelProvider.isHeaderSent(
266             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
267             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
268   }
269 
270   @Test
listLocationsExceptionTest()271   public void listLocationsExceptionTest() throws Exception {
272     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
273     mockLocations.addException(exception);
274 
275     try {
276       ListLocationsRequest request =
277           ListLocationsRequest.newBuilder()
278               .setName("name3373707")
279               .setFilter("filter-1274492040")
280               .setPageSize(883849137)
281               .setPageToken("pageToken873572522")
282               .build();
283       client.listLocations(request);
284       Assert.fail("No exception raised");
285     } catch (InvalidArgumentException e) {
286       // Expected exception.
287     }
288   }
289 
290   @Test
getLocationTest()291   public void getLocationTest() throws Exception {
292     Location expectedResponse =
293         Location.newBuilder()
294             .setName("name3373707")
295             .setLocationId("locationId1541836720")
296             .setDisplayName("displayName1714148973")
297             .putAllLabels(new HashMap<String, String>())
298             .setMetadata(Any.newBuilder().build())
299             .build();
300     mockLocations.addResponse(expectedResponse);
301 
302     GetLocationRequest request = GetLocationRequest.newBuilder().setName("name3373707").build();
303 
304     Location actualResponse = client.getLocation(request);
305     Assert.assertEquals(expectedResponse, actualResponse);
306 
307     List<AbstractMessage> actualRequests = mockLocations.getRequests();
308     Assert.assertEquals(1, actualRequests.size());
309     GetLocationRequest actualRequest = ((GetLocationRequest) actualRequests.get(0));
310 
311     Assert.assertEquals(request.getName(), actualRequest.getName());
312     Assert.assertTrue(
313         channelProvider.isHeaderSent(
314             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
315             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
316   }
317 
318   @Test
getLocationExceptionTest()319   public void getLocationExceptionTest() throws Exception {
320     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
321     mockLocations.addException(exception);
322 
323     try {
324       GetLocationRequest request = GetLocationRequest.newBuilder().setName("name3373707").build();
325       client.getLocation(request);
326       Assert.fail("No exception raised");
327     } catch (InvalidArgumentException e) {
328       // Expected exception.
329     }
330   }
331 }
332