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 <grpc/support/port_platform.h>
20
21 #include "src/core/lib/security/credentials/alts/check_gcp_environment.h"
22
23 #if GPR_LINUX
24
25 #include <stdio.h>
26 #include <string.h>
27
28 #include <grpc/support/alloc.h>
29 #include <grpc/support/log.h>
30
31 #include "src/core/lib/gpr/tmpfile.h"
32
check_bios_data_linux_test(const char * data)33 static bool check_bios_data_linux_test(const char* data) {
34 /* Create a file with contents data. */
35 char* filename = nullptr;
36 FILE* fp = gpr_tmpfile("check_gcp_environment_test", &filename);
37 GPR_ASSERT(filename != nullptr);
38 GPR_ASSERT(fp != nullptr);
39 GPR_ASSERT(fwrite(data, 1, strlen(data), fp) == strlen(data));
40 fclose(fp);
41 bool result = grpc_core::internal::check_bios_data(
42 reinterpret_cast<const char*>(filename));
43 /* Cleanup. */
44 remove(filename);
45 gpr_free(filename);
46 return result;
47 }
48
test_gcp_environment_check_success()49 static void test_gcp_environment_check_success() {
50 /* Exact match. */
51 GPR_ASSERT(check_bios_data_linux_test("Google"));
52 GPR_ASSERT(check_bios_data_linux_test("Google Compute Engine"));
53 /* With leading and trailing whitespaces. */
54 GPR_ASSERT(check_bios_data_linux_test(" Google "));
55 GPR_ASSERT(check_bios_data_linux_test("Google "));
56 GPR_ASSERT(check_bios_data_linux_test(" Google"));
57 GPR_ASSERT(check_bios_data_linux_test(" Google Compute Engine "));
58 GPR_ASSERT(check_bios_data_linux_test("Google Compute Engine "));
59 GPR_ASSERT(check_bios_data_linux_test(" Google Compute Engine"));
60 /* With leading and trailing \t and \n. */
61 GPR_ASSERT(check_bios_data_linux_test("\t\tGoogle Compute Engine\t"));
62 GPR_ASSERT(check_bios_data_linux_test("Google Compute Engine\n"));
63 GPR_ASSERT(check_bios_data_linux_test("\n\n\tGoogle Compute Engine \n\t\t"));
64 }
65
test_gcp_environment_check_failure()66 static void test_gcp_environment_check_failure() {
67 GPR_ASSERT(!check_bios_data_linux_test("non_existing-file"));
68 GPR_ASSERT(!check_bios_data_linux_test("Google-Chrome"));
69 GPR_ASSERT(!check_bios_data_linux_test("Amazon"));
70 GPR_ASSERT(!check_bios_data_linux_test("Google-Chrome\t\t"));
71 GPR_ASSERT(!check_bios_data_linux_test("Amazon"));
72 GPR_ASSERT(!check_bios_data_linux_test("\n"));
73 }
74
main(int argc,char ** argv)75 int main(int argc, char** argv) {
76 /* Tests. */
77 test_gcp_environment_check_success();
78 test_gcp_environment_check_failure();
79 return 0;
80 }
81
82 #else // GPR_LINUX
83
main(int argc,char ** argv)84 int main(int argc, char** argv) { return 0; }
85
86 #endif // GPR_LINUX
87