Debian Codegraph @

test/core/security/grpc_alts_credentials_options_test.cc

raw
1 /*
2 *
3 * Copyright 2018 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19 #include "src/core/lib/security/credentials/alts/grpc_alts_credentials_options.h"
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include <gtest/gtest.h>
26
27 #include <grpc/grpc.h>
28 #include <grpc/support/log.h>
29
30 #define ALTS_CLIENT_OPTIONS_TEST_TARGET_SERVICE_ACCOUNT_1 "[email protected]"
31 #define ALTS_CLIENT_OPTIONS_TEST_TARGET_SERVICE_ACCOUNT_2 "[email protected]"
32
33 const size_t kTargetServiceAccountNum = 2;
34
35 TEST(GrpcAltsCredentialsOptionsTest, CopyClientOptionsFailure) {
36 /* Initialization. */
37 grpc_alts_credentials_options* options =
38 grpc_alts_credentials_client_options_create();
39 /* Test. */
40 ASSERT_EQ(grpc_alts_credentials_options_copy(nullptr), nullptr);
41 /* Cleanup. */
42 grpc_alts_credentials_options_destroy(options);
43 }
44
45 static size_t get_target_service_account_num(
46 grpc_alts_credentials_options* options) {
47 auto client_options =
48 reinterpret_cast<grpc_alts_credentials_client_options*>(options);
49 size_t num = 0;
50 target_service_account* node = client_options->target_account_list_head;
51 while (node != nullptr) {
52 num++;
53 node = node->next;
54 }
55 return num;
56 }
57
58 TEST(GrpcAltsCredentialsOptionsTest, ClientOptionsApiSuccess) {
59 /* Initialization. */
60 grpc_alts_credentials_options* options =
61 grpc_alts_credentials_client_options_create();
62 /* Set client options fields. */
63 grpc_alts_credentials_client_options_add_target_service_account(
64 options, ALTS_CLIENT_OPTIONS_TEST_TARGET_SERVICE_ACCOUNT_1);
65 grpc_alts_credentials_client_options_add_target_service_account(
66 options, ALTS_CLIENT_OPTIONS_TEST_TARGET_SERVICE_ACCOUNT_2);
67 /* Validate client option fields. */
68 ASSERT_EQ(get_target_service_account_num(options), kTargetServiceAccountNum);
69 auto client_options =
70 reinterpret_cast<grpc_alts_credentials_client_options*>(options);
71 ASSERT_STREQ(client_options->target_account_list_head->data,
72 ALTS_CLIENT_OPTIONS_TEST_TARGET_SERVICE_ACCOUNT_2);
73 ASSERT_STREQ(client_options->target_account_list_head->next->data,
74 ALTS_CLIENT_OPTIONS_TEST_TARGET_SERVICE_ACCOUNT_1);
75 /* Perform a copy operation and validate its correctness. */
76 grpc_alts_credentials_options* new_options =
77 grpc_alts_credentials_options_copy(options);
78 ASSERT_EQ(get_target_service_account_num(new_options),
79 kTargetServiceAccountNum);
80 auto new_client_options =
81 reinterpret_cast<grpc_alts_credentials_client_options*>(new_options);
82 ASSERT_STREQ(new_client_options->target_account_list_head->data,
83 ALTS_CLIENT_OPTIONS_TEST_TARGET_SERVICE_ACCOUNT_2);
84 ASSERT_STREQ(new_client_options->target_account_list_head->next->data,
85 ALTS_CLIENT_OPTIONS_TEST_TARGET_SERVICE_ACCOUNT_1);
86 /* Cleanup.*/
87 grpc_alts_credentials_options_destroy(options);
88 grpc_alts_credentials_options_destroy(new_options);
89 }
90
91 int main(int argc, char** argv) {
92 ::testing::InitGoogleTest(&argc, argv);
93 return RUN_ALL_TESTS();
94 }