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