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