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 const * 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 += "Rsync Vault Manager - ";
00151 str += estring(VERSION);
00152 assert(same(str,line));
00153
00154 in.ignore();
00155
00156 in.get(line,1024);
00157 str = stamp(pid());
00158 str += "Testing... 1... 2... 3... ";
00159 assert(same(str,line));
00160
00161 in.ignore();
00162
00163 in.get(line,1024);
00164 str = stamp(pid());
00165 str += "Testing...";
00166 assert(same(str,line));
00167
00168 in.ignore();
00169
00170 in.get(line,1024);
00171 str = stamp(pid());
00172 str += " 1...";
00173 assert(same(str,line));
00174
00175 in.ignore();
00176
00177 in.get(line,1024);
00178 str = stamp(pid());
00179 str += " 2...";
00180 assert(same(str,line));
00181
00182 in.ignore();
00183
00184 in.get(line,1024);
00185 str = stamp(pid());
00186 str += " 3...";
00187 assert(same(str,line));
00188
00189 in.ignore();
00190
00191 in.get(line,1024);
00192 str = stamp(pid());
00193 str += " Testing...";
00194 assert(same(str,line));
00195
00196 in.ignore();
00197
00198 in.get(line,1024);
00199 str = stamp(pid());
00200 str += " 1...";
00201 assert(same(str,line));
00202
00203 in.ignore();
00204
00205 in.get(line,1024);
00206 str = stamp(pid());
00207 str += " 2...";
00208 assert(same(str,line));
00209
00210 in.ignore();
00211
00212 in.get(line,1024);
00213 str = stamp(pid());
00214 str += " 3...";
00215 assert(same(str,line));
00216
00217 in.ignore();
00218
00219 in.get(line,1024);
00220 str = stamp(pid());
00221 str += "PID is ";
00222 str += estring(static_cast<unsigned long>(pid()));
00223 assert(same(str,line));
00224
00225 in.close();
00226 }
00227
00228 int main(int argc, char const * argv[])
00229 {
00230 cleanup();
00231 setup();
00232 try {
00233 test();
00234 }
00235 catch(error e) {
00236 std::cerr << e;
00237 assert(0);
00238 }
00239 catch(...) {
00240 std::cerr << err_unknown;
00241 assert(0);
00242 }
00243 cleanup();
00244 return(0);
00245 }
00246