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 #include "src/core/lib/gprpp/crash.h"
31
32 #define ALTS_CLIENT_OPTIONS_TEST_TARGET_SERVICE_ACCOUNT_1 "[email protected]"
33 #define ALTS_CLIENT_OPTIONS_TEST_TARGET_SERVICE_ACCOUNT_2 "[email protected]"
34
35 const size_t kTargetServiceAccountNum = 2;
36
TEST(GrpcAltsCredentialsOptionsTest,CopyClientOptionsFailure)37 TEST(GrpcAltsCredentialsOptionsTest, CopyClientOptionsFailure) {
38 // Initialization.
39 grpc_alts_credentials_options* options =
40 grpc_alts_credentials_client_options_create();
41 // Test.
42 ASSERT_EQ(grpc_alts_credentials_options_copy(nullptr), nullptr);
43 // Cleanup.
44 grpc_alts_credentials_options_destroy(options);
45 }
46
get_target_service_account_num(grpc_alts_credentials_options * options)47 static size_t get_target_service_account_num(
48 grpc_alts_credentials_options* options) {
49 auto client_options =
50 reinterpret_cast<grpc_alts_credentials_client_options*>(options);
51 size_t num = 0;
52 target_service_account* node = client_options->target_account_list_head;
53 while (node != nullptr) {
54 num++;
55 node = node->next;
56 }
57 return num;
58 }
59
TEST(GrpcAltsCredentialsOptionsTest,ClientOptionsApiSuccess)60 TEST(GrpcAltsCredentialsOptionsTest, ClientOptionsApiSuccess) {
61 // Initialization.
62 grpc_alts_credentials_options* options =
63 grpc_alts_credentials_client_options_create();
64 // Set client options fields.
65 grpc_alts_credentials_client_options_add_target_service_account(
66 options, ALTS_CLIENT_OPTIONS_TEST_TARGET_SERVICE_ACCOUNT_1);
67 grpc_alts_credentials_client_options_add_target_service_account(
68 options, ALTS_CLIENT_OPTIONS_TEST_TARGET_SERVICE_ACCOUNT_2);
69 // Validate client option fields.
70 ASSERT_EQ(get_target_service_account_num(options), kTargetServiceAccountNum);
71 auto client_options =
72 reinterpret_cast<grpc_alts_credentials_client_options*>(options);
73 ASSERT_STREQ(client_options->target_account_list_head->data,
74 ALTS_CLIENT_OPTIONS_TEST_TARGET_SERVICE_ACCOUNT_2);
75 ASSERT_STREQ(client_options->target_account_list_head->next->data,
76 ALTS_CLIENT_OPTIONS_TEST_TARGET_SERVICE_ACCOUNT_1);
77 // Perform a copy operation and validate its correctness.
78 grpc_alts_credentials_options* new_options =
79 grpc_alts_credentials_options_copy(options);
80 ASSERT_EQ(get_target_service_account_num(new_options),
81 kTargetServiceAccountNum);
82 auto new_client_options =
83 reinterpret_cast<grpc_alts_credentials_client_options*>(new_options);
84 ASSERT_STREQ(new_client_options->target_account_list_head->data,
85 ALTS_CLIENT_OPTIONS_TEST_TARGET_SERVICE_ACCOUNT_2);
86 ASSERT_STREQ(new_client_options->target_account_list_head->next->data,
87 ALTS_CLIENT_OPTIONS_TEST_TARGET_SERVICE_ACCOUNT_1);
88 // Cleanup.
89 grpc_alts_credentials_options_destroy(options);
90 grpc_alts_credentials_options_destroy(new_options);
91 }
92
main(int argc,char ** argv)93 int main(int argc, char** argv) {
94 ::testing::InitGoogleTest(&argc, argv);
95 return RUN_ALL_TESTS();
96 }
97