|
1 /* |
|
2 * Copyright 2005-2006 Intel Corporation |
|
3 * |
|
4 * Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 * you may not use this file except in compliance with the License. |
|
6 * You may obtain a copy of the License at |
|
7 * |
|
8 * http://www.apache.org/licenses/LICENSE-2.0 |
|
9 * |
|
10 * Unless required by applicable law or agreed to in writing, software |
|
11 * distributed under the License is distributed on an "AS IS" BASIS, |
|
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 * See the License for the specific language governing permissions and |
|
14 * limitations under the License. |
|
15 */ |
|
16 |
|
17 #ifdef HAVE_CONFIG_H |
|
18 # include <dtn-config.h> |
|
19 #endif |
|
20 |
|
21 #include <oasys/util/UnitTest.h> |
|
22 |
|
23 #include "bundling/Bundle.h" |
|
24 #include "bundling/BundleList.h" |
|
25 #include <oasys/thread/SpinLock.h> |
|
26 #include "storage/BundleStore.h" |
|
27 #include "storage/DTNStorageConfig.h" |
|
28 |
|
29 using namespace dtn; |
|
30 using namespace oasys; |
|
31 |
|
32 int |
|
33 payload_test(BundlePayload::location_t location) |
|
34 { |
|
35 u_char buf[64]; |
|
36 const u_char* data; |
|
37 SpinLock l; |
|
38 BundlePayload p(&l); |
|
39 bzero(buf, sizeof(buf)); |
|
40 |
|
41 int errno_; const char* strerror_; |
|
42 |
|
43 log_debug_p("/test", "checking initialization"); |
|
44 p.init(1, location); |
|
45 CHECK_EQUAL(p.location(), location); |
|
46 CHECK(p.length() == 0); |
|
47 |
|
48 log_debug_p("/test", "checking set_data"); |
|
49 p.set_data((const u_char*)"abcdefghijklmnopqrstuvwxyz", 26); |
|
50 CHECK_EQUAL(p.location(), location); |
|
51 CHECK_EQUAL(p.length(), 26); |
|
52 |
|
53 log_debug_p("/test", "checking read_data"); |
|
54 data = p.read_data(0, 26, buf); |
|
55 CHECK_EQUALSTR((char*)data, "abcdefghijklmnopqrstuvwxyz"); |
|
56 data = p.read_data(10, 5, buf); |
|
57 CHECK_EQUALSTRN((char*)data, "klmno", 5); |
|
58 |
|
59 log_debug_p("/test", "checking truncate"); |
|
60 p.truncate(10); |
|
61 data = p.read_data(0, 10, buf); |
|
62 CHECK_EQUAL(p.length(), 10); |
|
63 CHECK_EQUALSTRN((char*)data, "abcdefghij", 10); |
|
64 |
|
65 log_debug_p("/test", "checking append_data"); |
|
66 p.append_data((u_char*)"ABCDE", 5); |
|
67 data = p.read_data(0, 15, buf); |
|
68 CHECK_EQUALSTRN((char*)data, "abcdefghijABCDE", 15); |
|
69 |
|
70 p.append_data((u_char*)"FGH", 3); |
|
71 data = p.read_data(0, 18, buf); |
|
72 CHECK_EQUAL(p.length(), 18); |
|
73 CHECK_EQUALSTRN((char*)data, "abcdefghijABCDEFGH", 18); |
|
74 |
|
75 log_debug_p("/test", "checking write_data overwrite"); |
|
76 p.write_data((u_char*)"BCD", 1, 3); |
|
77 data = p.read_data(0, 18, buf); |
|
78 CHECK_EQUAL(p.length(), 18); |
|
79 CHECK_EQUALSTRN((char*)data, "aBCDefghijABCDEFGH", 18); |
|
80 |
|
81 log_debug_p("/test", "checking write_data with gap"); |
|
82 p.set_length(30); |
|
83 p.write_data((u_char*)"XXXXX", 25, 5); |
|
84 CHECK_EQUAL(p.length(), 30); |
|
85 |
|
86 p.write_data((u_char*)"_______", 18, 7); |
|
87 data = p.read_data(0, 30, buf); |
|
88 CHECK_EQUAL(p.length(), 30); |
|
89 CHECK_EQUALSTR((char*)data, "aBCDefghijABCDEFGH_______XXXXX"); |
|
90 |
|
91 return UNIT_TEST_PASSED; |
|
92 } |
|
93 |
|
94 DECLARE_TEST(MemoryPayloadTest) { |
|
95 return payload_test(BundlePayload::MEMORY); |
|
96 } |
|
97 |
|
98 DECLARE_TEST(DiskPayloadTest) { |
|
99 return payload_test(BundlePayload::DISK); |
|
100 } |
|
101 |
|
102 DECLARE_TESTER(BundlePayloadTester) { |
|
103 ADD_TEST(MemoryPayloadTest); |
|
104 ADD_TEST(DiskPayloadTest); |
|
105 } |
|
106 |
|
107 int |
|
108 main(int argc, const char** argv) |
|
109 { |
|
110 BundlePayloadTester t("bundle payload test"); |
|
111 t.init(argc, argv, true); |
|
112 |
|
113 system("rm -rf .bundle-payload-test"); |
|
114 system("mkdir .bundle-payload-test"); |
|
115 DTNStorageConfig cfg("", "memorydb", "", ""); |
|
116 cfg.init_ = true; |
|
117 cfg.payload_dir_.assign(".bundle-payload-test"); |
|
118 cfg.leave_clean_file_ = false; |
|
119 oasys::DurableStore ds("/test/ds"); |
|
120 ds.create_store(cfg); |
|
121 BundleStore::init(cfg, &ds); |
|
122 |
|
123 t.run_tests(); |
|
124 |
|
125 system("rm -rf .bundle-payload-test"); |
|
126 } |