-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.cpp
More file actions
101 lines (83 loc) · 3.41 KB
/
Client.cpp
File metadata and controls
101 lines (83 loc) · 3.41 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
/* ===============================
Client.cpp (Camera Testing)
=============================== */
#define NOMINMAX
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "Client.h"
#include <iostream>
#include <winsock2.h>
#include <ws2tcpip.h>
#include "doctest.h"
#include <regex>
#pragma comment(lib, "ws2_32.lib")
Client::Client() {}
std::string Client::sendCommand(const std::string& command) {
WSADATA wsaData;
int result = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (result != 0) {
throw std::runtime_error("WSAStartup failed");
}
SOCKET clientSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (clientSocket == INVALID_SOCKET) {
WSACleanup();
throw std::runtime_error("Socket creation failed");
}
sockaddr_in serverAddr = {};
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(8080);
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
if (connect(clientSocket, (struct sockaddr*)&serverAddr, sizeof(serverAddr)) == SOCKET_ERROR) {
closesocket(clientSocket);
WSACleanup();
throw std::runtime_error("Connection failed");
}
result = send(clientSocket, command.c_str(), static_cast<int>(command.length()), 0);
if (result == SOCKET_ERROR) {
closesocket(clientSocket);
WSACleanup();
throw std::runtime_error("Send failed");
}
char buffer[1024] = { 0 };
int bytesReceived = recv(clientSocket, buffer, sizeof(buffer) - 1, 0);
std::string response;
if (bytesReceived > 0) {
buffer[bytesReceived] = '\0';
response = buffer;
}
closesocket(clientSocket);
WSACleanup();
return response;
}
TEST_CASE("Camera function tests") {
Client client;
SUBCASE("Status check") {
std::string response = client.sendCommand("GET /192.168.1.98/status 192.168.2.2");
CHECK(response.find("Camera is idle") != std::string::npos);
CHECK(response.find("Night vision is disabled") != std::string::npos);
}
SUBCASE("Recording check") {
// Start recording
std::string response = client.sendCommand("POST /192.168.1.98/start 192.168.2.2");
CHECK(response.find("Camera started recording") != std::string::npos);
// Check status of camera
response = client.sendCommand("GET /192.168.1.98/status 192.168.2.2");
CHECK(response.find("Recording is in progress") != std::string::npos);
// Stop recording
response = client.sendCommand("POST /192.168.1.98/stop 192.168.2.2");
CHECK(response.find("Camera stopped recording.") != std::string::npos);
// Verify stopped status
response = client.sendCommand("GET /192.168.1.98/status 192.168.2.2");
CHECK(response.find("Camera is idle") != std::string::npos);
}
SUBCASE("Night vision check") {
// Toggle night vision on
std::string response = client.sendCommand("POST /192.168.1.98/toggle/nightvision 192.168.2.2");
CHECK(response.find("Night vision is now enabled") != std::string::npos);
// Verify night vision status
response = client.sendCommand("GET /192.168.1.98/status 192.168.2.2");
CHECK(response.find("Night vision is enabled") != std::string::npos);
// Toggle night vision off
response = client.sendCommand("POST /192.168.1.98/toggle/nightvision 192.168.2.2");
CHECK(response.find("Night vision is now disabled") != std::string::npos);
}
}