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 \*****************************************************************************/
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>
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;
28 //#define DEBUG_TEST_FILE_INFO
29 // uncomment for noisy version.
31 #define LOG(s) CLASS_EMERGENCY_LOG(program_wide_logger::get(), s)
33 static chaos a_randomizer;
37 class test_file_info : public application_shell, public unit_base
40 test_file_info() : application_shell(), unit_base() {}
42 DEFINE_CLASS_NAME("test_file_info");
44 virtual int execute();
49 //hmmm: stolen from ssl_init.
50 byte_array random_bytes(int length)
53 for (int i = 0; i < length; i++)
54 seed += abyte(chaos().inclusive(0, 255));
58 int test_file_info::execute()
62 file_time absurdity_time("/");
65 file_time absurdity_time("c:/");
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");
78 // test packing the object and packed_size.
80 int size = testing.packed_size();
82 ASSERT_EQUAL(size, packed.length(), "basic packed size accuracy");
84 ASSERT_TRUE(unstuffy.unpack(packed), "basic unpacking");
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");
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);
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");
107 return final_report();
110 HOOPLE_MAIN(test_file_info, )