00001 #include "config.h"
00002
00003 #include <iostream>
00004 #include <fstream>
00005 #include <string>
00006 #include <cassert>
00007
00008 #ifdef HAVE_UNISTD_H
00009 #include <unistd.h>
00010 #endif
00011
00012 #include <errno.h>
00013
00014 #include "asserts.h"
00015 #include "types.h"
00016 #include "error.h"
00017 #include "estring.h"
00018 #include "fs.h"
00019 #include "tstamp.h"
00020 #include "rconfig.h"
00021 #include "timer.h"
00022
00023 #include "logger.h"
00024
00025
00026 #define ERR_OUT(e)
00027
00028 #define TEST(code,test) \
00029 thrown = false; \
00030 try { \
00031 code; \
00032 } \
00033 catch(error e) { \
00034 ERR_OUT(e); \
00035 thrown = true; \
00036 } \
00037 catch(...) { \
00038 assert(0); \
00039 }
00040
00041
00042 bool make_dir(const std::string& path)
00043 {
00044 bool thrown;
00045
00046 thrown = false;
00047 try {
00048 mk_dir(path);
00049 }
00050 catch(...) {
00051 thrown = true;
00052 }
00053
00054 return(thrown == false);
00055 }
00056
00057 void cleanup(void)
00058 {
00059 assert(system("rm -fr ./test-logger.dir") == 0);
00060 }
00061
00062 void setup(void)
00063 {
00064 std::ofstream out;
00065
00066 assert(make_dir("./test-logger.dir"));
00067 assert(make_dir("./test-logger.dir/log.dir"));
00068 assert(make_dir("./test-logger.dir/vault.dir"));
00069
00070 out.open("./test-logger.dir/rvm.conf");
00071 assert(out.is_open());
00072 out << "log-dir ./test-logger.dir/log.dir" << std::endl;
00073 out << "vault ./test-logger.dir/vault.dir" << std::endl;
00074 out << "<job>" << std::endl;
00075 out << " archive-path pathname" << std::endl;
00076 out << " rsync-connection-type local" << std::endl;
00077 out << " path /var/spool" << std::endl;
00078 out << "</job>" << std::endl;
00079 out.close();
00080 }
00081
00082 const bool same(std::string s1, std::string s2)
00083 {
00084 s1[11] = 'x'; s2[11] = 'x';
00085 s1[12] = 'x'; s2[12] = 'x';
00086 s1[14] = 'x'; s2[14] = 'x';
00087 s1[15] = 'x'; s2[15] = 'x';
00088 s1[17] = 'x'; s2[17] = 'x';
00089 s1[18] = 'x'; s2[18] = 'x';
00090
00091 if (s1 == s2) {
00092 return(true);
00093 }
00094 return(false);
00095 }
00096
00097 void test(void)
00098 {
00099 char *argv[256] = { 0 };
00100 int argc = 0;
00101 bool thrown;
00102
00103 thrown = false;
00104 TEST(
00105 logger.init(),
00106 assert(e.num() == 0);
00107 assert(!e.internal());
00108 assert(e.size() == 1);
00109 assert(e[0].what() == "Configuration manager is not initialized");
00110 );
00111 assert(thrown);
00112
00113 argv[argc++] = "<program>";
00114 argv[argc++] = "--archive";
00115 config.default_file("./test-logger.dir/rvm.conf");
00116 config.init(argc, argv);
00117
00118 logger.clear();
00119 TEST(logger.init(),);
00120 assert(!thrown);
00121
00122 logger.write("Testing... ");
00123 logger.write("1... ");
00124 logger.write("2... ");
00125 logger.write("3... ");
00126 logger.write("\n");
00127 logger.write("Testing...\n");
00128 logger.write("1...\n",1);
00129 logger.write("2...\n",2);
00130 logger.write("3...\n",3);
00131 logger.write("Testing...\n1...\n2...\n3...\n",1);
00132 logger.write("PID is ");
00133 logger.write(estring(static_cast<unsigned long>(pid())));
00134 logger.write("\n");
00135 logger.clear();
00136
00137 std::ifstream in;
00138 std::string filename;
00139 std::string str;
00140 char line[1024];
00141
00142 filename = "./test-logger.dir/log.dir/";
00143 filename += config.timestamp().str();
00144 filename += ".log";
00145 in.open(filename.c_str());
00146 assert(in.is_open());
00147
00148 in.get(line,1024);
00149 str = stamp(pid());
00150 str += "Testing... 1... 2... 3... ";
00151 assert(same(str,line));
00152
00153 in.ignore();
00154
00155 in.get(line,1024);
00156 str = stamp(pid());
00157 str += "Testing...";
00158 assert(same(str,line));
00159
00160 in.ignore();
00161
00162 in.get(line,1024);
00163 str = stamp(pid());
00164 str += " 1...";
00165 assert(same(str,line));
00166
00167 in.ignore();
00168
00169 in.get(line,1024);
00170 str = stamp(pid());
00171 str += " 2...";
00172 assert(same(str,line));
00173
00174 in.ignore();
00175
00176 in.get(line,1024);
00177 str = stamp(pid());
00178 str += " 3...";
00179 assert(same(str,line));
00180
00181 in.ignore();
00182
00183 in.get(line,1024);
00184 str = stamp(pid());
00185 str += " Testing...";
00186 assert(same(str,line));
00187
00188 in.ignore();
00189
00190 in.get(line,1024);
00191 str = stamp(pid());
00192 str += " 1...";
00193 assert(same(str,line));
00194
00195 in.ignore();
00196
00197 in.get(line,1024);
00198 str = stamp(pid());
00199 str += " 2...";
00200 assert(same(str,line));
00201
00202 in.ignore();
00203
00204 in.get(line,1024);
00205 str = stamp(pid());
00206 str += " 3...";
00207 assert(same(str,line));
00208
00209 in.ignore();
00210
00211 in.get(line,1024);
00212 str = stamp(pid());
00213 str += "PID is ";
00214 str += estring(static_cast<unsigned long>(pid()));
00215 assert(same(str,line));
00216
00217 in.close();
00218 }
00219
00220 int main(int argc, char *argv[])
00221 {
00222 cleanup();
00223 setup();
00224 try {
00225 test();
00226 }
00227 catch(error e) {
00228 std::cerr << e;
00229 assert(0);
00230 }
00231 catch(...) {
00232 std::cerr << err_unknown;
00233 assert(0);
00234 }
00235 cleanup();
00236 return(0);
00237 }
00238