1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 07:48:11 +00:00

test-bindtodevice: Port to LibMain

This ports 'test-bindtodevice' to LibMain and convert the local 'test'
function to return ErrorOr<T>.
This commit is contained in:
Kenneth Myhra 2022-03-28 22:44:10 +02:00 committed by Brian Gianforcaro
parent e302fac34b
commit b47a9ab4dc
2 changed files with 40 additions and 31 deletions

View file

@ -196,6 +196,7 @@ target_link_libraries(tail LibMain)
target_link_libraries(tar LibMain LibArchive LibCompress) target_link_libraries(tar LibMain LibArchive LibCompress)
target_link_libraries(tee LibMain) target_link_libraries(tee LibMain)
target_link_libraries(telws LibProtocol LibLine LibMain) target_link_libraries(telws LibProtocol LibLine LibMain)
target_link_libraries(test-bindtodevice LibMain)
target_link_libraries(test-fuzz LibCore LibGemini LibGfx LibHTTP LibIPC LibJS LibMarkdown LibShell) target_link_libraries(test-fuzz LibCore LibGemini LibGfx LibHTTP LibIPC LibJS LibMarkdown LibShell)
target_link_libraries(test-imap LibIMAP LibMain) target_link_libraries(test-imap LibIMAP LibMain)
target_link_libraries(test-pthread LibThreading) target_link_libraries(test-pthread LibThreading)

View file

@ -1,43 +1,43 @@
/* /*
* Copyright (c) 2020, the SerenityOS developers. * Copyright (c) 2020-2022, the SerenityOS developers.
* *
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
#include <AK/Function.h> #include <AK/Function.h>
#include <AK/IPv4Address.h> #include <AK/IPv4Address.h>
#include <LibCore/System.h>
#include <LibMain/Main.h>
#include <net/if.h> #include <net/if.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <unistd.h>
static void test_invalid(int); static void test_invalid(int);
static void test_no_route(int); static void test_no_route(int);
static void test_valid(int); static void test_valid(int);
static void test_send(int); static void test_send(int);
static void test(Function<void(int)> test_fn) static ErrorOr<void> test(Function<void(int)> test_fn)
{ {
int fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); auto fd = TRY(Core::System::socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP));
if (fd < 0) {
perror("socket");
return;
}
test_fn(fd); test_fn(fd);
// be a responsible boi // be a responsible boi
close(fd); TRY(Core::System::close(fd));
return {};
} }
auto main() -> int ErrorOr<int> serenity_main(Main::Arguments)
{ {
test(test_invalid); TRY(test(test_invalid));
test(test_valid); TRY(test(test_valid));
test(test_no_route); TRY(test(test_no_route));
test(test_send); TRY(test(test_send));
return 0;
} }
void test_invalid(int fd) void test_invalid(int fd)
@ -47,8 +47,9 @@ void test_invalid(int fd)
socklen_t buflen = IFNAMSIZ; socklen_t buflen = IFNAMSIZ;
memcpy(buf, "foodev", 7); memcpy(buf, "foodev", 7);
if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, buf, buflen) < 0) { auto setsockopt_maybe_error = Core::System::setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, buf, buflen);
perror("setsockopt(SO_BINDTODEVICE) :: invalid (Should fail with ENODEV)"); if (setsockopt_maybe_error.is_error()) {
warnln("setsockopt(SO_BINDTODEVICE) :: invalid (Should fail with ENODEV).");
puts("PASS invalid"); puts("PASS invalid");
} else { } else {
puts("FAIL invalid"); puts("FAIL invalid");
@ -62,8 +63,9 @@ void test_valid(int fd)
socklen_t buflen = IFNAMSIZ; socklen_t buflen = IFNAMSIZ;
memcpy(buf, "loop", 5); memcpy(buf, "loop", 5);
if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, buf, buflen) < 0) { auto setsockopt_maybe_error = Core::System::setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, buf, buflen);
perror("setsockopt(SO_BINDTODEVICE) :: valid"); if (setsockopt_maybe_error.is_error()) {
warnln("setsockopt(SO_BINDTODEVICE) :: valid");
puts("FAIL valid"); puts("FAIL valid");
} else { } else {
puts("PASS valid"); puts("PASS valid");
@ -77,8 +79,9 @@ void test_no_route(int fd)
socklen_t buflen = IFNAMSIZ; socklen_t buflen = IFNAMSIZ;
memcpy(buf, "loop", 5); memcpy(buf, "loop", 5);
if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, buf, buflen) < 0) { auto setsockopt_maybe_error = Core::System::setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, buf, buflen);
perror("setsockopt(SO_BINDTODEVICE) :: no_route"); if (setsockopt_maybe_error.is_error()) {
warnln("setsockopt(SO_BINDTODEVICE) :: no_route");
puts("FAIL no_route"); puts("FAIL no_route");
return; return;
} }
@ -89,14 +92,16 @@ void test_no_route(int fd)
sin.sin_port = 8080; sin.sin_port = 8080;
sin.sin_family = AF_INET; sin.sin_family = AF_INET;
if (bind(fd, (sockaddr*)&sin, sizeof(sin)) < 0) { auto bind_maybe_error = Core::System::bind(fd, (sockaddr*)&sin, sizeof(sin));
perror("bind() :: no_route"); if (bind_maybe_error.is_error()) {
warnln("bind() :: no_route");
puts("FAIL no_route"); puts("FAIL no_route");
return; return;
} }
if (sendto(fd, "TEST", 4, 0, (sockaddr*)&sin, sizeof(sin)) < 0) { auto sendto_maybe_error = Core::System::sendto(fd, "TEST", 4, 0, (sockaddr*)&sin, sizeof(sin));
perror("sendto() :: no_route (Should fail with EHOSTUNREACH)"); if (sendto_maybe_error.is_error()) {
warnln("sendto() :: no_route (Should fail with EHOSTUNREACH)");
puts("PASS no_route"); puts("PASS no_route");
} else } else
puts("FAIL no_route"); puts("FAIL no_route");
@ -110,8 +115,9 @@ void test_send(int fd)
// FIXME: Look up the proper device name instead of hard-coding it // FIXME: Look up the proper device name instead of hard-coding it
memcpy(buf, "ep0s7", 6); memcpy(buf, "ep0s7", 6);
if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, buf, buflen) < 0) { auto setsockopt_maybe_error = Core::System::setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, buf, buflen);
perror("setsockopt(SO_BINDTODEVICE) :: send"); if (setsockopt_maybe_error.is_error()) {
warnln("setsockopt(SO_BINDTODEVICE) :: send");
puts("FAIL send"); puts("FAIL send");
return; return;
} }
@ -122,14 +128,16 @@ void test_send(int fd)
sin.sin_port = 8080; sin.sin_port = 8080;
sin.sin_family = AF_INET; sin.sin_family = AF_INET;
if (bind(fd, (sockaddr*)&sin, sizeof(sin)) < 0) { auto bind_maybe_error = Core::System::bind(fd, (sockaddr*)&sin, sizeof(sin));
perror("bind() :: send"); if (bind_maybe_error.is_error()) {
warnln("bind() :: send");
puts("FAIL send"); puts("FAIL send");
return; return;
} }
if (sendto(fd, "TEST", 4, 0, (sockaddr*)&sin, sizeof(sin)) < 0) { auto sendto_maybe_error = Core::System::sendto(fd, "TEST", 4, 0, (sockaddr*)&sin, sizeof(sin));
perror("sendto() :: send"); if (sendto_maybe_error.is_error()) {
warnln("sendto() :: send");
puts("FAIL send"); puts("FAIL send");
return; return;
} }