added option handling and more tests

This commit is contained in:
Bob Polis 2020-04-18 20:39:32 +02:00
parent 63417fa847
commit 40aac7eeca

View File

@ -6,15 +6,30 @@
// Copyright (c) 2020 SwiftCoder. All rights reserved.
//
#include <getopt.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <iostream>
#include <cstdlib>
#include <libcommon.hpp>
#include <string>
#include <libscerror.hpp>
#include <libscstring.hpp>
#include <libscio.hpp>
using namespace std;
int main(int /*argc*/, const char * argv[]) {
try {
void print_help() {
std::cout << "usage: sciotest [-h][-v][-t TEST_ID]\n";
std::cout << " -h, --help show this help text and exit\n";
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";
@ -22,7 +37,9 @@ int main(int /*argc*/, const char * argv[]) {
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};
@ -31,12 +48,84 @@ int main(int /*argc*/, const char * argv[]) {
sc::io::socketstream client = server.accept();
cerr << "connection from " << client.address() << '\n';
client << "ready to echo...\n";
string line;
while (getline(client, line)) {
client << line;
client << line << '\n';
}
}
}
void test3() {
string passwd {sc::file_get_contents("/etc/passwd")};
int fd = -1;
throw_if_min1(fd = ::open("passwd", O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR, S_IWUSR));
sc::io::fdostream os {fd};
os << passwd;
throw_if_min1(::close(fd));
}
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';
}
throw_if_min1(::close(fd));
}
int main(int argc, const char * argv[]) {
try {
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, const_cast<char* const *>(argv), "ht:v", long_options, &option_index)) != -1) {
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;
default: throw std::runtime_error("no such test");
}
} catch (const exception& ex) {
cerr << su::basename(argv[0]) << ": " << ex.what() << '\n';
cerr << "sciotest: " << ex.what() << '\n';
return EXIT_FAILURE;
}
return EXIT_SUCCESS;