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


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:58:45