|
31 |
// Some utility functions for the tests. |
31 |
// Some utility functions for the tests. |
32 |
// =========================================================================== |
32 |
// =========================================================================== |
33 |
|
33 |
|
34 |
uint16_t |
34 |
static uint16_t |
35 |
Swap (uint16_t val) |
35 |
Swap (uint16_t val) |
36 |
{ |
36 |
{ |
37 |
return ((val >> 8) & 0x00ff) | ((val << 8) & 0xff00); |
37 |
return ((val >> 8) & 0x00ff) | ((val << 8) & 0xff00); |
38 |
} |
38 |
} |
39 |
|
39 |
|
40 |
uint32_t |
40 |
static uint32_t |
41 |
Swap (uint32_t val) |
41 |
Swap (uint32_t val) |
42 |
{ |
42 |
{ |
43 |
return ((val >> 24) & 0x000000ff) | ((val >> 8) & 0x0000ff00) | ((val << 8) & 0x00ff0000) | ((val << 24) & 0xff000000); |
43 |
return ((val >> 24) & 0x000000ff) | ((val >> 8) & 0x0000ff00) | ((val << 8) & 0x00ff0000) | ((val << 24) & 0xff000000); |
44 |
} |
44 |
} |
45 |
|
45 |
|
46 |
bool |
46 |
static bool |
47 |
CheckFileExists (std::string filename) |
47 |
CheckFileExists (std::string filename) |
48 |
{ |
48 |
{ |
49 |
FILE * p = fopen (filename.c_str (), "rb"); |
49 |
FILE * p = fopen (filename.c_str (), "rb"); |
|
57 |
} |
57 |
} |
58 |
|
58 |
|
59 |
|
59 |
|
60 |
bool |
60 |
static bool |
61 |
CheckFileLength (std::string filename, uint64_t sizeExpected) |
61 |
CheckFileLength (std::string filename, uint64_t sizeExpected) |
62 |
{ |
62 |
{ |
63 |
FILE * p = fopen (filename.c_str (), "rb"); |
63 |
FILE * p = fopen (filename.c_str (), "rb"); |
|
93 |
}; |
93 |
}; |
94 |
|
94 |
|
95 |
WriteModeCreateTestCase::WriteModeCreateTestCase () |
95 |
WriteModeCreateTestCase::WriteModeCreateTestCase () |
96 |
: TestCase ("Check to see that PcapFile::Open with mode \"w\" works") |
96 |
: TestCase ("Check to see that PcapFile::Open with mode std::ios::out works") |
97 |
{ |
97 |
{ |
98 |
} |
98 |
} |
99 |
|
99 |
|
|
125 |
// Opening a new file in write mode should result in an empty file of the |
125 |
// Opening a new file in write mode should result in an empty file of the |
126 |
// given name. |
126 |
// given name. |
127 |
// |
127 |
// |
128 |
bool err = f.Open (m_testFilename, "w"); |
128 |
f.Open (m_testFilename, std::ios::out); |
129 |
|
129 |
|
130 |
NS_TEST_ASSERT_MSG_EQ (err, false, "Open (" << m_testFilename << ", \"w\") returns error"); |
130 |
NS_TEST_ASSERT_MSG_EQ (f.Fail (), false, "Open (" << m_testFilename << ", \"w\") returns error"); |
131 |
f.Close (); |
131 |
f.Close (); |
132 |
|
132 |
|
133 |
NS_TEST_ASSERT_MSG_EQ (CheckFileExists (m_testFilename), true, |
133 |
NS_TEST_ASSERT_MSG_EQ (CheckFileExists (m_testFilename), true, |
134 |
"Open (" << m_testFilename << ", \"w\") does not create file"); |
134 |
"Open (" << m_testFilename << ", \"std::ios::out\") does not create file"); |
135 |
NS_TEST_ASSERT_MSG_EQ (CheckFileLength (m_testFilename, 0), true, |
135 |
NS_TEST_ASSERT_MSG_EQ (CheckFileLength (m_testFilename, 0), true, |
136 |
"Open (" << m_testFilename << ", \"w\") does not result in an empty file"); |
136 |
"Open (" << m_testFilename << ", \"std::ios::out\") does not result in an empty file"); |
137 |
|
137 |
|
138 |
// |
138 |
// |
139 |
// Calling Init() on a file created with "w" should result in a file just |
139 |
// Calling Init() on a file created with "std::ios::out" should result in a file just |
140 |
// long enough to contain the pcap file header. |
140 |
// long enough to contain the pcap file header. |
141 |
// |
141 |
// |
142 |
err = f.Open (m_testFilename, "w"); |
142 |
f.Open (m_testFilename, std::ios::out); |
143 |
NS_TEST_ASSERT_MSG_EQ (err, false, "Open (" << m_testFilename << ", \"w\") returns error"); |
143 |
NS_TEST_ASSERT_MSG_EQ (f.Fail (), false, "Open (" << m_testFilename << |
|
|
144 |
", \"std::ios::out\") returns error"); |
144 |
|
145 |
|
145 |
err = f.Init (1234, 5678, 7); |
146 |
f.Init (1234, 5678, 7); |
146 |
NS_TEST_ASSERT_MSG_EQ (err, false, "Init (1234, 5678, 7) returns error"); |
147 |
NS_TEST_ASSERT_MSG_EQ (f.Fail (), false, "Init (1234, 5678, 7) returns error"); |
147 |
|
148 |
|
148 |
f.Close (); |
149 |
f.Close (); |
149 |
|
150 |
|
|
154 |
// Opening an existing file in write mode should result in that file being |
155 |
// Opening an existing file in write mode should result in that file being |
155 |
// emptied. |
156 |
// emptied. |
156 |
// |
157 |
// |
157 |
err = f.Open (m_testFilename, "w"); |
158 |
f.Open (m_testFilename, std::ios::out); |
158 |
NS_TEST_ASSERT_MSG_EQ (err, false, "Open (" << m_testFilename << ", \"w\") returns error"); |
159 |
NS_TEST_ASSERT_MSG_EQ (f.Fail (), false, "Open (" << m_testFilename << |
|
|
160 |
", \"std::ios::out\") returns error"); |
159 |
|
161 |
|
160 |
f.Close (); |
162 |
f.Close (); |
161 |
|
163 |
|
|
165 |
// |
167 |
// |
166 |
// Initialize the file again. |
168 |
// Initialize the file again. |
167 |
// |
169 |
// |
168 |
err = f.Open (m_testFilename, "w"); |
170 |
f.Open (m_testFilename, std::ios::out); |
169 |
NS_TEST_ASSERT_MSG_EQ (err, false, |
171 |
NS_TEST_ASSERT_MSG_EQ (f.Fail (), false, |
170 |
"Open (" << m_testFilename << ", \"w\") returns error"); |
172 |
"Open (" << m_testFilename << ", \"w\") returns error"); |
171 |
|
173 |
|
172 |
err = f.Init (1234, 5678, 7); |
174 |
f.Init (1234, 5678, 7); |
173 |
NS_TEST_ASSERT_MSG_EQ (err, false, "Init (1234, 5678, 7) returns error"); |
175 |
NS_TEST_ASSERT_MSG_EQ (f.Fail (), false, "Init (1234, 5678, 7) returns error"); |
174 |
|
176 |
|
175 |
// |
177 |
// |
176 |
// Now we should be able to write to it since it was opened in "w" mode. |
178 |
// Now we should be able to write to it since it was opened in std::ios::out mode. |
177 |
// This is just a permissions check so we don't actually look at the |
179 |
// This is just a permissions check so we don't actually look at the |
178 |
// data. |
180 |
// data. |
179 |
// |
181 |
// |
180 |
uint8_t buffer[128]; |
182 |
uint8_t buffer[128]; |
181 |
memset(buffer, 0, sizeof(buffer)); |
183 |
memset(buffer, 0, sizeof(buffer)); |
182 |
err = f.Write (0, 0, buffer, 128); |
184 |
f.Write (0, 0, buffer, 128); |
183 |
NS_TEST_ASSERT_MSG_EQ (err, false, "Write (write-only-file " << m_testFilename << ") returns error"); |
185 |
NS_TEST_ASSERT_MSG_EQ (f.Fail (), false, "Write (write-only-file " << m_testFilename << |
|
|
186 |
") returns error"); |
184 |
|
187 |
|
185 |
return false; |
188 |
return false; |
186 |
} |
189 |
} |
|
204 |
}; |
207 |
}; |
205 |
|
208 |
|
206 |
ReadModeCreateTestCase::ReadModeCreateTestCase () |
209 |
ReadModeCreateTestCase::ReadModeCreateTestCase () |
207 |
: TestCase ("Check to see that PcapFile::Open with mode \"r\" works") |
210 |
: TestCase ("Check to see that PcapFile::Open with mode \"std::ios::in\" works") |
208 |
{ |
211 |
{ |
209 |
} |
212 |
} |
210 |
|
213 |
|
|
235 |
// |
238 |
// |
236 |
// Opening a non-existing file in read mode should result in an error. |
239 |
// Opening a non-existing file in read mode should result in an error. |
237 |
// |
240 |
// |
238 |
bool err = f.Open (m_testFilename, "r"); |
241 |
f.Open (m_testFilename, std::ios::in); |
239 |
NS_TEST_ASSERT_MSG_EQ (err, true, "Open (non-existing-filename " << m_testFilename << ", \"r\") does not return error"); |
242 |
NS_TEST_ASSERT_MSG_EQ (f.Fail (), true, "Open (non-existing-filename " << m_testFilename << |
240 |
|
243 |
", \"std::ios::in\") does not return error"); |
241 |
NS_TEST_ASSERT_MSG_EQ (CheckFileExists (m_testFilename), false, |
244 |
f.Close (); |
242 |
"Open (" << m_testFilename << ", \"r\") unexpectedly created a file"); |
245 |
f.Clear (); |
|
|
246 |
NS_TEST_ASSERT_MSG_EQ (CheckFileExists (m_testFilename), false, "Open (" << m_testFilename << |
247 |
", \"std::ios::in\") unexpectedly created a file"); |
243 |
|
248 |
|
244 |
// |
249 |
// |
245 |
// Okay, now create an uninitialized file using previously tested operations |
250 |
// Okay, now create an uninitialized file using previously tested operations |
246 |
// |
251 |
// |
247 |
err = f.Open (m_testFilename, "w"); |
252 |
f.Open (m_testFilename, std::ios::out); |
248 |
NS_TEST_ASSERT_MSG_EQ (err, false, "Open (filename, \"w\") returns error"); |
253 |
NS_TEST_ASSERT_MSG_EQ (f.Fail (), false, "Open (filename, \"std::ios::out\") returns error"); |
249 |
f.Close (); |
254 |
f.Close (); |
250 |
|
255 |
|
251 |
// |
256 |
// |
252 |
// Opening this file should result in an error since it has no pcap file header. |
257 |
// Opening this file should result in an error since it has no pcap file header. |
253 |
// |
258 |
// |
254 |
err = f.Open (m_testFilename, "r"); |
259 |
f.Open (m_testFilename, std::ios::in); |
255 |
NS_TEST_ASSERT_MSG_EQ (err, true, "Open (non-initialized-filename " << m_testFilename << ", \"r\") does not return error"); |
260 |
NS_TEST_ASSERT_MSG_EQ (f.Fail (), true, "Open (non-initialized-filename " << m_testFilename << |
|
|
261 |
", \"std::ios::in\") does not return error"); |
262 |
f.Close (); |
263 |
f.Clear (); |
256 |
|
264 |
|
257 |
// |
265 |
// |
258 |
// Okay, now open that non-initialized file in write mode and initialize it |
266 |
// Okay, now open that non-initialized file in write mode and initialize it |
259 |
// Note that we open it in write mode to initialize it. |
267 |
// Note that we open it in write mode to initialize it. |
260 |
// |
268 |
// |
261 |
err = f.Open (m_testFilename, "w"); |
269 |
f.Open (m_testFilename, std::ios::out); |
262 |
NS_TEST_ASSERT_MSG_EQ (err, false, "Open (" << m_testFilename << ", \"w\") returns error"); |
270 |
NS_TEST_ASSERT_MSG_EQ (f.Fail (), false, "Open (" << m_testFilename << |
|
|
271 |
", \"std::ios::out\") returns error"); |
263 |
|
272 |
|
264 |
err = f.Init (1234, 5678, 7); |
273 |
f.Init (1234, 5678, 7); |
265 |
NS_TEST_ASSERT_MSG_EQ (err, false, "Init (1234, 5678, 7) returns error"); |
274 |
NS_TEST_ASSERT_MSG_EQ (f.Fail (), false, "Init (1234, 5678, 7) returns error"); |
266 |
f.Close (); |
275 |
f.Close (); |
267 |
|
276 |
|
268 |
// |
277 |
// |
269 |
// Opening this file should now work since it has a pcap file header. |
278 |
// Opening this file should now work since it has a pcap file header. |
270 |
// |
279 |
// |
271 |
err = f.Open (m_testFilename, "r"); |
280 |
f.Open (m_testFilename, std::ios::in); |
272 |
NS_TEST_ASSERT_MSG_EQ (err, false, "Open (initialized-filename " << m_testFilename << ", \"r\") returns error"); |
281 |
NS_TEST_ASSERT_MSG_EQ (f.Fail (), false, "Open (initialized-filename " << m_testFilename << |
|
|
282 |
", \"std::ios::in\") returns error"); |
273 |
|
283 |
|
274 |
// |
284 |
// |
275 |
// Now we should not be able to write to it since it was opened in "r" mode |
285 |
// Now we should not be able to write to it since it was opened in "r" mode |
276 |
// even if it has been initialized.. |
286 |
// even if it has been initialized.. |
277 |
// |
287 |
// |
278 |
uint8_t buffer[128]; |
288 |
uint8_t buffer[128]; |
279 |
err = f.Write (0, 0, buffer, 128); |
289 |
f.Write (0, 0, buffer, 128); |
280 |
NS_TEST_ASSERT_MSG_EQ (err, true, "Write (read-only-file " << m_testFilename << ") does not return error"); |
290 |
NS_TEST_ASSERT_MSG_EQ (f.Fail (), true, "Write (read-only-file " << m_testFilename << |
281 |
|
291 |
") does not return error"); |
282 |
f.Close (); |
292 |
f.Close (); |
|
|
293 |
f.Clear (); |
283 |
|
294 |
|
284 |
return false; |
295 |
return false; |
285 |
} |
296 |
} |
286 |
|
297 |
|
|
|
298 |
#if 0 |
287 |
// =========================================================================== |
299 |
// =========================================================================== |
288 |
// Test case to make sure that the Pcap File Object can open an existing pcap |
300 |
// Test case to make sure that the Pcap File Object can open an existing pcap |
289 |
// file for appending. |
301 |
// file for appending. |
|
303 |
}; |
315 |
}; |
304 |
|
316 |
|
305 |
AppendModeCreateTestCase::AppendModeCreateTestCase () |
317 |
AppendModeCreateTestCase::AppendModeCreateTestCase () |
306 |
: TestCase ("Check to see that PcapFile::Open with mode \"a\" works") |
318 |
: TestCase ("Check to see that PcapFile::Open with mode \"std::ios::app\" works") |
307 |
{ |
319 |
{ |
308 |
} |
320 |
} |
309 |
|
321 |
|
|
334 |
// |
346 |
// |
335 |
// Opening a non-existing file in append mode should result in an error. |
347 |
// Opening a non-existing file in append mode should result in an error. |
336 |
// |
348 |
// |
337 |
bool err = f.Open (m_testFilename, "a"); |
349 |
f.Open (m_testFilename, std::ios::out | std::ios::app); |
338 |
NS_TEST_ASSERT_MSG_EQ (err, true, "Open (non-existing-filename " << m_testFilename << ", \"a\") does not return error"); |
350 |
NS_TEST_ASSERT_MSG_EQ (f.Fail (), true, "Open (non-existing-filename " << m_testFilename << |
|
|
351 |
", \"std::ios::app\") does not return error"); |
339 |
f.Close (); |
352 |
f.Close (); |
|
|
353 |
f.Clear (); |
340 |
|
354 |
|
341 |
NS_TEST_ASSERT_MSG_EQ (CheckFileExists (m_testFilename), false, |
355 |
NS_TEST_ASSERT_MSG_EQ (CheckFileExists (m_testFilename), false, |
342 |
"Open (" << m_testFilename << ", \"a\") unexpectedly created a file"); |
356 |
"Open (" << m_testFilename << ", \"std::ios::app\") unexpectedly created a file"); |
343 |
|
357 |
|
344 |
// |
358 |
// |
345 |
// Okay, now create an uninitialized file using previously tested operations |
359 |
// Okay, now create an uninitialized file using previously tested operations |
346 |
// |
360 |
// |
347 |
err = f.Open (m_testFilename, "w"); |
361 |
f.Open (m_testFilename, std::ios::out); |
348 |
NS_TEST_ASSERT_MSG_EQ (err, false, "Open (" << m_testFilename << ", \"w\") returns error"); |
362 |
NS_TEST_ASSERT_MSG_EQ (f.Fail (), false, "Open (" << m_testFilename << |
|
|
363 |
", \"std::ios::out\") returns error"); |
349 |
f.Close (); |
364 |
f.Close (); |
350 |
|
365 |
|
351 |
// |
366 |
// |
352 |
// Opening this file should result in an error since it has no pcap file header. |
367 |
// Opening this file should result in an error since it has no pcap file header. |
353 |
// |
368 |
// |
354 |
err = f.Open (m_testFilename, "a"); |
369 |
f.Open (m_testFilename, std::ios::out | std::ios::app); |
355 |
NS_TEST_ASSERT_MSG_EQ (err, true, "Open (non-initialized-filename " << m_testFilename << ", \"a\") does not return error"); |
370 |
NS_TEST_ASSERT_MSG_EQ (f.Fail (), true, "Open (non-initialized-filename " << m_testFilename << |
|
|
371 |
", \"std::ios::app\") does not return error"); |
372 |
f.Close (); |
373 |
f.Clear (); |
356 |
|
374 |
|
357 |
// |
375 |
// |
358 |
// Okay, now open that non-initialized file in write mode and initialize it. |
376 |
// Okay, now open that non-initialized file in write mode and initialize it. |
359 |
// |
377 |
// |
360 |
err = f.Open (m_testFilename, "w"); |
378 |
f.Open (m_testFilename, std::ios::out); |
361 |
NS_TEST_ASSERT_MSG_EQ (err, false, "Open (non-initialized-filename " << m_testFilename << ", \"w\") returns error"); |
379 |
NS_TEST_ASSERT_MSG_EQ (f.Fail (), false, "Open (non-initialized-filename " << m_testFilename << |
|
|
380 |
", \"std::ios::out\") returns error"); |
362 |
|
381 |
|
363 |
err = f.Init (1234, 5678, 7); |
382 |
f.Init (1234, 5678, 7); |
364 |
NS_TEST_ASSERT_MSG_EQ (err, false, "Init (1234, 5678, 7) returns error"); |
383 |
NS_TEST_ASSERT_MSG_EQ (f.Fail (), false, "Init (1234, 5678, 7) returns error"); |
365 |
f.Close (); |
384 |
f.Close (); |
366 |
|
385 |
|
367 |
// |
386 |
// |
368 |
// Opening this file should now work since it has a pcap file header. |
387 |
// Opening this file should now work since it has a pcap file header. |
369 |
// |
388 |
// |
370 |
err = f.Open (m_testFilename, "a"); |
389 |
f.Open (m_testFilename, std::ios::out | std::ios::app); |
371 |
NS_TEST_ASSERT_MSG_EQ (err, false, "Open (initialized-filename " << m_testFilename << ", \"r\") returns error"); |
390 |
NS_TEST_ASSERT_MSG_EQ (f.Fail (), false, "Open (initialized-filename " << m_testFilename << |
|
|
391 |
", \"std::ios::app\") returns error"); |
372 |
|
392 |
|
373 |
// |
393 |
// |
374 |
// We should be able to write to it since it was opened in "a" mode. |
394 |
// We should be able to write to it since it was opened in "std::ios::app" mode. |
375 |
// |
395 |
// |
376 |
uint8_t buffer[128]; |
396 |
uint8_t buffer[128]; |
377 |
memset(buffer, 0, sizeof(buffer)); |
397 |
memset(buffer, 0, sizeof(buffer)); |
378 |
err = f.Write (0, 0, buffer, 128); |
398 |
f.Write (0, 0, buffer, 128); |
379 |
NS_TEST_ASSERT_MSG_EQ (err, false, "Write (append-mode-file " << m_testFilename << ") returns error"); |
399 |
NS_TEST_ASSERT_MSG_EQ (f.Fail (), false, "Write (append-mode-file " << m_testFilename << ") returns error"); |
380 |
|
400 |
|
381 |
f.Close (); |
401 |
f.Close (); |
382 |
|
402 |
|
383 |
return false; |
403 |
return false; |
384 |
} |
404 |
} |
|
|
405 |
#endif |
385 |
|
406 |
|
386 |
// =========================================================================== |
407 |
// =========================================================================== |
387 |
// Test case to make sure that the Pcap File Object can write out correct pcap |
408 |
// Test case to make sure that the Pcap File Object can write out correct pcap |
|
433 |
// |
454 |
// |
434 |
// Create an uninitialized file using previously tested operations |
455 |
// Create an uninitialized file using previously tested operations |
435 |
// |
456 |
// |
436 |
bool err = f.Open (m_testFilename, "w"); |
457 |
f.Open (m_testFilename, std::ios::out); |
437 |
NS_TEST_ASSERT_MSG_EQ (err, false, "Open (" << m_testFilename << ", \"w\") returns error"); |
458 |
NS_TEST_ASSERT_MSG_EQ (f.Fail (), false, "Open (" << m_testFilename << |
438 |
|
459 |
", \"std::ios::out\") returns error"); |
|
|
460 |
|
439 |
// |
461 |
// |
440 |
// Initialize the pcap file header. |
462 |
// Initialize the pcap file header. |
441 |
// |
463 |
// |
442 |
err = f.Init (1234, 5678, 7); |
464 |
f.Init (1234, 5678, 7); |
443 |
NS_TEST_ASSERT_MSG_EQ (err, false, |
465 |
NS_TEST_ASSERT_MSG_EQ (f.Fail (), false, |
444 |
"Init (1234, 5678, 7) returns error"); |
466 |
"Init (1234, 5678, 7) returns error"); |
445 |
f.Close (); |
467 |
f.Close (); |
446 |
|
468 |
|
|
525 |
// automagically fixed up when using a PcapFile to read the values, so we |
547 |
// automagically fixed up when using a PcapFile to read the values, so we |
526 |
// don't have to do anything special here. |
548 |
// don't have to do anything special here. |
527 |
// |
549 |
// |
528 |
err = f.Open (m_testFilename, "r"); |
550 |
f.Open (m_testFilename, std::ios::in); |
529 |
NS_TEST_ASSERT_MSG_EQ (err, false, "Open (existing-initialized-file " << m_testFilename << ", \"r\") returns error"); |
551 |
NS_TEST_ASSERT_MSG_EQ (f.Fail (), false, "Open (existing-initialized-file " << m_testFilename << |
|
|
552 |
", \"std::ios::in\") returns error"); |
530 |
|
553 |
|
531 |
NS_TEST_ASSERT_MSG_EQ (f.GetMagic (), 0xa1b2c3d4, "Read back magic number incorrectly"); |
554 |
NS_TEST_ASSERT_MSG_EQ (f.GetMagic (), 0xa1b2c3d4, "Read back magic number incorrectly"); |
532 |
NS_TEST_ASSERT_MSG_EQ (f.GetVersionMajor (), 2, "Read back version major incorrectly"); |
555 |
NS_TEST_ASSERT_MSG_EQ (f.GetVersionMajor (), 2, "Read back version major incorrectly"); |
|
535 |
NS_TEST_ASSERT_MSG_EQ (f.GetSigFigs (), 0, "Read back sig figs incorrectly"); |
558 |
NS_TEST_ASSERT_MSG_EQ (f.GetSigFigs (), 0, "Read back sig figs incorrectly"); |
536 |
NS_TEST_ASSERT_MSG_EQ (f.GetSnapLen (), 5678, "Read back snap len incorrectly"); |
559 |
NS_TEST_ASSERT_MSG_EQ (f.GetSnapLen (), 5678, "Read back snap len incorrectly"); |
537 |
NS_TEST_ASSERT_MSG_EQ (f.GetDataLinkType (), 1234, "Read back data link type incorrectly"); |
560 |
NS_TEST_ASSERT_MSG_EQ (f.GetDataLinkType (), 1234, "Read back data link type incorrectly"); |
|
|
561 |
f.Close (); |
538 |
|
562 |
|
539 |
// |
563 |
// |
540 |
// Re-open the file to erase its contents. |
564 |
// Re-open the file to erase its contents. |
541 |
// |
565 |
// |
542 |
err = f.Open (m_testFilename, "w"); |
566 |
f.Open (m_testFilename, std::ios::out); |
543 |
NS_TEST_ASSERT_MSG_EQ (err, false, "Open (" << m_testFilename << ", \"w\") returns error"); |
567 |
NS_TEST_ASSERT_MSG_EQ (f.Fail (), false, "Open (" << m_testFilename << |
|
|
568 |
", \"std::ios::out\") returns error"); |
544 |
|
569 |
|
545 |
// |
570 |
// |
546 |
// Initialize the pcap file header, turning on swap mode manually to force |
571 |
// Initialize the pcap file header, turning on swap mode manually to force |
|
550 |
// a no-op and we're always writing foreign-endian files. In that case, |
575 |
// a no-op and we're always writing foreign-endian files. In that case, |
551 |
// this test case is really just a duplicate of the previous. |
576 |
// this test case is really just a duplicate of the previous. |
552 |
// |
577 |
// |
553 |
err = f.Init (1234, 5678, 7, true); |
578 |
f.Init (1234, 5678, 7, true); |
554 |
NS_TEST_ASSERT_MSG_EQ (err, false, "Init (1234, 5678, 7) returns error"); |
579 |
NS_TEST_ASSERT_MSG_EQ (f.Fail (), false, "Init (1234, 5678, 7) returns error"); |
555 |
f.Close (); |
580 |
f.Close (); |
556 |
|
581 |
|
557 |
// |
582 |
// |
|
599 |
// machine is writing out a big-endian file by default, but we can't do that |
624 |
// machine is writing out a big-endian file by default, but we can't do that |
600 |
// since it breaks regression testing. |
625 |
// since it breaks regression testing. |
601 |
// |
626 |
// |
602 |
err = f.Open (m_testFilename, "r"); |
627 |
f.Open (m_testFilename, std::ios::in); |
603 |
NS_TEST_ASSERT_MSG_EQ (err, false, "Open (existing-initialized-file " << m_testFilename << ", \"r\") returns error"); |
628 |
NS_TEST_ASSERT_MSG_EQ (f.Fail (), false, "Open (existing-initialized-file " << m_testFilename << |
|
|
629 |
", \"std::ios::in\") returns error"); |
604 |
|
630 |
|
605 |
NS_TEST_ASSERT_MSG_EQ (f.GetSwapMode (), true, "Byte-swapped file not correctly indicated"); |
631 |
NS_TEST_ASSERT_MSG_EQ (f.GetSwapMode (), true, "Byte-swapped file not correctly indicated"); |
606 |
|
632 |
|
|
667 |
// |
693 |
// |
668 |
// Create an uninitialized file using previously tested operations |
694 |
// Create an uninitialized file using previously tested operations |
669 |
// |
695 |
// |
670 |
bool err = f.Open (m_testFilename, "w"); |
696 |
f.Open (m_testFilename, std::ios::out); |
671 |
NS_TEST_ASSERT_MSG_EQ (err, false, "Open (" << m_testFilename << ", \"w\") returns error"); |
697 |
NS_TEST_ASSERT_MSG_EQ (f.Fail (), false, "Open (" << m_testFilename << |
|
|
698 |
", \"std::ios::out\") returns error"); |
672 |
|
699 |
|
673 |
// |
700 |
// |
674 |
// Initialize the pcap file header. |
701 |
// Initialize the pcap file header. |
675 |
// |
702 |
// |
676 |
err = f.Init (37, 43, -7); |
703 |
f.Init (37, 43, -7); |
677 |
NS_TEST_ASSERT_MSG_EQ (err, false, "Init (37, 43, -7) returns error"); |
704 |
NS_TEST_ASSERT_MSG_EQ (f.Fail (), false, "Init (37, 43, -7) returns error"); |
678 |
|
705 |
|
679 |
// |
706 |
// |
680 |
// Initialize a buffer with a counting pattern to check the data later. |
707 |
// Initialize a buffer with a counting pattern to check the data later. |
|
690 |
// mode. The packet data written should be limited to 43 bytes in length |
717 |
// mode. The packet data written should be limited to 43 bytes in length |
691 |
// by the Init() call above. |
718 |
// by the Init() call above. |
692 |
// |
719 |
// |
693 |
err = f.Write (1234, 5678, bufferOut, 128); |
720 |
f.Write (1234, 5678, bufferOut, 128); |
694 |
NS_TEST_ASSERT_MSG_EQ (err, false, "Write (write-only-file " << m_testFilename << ") returns error"); |
721 |
NS_TEST_ASSERT_MSG_EQ (f.Fail (), false, "Write (write-only-file " << m_testFilename << ") returns error"); |
695 |
f.Close (); |
722 |
f.Close (); |
696 |
|
723 |
|
697 |
// |
724 |
// |
|
785 |
// Let's see if the PcapFile object can figure out how to do the same thing and |
812 |
// Let's see if the PcapFile object can figure out how to do the same thing and |
786 |
// correctly read in a packet. |
813 |
// correctly read in a packet. |
787 |
// |
814 |
// |
788 |
err = f.Open (m_testFilename, "r"); |
815 |
f.Open (m_testFilename, std::ios::in); |
789 |
NS_TEST_ASSERT_MSG_EQ (err, false, "Open (" << m_testFilename << ", \"r\") of existing good file returns error"); |
816 |
NS_TEST_ASSERT_MSG_EQ (f.Fail (), false, "Open (" << m_testFilename << |
|
|
817 |
", \"std::ios::in\") of existing good file returns error"); |
790 |
|
818 |
|
791 |
uint32_t tsSec, tsUsec, inclLen, origLen, readLen; |
819 |
uint32_t tsSec, tsUsec, inclLen, origLen, readLen; |
792 |
|
820 |
|
793 |
err = f.Read (bufferIn, sizeof(bufferIn), tsSec, tsUsec, inclLen, origLen, readLen); |
821 |
f.Read (bufferIn, sizeof(bufferIn), tsSec, tsUsec, inclLen, origLen, readLen); |
794 |
NS_TEST_ASSERT_MSG_EQ (err, false, "Read() of known good packet returns error"); |
822 |
NS_TEST_ASSERT_MSG_EQ (f.Fail (), false, "Read() of known good packet returns error"); |
795 |
NS_TEST_ASSERT_MSG_EQ (tsSec, 1234, "Incorrectly read seconds timestap from known good packet"); |
823 |
NS_TEST_ASSERT_MSG_EQ (tsSec, 1234, "Incorrectly read seconds timestap from known good packet"); |
796 |
NS_TEST_ASSERT_MSG_EQ (tsUsec, 5678, "Incorrectly read microseconds timestap from known good packet"); |
824 |
NS_TEST_ASSERT_MSG_EQ (tsUsec, 5678, "Incorrectly read microseconds timestap from known good packet"); |
797 |
NS_TEST_ASSERT_MSG_EQ (inclLen, 43, "Incorrectly read included length from known good packet"); |
825 |
NS_TEST_ASSERT_MSG_EQ (inclLen, 43, "Incorrectly read included length from known good packet"); |
798 |
NS_TEST_ASSERT_MSG_EQ (origLen, 128, "Incorrectly read original length from known good packet"); |
826 |
NS_TEST_ASSERT_MSG_EQ (origLen, 128, "Incorrectly read original length from known good packet"); |
799 |
NS_TEST_ASSERT_MSG_EQ (readLen, 43, "Incorrectly constructed actual read length from known good packet given buffer size"); |
827 |
NS_TEST_ASSERT_MSG_EQ (readLen, 43, "Incorrectly constructed actual read length from known good packet given buffer size"); |
|
|
828 |
f.Close (); |
800 |
|
829 |
|
801 |
// |
830 |
// |
802 |
// Did the data come back correctly? |
831 |
// Did the data come back correctly? |
|
815 |
// |
844 |
// |
816 |
// Open the file in write mode to clear the data. |
845 |
// Open the file in write mode to clear the data. |
817 |
// |
846 |
// |
818 |
err = f.Open (m_testFilename, "w"); |
847 |
f.Open (m_testFilename, std::ios::out); |
819 |
NS_TEST_ASSERT_MSG_EQ (err, false, "Open (" << m_testFilename << ", \"w\") returns error"); |
848 |
NS_TEST_ASSERT_MSG_EQ (f.Fail (), false, "Open (" << m_testFilename << |
|
|
849 |
", \"std::ios::out\") returns error"); |
820 |
|
850 |
|
821 |
// |
851 |
// |
822 |
// Initialize the pcap file header, forcing the object into swap mode. |
852 |
// Initialize the pcap file header, forcing the object into swap mode. |
823 |
// |
853 |
// |
824 |
err = f.Init (37, 43, -7, true); |
854 |
f.Init (37, 43, -7, true); |
825 |
NS_TEST_ASSERT_MSG_EQ (err, false, "Init (37, 43, -7) returns error"); |
855 |
NS_TEST_ASSERT_MSG_EQ (f.Fail (), false, "Init (37, 43, -7) returns error"); |
826 |
|
856 |
|
827 |
// |
857 |
// |
828 |
// Now we should be able to write a packet to it since it was opened in "w" |
858 |
// Now we should be able to write a packet to it since it was opened in "w" |
829 |
// mode. The packet data written should be limited to 43 bytes in length |
859 |
// mode. The packet data written should be limited to 43 bytes in length |
830 |
// by the Init() call above. |
860 |
// by the Init() call above. |
831 |
// |
861 |
// |
832 |
err = f.Write (1234, 5678, bufferOut, 128); |
862 |
f.Write (1234, 5678, bufferOut, 128); |
833 |
NS_TEST_ASSERT_MSG_EQ (err, false, "Write (write-only-file " << m_testFilename << ") returns error"); |
863 |
NS_TEST_ASSERT_MSG_EQ (f.Fail (), false, "Write (write-only-file " << m_testFilename << ") returns error"); |
834 |
f.Close (); |
864 |
f.Close (); |
835 |
|
865 |
|
836 |
// |
866 |
// |
|
892 |
// correctly read in a packet. The record header info should come back to us |
922 |
// correctly read in a packet. The record header info should come back to us |
893 |
// swapped back into correct form. |
923 |
// swapped back into correct form. |
894 |
// |
924 |
// |
895 |
err = f.Open (m_testFilename, "r"); |
925 |
f.Open (m_testFilename, std::ios::in); |
896 |
NS_TEST_ASSERT_MSG_EQ (err, false, "Open (" << m_testFilename << ", \"r\") of existing good file returns error"); |
926 |
NS_TEST_ASSERT_MSG_EQ (f.Fail (), false, "Open (" << m_testFilename << |
|
|
927 |
", \"std::ios::in\") of existing good file returns error"); |
897 |
|
928 |
|
898 |
err = f.Read (bufferIn, sizeof(bufferIn), tsSec, tsUsec, inclLen, origLen, readLen); |
929 |
f.Read (bufferIn, sizeof(bufferIn), tsSec, tsUsec, inclLen, origLen, readLen); |
899 |
NS_TEST_ASSERT_MSG_EQ (err, false, "Read() of known good packet returns error"); |
930 |
NS_TEST_ASSERT_MSG_EQ (f.Fail (), false, "Read() of known good packet returns error"); |
900 |
NS_TEST_ASSERT_MSG_EQ (tsSec, 1234, "Incorrectly read seconds timestap from known good packet"); |
931 |
NS_TEST_ASSERT_MSG_EQ (tsSec, 1234, "Incorrectly read seconds timestap from known good packet"); |
901 |
NS_TEST_ASSERT_MSG_EQ (tsUsec, 5678, "Incorrectly read microseconds timestap from known good packet"); |
932 |
NS_TEST_ASSERT_MSG_EQ (tsUsec, 5678, "Incorrectly read microseconds timestap from known good packet"); |
902 |
NS_TEST_ASSERT_MSG_EQ (inclLen, 43, "Incorrectly read included length from known good packet"); |
933 |
NS_TEST_ASSERT_MSG_EQ (inclLen, 43, "Incorrectly read included length from known good packet"); |
|
988 |
// |
1019 |
// |
989 |
// |
1020 |
// |
990 |
std::string filename = NS_TEST_SOURCEDIR + "known.pcap"; |
1021 |
std::string filename = NS_TEST_SOURCEDIR + "known.pcap"; |
991 |
bool err = f.Open (filename, "r"); |
1022 |
f.Open (filename, std::ios::in); |
992 |
NS_TEST_ASSERT_MSG_EQ (err, false, "Open (" << filename << ", \"w\") returns error"); |
1023 |
NS_TEST_ASSERT_MSG_EQ (f.Fail (), false, "Open (" << filename << |
|
|
1024 |
", \"std::ios::in\") returns error"); |
993 |
|
1025 |
|
994 |
// |
1026 |
// |
995 |
// We are going to read out the file header and all of the packets to make |
1027 |
// We are going to read out the file header and all of the packets to make |
|
1006 |
|
1038 |
|
1007 |
for (uint32_t i = 0; i < N_KNOWN_PACKETS; ++i, ++p) |
1039 |
for (uint32_t i = 0; i < N_KNOWN_PACKETS; ++i, ++p) |
1008 |
{ |
1040 |
{ |
1009 |
err = f.Read (data, sizeof(data), tsSec, tsUsec, inclLen, origLen, readLen); |
1041 |
f.Read (data, sizeof(data), tsSec, tsUsec, inclLen, origLen, readLen); |
1010 |
NS_TEST_ASSERT_MSG_EQ (err, false, "Read() of known good pcap file returns error"); |
1042 |
NS_TEST_ASSERT_MSG_EQ (f.Fail (), false, "Read() of known good pcap file returns error"); |
1011 |
NS_TEST_ASSERT_MSG_EQ (tsSec, p->tsSec, "Incorrectly read seconds timestap from known good pcap file"); |
1043 |
NS_TEST_ASSERT_MSG_EQ (tsSec, p->tsSec, "Incorrectly read seconds timestap from known good pcap file"); |
1012 |
NS_TEST_ASSERT_MSG_EQ (tsUsec, p->tsUsec, "Incorrectly read microseconds timestap from known good pcap file"); |
1044 |
NS_TEST_ASSERT_MSG_EQ (tsUsec, p->tsUsec, "Incorrectly read microseconds timestap from known good pcap file"); |
1013 |
NS_TEST_ASSERT_MSG_EQ (inclLen, p->inclLen, "Incorrectly read included length from known good packet"); |
1045 |
NS_TEST_ASSERT_MSG_EQ (inclLen, p->inclLen, "Incorrectly read included length from known good packet"); |
|
1019 |
// The file should now be at EOF since we've read all of the packets. |
1051 |
// The file should now be at EOF since we've read all of the packets. |
1020 |
// Another packet read should return an error. |
1052 |
// Another packet read should return an error. |
1021 |
// |
1053 |
// |
1022 |
err = f.Read (data, 1, tsSec, tsUsec, inclLen, origLen, readLen); |
1054 |
f.Read (data, 1, tsSec, tsUsec, inclLen, origLen, readLen); |
1023 |
NS_TEST_ASSERT_MSG_EQ (err, true, "Read() of known good pcap file at EOF does not return error"); |
1055 |
NS_TEST_ASSERT_MSG_EQ (f.Eof (), true, "Read() of known good pcap file at EOF does not return error"); |
1024 |
|
1056 |
|
1025 |
f.Close (); |
1057 |
f.Close (); |
1026 |
|
1058 |
|
|
1061 |
std::string filename2 = "different.pcap"; |
1093 |
std::string filename2 = "different.pcap"; |
1062 |
PcapFile f; |
1094 |
PcapFile f; |
1063 |
|
1095 |
|
1064 |
bool err = f.Open (filename2, "w"); |
1096 |
f.Open (filename2, std::ios::out); |
1065 |
NS_TEST_ASSERT_MSG_EQ (err, false, "Open (" << filename2 << ", \"w\") returns error"); |
1097 |
NS_TEST_ASSERT_MSG_EQ (f.Fail (), false, "Open (" << filename2 << ", \"std::ios::out\") returns error"); |
1066 |
err = f.Init (1, N_PACKET_BYTES); |
1098 |
f.Init (1, N_PACKET_BYTES); |
1067 |
NS_TEST_ASSERT_MSG_EQ (err, false, "Init (1, " << N_PACKET_BYTES << ") returns error"); |
1099 |
NS_TEST_ASSERT_MSG_EQ (f.Fail (), false, "Init (1, " << N_PACKET_BYTES << ") returns error"); |
1068 |
|
1100 |
|
1069 |
for (uint32_t i = 0; i < N_KNOWN_PACKETS; ++i) |
1101 |
for (uint32_t i = 0; i < N_KNOWN_PACKETS; ++i) |
1070 |
{ |
1102 |
{ |
1071 |
PacketEntry const & p = knownPackets[i]; |
1103 |
PacketEntry const & p = knownPackets[i]; |
1072 |
|
1104 |
|
1073 |
err = f.Write (p.tsSec, p.tsUsec, (uint8_t const *)p.data, p.origLen); |
1105 |
f.Write (p.tsSec, p.tsUsec, (uint8_t const *)p.data, p.origLen); |
1074 |
NS_TEST_EXPECT_MSG_EQ (err, false, "Write must not fail"); |
1106 |
NS_TEST_EXPECT_MSG_EQ (f.Fail (), false, "Write must not fail"); |
1075 |
} |
1107 |
} |
1076 |
f.Close (); |
1108 |
f.Close (); |
1077 |
|
1109 |
|
|
1094 |
{ |
1126 |
{ |
1095 |
AddTestCase (new WriteModeCreateTestCase); |
1127 |
AddTestCase (new WriteModeCreateTestCase); |
1096 |
AddTestCase (new ReadModeCreateTestCase); |
1128 |
AddTestCase (new ReadModeCreateTestCase); |
1097 |
AddTestCase (new AppendModeCreateTestCase); |
1129 |
//AddTestCase (new AppendModeCreateTestCase); |
1098 |
AddTestCase (new FileHeaderTestCase); |
1130 |
AddTestCase (new FileHeaderTestCase); |
1099 |
AddTestCase (new RecordHeaderTestCase); |
1131 |
AddTestCase (new RecordHeaderTestCase); |
1100 |
AddTestCase (new ReadFileTestCase); |
1132 |
AddTestCase (new ReadFileTestCase); |