General Purpose library for Freestanding C++ and POSIX systems
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

287 rivejä
6.6 KiB

#include "shared_fd.hpp"
#include "test_scaffold.h"
#include <thread>
#include <chrono>
#include <iostream>
#include <iomanip>
struct create_test : public test_scaffold {
create_test() {
name = __FILE__ ":1";
}
virtual int run() {
auto fd = gp::shared_fd::create("./bin/test_n", int(gp::open_modes::user_read) | int(gp::open_modes::user_write));
return fd.is_valid()?0:1;
}
};
append_test dummy_sdfhuisd3(new create_test{});
struct open_test : public test_scaffold {
open_test() {
name = __FILE__ ":2";
}
virtual int run() {
auto fd = gp::shared_fd::open("./bin/test_n", int(gp::open_options::append));
return fd.is_valid()?0:1;
}
};
append_test dummy_sdf564dd3(new open_test{});
struct manip_test : public test_scaffold {
manip_test() {
name = __FILE__ ":3";
}
virtual int run() {
auto fd = gp::shared_fd::open("./bin/test_n", int(gp::open_options::append));
int error = fd.is_valid()?0:1;
error += !(fd.get()>=0);
auto fd_cpy = fd;
error += fd_cpy.is_valid()?0:1;
gp::shared_fd constr_cpy(fd_cpy);
error += constr_cpy.is_valid()?0:1;
gp::shared_fd constr_mv(std::move(constr_cpy));
error += constr_mv.is_valid()?0:1;
gp::shared_fd assign_cpy;
assign_cpy.operator=(fd_cpy);
error += assign_cpy.is_valid()?0:1;
gp::shared_fd assign_mv;
assign_mv.operator=(std::move(assign_cpy));
error += assign_mv.is_valid()?0:1;
error += (!assign_cpy.is_valid())?0:1;
return error;
}
};
append_test dummy_lkjs64dd3(new manip_test{});
struct rw_test : public test_scaffold {
rw_test() {
name = __FILE__ ":4";
}
virtual int run() {
auto fd = gp::shared_fd::open("./bin/test_n", int(gp::open_options::write));
int error = fd.is_valid()?0:1;
fd.write("potatoes");
error += fd.has_failed();
fd = gp::shared_fd::open("./bin/test_n", int(gp::open_options::read));
std::array<char,8> buffer;
auto str = fd.read(std::string_view(buffer.begin(), buffer.size()));
error += fd.has_failed();
error += (str != "potatoes");
return error;
}
};
append_test dummy_l6z5e4rdd3(new rw_test{});
struct rw_err_test : public test_scaffold {
rw_err_test() {
name = __FILE__ ":5";
}
virtual int run() {
auto fd = gp::shared_fd::create("./bin/test_n", int(gp::open_modes::user_read) | int(gp::open_modes::user_write));
fd = gp::shared_fd::open("./bin/test_n", int(gp::open_options::read));
int error = fd.is_valid()?0:1;
fd.write("potatoes");
error += fd.has_failed();
fd = gp::shared_fd::open("./bin/test_n", int(gp::open_options::write));
error += fd.is_valid()?0:1;
std::array<char,8> buffer;
auto str = fd.read(std::string_view(buffer.begin(), buffer.size()));
error += fd.has_failed();
return error == 2 ? 0 : 1;
}
};
append_test dummy_l6987erd3(new rw_err_test{});
struct make_address_test : public test_scaffold {
make_address_test() {
name = __FILE__ ":6";
}
virtual int run() {
int error = 0;
gp::address ipv4 = gp::make_ipv4("127.0.0.1", 0x1234);
auto p = std::get<sockaddr_in>(ipv4);
error += (p.sin_family != AF_INET);
error += (p.sin_addr.s_addr != htonl(0x7F000001));
error += (p.sin_port != htons(0x1234));
try{
gp::make_ipv4("not an IP", 1234);
error += 1;
}catch(...){}
std::string filename = "/tmp/my_socket";
gp::address unix = gp::make_unix(filename);
auto q = std::get<sockaddr_un>(unix);
error += (q.sun_family != AF_UNIX);
error += strcmp(filename.c_str(), q.sun_path);
try{
std::string long_str(1024, 'p');
gp::make_unix(long_str);
error += 1;
}catch(...){}
return error;
}
};
append_test dummy_l6923ml3(new make_address_test{});
struct sockets_test : public test_scaffold {
sockets_test() {
name = __FILE__ ":7";
}
virtual int run() {
int error = 0;
auto v = gp::shared_fd::socket(gp::socket_domain::ip4, gp::socket_protocol::tcp_like);
error += !(v.is_valid());
v = gp::shared_fd::socket(gp::socket_domain::ip4, gp::socket_protocol::tcp_like);
error += !(v.is_valid());
auto pair_v = gp::shared_fd::unix_socket_pair(gp::socket_protocol::tcp_like, (gp::net_socket_opt_flags)0);
error += !(pair_v.first.is_valid());
error += !(pair_v.second.is_valid());
pair_v = gp::shared_fd::unix_socket_pair(gp::socket_protocol::tcp_like, (gp::net_socket_opt_flags)-1);
error += pair_v.first.is_valid();
error += pair_v.second.is_valid();
auto u_v = gp::shared_fd::unix_socket(gp::socket_protocol::tcp_like, (gp::socket_opt_flags)0);
error += !(u_v.is_valid());
u_v = gp::shared_fd::unix_socket(gp::socket_protocol::tcp_like, (gp::socket_opt_flags)-1);
error += u_v.is_valid();
return error;
}
};
append_test dummy_r3321443(new sockets_test{});
using namespace std::chrono_literals;
struct sockets_co_test : public test_scaffold {
sockets_co_test() {
name = __FILE__ ":8";
}
virtual int run() {
std::atomic_int error = 0;
{
auto v1 = gp::shared_fd::tcp_socket();
error += !(v1.is_valid());
auto v2 = gp::shared_fd::tcp_socket();
error += !(v2.is_valid());
v1.bind(gp::make_ipv4("254.254.254.254",1234));
error += !(v1.has_failed());
v1.bind(gp::make_ipv4("0.0.0.0",1235));
error += v1.has_failed();
v1.listen();
std::cout << "listens?:" << v1.is_valid()<<std::endl;
std::thread sside([&](){
gp::shared_fd listener = v1;
gp::shared_fd v3;
do
{
v3 = listener.accept();
std::this_thread::sleep_for(500ms);
std::cout << "accept_attempt:" << v3.is_valid()<<std::endl;
}while(listener.must_retry());
puts("accepted");
error += !(listener.has_failed());
do
{
v3.write("potatoes");
std::this_thread::sleep_for(1ms);
}while(v3.must_retry());
puts("sent");
error += !(v3.has_failed());
return;
});
std::this_thread::sleep_for(1ms);
v2.connect(gp::make_ipv4("255.255.255.255",1235));
error += v2.has_failed();
do{
v2.connect(gp::make_ipv4("127.0.0.1",1235));
std::this_thread::sleep_for(500ms);
std::cout << "connect_attempt:" << v2.is_valid() << " with " << v2.error()<<std::endl;
}while(v2.error() != EISCONN);
std::this_thread::sleep_for(1ms);
auto status = v2.was_connection_refused();
error += status;
if(!status)
{
puts("connected");
std::array<char, 32> buffer;
std::string_view retstr;
do
{
retstr = v2.read(std::string_view(buffer.begin(), buffer.size()));
std::this_thread::sleep_for(1ms);
}
while(v2.must_retry());
puts("received");
std::cout<< "out:" << std::quoted(retstr) << std::endl;
error += !(v2.has_failed());
error += retstr != "potatoes";
if(sside.joinable())
sside.join();
}
return error;
}
return error;
}
};
//append_test dummy_polmdf43(new sockets_co_test{});