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