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 <gmock/gmock.h>
18 #include <grpc/grpc.h>
19 #include <grpcpp/grpcpp.h>
20 #include <gtest/gtest.h>
21
22 #include <thread>
23
24 #include "absl/strings/str_format.h"
25 #include "src/core/util/sync.h"
26 #include "src/proto/grpc/testing/empty.pb.h"
27 #include "src/proto/grpc/testing/test.grpc.pb.h"
28 #include "test/core/test_util/port.h"
29 #include "test/core/test_util/test_config.h"
30 #include "test/cpp/interop/xds_interop_server_lib.h"
31
32 namespace grpc {
33 namespace testing {
34 namespace {
35
ServerLoop(int port,grpc_core::Mutex * mutex,grpc_core::CondVar * condition,Server ** server)36 void ServerLoop(int port, grpc_core::Mutex* mutex,
37 grpc_core::CondVar* condition, Server** server) {
38 RunServer(false, port, /* should not be used */ -1, "127.0.0.1",
39 "test_server", [&](Server* s) {
40 grpc_core::MutexLock lock(mutex);
41 *server = s;
42 condition->Signal();
43 });
44 }
45
TEST(GetRpcBehaviorMetadataTest,ErrorCodeNoFilter)46 TEST(GetRpcBehaviorMetadataTest, ErrorCodeNoFilter) {
47 auto status = GetStatusForRpcBehaviorMetadata("error-code-16", "hostname");
48 ASSERT_TRUE(status.has_value());
49 ASSERT_EQ(status->error_code(), 16) << status->error_message();
50 }
51
TEST(GetRpcBehaviorMetadataTest,ErrorCodeThisHost)52 TEST(GetRpcBehaviorMetadataTest, ErrorCodeThisHost) {
53 auto status = GetStatusForRpcBehaviorMetadata(
54 "hostname=hostname error-code-16", "hostname");
55 ASSERT_TRUE(status.has_value());
56 ASSERT_EQ(status->error_code(), 16) << status->error_message();
57 }
58
TEST(GetRpcBehaviorMetadataTest,ErrorCodeOtherHost)59 TEST(GetRpcBehaviorMetadataTest, ErrorCodeOtherHost) {
60 auto status = GetStatusForRpcBehaviorMetadata(
61 "hostname=hostname2 error-code-16", "hostname");
62 ASSERT_FALSE(status.has_value());
63 }
64
TEST(GetRpcBehaviorMetadataTest,MalformedErrorCode)65 TEST(GetRpcBehaviorMetadataTest, MalformedErrorCode) {
66 auto status = GetStatusForRpcBehaviorMetadata("error-code-", "hostname");
67 ASSERT_TRUE(status.has_value());
68 ASSERT_EQ(status->error_code(), grpc::StatusCode::INVALID_ARGUMENT)
69 << status->error_message();
70 }
71
TEST(GetRpcBehaviorMetadataTest,MalformedHostName)72 TEST(GetRpcBehaviorMetadataTest, MalformedHostName) {
73 auto status =
74 GetStatusForRpcBehaviorMetadata("hostname= error-code-16", "hostname");
75 ASSERT_TRUE(status.has_value());
76 ASSERT_EQ(status->error_code(), grpc::StatusCode::INVALID_ARGUMENT)
77 << status->error_message();
78 }
79
TEST(GetRpcBehaviorMetadataTest,ErrorWhenUnsupported)80 TEST(GetRpcBehaviorMetadataTest, ErrorWhenUnsupported) {
81 auto status = GetStatusForRpcBehaviorMetadata("unsupported", "hostname");
82 ASSERT_TRUE(status.has_value());
83 ASSERT_EQ(status->error_code(), grpc::StatusCode::INVALID_ARGUMENT)
84 << status->error_message();
85 }
86
TEST(MaintenanceServerHookServiceTest,HookServiceInstalled)87 TEST(MaintenanceServerHookServiceTest, HookServiceInstalled) {
88 int port = grpc_pick_unused_port_or_die();
89 grpc_core::Mutex mutex;
90 grpc_core::CondVar condition;
91 Server* server = nullptr;
92 std::thread thread(ServerLoop, port, &mutex, &condition, &server);
93 {
94 grpc_core::MutexLock lock(&mutex);
95 while (server == nullptr) {
96 condition.Wait(&mutex);
97 }
98 }
99 HookService::Stub stub(CreateChannel(absl::StrFormat("127.0.0.1:%d", port),
100 InsecureChannelCredentials()));
101 ClientContext ctx;
102 Empty req, res;
103 auto status = stub.ClearReturnStatus(&ctx, req, &res);
104 EXPECT_EQ(status.error_code(), StatusCode::OK);
105 server->Shutdown();
106 thread.join();
107 }
108
109 } // namespace
110 } // namespace testing
111 } // namespace grpc
112
main(int argc,char ** argv)113 int main(int argc, char** argv) {
114 ::testing::InitGoogleTest(&argc, argv);
115 grpc::testing::TestEnvironment env(&argc, argv);
116 grpc_init();
117 auto result = RUN_ALL_TESTS();
118 grpc_shutdown();
119 return result;
120 }
121