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 <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22
23 #include <grpc/grpc.h>
24 #include <grpc/support/log.h>
25
26 #include "src/core/lib/security/credentials/alts/grpc_alts_credentials_options.h"
27
28 #define ALTS_CLIENT_OPTIONS_TEST_TARGET_SERVICE_ACCOUNT_1 "abc@google.com"
29 #define ALTS_CLIENT_OPTIONS_TEST_TARGET_SERVICE_ACCOUNT_2 "def@google.com"
30
31 const size_t kTargetServiceAccountNum = 2;
32
test_copy_client_options_failure()33 static void test_copy_client_options_failure() {
34 /* Initialization. */
35 grpc_alts_credentials_options* options =
36 grpc_alts_credentials_client_options_create();
37 /* Test. */
38 GPR_ASSERT(grpc_alts_credentials_options_copy(nullptr) == nullptr);
39 /* Cleanup. */
40 grpc_alts_credentials_options_destroy(options);
41 }
42
get_target_service_account_num(grpc_alts_credentials_options * options)43 static size_t get_target_service_account_num(
44 grpc_alts_credentials_options* options) {
45 auto client_options =
46 reinterpret_cast<grpc_alts_credentials_client_options*>(options);
47 size_t num = 0;
48 target_service_account* node = client_options->target_account_list_head;
49 while (node != nullptr) {
50 num++;
51 node = node->next;
52 }
53 return num;
54 }
55
test_client_options_api_success()56 static void test_client_options_api_success() {
57 /* Initialization. */
58 grpc_alts_credentials_options* options =
59 grpc_alts_credentials_client_options_create();
60 /* Set client options fields. */
61 grpc_alts_credentials_client_options_add_target_service_account(
62 options, ALTS_CLIENT_OPTIONS_TEST_TARGET_SERVICE_ACCOUNT_1);
63 grpc_alts_credentials_client_options_add_target_service_account(
64 options, ALTS_CLIENT_OPTIONS_TEST_TARGET_SERVICE_ACCOUNT_2);
65 /* Validate client option fields. */
66 GPR_ASSERT(get_target_service_account_num(options) ==
67 kTargetServiceAccountNum);
68 auto client_options =
69 reinterpret_cast<grpc_alts_credentials_client_options*>(options);
70 GPR_ASSERT(strcmp(client_options->target_account_list_head->data,
71 ALTS_CLIENT_OPTIONS_TEST_TARGET_SERVICE_ACCOUNT_2) == 0);
72 GPR_ASSERT(strcmp(client_options->target_account_list_head->next->data,
73 ALTS_CLIENT_OPTIONS_TEST_TARGET_SERVICE_ACCOUNT_1) == 0);
74 /* Perform a copy operation and validate its correctness. */
75 grpc_alts_credentials_options* new_options =
76 grpc_alts_credentials_options_copy(options);
77 GPR_ASSERT(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 GPR_ASSERT(strcmp(new_client_options->target_account_list_head->data,
82 ALTS_CLIENT_OPTIONS_TEST_TARGET_SERVICE_ACCOUNT_2) == 0);
83 GPR_ASSERT(strcmp(new_client_options->target_account_list_head->next->data,
84 ALTS_CLIENT_OPTIONS_TEST_TARGET_SERVICE_ACCOUNT_1) == 0);
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 /* Test. */
92 test_copy_client_options_failure();
93 test_client_options_api_success();
94 return 0;
95 }
96