00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include <basis/byte_array.h>
00016 #include <basis/chaos.h>
00017 #include <basis/function.h>
00018 #include <basis/guards.h>
00019 #include <basis/istring.h>
00020 #include <basis/portable.h>
00021 #include <basis/string_array.h>
00022 #include <opsystem/application_shell.h>
00023 #include <opsystem/byte_filer.h>
00024 #include <loggers/console_logger.h>
00025 #include <loggers/file_logger.h>
00026 #include <opsystem/directory.h>
00027 #include <opsystem/filename.h>
00028 #include <data_struct/static_memory_gremlin.h>
00029
00030 #define LOG(s) CLASS_EMERGENCY_LOG(program_wide_logger(), s)
00031
00032 class test_byte_filer : public application_shell
00033 {
00034 public:
00035 test_byte_filer() : application_shell(class_name()) {}
00036 IMPLEMENT_CLASS_NAME("test_byte_filer");
00037 int run_simple_test();
00038 int run_file_scan();
00039 virtual int execute();
00040 };
00041
00042 const istring TEST_FILE_BASE = "garbage.txt";
00043
00044 const istring TEST_FILE = portable::env_string("TMP") + "/" + TEST_FILE_BASE;
00045
00046 int test_byte_filer::run_simple_test()
00047 {
00048 FUNCDEF("run_simple_test");
00049 #ifdef DEBUG
00050 LOG("ahoy, beginning file test...");
00051 #endif
00052 chaos randomizer;
00053
00054
00055
00056 byte_filer garbage(TEST_FILE.s(), "wb");
00057 garbage.write("oy.\n");
00058 garbage.close();
00059 filename test1(TEST_FILE);
00060 if (!test1.exists())
00061 deadly_error(class_name(), "exists test", "file didn't exist when should!");
00062 filename test2("c:\this_file_shouldNt_exist_ever.txt");
00063 if (test2.exists())
00064 deadly_error(class_name(), "exists test", "file existed when shouldn't!");
00065
00066 if (test2.exists())
00067 deadly_error(class_name(), "exists test", "file existed when shouldn't!");
00068 test1.unlink();
00069
00070 int block_size = randomizer.inclusive(3000, 30000);
00071 #ifdef DEBUG
00072 LOG(isprintf("block size=%d", block_size));
00073 #endif
00074 byte *original_block = new byte[block_size];
00075 for (int i = 0; i < block_size; i++)
00076 original_block[i] = byte(randomizer.inclusive(32, 126));
00077 unsigned int original_checksum
00078 = utility::bizarre_checksum((byte *)original_block, block_size);
00079 if (original_checksum) {}
00080 #ifdef DEBUG
00081 LOG(isprintf("random block checksum=%d", original_checksum));
00082 #endif
00083 {
00084 byte_array to_stuff_in_file(block_size, original_block);
00085 delete [] original_block;
00086 byte_filer fred(TEST_FILE, "w+");
00087 fred.write(to_stuff_in_file);
00088 }
00089 #ifdef DEBUG
00090 LOG(istring("about to compare file to checksum"));
00091 #endif
00092 {
00093 byte *temp_array = new byte[21309];
00094 byte_array to_fake_stuff(21309, temp_array);
00095 delete [] temp_array;
00096 byte_filer fred(TEST_FILE, "r");
00097 #ifdef DEBUG
00098 LOG(istring("about to try writing to file"));
00099 #endif
00100 int should_be_failure = fred.write(to_fake_stuff);
00101 if (should_be_failure != 0)
00102 deadly_error(class_name(), "write on read only",
00103 istring(istring::SPRINTF, "write supposedly succeeded (len = %d)!",
00104 should_be_failure));
00107
00108 }
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145 return 0;
00146 }
00147
00148 int test_byte_filer::run_file_scan()
00149 {
00150 FUNCDEF("run_file_scan");
00151 chaos randomizer;
00152
00153 string_array files(__argc, (const char **)__argv);
00154 files.zap(0, 0);
00155
00156 if (!files.length()) {
00157
00158
00159
00160 istring tmpdir = portable::current_directory();
00161 directory dir(tmpdir);
00162 for (int i = 0; i < dir.files().length(); i++) {
00163
00164 if ( (dir.files()[i].ends(".txt")) || (dir.files()[i].ends(".txt")) )
00165 continue;
00166 istring chewed_string = tmpdir + "/" + dir.files()[i];
00167 files += chewed_string;
00168 }
00169 LOG(istring("added files since no cmd args: ") + files.text_form());
00170 }
00171
00172 byte_array data_found;
00173 for (int i = 0; i < files.length(); i++) {
00174 istring curr = files[i];
00175 LOG(isprintf("file %d: ", i) + curr);
00176 byte_filer test(curr, "rb");
00177 if (!test.good()) {
00178 LOG(istring("good check: ") + curr + " cannot be opened. is this bad?");
00179 continue;
00180 }
00181
00182
00183
00184 test.seek(0, byte_filer::FROM_END);
00185 if (test.tell() != test.length())
00186 deadly_error(class_name(), "seek check",
00187 isprintf("seek to end, posn at %d bytes but should be at %d",
00188 test.tell(), test.length()));
00189 test.seek(0, byte_filer::FROM_START);
00190
00191 size_t len = test.length();
00192
00193 size_t posn = 0;
00194 while ( (posn < len) && !test.eof() ) {
00195 size_t readlen = randomizer.inclusive(1, 256 * KILOBYTE);
00196
00197 int bytes_read = int(test.read(data_found, int(readlen)));
00198 if (bytes_read < 0) {
00199 deadly_error(class_name(), "reading", curr + " failed to be read.");
00200 } else {
00201 posn += bytes_read;
00202 }
00203 }
00204 if (!test.eof())
00205 deadly_error(class_name(), "eof check", curr + " not at eof.");
00206 if (posn != len)
00207 deadly_error(class_name(), "eof check", curr + " is at wrong position: "
00208 + isprintf("want %.0f, got %.0f", double(len), double(posn)));
00209 log(istring("successfully read ") + curr);
00210 }
00211
00212 return 0;
00213 }
00214
00215 int test_byte_filer::execute()
00216 {
00217 FUNCDEF("execute");
00218 SET_DEFAULT_COMBO_LOGGER;
00219 int ret = run_simple_test();
00220 if (ret) return ret;
00221 ret = run_file_scan();
00222 if (ret) return ret;
00223
00224 guards::alert_message("byte_filer:: works for those functions tested.\n");
00225 return 0;
00226 }
00227
00228 HOOPLE_MAIN(test_byte_filer, )
00229