xref: /aosp_15_r20/external/grpc-grpc/test/cpp/ext/csm/mesh_id_test.cc (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1 //
2 //
3 // Copyright 2023 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 "gtest/gtest.h"
20 
21 #include <grpcpp/grpcpp.h>
22 
23 #include "src/core/lib/gpr/tmpfile.h"
24 #include "src/core/lib/gprpp/env.h"
25 #include "src/cpp/ext/csm/metadata_exchange.h"
26 #include "test/core/util/test_config.h"
27 
28 namespace grpc {
29 namespace testing {
30 namespace {
31 
32 class TestScenario {
33  public:
34   enum class XdsBootstrapSource : std::uint8_t { kFromFile, kFromConfig };
35 
TestScenario(XdsBootstrapSource bootstrap_source)36   explicit TestScenario(XdsBootstrapSource bootstrap_source)
37       : bootstrap_source_(bootstrap_source) {}
38 
Name(const::testing::TestParamInfo<TestScenario> & info)39   static std::string Name(const ::testing::TestParamInfo<TestScenario>& info) {
40     switch (info.param.bootstrap_source_) {
41       case TestScenario::XdsBootstrapSource::kFromFile:
42         return "BootstrapFromFile";
43       case TestScenario::XdsBootstrapSource::kFromConfig:
44         return "BootstrapFromConfig";
45     }
46   }
47 
bootstrap_source() const48   XdsBootstrapSource bootstrap_source() const { return bootstrap_source_; }
49 
50  private:
51   XdsBootstrapSource bootstrap_source_;
52 };
53 
54 class MeshIdTest : public ::testing::TestWithParam<TestScenario> {
55  protected:
SetBootstrap(const char * bootstrap)56   void SetBootstrap(const char* bootstrap) {
57     switch (GetParam().bootstrap_source()) {
58       case TestScenario::XdsBootstrapSource::kFromFile: {
59         ASSERT_EQ(bootstrap_file_name_, nullptr);
60         FILE* bootstrap_file =
61             gpr_tmpfile("gcp_observability_config", &bootstrap_file_name_);
62         fputs(bootstrap, bootstrap_file);
63         fclose(bootstrap_file);
64         grpc_core::SetEnv("GRPC_XDS_BOOTSTRAP", bootstrap_file_name_);
65         break;
66       }
67       case TestScenario::XdsBootstrapSource::kFromConfig:
68         grpc_core::SetEnv("GRPC_XDS_BOOTSTRAP_CONFIG", bootstrap);
69         break;
70     }
71   }
72 
~MeshIdTest()73   ~MeshIdTest() override {
74     grpc_core::UnsetEnv("GRPC_GCP_OBSERVABILITY_CONFIG");
75     grpc_core::UnsetEnv("GRPC_XDS_BOOTSTRAP");
76     if (bootstrap_file_name_ != nullptr) {
77       remove(bootstrap_file_name_);
78       gpr_free(bootstrap_file_name_);
79     }
80   }
81 
82   char* bootstrap_file_name_ = nullptr;
83 };
84 
TEST_P(MeshIdTest,Empty)85 TEST_P(MeshIdTest, Empty) {
86   SetBootstrap("");
87   EXPECT_EQ(grpc::internal::GetMeshId(), "unknown");
88 }
89 
TEST_P(MeshIdTest,NothingSet)90 TEST_P(MeshIdTest, NothingSet) {
91   EXPECT_EQ(grpc::internal::GetMeshId(), "unknown");
92 }
93 
TEST_P(MeshIdTest,BadJson)94 TEST_P(MeshIdTest, BadJson) {
95   SetBootstrap("{");
96   EXPECT_EQ(grpc::internal::GetMeshId(), "unknown");
97 }
98 
TEST_P(MeshIdTest,UnexpectedMeshIdFormatType1)99 TEST_P(MeshIdTest, UnexpectedMeshIdFormatType1) {
100   SetBootstrap(
101       "{\"node\": {\"id\": "
102       "\"abcdef\"}}");
103   EXPECT_EQ(grpc::internal::GetMeshId(), "unknown");
104 }
105 
TEST_P(MeshIdTest,UnexpectedMeshIdFormatType2)106 TEST_P(MeshIdTest, UnexpectedMeshIdFormatType2) {
107   SetBootstrap(
108       "{\"node\": {\"id\": "
109       "\"projects/1234567890/networks/mesh-id/nodes/"
110       "01234567-89ab-4def-8123-456789abcdef\"}}");
111   EXPECT_EQ(grpc::internal::GetMeshId(), "unknown");
112 }
113 
TEST_P(MeshIdTest,Basic)114 TEST_P(MeshIdTest, Basic) {
115   SetBootstrap(
116       "{\"node\": {\"id\": "
117       "\"projects/1234567890/networks/mesh:mesh-id/nodes/"
118       "01234567-89ab-4def-8123-456789abcdef\"}}");
119   EXPECT_EQ(grpc::internal::GetMeshId(), "mesh-id");
120 }
121 
122 INSTANTIATE_TEST_SUITE_P(
123     MeshId, MeshIdTest,
124     ::testing::Values(
125         TestScenario(TestScenario::XdsBootstrapSource::kFromFile),
126         TestScenario(TestScenario::XdsBootstrapSource::kFromConfig)),
127     &TestScenario::Name);
128 
129 }  // namespace
130 }  // namespace testing
131 }  // namespace grpc
132 
main(int argc,char ** argv)133 int main(int argc, char** argv) {
134   grpc::testing::TestEnvironment env(&argc, argv);
135   ::testing::InitGoogleTest(&argc, argv);
136   return RUN_ALL_TESTS();
137 }
138