Line data Source code
1 : /*
2 : * OksSystemTest.cxx
3 : * Test
4 : *
5 : * JCF, Oct-17-2022: OksSystemTest as written by Matthias Wiesmann has
6 : * been modified to work in the DUNE DAQ framework
7 : *
8 : * Created by Matthias Wiesmann on 24.01.05.
9 : * Copyright 2005 CERN. All rights reserved.
10 : *
11 : */
12 : #include <iostream>
13 : #include <sstream>
14 : #include <sys/types.h>
15 : #include <sys/stat.h>
16 : #include <fcntl.h>
17 :
18 : #include "ers/ers.hpp"
19 : #include "logging/Logging.hpp"
20 :
21 : #include "okssystem/OksSystem.hpp"
22 : #include "okssystem/exceptions.hpp"
23 :
24 : #include "OksSystemTest.hpp"
25 :
26 :
27 0 : void test_okssystem(const char* path, const char *text) {
28 0 : TLOG_DEBUG( 1) << "Testing executable \"" << path << "\" with command " << text ;
29 0 : std::ostringstream stream ;
30 0 : stream << "echo \"" << text << "\" > " << path;
31 0 : std::string result = OksSystem::Executable::okssystem(stream.str());
32 0 : TLOG_DEBUG( 1) << "result: " << result ;
33 0 : } // test_okssystem
34 :
35 0 : void test_write_chmod(const OksSystem::File &file) {
36 0 : TLOG_DEBUG( 1) << "Testing OksSystem::File::output " << file.c_full_name() ;
37 0 : std::ostream* stream= file.output();
38 0 : (*stream) << "#!/bin/sh\necho $1\nexit $1\n";
39 0 : delete(stream);
40 0 : mode_t permission = 0700;
41 0 : TLOG_DEBUG( 1) << "Testing OksSystem::File::permissions " << file.c_full_name();
42 0 : TLOG_DEBUG( 1) << "Setting permissions: " << OksSystem::File::pretty_permissions(permission).c_str() << " " << permission;
43 0 : file.permissions(permission);
44 0 : TLOG_DEBUG( 1) << "Checking permisions: " << file.pretty_permissions().c_str() << " " << file.permissions();
45 0 : if (file.permissions()==permission) {
46 0 : TLOG_DEBUG( 1) << "Permission check: ok";
47 : } else {
48 0 : ers::warning(OksSystem::Exception(ERS_HERE, std::string("Permission check: fail")));
49 0 : exit (183);
50 : }
51 0 : } // text_exec
52 :
53 0 : int test_exec(const OksSystem::Executable &executable, int status ) {
54 0 : TLOG_DEBUG( 1) << "Testing OksSystem::Executable::start \"" << executable.c_full_name() << "\" parameter: " << status;
55 0 : std::vector<std::string> params;
56 0 : std::ostringstream temp_stream;
57 0 : temp_stream << status;
58 0 : params.push_back(temp_stream.str());
59 0 : OksSystem::Process p = executable.start(params);
60 0 : int exec_status = p.join(true) ;
61 0 : TLOG_DEBUG( 1) << "Testing executable test: result %d" << exec_status;
62 0 : return exec_status;
63 0 : } // int
64 :
65 0 : void test_map_file(const OksSystem::File &file) {
66 0 : TLOG_DEBUG( 1) << "Mapping file " << file.c_full_name();
67 0 : const int s = 4096 ;
68 0 : OksSystem::MapFile map_file(file,s,0,true,false);
69 0 : map_file.map();
70 0 : void *a = map_file.address() ;
71 0 : char *buffer = (char *) calloc(sizeof(char), s);
72 0 : memcpy(buffer,a,s);
73 0 : buffer[s-1] = '\0' ;
74 0 : TLOG_DEBUG( 1) << "Memory map buffer contains " << buffer;
75 0 : map_file.unmap();
76 0 : } // test_map_file
77 :
78 0 : void test_mkdir(const OksSystem::File &file) {
79 0 : TLOG_DEBUG( 1) << "Creating directory " << file.c_full_name();
80 0 : file.make_path(0700);
81 0 : TLOG_DEBUG( 1) << "Directory depth is " << file.depth();
82 0 : } //test_mkdir
83 :
84 0 : void test_rmdir(const OksSystem::File &file) {
85 0 : TLOG_DEBUG( 1) << "Deleting directory " << file.c_full_name();
86 0 : file.remove();
87 0 : } // test_rmdir
88 :
89 0 : void test_host() {
90 0 : TLOG_DEBUG( 1) << "Checking host information" ;
91 0 : const OksSystem::LocalHost *host = OksSystem::LocalHost::instance();
92 0 : std::cout << "short name\t"<< host->name() << std::endl;
93 0 : std::cout << "full name\t" << host->full_name() << std::endl;
94 0 : std::cout << "ip address\t" << host->ip_string() << std::endl;
95 0 : std::cout << "description\t" << host->description() << std::endl ;
96 0 : } // test_host
97 :
98 0 : void test_delete_file(const OksSystem::File &file) {
99 0 : TLOG_DEBUG( 1) << "Deleting file \"%s\"" << file.c_full_name();
100 0 : file.unlink();
101 0 : } // test_delete_file
102 :
103 0 : void test_path(const OksSystem::Path &path, const std::string &name) {
104 0 : TLOG() << "Testing path " << path.to_string().c_str() ;
105 0 : TLOG() << "Searching for " << name.c_str() ;
106 0 : OksSystem::File file = path.which(name);
107 0 : TLOG() << "Found \"" << file.c_full_name() << "\" of type " << file.file_type() ;
108 0 : }// test_path
109 :
110 0 : void test_user() {
111 0 : OksSystem::User user;
112 0 : std::cout << "username\t" << user << std::endl ;
113 0 : } //
114 :
115 0 : void test_process() {
116 0 : const OksSystem::Process *process = OksSystem::Process::instance();
117 0 : std::cout << "process\t" << *process << std::endl;
118 0 : } // test_process
119 :
120 :
121 0 : int main(int , char** argv) {
122 :
123 0 : try {
124 0 : OksSystem::Process::set_name(argv[0]);
125 0 : OksSystem::Executable file("/tmp/okssystem_test") ;
126 0 : test_okssystem(file,"Hello world");
127 0 : test_map_file(file);
128 0 : test_write_chmod(file);
129 0 : test_exec(file,0);
130 0 : test_delete_file(file);
131 0 : OksSystem::File dir_a("/tmp/really/stupid/path/");
132 0 : test_mkdir(dir_a);
133 0 : OksSystem::File dir_b("/tmp/really/");
134 0 : test_rmdir(dir_b);
135 0 : OksSystem::Path path("/bin::/usr/bin:/usr/local/bin:/sbin/");
136 0 : test_path(path,"ping");
137 0 : test_host();
138 0 : test_user();
139 0 : test_process();
140 :
141 0 : return 0;
142 0 : } catch (ers::Issue & e) {
143 0 : ers::error(e) ;
144 0 : return 183;
145 0 : } // catch
146 : } // main
147 :
148 :
|