]>
Commit | Line | Data |
---|---|---|
f67539c2 TL |
1 | /* |
2 | * Copyright (c) 2017 Uber Technologies, Inc. | |
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 "jaegertracing/net/IPAddress.h" | |
18 | #include "jaegertracing/net/Socket.h" | |
19 | #include "jaegertracing/testutils/TUDPTransport.h" | |
20 | #include <array> | |
21 | #include <cstdint> | |
22 | #include <gtest/gtest.h> | |
23 | #include <string> | |
24 | #include <thread> | |
25 | ||
26 | #ifdef _MSC_VER | |
27 | #pragma warning(disable : 4267) | |
28 | #endif | |
29 | ||
30 | namespace jaegertracing { | |
31 | namespace testutils { | |
32 | namespace { | |
33 | ||
34 | constexpr auto kBufferSize = 256; | |
35 | ||
36 | } // anonymous namespace | |
37 | ||
38 | TEST(TUDPTransport, testUDPTransport) | |
39 | { | |
40 | const std::string message("test"); | |
41 | ||
42 | TUDPTransport server(net::IPAddress::v4("127.0.0.1", 0)); | |
43 | server.open(); // Not necessary. Just making sure this is called. | |
44 | ASSERT_TRUE(server.isOpen()); | |
45 | ||
46 | const auto serverAddr = server.addr(); | |
47 | std::thread clientThread([serverAddr, message]() { | |
48 | net::Socket connUDP; | |
49 | connUDP.open(AF_INET, SOCK_DGRAM); | |
50 | const auto numWritten = | |
51 | ::sendto(connUDP.handle(), | |
52 | message.c_str(), | |
53 | message.size(), | |
54 | 0, | |
55 | reinterpret_cast<const ::sockaddr*>(&serverAddr.addr()), | |
56 | serverAddr.addrLen()); | |
57 | ASSERT_EQ(numWritten, message.size()); | |
58 | ||
59 | std::array<char, kBufferSize> buffer; | |
60 | const auto numRead = ::recvfrom( | |
61 | connUDP.handle(), &buffer[0], buffer.size(), 0, nullptr, 0); | |
62 | const std::string received(&buffer[0], &buffer[numRead]); | |
63 | ASSERT_EQ(message.size(), numRead); | |
64 | ASSERT_EQ(message, received); | |
65 | ||
66 | connUDP.close(); | |
67 | }); | |
68 | ||
69 | std::array<uint8_t, kBufferSize> buffer; | |
70 | const auto numRead = server.readAll(&buffer[0], message.size()); | |
71 | ASSERT_LT(0, numRead); | |
72 | server.write(&buffer[0], numRead); | |
73 | ||
74 | clientThread.join(); | |
75 | server.close(); | |
76 | } | |
77 | ||
78 | } // namespace testutils | |
79 | } // namespace jaegertracing |