General Purpose library for Freestanding C++ and POSIX systems
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 

198 rader
4.6 KiB

#include "shared_fd.hpp"
#include "test_scaffold.h"
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", 1234);
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 != 1234);
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, (gp::net_socket_opt_flags)0);
error += !(v.is_valid());
v = gp::shared_fd::socket(gp::socket_domain::ip4, gp::socket_protocol::tcp_like, (gp::net_socket_opt_flags)-1);
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{});