libscio/sciotest/main.cpp

150 lines
4.3 KiB
C++
Raw Normal View History

2020-04-18 20:39:32 +02:00
#include <getopt.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
2020-02-14 18:17:17 +01:00
#include <iostream>
#include <cstdlib>
2020-04-18 20:39:32 +02:00
#include <string>
#include <libscerror.hpp>
#include <libscstring.hpp>
2020-02-14 18:17:17 +01:00
#include <libscio.hpp>
using namespace std;
2020-04-18 20:39:32 +02:00
void print_help() {
2020-04-25 16:39:57 +02:00
std::cout << "usage: sciotest -h|-v|-t TEST_ID\n";
2020-04-18 20:39:32 +02:00
std::cout << " -h, --help show this help text and exit\n";
2020-04-25 16:39:57 +02:00
std::cout << " -t, --test perform test with given TEST_ID\n";
std::cout << " currently a number between 1 and 5, inclusive\n";
2020-04-18 20:39:32 +02:00
std::cout << " -v, --version show version number and exit\n";
}
void print_version() {
std::cout << "sciotest version 1.0\n";
}
void test1() {
sc::io::fdostream chan {1};
chan << "Please enter a line of text:\n";
string line;
sc::io::fdistream is {0};
getline(is, line);
cout << line << '\n';
}
void test2() {
cout << "starting server\n";
sc::io::socketstream server(AF_INET6, SOCK_STREAM);
sc::io::socket_address addr {"", 4242};
server.make_server(addr);
for (;;) {
sc::io::socketstream client = server.accept();
cerr << "connection from " << client.address() << '\n';
client << "ready to echo...\necho> ";
2020-04-18 20:39:32 +02:00
string line;
while (getline(client, line)) {
cerr << "received: " << line << "\n";
if (line == "quit\r") {
client << "bye\n";
client.close();
break;
} else {
client << line << '\n';
}
client << "echo> ";
2020-04-18 20:39:32 +02:00
}
}
}
void test3() {
string passwd {sc::file_get_contents("/etc/passwd")};
int fd = -1;
2020-04-24 10:06:01 +02:00
int perm = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
int mode = O_WRONLY | O_CREAT | O_TRUNC;
throw_if_min1(fd = ::open("passwd", mode, perm));
2020-04-18 20:39:32 +02:00
sc::io::fdostream os {fd};
os << passwd;
}
void test4() {
int fd = -1;
throw_if_min1(fd = ::open("/etc/passwd", O_RDONLY));
sc::io::fdistream is {fd};
string line;
while (getline(is, line)) {
cout << line << '\n';
}
}
2020-04-24 10:06:01 +02:00
void test5() {
int fd = -1;
int perm = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
int mode = O_RDWR | O_CREAT | O_TRUNC;
throw_if_min1(fd = ::open("test5", mode, perm));
sc::io::fdstream file {fd};
file << "A line of text\n";
file.seekg(0);
string line;
2020-04-25 16:39:57 +02:00
if (getline(file, line)) {
cout << line << '\n';
}
2020-04-24 10:06:01 +02:00
}
int main(int argc, char * argv[]) {
2020-02-14 18:17:17 +01:00
try {
2020-04-18 20:39:32 +02:00
struct option long_options[] = {
{"help", no_argument, nullptr, 'h'},
{"test", required_argument, nullptr, 't'},
{"version", no_argument, nullptr, 'v'},
{nullptr, 0, nullptr, 0}
};
int opt_char, option_index;
int test_id = 0;
while ((opt_char = getopt_long(argc, argv, "ht:v", long_options, &option_index)) != -1) {
2020-04-18 20:39:32 +02:00
switch (opt_char) {
case 0: {
// handle long-only options here
std::string optname {long_options[option_index].name};
break;
}
case 'h':
print_help();
return EXIT_SUCCESS;
case 't':
test_id = atoi(optarg);
break;
case 'v':
print_version();
return EXIT_SUCCESS;
case '?':
throw std::runtime_error("unrecognized option");
}
}
if (optind == argc) {
// here when no file args
}
for (int i = optind; i < argc; ++i) {
try {
// process file argv[i]
} catch (const std::runtime_error& ex) {
std::cerr << "sciotest: " << ex.what() << endl;
}
}
if (test_id == 0) throw std::runtime_error("please provide a test id");
switch (test_id) {
case 1: test1(); break;
case 2: test2(); break;
case 3: test3(); break;
case 4: test4(); break;
2020-04-24 10:06:01 +02:00
case 5: test5(); break;
2020-04-18 20:39:32 +02:00
default: throw std::runtime_error("no such test");
}
2020-02-14 18:17:17 +01:00
} catch (const exception& ex) {
2020-04-18 20:39:32 +02:00
cerr << "sciotest: " << ex.what() << '\n';
2020-02-14 18:17:17 +01:00
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}