first check-in of feisty meow codebase. many things broken still due to recent
[feisty_meow.git] / core / library / tests_filesystem / test_file_info.cpp
1 /*
2 *  Name   : test_file_info
3 *  Author : Chris Koeritz
4 * Copyright (c) 1991-$now By Author.  This program is free software; you can  *
5 *     http://www.fsf.org/copyleft/gpl.html                                    *
6 * Please send any updates to: fred@gruntose.com                               *
7 \*****************************************************************************/
8
9 #include <application/hoople_main.h>
10 #include <basis/enhance_cpp.h>
11 #include <basis/functions.h>
12 #include <basis/astring.h>
13 #include <filesystem/file_info.h>
14 #include <loggers/program_wide_logger.h>
15 #include <loggers/critical_events.h>
16 #include <mathematics/chaos.h>
17 #include <timely/time_stamp.h>
18 #include <unit_test/unit_base.h>
19
20 using namespace application;
21 using namespace basis;
22 using namespace filesystem;
23 using namespace loggers;
24 using namespace mathematics;
25 using namespace timely;
26 using namespace unit_test;
27
28 //#define DEBUG_TEST_FILE_INFO
29   // uncomment for noisy version.
30
31 #define LOG(s) CLASS_EMERGENCY_LOG(program_wide_logger::get(), s)
32
33 static chaos a_randomizer;
34
35 //////////////
36
37 class test_file_info : public application_shell, public unit_base
38 {
39 public:
40   test_file_info() : application_shell(), unit_base() {}
41
42   DEFINE_CLASS_NAME("test_file_info");
43
44   virtual int execute();
45 };
46
47 //////////////
48
49 //hmmm: stolen from ssl_init.
50 byte_array random_bytes(int length)
51 {
52   byte_array seed;
53   for (int i = 0; i < length; i++)
54     seed += abyte(chaos().inclusive(0, 255));
55   return seed;
56 }
57
58 int test_file_info::execute()
59 {
60   FUNCDEF("execute");
61 #ifdef __UNIX__
62   file_time absurdity_time("/");
63 #endif
64 #ifdef __WIN32__
65   file_time absurdity_time("c:/");
66 #endif
67
68   // test storing info via the constructor.
69   file_info testing(filename("/usr/schrodingers/dog/got/away"), 7298238);
70   testing._time = absurdity_time;
71   testing._checksum = 1283412;
72   ASSERT_EQUAL((int)testing._file_size, (int)7298238, "constructor file size");
73   ASSERT_EQUAL(testing._time, absurdity_time, "constructor file time");
74   ASSERT_EQUAL(testing._checksum, 1283412, "constructor checksum");
75   ASSERT_EQUAL((filename &)testing, filename("/usr/schrodingers/dog/got/away"),
76       "constructor filename");
77
78   // test packing the object and packed_size.
79   byte_array packed;
80   int size = testing.packed_size();
81   testing.pack(packed);
82   ASSERT_EQUAL(size, packed.length(), "basic packed size accuracy");
83   file_info unstuffy;
84   ASSERT_TRUE(unstuffy.unpack(packed), "basic unpacking");
85
86   // test validity after unpacking.
87   ASSERT_EQUAL((int)unstuffy._file_size, (int)7298238, "constructor file size");
88   ASSERT_EQUAL(unstuffy._time, absurdity_time, "constructor file time");
89   ASSERT_EQUAL(unstuffy._checksum, 1283412, "constructor checksum");
90   ASSERT_EQUAL((filename &)unstuffy, filename("/usr/schrodingers/dog/got/away"),
91       "constructor filename");
92
93   // test the extra bits, the attachment and secondary name.
94   astring seconame = "glorabahotep";
95   testing.secondary(seconame );
96   const byte_array randobytes = random_bytes(chaos().inclusive(37, 4128));
97   testing.attachment(randobytes);
98   packed.reset();
99   size = testing.packed_size();
100   testing.pack(packed);
101   ASSERT_EQUAL(size, packed.length(), "secondary packed size accuracy");
102   ASSERT_TRUE(unstuffy.unpack(packed), "secondary unpacking");
103   // test that the secondary name and attachment came back.
104   ASSERT_EQUAL(seconame, unstuffy.secondary(), "secondary name incorrect");
105   ASSERT_EQUAL(randobytes, unstuffy.attachment(), "secondary attachment inaccurate");
106
107   return final_report();
108 }
109
110 HOOPLE_MAIN(test_file_info, )
111