updated to not be broken.
[feisty_meow.git] / nucleus / library / tests_filesystem / test_byte_filer.cpp
1 /*****************************************************************************\
2 *                                                                             *
3 *  Name   : test_byte_filer                                                   *
4 *  Author : Chris Koeritz                                                     *
5 *                                                                             *
6 *******************************************************************************
7 * Copyright (c) 1991-$now By Author.  This program is free software; you can  *
8 * redistribute it and/or modify it under the terms of the GNU General Public  *
9 * License as published by the Free Software Foundation; either version 2 of   *
10 * the License or (at your option) any later version.  This is online at:      *
11 *     http://www.fsf.org/copyleft/gpl.html                                    *
12 * Please send any updates to: fred@gruntose.com                               *
13 \*****************************************************************************/
14
15 #include <basis/byte_array.h>
16 #include <mathematics/chaos.h>
17 #include <basis/functions.h>
18 #include <basis/guards.h>
19 #include <basis/astring.h>
20
21 #include <application/hoople_main.h>
22 #include <configuration/application_configuration.h>
23 #include <filesystem/byte_filer.h>
24 #include <filesystem/directory.h>
25 #include <filesystem/filename.h>
26 #include <loggers/critical_events.h>
27 #include <loggers/program_wide_logger.h>
28 #include <mathematics/chaos.h>
29 #include <structures/checksums.h>
30 #include <structures/static_memory_gremlin.h>
31 #include <structures/string_array.h>
32 #include <unit_test/unit_base.h>
33
34 using namespace application;
35 using namespace basis;
36 using namespace configuration;
37 using namespace mathematics;
38 using namespace filesystem;
39 using namespace loggers;
40 using namespace structures;
41 using namespace textual;
42 using namespace timely;
43 using namespace unit_test;
44
45 #define LOG(s) CLASS_EMERGENCY_LOG(program_wide_logger::get(), s)
46
47 #define DEBUG_BYTE_FILER
48   // uncomment for noisy test run.
49
50 class test_byte_filer : virtual public unit_base, virtual public application_shell
51 {
52 public:
53   test_byte_filer() : application_shell() {}
54   DEFINE_CLASS_NAME("test_byte_filer");
55   int run_simple_test();
56   int run_file_scan();
57   virtual int execute();
58 };
59
60 const astring &TEST_FILE()
61 {
62   const char *TEST_FILE_BASE = "/zz_garbage";
63   const char *TEST_FILE_SUFFIX = ".txt";
64   static astring __hidden_filename;
65   if (!__hidden_filename) {
66     __hidden_filename = environment::get("TMP");
67     if (!__hidden_filename) __hidden_filename = "/tmp";
68     __hidden_filename += TEST_FILE_BASE;
69     __hidden_filename += a_sprintf("%d", chaos().inclusive(0, 65535));
70     __hidden_filename += TEST_FILE_SUFFIX;
71   }
72   return __hidden_filename;
73 }
74
75 int test_byte_filer::run_simple_test()
76 {
77   FUNCDEF("run_simple_test");
78 #ifdef DEBUG_BYTE_FILER
79   LOG("ahoy, beginning file test...");
80   LOG(astring("test file is ") + TEST_FILE());
81 #endif
82
83   chaos randomizer;
84
85 //hmmm: move to t_filename.
86   // test filename's exist operation.
87   byte_filer garbage(TEST_FILE().s(), "wb");
88   garbage.write("oy.\n");
89   garbage.close();
90   filename test1(TEST_FILE());
91   ASSERT_TRUE(test1.exists(), "exists test file should exist");
92   filename test2("c:\\this_file_shouldNt_exist_ever.txt");
93   ASSERT_FALSE(test2.exists(), "weird file should not existed");
94   // test again to make sure it didn't create it.
95   ASSERT_FALSE(test2.exists(), "weird file should still not exist");
96   test1.unlink();
97
98   int block_size = randomizer.inclusive(3000, 30000);
99 #ifdef DEBUG_BYTE_FILER
100   LOG(a_sprintf("block size=%d", block_size));
101 #endif
102   abyte *original_block = new abyte[block_size];
103   for (int i = 0; i < block_size; i++)
104     original_block[i] = abyte(randomizer.inclusive(32, 126));
105   unsigned int original_checksum
106       = checksums::bizarre_checksum((abyte *)original_block, block_size);
107   if (original_checksum) {} // compiler quieting.
108 #ifdef DEBUG_BYTE_FILER
109   LOG(a_sprintf("random block checksum=%d", original_checksum));
110 #endif
111   {
112     byte_array to_stuff_in_file(block_size, original_block);
113     delete [] original_block;
114     byte_filer fred(TEST_FILE(), "w+");
115     fred.write(to_stuff_in_file);
116   }
117 #ifdef DEBUG_BYTE_FILER
118   LOG(astring("about to compare file to checksum"));
119 #endif
120   {
121     abyte *temp_array = new abyte[21309];
122     byte_array to_fake_stuff(21309, temp_array);
123     delete [] temp_array;
124     byte_filer fred(TEST_FILE(), "r");
125 #ifdef DEBUG_BYTE_FILER
126     LOG(astring("about to try writing to file"));
127 #endif
128     int should_be_failure = fred.write(to_fake_stuff);
129     ASSERT_EQUAL(should_be_failure, 0, "write on read only, should not succeed");
130
131 #ifdef DEBUG_BYTE_FILER
132     LOG(a_sprintf("about to try reading from file %d bytes", block_size * 2));
133 #endif
134     byte_array file_contents;
135     int bytes_read = fred.read(file_contents, block_size * 2);
136     ASSERT_EQUAL(bytes_read, block_size, "reading entire file should get proper size");
137     un_int check_2 = checksums::bizarre_checksum((abyte *)file_contents.access(), file_contents.length());
138     ASSERT_EQUAL((int)check_2, (int)original_checksum, "should read correct contents for checksum");
139   }
140
141 #define FACTOR 1354
142
143   {
144     int numpacs = number_of_packets(block_size, FACTOR);
145     byte_filer fred(TEST_FILE(), "rb");
146 ///file::READ_ONLY);
147 ///    fred.chunk_factor(FACTOR);
148     int whole_size = 0;
149     for (int i = 0; i < numpacs; i++) {
150       byte_array blob_i;
151       int bytes_in = fred.read(blob_i, FACTOR);
152       ASSERT_FALSE(bytes_in > FACTOR, "we should never somehow read in more than we asked for");
153       whole_size += blob_i.length();
154     }
155     ASSERT_EQUAL(whole_size, fred.length(), "chunking comparison should see sizes as same");
156   }
157
158 // test writing out a copy and comparing them... there's no == on files!
159
160   ASSERT_TRUE(filename(TEST_FILE()).unlink(), "cleanup should be able to remove temporary file");
161
162   // it seems everything worked during our tests.
163   return 0;
164 }
165
166 int test_byte_filer::run_file_scan()
167 {
168   FUNCDEF("run_file_scan");
169   chaos randomizer;
170
171   string_array files(_global_argc, (const char **)_global_argv);
172   files.zap(0, 0);  // toss the first element since that's our app filename.
173
174   if (!files.length()) {
175     // pretend they gave us the list of files in the TMP directory.  some of
176     // these might fail if they're locked up.
177 //    astring tmpdir = environment::get("TMP");
178     astring tmpdir = application_configuration::current_directory();
179     directory dir(tmpdir);
180     for (int i = 0; i < dir.files().length(); i++) {
181       // skip text files since we use those right here.
182       if ( (dir.files()[i].ends(".txt")) || (dir.files()[i].ends(".txt")) )
183         continue;
184       astring chewed_string = tmpdir + "/" + dir.files()[i];
185       files += chewed_string;
186     }
187 //LOG(astring("added files since no cmd args: ") + files.text_form());
188   }
189
190   byte_array data_found;
191   for (int i = 0; i < files.length(); i++) {
192     astring curr = files[i];
193 //    LOG(a_sprintf("file %d: ", i) + curr);
194     byte_filer test(curr, "rb");
195     if (!test.good()) {
196       LOG(astring("good check: ") + curr + " cannot be opened.  is this bad?");
197       continue;
198     }
199
200     // check that we get the expected position report from scooting to the
201     // end of a file.
202     test.seek(0, byte_filer::FROM_END);
203     ASSERT_EQUAL((int)test.tell(), (int)test.length(), "seek check should get to end as expected");
204     test.seek(0, byte_filer::FROM_START);
205
206     size_t len = test.length();
207 //log(a_sprintf("file len is %.0f", double(len)));
208     size_t posn = 0;
209     while ( (posn < len) && !test.eof() ) {
210       size_t readlen = randomizer.inclusive(1, 256 * KILOBYTE);
211 //log(a_sprintf("read %u bytes, posn now %d bytes", readlen, posn));
212       int bytes_read = int(test.read(data_found, int(readlen)));
213       ASSERT_TRUE(bytes_read >= 0, "reading should not fail to read some bytes");
214       if (bytes_read > 0) {
215         posn += bytes_read;
216       }
217     }
218     ASSERT_TRUE(test.eof(), "eof check should see us at eof");
219     ASSERT_EQUAL((int)posn, (int)len, "eof check should be at right position");
220 //    log(astring("successfully read ") + curr);
221   }
222
223   return 0;
224 }
225
226 int test_byte_filer::execute()
227 {
228 //  FUNCDEF("execute");
229   int ret = run_simple_test();
230   if (ret) return ret;  // failed.
231   ret = run_file_scan();
232   if (ret) return ret;  // failed here.
233
234   return final_report();
235 }
236
237 HOOPLE_MAIN(test_byte_filer, )
238