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