1 //
2 // Copyright 2022 gRPC authors.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 #include <thread>
18
19 #include <gmock/gmock.h>
20 #include <gtest/gtest.h>
21
22 #include "absl/strings/str_format.h"
23
24 #include <grpc/grpc.h>
25 #include <grpcpp/grpcpp.h>
26
27 #include "src/core/lib/gprpp/sync.h"
28 #include "src/proto/grpc/testing/empty.pb.h"
29 #include "src/proto/grpc/testing/test.grpc.pb.h"
30 #include "test/core/util/port.h"
31 #include "test/core/util/test_config.h"
32 #include "test/cpp/interop/xds_interop_server_lib.h"
33
34 namespace grpc {
35 namespace testing {
36 namespace {
37
ServerLoop(int port,grpc_core::Mutex * mutex,grpc_core::CondVar * condition,Server ** server)38 void ServerLoop(int port, grpc_core::Mutex* mutex,
39 grpc_core::CondVar* condition, Server** server) {
40 RunServer(false, /*enable_csm_observability=*/false, port,
41 /* should not be used */ -1, "127.0.0.1", "test_server",
42 [&](Server* s) {
43 grpc_core::MutexLock lock(mutex);
44 *server = s;
45 condition->Signal();
46 });
47 }
48
TEST(GetRpcBehaviorMetadataTest,ErrorCodeNoFilter)49 TEST(GetRpcBehaviorMetadataTest, ErrorCodeNoFilter) {
50 auto status = GetStatusForRpcBehaviorMetadata("error-code-16", "hostname");
51 ASSERT_TRUE(status.has_value());
52 ASSERT_EQ(status->error_code(), 16) << status->error_message();
53 }
54
TEST(GetRpcBehaviorMetadataTest,ErrorCodeThisHost)55 TEST(GetRpcBehaviorMetadataTest, ErrorCodeThisHost) {
56 auto status = GetStatusForRpcBehaviorMetadata(
57 "hostname=hostname error-code-16", "hostname");
58 ASSERT_TRUE(status.has_value());
59 ASSERT_EQ(status->error_code(), 16) << status->error_message();
60 }
61
TEST(GetRpcBehaviorMetadataTest,ErrorCodeOtherHost)62 TEST(GetRpcBehaviorMetadataTest, ErrorCodeOtherHost) {
63 auto status = GetStatusForRpcBehaviorMetadata(
64 "hostname=hostname2 error-code-16", "hostname");
65 ASSERT_FALSE(status.has_value());
66 }
67
TEST(GetRpcBehaviorMetadataTest,MalformedErrorCode)68 TEST(GetRpcBehaviorMetadataTest, MalformedErrorCode) {
69 auto status = GetStatusForRpcBehaviorMetadata("error-code-", "hostname");
70 ASSERT_TRUE(status.has_value());
71 ASSERT_EQ(status->error_code(), grpc::StatusCode::INVALID_ARGUMENT)
72 << status->error_message();
73 }
74
TEST(GetRpcBehaviorMetadataTest,MalformedHostName)75 TEST(GetRpcBehaviorMetadataTest, MalformedHostName) {
76 auto status =
77 GetStatusForRpcBehaviorMetadata("hostname= error-code-16", "hostname");
78 ASSERT_TRUE(status.has_value());
79 ASSERT_EQ(status->error_code(), grpc::StatusCode::INVALID_ARGUMENT)
80 << status->error_message();
81 }
82
TEST(GetRpcBehaviorMetadataTest,ErrorWhenUnsupported)83 TEST(GetRpcBehaviorMetadataTest, ErrorWhenUnsupported) {
84 auto status = GetStatusForRpcBehaviorMetadata("unsupported", "hostname");
85 ASSERT_TRUE(status.has_value());
86 ASSERT_EQ(status->error_code(), grpc::StatusCode::INVALID_ARGUMENT)
87 << status->error_message();
88 }
89
TEST(MaintenanceServerHookServiceTest,HookServiceInstalled)90 TEST(MaintenanceServerHookServiceTest, HookServiceInstalled) {
91 int port = grpc_pick_unused_port_or_die();
92 grpc_core::Mutex mutex;
93 grpc_core::CondVar condition;
94 Server* server = nullptr;
95 std::thread thread(ServerLoop, port, &mutex, &condition, &server);
96 {
97 grpc_core::MutexLock lock(&mutex);
98 while (server == nullptr) {
99 condition.Wait(&mutex);
100 }
101 }
102 HookService::Stub stub(CreateChannel(absl::StrFormat("127.0.0.1:%d", port),
103 InsecureChannelCredentials()));
104 ClientContext ctx;
105 Empty req, res;
106 auto status = stub.ClearReturnStatus(&ctx, req, &res);
107 EXPECT_EQ(status.error_code(), StatusCode::OK);
108 server->Shutdown();
109 thread.join();
110 }
111
112 } // namespace
113 } // namespace testing
114 } // namespace grpc
115
main(int argc,char ** argv)116 int main(int argc, char** argv) {
117 ::testing::InitGoogleTest(&argc, argv);
118 grpc::testing::TestEnvironment env(&argc, argv);
119 grpc_init();
120 auto result = RUN_ALL_TESTS();
121 grpc_shutdown();
122 return result;
123 }
124