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