Browse Source

Don't allocate Test and TestResult objects on the heap

Michał Śledź 1 month ago
parent
commit
607ae54fae

+ 3 - 3
test/capi_connectivity.cpp

@@ -393,8 +393,8 @@ error:
 
 #include <stdexcept>
 
-TestResult *test_capi_connectivity() {
+TestResult test_capi_connectivity() {
 	if (test_capi_connectivity_main())
-		return new TestResult(false, "Connection failed");
-	return new TestResult(true);
+		return TestResult(false, "Connection failed");
+	return TestResult(true);
 }

+ 3 - 3
test/capi_track.cpp

@@ -239,8 +239,8 @@ error:
 
 #include <stdexcept>
 
-TestResult *test_capi_track() {
+TestResult test_capi_track() {
 	if (test_capi_track_main())
-		return new TestResult(false, "Connection failed");
-	return new TestResult(true);
+		return TestResult(false, "Connection failed");
+	return TestResult(true);
 }

+ 3 - 3
test/capi_websocketserver.cpp

@@ -165,10 +165,10 @@ error:
 
 #include <stdexcept>
 
-TestResult *test_capi_websocketserver() {
+TestResult test_capi_websocketserver() {
 	if (test_capi_websocketserver_main())
-		return new TestResult(false, "WebSocketServer test failed");
-	return new TestResult(true);
+		return TestResult(false, "WebSocketServer test failed");
+	return TestResult(true);
 }
 
 #endif

+ 21 - 21
test/connectivity.cpp

@@ -22,13 +22,13 @@ using namespace std;
 
 template <class T> weak_ptr<T> make_weak_ptr(shared_ptr<T> ptr) { return ptr; }
 
-TestResult *test_connectivity(bool);
+TestResult test_connectivity(bool);
 
-TestResult *test_connectivity() { return test_connectivity(false); }
+TestResult test_connectivity() { return test_connectivity(false); }
 
-TestResult *test_connectivity_fail_on_wrong_fingerprint() { return test_connectivity(true); }
+TestResult test_connectivity_fail_on_wrong_fingerprint() { return test_connectivity(true); }
 
-TestResult *test_connectivity(bool signal_wrong_fingerprint) {
+TestResult test_connectivity(bool signal_wrong_fingerprint) {
 	InitLogger(LogLevel::Debug);
 
 	Configuration config1;
@@ -135,7 +135,7 @@ TestResult *test_connectivity(bool signal_wrong_fingerprint) {
 	auto dc1 = pc1.createDataChannel("test");
 
 	if (dc1->id().has_value())
-		return new TestResult(false, "DataChannel stream id assigned before connection");
+		return TestResult(false, "DataChannel stream id assigned before connection");
 
 	dc1->onOpen([wdc1 = make_weak_ptr(dc1)]() {
 		if (auto dc1 = wdc1.lock()) {
@@ -161,9 +161,9 @@ TestResult *test_connectivity(bool signal_wrong_fingerprint) {
 	if (pc1.state() != PeerConnection::State::Connected ||
 	    pc2.state() != PeerConnection::State::Connected) {
 		if (signal_wrong_fingerprint) {
-			return new TestResult(true);
+			return TestResult(true);
 		} else {
-			return new TestResult(false, "PeerConnection is not connected");
+			return TestResult(false, "PeerConnection is not connected");
 		}
 	}
 
@@ -171,23 +171,23 @@ TestResult *test_connectivity(bool signal_wrong_fingerprint) {
 	     pc1.iceState() != PeerConnection::IceState::Completed) ||
 	    (pc2.iceState() != PeerConnection::IceState::Connected &&
 	     pc2.iceState() != PeerConnection::IceState::Completed))
-		return new TestResult(false, "ICE is not connected");
+		return TestResult(false, "ICE is not connected");
 
 	if (!adc2 || !adc2->isOpen() || !dc1->isOpen())
-		return new TestResult(false, "DataChannel is not open");
+		return TestResult(false, "DataChannel is not open");
 
 	if (adc2->label() != "test")
-		return new TestResult(false, "Wrong DataChannel label");
+		return TestResult(false, "Wrong DataChannel label");
 
 	if (dc1->maxMessageSize() != CUSTOM_MAX_MESSAGE_SIZE ||
 	    dc2->maxMessageSize() != CUSTOM_MAX_MESSAGE_SIZE)
-		return new TestResult(false, "DataChannel max message size is incorrect");
+		return TestResult(false, "DataChannel max message size is incorrect");
 
 	if (!dc1->id().has_value())
-		return new TestResult(false, "DataChannel stream id is not assigned");
+		return TestResult(false, "DataChannel stream id is not assigned");
 
 	if (dc1->id().value() != adc2->id().value())
-		return new TestResult(false, "DataChannel stream ids do not match");
+		return TestResult(false, "DataChannel stream ids do not match");
 
 	if (auto addr = pc1.localAddress())
 		cout << "Local address 1:  " << *addr << endl;
@@ -256,16 +256,16 @@ TestResult *test_connectivity(bool signal_wrong_fingerprint) {
 		this_thread::sleep_for(1s);
 
 	if (!asecond2 || !asecond2->isOpen() || !second1->isOpen())
-		return new TestResult(false, "Second DataChannel is not open");
+		return TestResult(false, "Second DataChannel is not open");
 
 	if (asecond2->label() != "second")
-		return new TestResult(false, "Wrong second DataChannel label");
+		return TestResult(false, "Wrong second DataChannel label");
 
 	if (!second2->id().has_value() || !asecond2->id().has_value())
-		return new TestResult(false, "Second DataChannel stream id is not assigned");
+		return TestResult(false, "Second DataChannel stream id is not assigned");
 
 	if (second2->id().value() != asecond2->id().value())
-		return new TestResult(false, "Second DataChannel stream ids do not match");
+		return TestResult(false, "Second DataChannel stream ids do not match");
 
 	// Delay close of peer 2 to check closing works properly
 	pc1.close();
@@ -273,7 +273,7 @@ TestResult *test_connectivity(bool signal_wrong_fingerprint) {
 	pc2.close();
 	this_thread::sleep_for(1s);
 
-	return new TestResult(true);
+	return TestResult(true);
 }
 
 const char* key_pem =
@@ -296,7 +296,7 @@ const char* cert_pem =
 "Ma9ayzQy\n"
 "-----END CERTIFICATE-----\n";
 
-TestResult *test_pem() {
+TestResult test_pem() {
 	InitLogger(LogLevel::Debug);
 
 	Configuration config1;
@@ -324,7 +324,7 @@ TestResult *test_pem() {
 
 	if (f != "07:E5:6F:2A:1A:0C:2C:32:0E:C1:C3:9C:34:5A:78:4E:A5:8B:32:05:D1:57:D6:F4:E7:02:41:12:"
 	         "E6:01:C6:8F")
-		return new TestResult(false, "The fingerprint of the specified certificate do not match");
+		return TestResult(false, "The fingerprint of the specified certificate do not match");
 
-	return new TestResult(true);
+	return TestResult(true);
 }

+ 33 - 33
test/main.cpp

@@ -21,18 +21,18 @@ using chrono::milliseconds;
 using chrono::seconds;
 using chrono::steady_clock;
 
-TestResult *test_connectivity();
-TestResult *test_connectivity_fail_on_wrong_fingerprint();
-TestResult *test_pem();
-TestResult *test_negotiated();
-TestResult *test_reliability();
-TestResult *test_turn_connectivity();
-TestResult *test_track();
-TestResult *test_capi_connectivity();
-TestResult *test_capi_track();
-TestResult *test_websocket();
-TestResult *test_websocketserver();
-TestResult *test_capi_websocketserver();
+TestResult test_connectivity();
+TestResult test_connectivity_fail_on_wrong_fingerprint();
+TestResult test_pem();
+TestResult test_negotiated();
+TestResult test_reliability();
+TestResult test_turn_connectivity();
+TestResult test_track();
+TestResult test_capi_connectivity();
+TestResult test_capi_track();
+TestResult test_websocket();
+TestResult test_websocketserver();
+TestResult test_capi_websocketserver();
 size_t benchmark(chrono::milliseconds duration);
 
 void test_benchmark() {
@@ -46,53 +46,53 @@ void test_benchmark() {
 		throw runtime_error("Goodput is too low");
 }
 
-TestResult *testCppCleanup() {
+TestResult testCppCleanup() {
 	try {
 		// Every created object must have been destroyed, otherwise the wait will block
 		if (rtc::Cleanup().wait_for(10s) == future_status::timeout)
-			return new TestResult(false, "timeout");
-		return new TestResult(true);
+			return TestResult(false, "timeout");
+		return TestResult(true);
 	} catch (const exception &e) {
-		return new TestResult(false, e.what());
+		return TestResult(false, e.what());
 	}
 }
 
-TestResult *testCCleanup() {
+TestResult testCCleanup() {
 	try {
 		rtcCleanup();
-		return new TestResult(true);
+		return TestResult(true);
 	} catch (const exception &e) {
-		return new TestResult(false, e.what());
+		return TestResult(false, e.what());
 	}
 }
 
 static const auto tests = {
     // C++ API tests
-    new Test("WebRTC connectivity", test_connectivity),
-    new Test("WebRTC broken fingerprint", test_connectivity_fail_on_wrong_fingerprint),
-    new Test("pem", test_pem),
+    Test("WebRTC connectivity", test_connectivity),
+    Test("WebRTC broken fingerprint", test_connectivity_fail_on_wrong_fingerprint),
+    Test("pem", test_pem),
     // TODO: Temporarily disabled as the Open Relay TURN server is unreliable
     // new Test("WebRTC TURN connectivity", test_turn_connectivity),
-    new Test("WebRTC negotiated DataChannel", test_negotiated),
-    new Test("WebRTC reliability mode", test_reliability),
+    Test("WebRTC negotiated DataChannel", test_negotiated),
+    Test("WebRTC reliability mode", test_reliability),
 #if RTC_ENABLE_MEDIA
-    new Test("WebRTC track", test_track),
+    Test("WebRTC track", test_track),
 #endif
 #if RTC_ENABLE_WEBSOCKET
     // TODO: Temporarily disabled as the echo service is unreliable
     // new Test("WebSocket", test_websocket),
-    new Test("WebSocketServer", test_websocketserver),
+    Test("WebSocketServer", test_websocketserver),
 #endif
-    new Test("WebRTC Cpp API cleanup", testCppCleanup),
+    Test("WebRTC Cpp API cleanup", testCppCleanup),
     // C API tests
-    new Test("WebRTC C API connectivity", test_capi_connectivity),
+    Test("WebRTC C API connectivity", test_capi_connectivity),
 #if RTC_ENABLE_MEDIA
-    new Test("WebRTC C API track", test_capi_track),
+    Test("WebRTC C API track", test_capi_track),
 #endif
 #if RTC_ENABLE_WEBSOCKET
-    new Test("WebSocketServer C API", test_capi_websocketserver),
+    Test("WebSocketServer C API", test_capi_websocketserver),
 #endif
-    new Test("WebRTC C API cleanup", testCCleanup),
+    Test("WebRTC C API cleanup", testCCleanup),
 };
 
 int main(int argc, char **argv) {
@@ -103,8 +103,8 @@ int main(int argc, char **argv) {
 	startTime = steady_clock::now();
 
 	for (auto test : tests) {
-		auto res = test->run();
-		if (res->success) {
+		auto res = test.run();
+		if (res.success) {
 			success_tests++;
 		} else {
 			failed_tests++;

+ 5 - 5
test/negotiated.cpp

@@ -18,7 +18,7 @@
 using namespace rtc;
 using namespace std;
 
-TestResult *test_negotiated() {
+TestResult test_negotiated() {
 	InitLogger(LogLevel::Debug);
 
 	Configuration config1;
@@ -67,10 +67,10 @@ TestResult *test_negotiated() {
 
 	if (pc1.state() != PeerConnection::State::Connected ||
 	    pc2.state() != PeerConnection::State::Connected)
-		return new TestResult(false, "PeerConnection is not connected");
+		return TestResult(false, "PeerConnection is not connected");
 
 	if (!negotiated1->isOpen() || !negotiated2->isOpen())
-		return new TestResult(false, "Negotiated DataChannel is not open");
+		return TestResult(false, "Negotiated DataChannel is not open");
 
 	std::atomic<bool> received = false;
 	negotiated2->onMessage([&received](const variant<binary, string> &message) {
@@ -88,7 +88,7 @@ TestResult *test_negotiated() {
 		this_thread::sleep_for(1s);
 
 	if (!received)
-		return new TestResult(false, "Negotiated DataChannel failed");
+		return TestResult(false, "Negotiated DataChannel failed");
 
 	// Delay close of peer 2 to check closing works properly
 	pc1.close();
@@ -96,5 +96,5 @@ TestResult *test_negotiated() {
 	pc2.close();
 	this_thread::sleep_for(1s);
 
-	return new TestResult(true);
+	return TestResult(true);
 }

+ 5 - 5
test/reliability.cpp

@@ -18,7 +18,7 @@
 using namespace rtc;
 using namespace std;
 
-TestResult *test_reliability() {
+TestResult test_reliability() {
 	InitLogger(LogLevel::Debug);
 
 	Configuration config1;
@@ -115,15 +115,15 @@ TestResult *test_reliability() {
 
 	if (pc1.state() != PeerConnection::State::Connected ||
 	    pc2.state() != PeerConnection::State::Connected)
-		return new TestResult(false, "PeerConnection is not connected");
+		return TestResult(false, "PeerConnection is not connected");
 
 	if (failed)
-		return new TestResult(false, "Incorrect reliability settings");
+		return TestResult(false, "Incorrect reliability settings");
 
 	if (count != 4)
-		return new TestResult(false, "Some DataChannels are not open");
+		return TestResult(false, "Some DataChannels are not open");
 
 	pc1.close();
 
-	return new TestResult(true);
+	return TestResult(true);
 }

+ 6 - 6
test/test.hpp

@@ -22,17 +22,17 @@ public:
 class Test {
 public:
 	string name;
-	function<TestResult *(void)> f;
+	function<TestResult(void)> f;
 
-	Test(string name, std::function<TestResult *(void)> testFunc) : name(name), f(testFunc) {}
+	Test(string name, std::function<TestResult(void)> testFunc) : name(name), f(testFunc) {}
 
-	TestResult *run() {
+	TestResult run() {
 		cout << endl << "*** Running " << name << " test" << endl;
-		TestResult *res = this->f();
-		if (res->success) {
+		TestResult res = this->f();
+		if (res.success) {
 			cout << "*** Finished " << name << " test" << endl;
 		} else {
-			cerr << name << " test failed. Reason: " << res->err_reason << endl;
+			cerr << name << " test failed. Reason: " << res.err_reason << endl;
 		}
 
 		return res;

+ 7 - 7
test/track.cpp

@@ -22,7 +22,7 @@ using namespace std;
 
 template <class T> weak_ptr<T> make_weak_ptr(shared_ptr<T> ptr) { return ptr; }
 
-TestResult *test_track() {
+TestResult test_track() {
 	InitLogger(LogLevel::Debug);
 
 	Configuration config1;
@@ -110,7 +110,7 @@ TestResult *test_track() {
 	const auto mediaSdp2 = string(Description::Media(mediaSdp1));
 	if (mediaSdp2 != mediaSdp1) {
 		cout << mediaSdp2 << endl;
-		return new TestResult(false, "Media description parsing test failed");
+		return TestResult(false, "Media description parsing test failed");
 	}
 
 	auto t1 = pc1.addTrack(media);
@@ -124,10 +124,10 @@ TestResult *test_track() {
 
 	if (pc1.state() != PeerConnection::State::Connected ||
 	    pc2.state() != PeerConnection::State::Connected)
-		return new TestResult(false, "PeerConnection is not connected");
+		return TestResult(false, "PeerConnection is not connected");
 
 	if (!at2 || !at2->isOpen() || !t1->isOpen())
-		return new TestResult(false, "Track is not open");
+		return TestResult(false, "Track is not open");
 
 	// Test renegotiation
 	newTrackMid = "added";
@@ -149,7 +149,7 @@ TestResult *test_track() {
 		this_thread::sleep_for(1s);
 
 	if (!at2 || !at2->isOpen() || !t1->isOpen())
-		return new TestResult(false, "Renegotiated track is not open");
+		return TestResult(false, "Renegotiated track is not open");
 
 	std::vector<std::byte> payload = {std::byte{0}, std::byte{1}, std::byte{2}, std::byte{3}};
 	std::vector<std::byte> rtpRaw(sizeof(RtpHeader) + payload.size());
@@ -188,7 +188,7 @@ TestResult *test_track() {
 	this_thread::sleep_for(1s);
 
 	if (!t1->isClosed() || !t2->isClosed())
-		return new TestResult(false, "Track is not closed");
+		return TestResult(false, "Track is not closed");
 
-	return new TestResult(true);
+	return TestResult(true);
 }

+ 10 - 10
test/turn_connectivity.cpp

@@ -20,7 +20,7 @@ using namespace std;
 
 template <class T> weak_ptr<T> make_weak_ptr(shared_ptr<T> ptr) { return ptr; }
 
-TestResult *test_turn_connectivity() {
+TestResult test_turn_connectivity() {
 	InitLogger(LogLevel::Debug);
 
 	Configuration config1;
@@ -134,16 +134,16 @@ TestResult *test_turn_connectivity() {
 
 	if (pc1.state() != PeerConnection::State::Connected ||
 	    pc2.state() != PeerConnection::State::Connected)
-		return new TestResult(false, "PeerConnection is not connected");
+		return TestResult(false, "PeerConnection is not connected");
 
 	if ((pc1.iceState() != PeerConnection::IceState::Connected &&
 	     pc1.iceState() != PeerConnection::IceState::Completed) ||
 	    (pc2.iceState() != PeerConnection::IceState::Connected &&
 	     pc2.iceState() != PeerConnection::IceState::Completed))
-		return new TestResult(false, "ICE is not connected");
+		return TestResult(false, "ICE is not connected");
 
 	if (!adc2 || !adc2->isOpen() || !dc1->isOpen())
-		return new TestResult(false, "DataChannel is not open");
+		return TestResult(false, "DataChannel is not open");
 
 	if (auto addr = pc1.localAddress())
 		cout << "Local address 1:  " << *addr << endl;
@@ -156,13 +156,13 @@ TestResult *test_turn_connectivity() {
 
 	Candidate local, remote;
 	if (!pc1.getSelectedCandidatePair(&local, &remote))
-		return new TestResult(false, "getSelectedCandidatePair failed");
+		return TestResult(false, "getSelectedCandidatePair failed");
 
 	cout << "Local candidate 1:  " << local << endl;
 	cout << "Remote candidate 1: " << remote << endl;
 
 	if (local.type() != Candidate::Type::Relayed)
-		return new TestResult(false, "Connection is not relayed as expected");
+		return TestResult(false, "Connection is not relayed as expected");
 
 	// Try to open a second data channel with another label
 	shared_ptr<DataChannel> second2;
@@ -215,7 +215,7 @@ TestResult *test_turn_connectivity() {
 		this_thread::sleep_for(1s);
 
 	if (!asecond2 || !asecond2->isOpen() || !second1->isOpen())
-		return new TestResult(false, "Second DataChannel is not open");
+		return TestResult(false, "Second DataChannel is not open");
 
 	// Try to open a negotiated channel
 	DataChannelInit init;
@@ -225,7 +225,7 @@ TestResult *test_turn_connectivity() {
 	auto negotiated2 = pc2.createDataChannel("negoctated", init);
 
 	if (!negotiated1->isOpen() || !negotiated2->isOpen())
-		return new TestResult(false, "Negotiated DataChannel is not open");
+		return TestResult(false, "Negotiated DataChannel is not open");
 
 	std::atomic<bool> received = false;
 	negotiated2->onMessage([&received](const variant<binary, string> &message) {
@@ -243,7 +243,7 @@ TestResult *test_turn_connectivity() {
 		this_thread::sleep_for(1s);
 
 	if (!received)
-		return new TestResult(false, "Negotiated DataChannel failed");
+		return TestResult(false, "Negotiated DataChannel failed");
 
 	// Delay close of peer 2 to check closing works properly
 	pc1.close();
@@ -251,5 +251,5 @@ TestResult *test_turn_connectivity() {
 	pc2.close();
 	this_thread::sleep_for(1s);
 
-	return new TestResult(true);
+	return TestResult(true);
 }

+ 4 - 4
test/websocket.cpp

@@ -22,7 +22,7 @@ using namespace std;
 
 template <class T> weak_ptr<T> make_weak_ptr(shared_ptr<T> ptr) { return ptr; }
 
-TestResult *test_websocket() {
+TestResult test_websocket() {
 	InitLogger(LogLevel::Debug);
 
 	const string myMessage = "Hello world from libdatachannel";
@@ -58,15 +58,15 @@ TestResult *test_websocket() {
 		this_thread::sleep_for(1s);
 
 	if (!ws.isOpen())
-		return new TestResult(false, "WebSocket is not open");
+		return TestResult(false, "WebSocket is not open");
 
 	if (!received)
-		return new TestResult(false, "Expected message not received");
+		return TestResult(false, "Expected message not received");
 
 	ws.close();
 	this_thread::sleep_for(1s);
 
-	return new TestResult(true);
+	return TestResult(true);
 }
 
 #endif

+ 4 - 4
test/websocketserver.cpp

@@ -22,7 +22,7 @@ using namespace std;
 
 template <class T> weak_ptr<T> make_weak_ptr(shared_ptr<T> ptr) { return ptr; }
 
-TestResult *test_websocketserver() {
+TestResult test_websocketserver() {
 	InitLogger(LogLevel::Debug);
 
 	WebSocketServer::Configuration serverConfig;
@@ -96,10 +96,10 @@ TestResult *test_websocketserver() {
 		this_thread::sleep_for(1s);
 
 	if (!ws.isOpen())
-		return new TestResult(false, "WebSocket is not open");
+		return TestResult(false, "WebSocket is not open");
 
 	if (!received || !maxSizeReceived)
-		return new TestResult(false, "Expected messages not received");
+		return TestResult(false, "Expected messages not received");
 
 	ws.close();
 	this_thread::sleep_for(1s);
@@ -107,7 +107,7 @@ TestResult *test_websocketserver() {
 	server.stop();
 	this_thread::sleep_for(1s);
 
-	return new TestResult(true);
+	return TestResult(true);
 }
 
 #endif