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 <fcntl.h>
13 : #include <iostream>
14 : #include <sstream>
15 : #include <sys/stat.h>
16 : #include <sys/types.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 : void
27 0 : test_okssystem(const char* path, const char* text)
28 : {
29 0 : TLOG_DEBUG(1) << "Testing executable \"" << path << "\" with command " << text;
30 0 : std::ostringstream stream;
31 0 : stream << "echo \"" << text << "\" > " << path;
32 0 : std::string result = OksSystem::Executable::okssystem(stream.str());
33 0 : TLOG_DEBUG(1) << "result: " << result;
34 0 : } // test_okssystem
35 :
36 : void
37 0 : test_write_chmod(const OksSystem::File& file)
38 : {
39 0 : TLOG_DEBUG(1) << "Testing OksSystem::File::output " << file.c_full_name();
40 0 : std::ostream* stream = file.output();
41 0 : (*stream) << "#!/bin/sh\necho $1\nexit $1\n";
42 0 : delete (stream);
43 0 : mode_t permission = 0700;
44 0 : TLOG_DEBUG(1) << "Testing OksSystem::File::permissions " << file.c_full_name();
45 0 : TLOG_DEBUG(1) << "Setting permissions: " << OksSystem::File::pretty_permissions(permission).c_str() << " "
46 0 : << permission;
47 0 : file.permissions(permission);
48 0 : TLOG_DEBUG(1) << "Checking permisions: " << file.pretty_permissions().c_str() << " " << file.permissions();
49 0 : if (file.permissions() == permission) {
50 0 : TLOG_DEBUG(1) << "Permission check: ok";
51 : } else {
52 0 : ers::warning(OksSystem::Exception(ERS_HERE, std::string("Permission check: fail")));
53 0 : exit(183);
54 : }
55 0 : } // text_exec
56 :
57 : int
58 0 : test_exec(const OksSystem::Executable& executable, int status)
59 : {
60 0 : TLOG_DEBUG(1) << "Testing OksSystem::Executable::start \"" << executable.c_full_name() << "\" parameter: " << status;
61 0 : std::vector<std::string> params;
62 0 : std::ostringstream temp_stream;
63 0 : temp_stream << status;
64 0 : params.push_back(temp_stream.str());
65 0 : OksSystem::Process p = executable.start(params);
66 0 : int exec_status = p.join(true);
67 0 : TLOG_DEBUG(1) << "Testing executable test: result %d" << exec_status;
68 0 : return exec_status;
69 0 : } // int
70 :
71 : void
72 0 : test_map_file(const OksSystem::File& file)
73 : {
74 0 : TLOG_DEBUG(1) << "Mapping file " << file.c_full_name();
75 0 : const int s = 4096;
76 0 : OksSystem::MapFile map_file(file, s, 0, true, false);
77 0 : map_file.map();
78 0 : void* a = map_file.address();
79 0 : char* buffer = (char*)calloc(sizeof(char), s);
80 0 : memcpy(buffer, a, s);
81 0 : buffer[s - 1] = '\0';
82 0 : TLOG_DEBUG(1) << "Memory map buffer contains " << buffer;
83 0 : map_file.unmap();
84 0 : } // test_map_file
85 :
86 : void
87 0 : test_mkdir(const OksSystem::File& file)
88 : {
89 0 : TLOG_DEBUG(1) << "Creating directory " << file.c_full_name();
90 0 : file.make_path(0700);
91 0 : TLOG_DEBUG(1) << "Directory depth is " << file.depth();
92 0 : } // test_mkdir
93 :
94 : void
95 0 : test_rmdir(const OksSystem::File& file)
96 : {
97 0 : TLOG_DEBUG(1) << "Deleting directory " << file.c_full_name();
98 0 : file.remove();
99 0 : } // test_rmdir
100 :
101 : void
102 0 : test_host()
103 : {
104 0 : TLOG_DEBUG(1) << "Checking host information";
105 0 : const OksSystem::LocalHost* host = OksSystem::LocalHost::instance();
106 0 : std::cout << "short name\t" << host->name() << std::endl;
107 0 : std::cout << "full name\t" << host->full_name() << std::endl;
108 0 : std::cout << "ip address\t" << host->ip_string() << std::endl;
109 0 : std::cout << "description\t" << host->description() << std::endl;
110 0 : } // test_host
111 :
112 : void
113 0 : test_delete_file(const OksSystem::File& file)
114 : {
115 0 : TLOG_DEBUG(1) << "Deleting file \"%s\"" << file.c_full_name();
116 0 : file.unlink();
117 0 : } // test_delete_file
118 :
119 : void
120 0 : test_path(const OksSystem::Path& path, const std::string& name)
121 : {
122 0 : TLOG() << "Testing path " << path.to_string().c_str();
123 0 : TLOG() << "Searching for " << name.c_str();
124 0 : OksSystem::File file = path.which(name);
125 0 : TLOG() << "Found \"" << file.c_full_name() << "\" of type " << file.file_type();
126 0 : } // test_path
127 :
128 : void
129 0 : test_user()
130 : {
131 0 : OksSystem::User user;
132 0 : std::cout << "username\t" << user << std::endl;
133 0 : } //
134 :
135 : void
136 0 : test_process()
137 : {
138 0 : const OksSystem::Process* process = OksSystem::Process::instance();
139 0 : std::cout << "process\t" << *process << std::endl;
140 0 : } // test_process
141 :
142 : int
143 0 : main(int, char** argv)
144 : {
145 :
146 0 : try {
147 0 : OksSystem::Process::set_name(argv[0]);
148 0 : OksSystem::Executable file("/tmp/okssystem_test");
149 0 : test_okssystem(file, "Hello world");
150 0 : test_map_file(file);
151 0 : test_write_chmod(file);
152 0 : test_exec(file, 0);
153 0 : test_delete_file(file);
154 0 : OksSystem::File dir_a("/tmp/really/stupid/path/");
155 0 : test_mkdir(dir_a);
156 0 : OksSystem::File dir_b("/tmp/really/");
157 0 : test_rmdir(dir_b);
158 0 : OksSystem::Path path("/bin::/usr/bin:/usr/local/bin:/sbin/");
159 0 : test_path(path, "ping");
160 0 : test_host();
161 0 : test_user();
162 0 : test_process();
163 :
164 0 : return 0;
165 0 : } catch (ers::Issue& e) {
166 0 : ers::error(e);
167 0 : return 183;
168 0 : } // catch
169 : } // main
|