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 #ifdef GPR_WINDOWS
30
31 namespace grpc_core {
32 namespace internal {
33
34 bool check_windows_registry_product_name(HKEY root_key,
35 const char* reg_key_path,
36 const char* reg_key_name);
37
38 } // namespace internal
39 } // namespace grpc_core
40
check_bios_data_windows_test(const char * data)41 static bool check_bios_data_windows_test(const char* data) {
42 char const reg_key_path[] = "SYSTEM\\HardwareConfig\\Current\\";
43 char const reg_key_name[] = "grpcTestValueName";
44 // Modify the registry for the current user to contain the
45 // test value. We cannot use the system registry because the
46 // user may not have privileges to change it.
47 auto rc = RegSetKeyValueA(HKEY_CURRENT_USER, reg_key_path, reg_key_name,
48 REG_SZ, reinterpret_cast<const BYTE*>(data),
49 static_cast<DWORD>(strlen(data)));
50 if (rc != 0) {
51 return false;
52 }
53
54 auto result = grpc_core::internal::check_windows_registry_product_name(
55 HKEY_CURRENT_USER, reg_key_path, reg_key_name);
56
57 (void)RegDeleteKeyValueA(HKEY_CURRENT_USER, reg_key_path, reg_key_name);
58
59 return result;
60 }
61
TEST(CheckGcpEnvironmentWindowsTest,GcpEnvironmentCheckSuccess)62 TEST(CheckGcpEnvironmentWindowsTest, GcpEnvironmentCheckSuccess) {
63 // This is the only value observed in production.
64 ASSERT_TRUE(check_bios_data_windows_test("Google Compute Engine"));
65 // Be generous and accept other values that were accepted by the previous
66 // implementation.
67 ASSERT_TRUE(check_bios_data_windows_test("Google"));
68 ASSERT_TRUE(check_bios_data_windows_test("Google\n"));
69 ASSERT_TRUE(check_bios_data_windows_test("Google\r"));
70 ASSERT_TRUE(check_bios_data_windows_test("Google\r\n"));
71 ASSERT_TRUE(check_bios_data_windows_test(" Google \r\n"));
72 ASSERT_TRUE(check_bios_data_windows_test(" \t\t Google\r\n"));
73 ASSERT_TRUE(check_bios_data_windows_test(" \t\t Google\t\t \r\n"));
74 }
75
TEST(CheckGcpEnvironmentWindowsTest,GcpEnvironmentCheckFailure)76 TEST(CheckGcpEnvironmentWindowsTest, GcpEnvironmentCheckFailure) {
77 ASSERT_FALSE(check_bios_data_windows_test("\t\tAmazon\n"));
78 ASSERT_FALSE(check_bios_data_windows_test(" Amazon\r\n"));
79 }
80
81 #endif // GPR_WINDOWS
82
main(int argc,char ** argv)83 int main(int argc, char** argv) {
84 ::testing::InitGoogleTest(&argc, argv);
85 return RUN_ALL_TESTS();
86 }
87