1 /*
2 *
3 * Copyright 2016 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 "google/protobuf/duration.upb.h"
20 #include "upb/upb.hpp"
21
22 #include <grpc/grpc.h>
23 #include <grpcpp/impl/codegen/config.h>
24 #include <gtest/gtest.h>
25
26 #include "src/core/ext/filters/client_channel/lb_policy/grpclb/load_balancer_api.h"
27 #include "src/core/lib/iomgr/sockaddr.h"
28 #include "src/core/lib/iomgr/sockaddr_utils.h"
29 #include "src/proto/grpc/lb/v1/load_balancer.pb.h" // C++ version
30
31 #include "test/core/util/test_config.h"
32
33 namespace grpc {
34 namespace {
35
36 using grpc::lb::v1::LoadBalanceRequest;
37 using grpc::lb::v1::LoadBalanceResponse;
38
39 class GrpclbTest : public ::testing::Test {
40 protected:
SetUpTestCase()41 static void SetUpTestCase() { grpc_init(); }
42
TearDownTestCase()43 static void TearDownTestCase() { grpc_shutdown(); }
44 };
45
Ip4ToPackedString(const char * ip_str)46 std::string Ip4ToPackedString(const char* ip_str) {
47 struct in_addr ip4;
48 GPR_ASSERT(inet_pton(AF_INET, ip_str, &ip4) == 1);
49 return std::string(reinterpret_cast<const char*>(&ip4), sizeof(ip4));
50 }
51
PackedStringToIp(const grpc_core::GrpcLbServer & server)52 std::string PackedStringToIp(const grpc_core::GrpcLbServer& server) {
53 char ip_str[46] = {0};
54 int af = -1;
55 if (server.ip_size == 4) {
56 af = AF_INET;
57 } else if (server.ip_size == 16) {
58 af = AF_INET6;
59 } else {
60 abort();
61 }
62 GPR_ASSERT(inet_ntop(af, (void*)server.ip_addr, ip_str, 46) != nullptr);
63 return ip_str;
64 }
65
TEST_F(GrpclbTest,CreateRequest)66 TEST_F(GrpclbTest, CreateRequest) {
67 const std::string service_name = "AServiceName";
68 LoadBalanceRequest request;
69 upb::Arena arena;
70 grpc_slice slice =
71 grpc_core::GrpcLbRequestCreate(service_name.c_str(), arena.ptr());
72 const int num_bytes_written = GRPC_SLICE_LENGTH(slice);
73 EXPECT_GT(num_bytes_written, 0);
74 request.ParseFromArray(GRPC_SLICE_START_PTR(slice), num_bytes_written);
75 EXPECT_EQ(request.initial_request().name(), service_name);
76 grpc_slice_unref(slice);
77 }
78
TEST_F(GrpclbTest,ParseInitialResponse)79 TEST_F(GrpclbTest, ParseInitialResponse) {
80 // Construct response to parse.
81 LoadBalanceResponse response;
82 auto* initial_response = response.mutable_initial_response();
83 auto* client_stats_report_interval =
84 initial_response->mutable_client_stats_report_interval();
85 client_stats_report_interval->set_seconds(123);
86 client_stats_report_interval->set_nanos(456000000);
87 const std::string encoded_response = response.SerializeAsString();
88 grpc_slice encoded_slice =
89 grpc_slice_from_copied_string(encoded_response.c_str());
90 // Test parsing.
91 grpc_core::GrpcLbResponse resp;
92 upb::Arena arena;
93 ASSERT_TRUE(
94 grpc_core::GrpcLbResponseParse(encoded_slice, arena.ptr(), &resp));
95 grpc_slice_unref(encoded_slice);
96 EXPECT_EQ(resp.type, resp.INITIAL);
97 EXPECT_EQ(resp.client_stats_report_interval, 123456);
98 EXPECT_EQ(resp.serverlist.size(), 0);
99 }
100
TEST_F(GrpclbTest,ParseResponseServerList)101 TEST_F(GrpclbTest, ParseResponseServerList) {
102 // Construct response to parse.
103 LoadBalanceResponse response;
104 auto* serverlist = response.mutable_server_list();
105 auto* server = serverlist->add_servers();
106 server->set_ip_address(Ip4ToPackedString("127.0.0.1"));
107 server->set_port(12345);
108 server->set_load_balance_token("rate_limting");
109 server->set_drop(true);
110 server = response.mutable_server_list()->add_servers();
111 server->set_ip_address(Ip4ToPackedString("10.0.0.1"));
112 server->set_port(54321);
113 server->set_load_balance_token("load_balancing");
114 server->set_drop(true);
115 const std::string encoded_response = response.SerializeAsString();
116 const grpc_slice encoded_slice = grpc_slice_from_copied_buffer(
117 encoded_response.data(), encoded_response.size());
118 // Test parsing.
119 grpc_core::GrpcLbResponse resp;
120 upb::Arena arena;
121 ASSERT_TRUE(
122 grpc_core::GrpcLbResponseParse(encoded_slice, arena.ptr(), &resp));
123 grpc_slice_unref(encoded_slice);
124 EXPECT_EQ(resp.type, resp.SERVERLIST);
125 EXPECT_EQ(resp.serverlist.size(), 2);
126 EXPECT_EQ(PackedStringToIp(resp.serverlist[0]), "127.0.0.1");
127 EXPECT_EQ(resp.serverlist[0].port, 12345);
128 EXPECT_STREQ(resp.serverlist[0].load_balance_token, "rate_limting");
129 EXPECT_TRUE(resp.serverlist[0].drop);
130 EXPECT_EQ(PackedStringToIp(resp.serverlist[1]), "10.0.0.1");
131 EXPECT_EQ(resp.serverlist[1].port, 54321);
132 EXPECT_STREQ(resp.serverlist[1].load_balance_token, "load_balancing");
133 EXPECT_TRUE(resp.serverlist[1].drop);
134 }
135
136 } // namespace
137 } // namespace grpc
138
main(int argc,char ** argv)139 int main(int argc, char** argv) {
140 grpc::testing::TestEnvironment env(argc, argv);
141 ::testing::InitGoogleTest(&argc, argv);
142 int ret = RUN_ALL_TESTS();
143 return ret;
144 }
145