-
Notifications
You must be signed in to change notification settings - Fork 229
Expand file tree
/
Copy pathIXSocketTest.cpp
More file actions
113 lines (95 loc) · 3.53 KB
/
IXSocketTest.cpp
File metadata and controls
113 lines (95 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
* IXSocketTest.cpp
* Author: Benjamin Sergeant
* Copyright (c) 2019 Machine Zone. All rights reserved.
*/
#include "IXTest.h"
#include "catch.hpp"
#include <iostream>
#include <ixwebsocket/IXCancellationRequest.h>
#include <ixwebsocket/IXSocket.h>
#include <ixwebsocket/IXSocketFactory.h>
#include <string.h>
using namespace ix;
namespace ix
{
void testSocket(const std::string& host,
int port,
const std::string& request,
std::shared_ptr<Socket> socket,
int expectedStatus,
int timeoutSecs)
{
std::string errMsg;
static std::atomic<bool> requestInitCancellation(false);
auto isCancellationRequested =
makeCancellationRequestWithTimeout(timeoutSecs, requestInitCancellation);
bool success = socket->connect(host, port, errMsg, isCancellationRequested);
TLogger() << "errMsg: " << errMsg;
REQUIRE(success);
TLogger() << "Sending request: " << request << "to " << host << ":" << port;
REQUIRE(socket->writeBytes(request, isCancellationRequested));
auto lineResult = socket->readLine(isCancellationRequested);
auto lineValid = lineResult.first;
auto line = lineResult.second;
TLogger() << "read error: " << strerror(Socket::getErrno());
REQUIRE(lineValid);
int status = -1;
REQUIRE(sscanf(line.c_str(), "HTTP/1.1 %d", &status) == 1);
REQUIRE(status == expectedStatus);
}
} // namespace ix
TEST_CASE("socket", "[socket]")
{
SECTION("Connect to a local websocket server over a free port. Send GET request without "
"header. Should return 400")
{
// Start a server first which we'll hit with our socket code
int port = getFreePort();
ix::WebSocketServer server(port);
REQUIRE(startWebSocketEchoServer(server));
std::string errMsg;
bool tls = false;
SocketTLSOptions tlsOptions;
std::shared_ptr<Socket> socket = createSocket(tls, -1, errMsg, tlsOptions);
std::string host("127.0.0.1");
std::stringstream ss;
ss << "GET / HTTP/1.1\r\n";
ss << "Host: " << host << "\r\n";
ss << "\r\n";
std::string request(ss.str());
int expectedStatus = 400;
int timeoutSecs = 3;
testSocket(host, port, request, socket, expectedStatus, timeoutSecs);
}
#if defined(IXWEBSOCKET_USE_TLS)
SECTION("Connect to google HTTPS server over port 443. Send GET request without header. Should "
"return 200")
{
std::string errMsg;
bool tls = true;
SocketTLSOptions tlsOptions;
tlsOptions.caFile = "cacert.pem";
std::shared_ptr<Socket> socket = createSocket(tls, -1, errMsg, tlsOptions);
std::string host("www.google.com");
int port = 443;
std::string request("GET / HTTP/1.1\r\n\r\n");
int expectedStatus = 200;
int timeoutSecs = 3;
testSocket(host, port, request, socket, expectedStatus, timeoutSecs);
}
#endif
TEST_CASE("isValidIpAddress") {
// Valid IPv4 addresses
CHECK(isValidIpAddress("127.0.0.1"));
CHECK(isValidIpAddress("192.168.0.1"));
CHECK(isValidIpAddress("10.0.0.1"));
CHECK(isValidIpAddress("172.16.0.1"));
// Invalid IPv4 addresses
CHECK(!isValidIpAddress("256.0.0.1"));
CHECK(!isValidIpAddress("1.2.3.4.5"));
CHECK(!isValidIpAddress("192.168.0."));
CHECK(!isValidIpAddress("192.168.0.-1"));
CHECK(!isValidIpAddress("192.168.0.256"));
}
}