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