File: dirparsertest.cpp

package info (click to toggle)
filezilla 3.0.0~beta2-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 8,516 kB
  • ctags: 6,577
  • sloc: cpp: 35,379; ansic: 30,783; sh: 10,721; makefile: 444; xml: 16
file content (774 lines) | stat: -rw-r--r-- 14,866 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
#include "FileZilla.h"
#include "directorylistingparser.h"
#include <cppunit/extensions/HelperMacros.h>
#include <list>

/*
 * This testsuite asserts the correctness of the directory listing parser.
 * It's main purpose is to ensure that all known formats are recognized and
 * parsed as expected. Due to the high amount of variety and unfortunately
 * also ambiguity, the parser is very fragile.
 */

typedef struct
{
	std::string data;
	CDirentry reference;
} t_entry;

class CDirectoryListingParserTest : public CppUnit::TestFixture
{
	CPPUNIT_TEST_SUITE(CDirectoryListingParserTest);
	InitEntries();
	for (unsigned int i = 0; i < m_entries.size(); i++)
		CPPUNIT_TEST(testIndividual);
	CPPUNIT_TEST(testAll);
	CPPUNIT_TEST_SUITE_END();

public:
	void setUp();
	void tearDown() {}

	void testIndividual();
	void testAll();

	static std::vector<t_entry> m_entries;

	static wxCriticalSection m_sync;

protected:
	static void InitEntries();

	t_entry m_entry;
};

wxCriticalSection CDirectoryListingParserTest::m_sync;
std::vector<t_entry> CDirectoryListingParserTest::m_entries;

CPPUNIT_TEST_SUITE_REGISTRATION(CDirectoryListingParserTest);

void CDirectoryListingParserTest::InitEntries()
{
	const int year = wxDateTime::Now().GetYear();
	const int month = wxDateTime::Now().GetMonth();

	// Unix-style listings
        // -------------------

	// We start with a perfect example of a unix style directory listing without anomalies.
	m_entries.push_back((t_entry){
			"dr-xr-xr-x   2 root     other        512 Apr  8  1994 01-unix-std dir",
			{
				_T("01-unix-std dir"),
				512,
				_T("dr-xr-xr-x"),
				_T("root other"),
				true,
				false,
				_T(""),
				true,
				false,
				{1994, 4, 8},
				{0, 0},
				false
			}
		});

	// This one is a recent file with a time instead of the year.
	m_entries.push_back((t_entry){
			"-rw-r--r--   1 root     other        531 3 29 03:26 02-unix-std file",
			{
				_T("02-unix-std file"),
				531,
				_T("-rw-r--r--"),
				_T("root other"),
				false,
				false,
				_T(""),
				true,
				true,
				{(3 > month) ? year - 1 : year, 3, 29},
				{3, 26},
				false
			}
		});

	// Group omitted
	m_entries.push_back((t_entry){
			"dr-xr-xr-x   2 root                  512 Apr  8  1994 03-unix-nogroup dir",
			{
				_T("03-unix-nogroup dir"),
				512,
				_T("dr-xr-xr-x"),
				_T("root"),
				true,
				false,
				_T(""),
				true,
				false,
				{1994, 4, 8},
				{0, 0},
				false
			}
		});

	// Symbolic link
	m_entries.push_back((t_entry){
			"lrwxrwxrwx   1 root     other          7 Jan 25 00:17 04-unix-std link -> usr/bin",
			{
				_T("04-unix-std link"),
				7,
				_T("lrwxrwxrwx"),
				_T("root other"),
				true,
				true,
				_T("usr/bin"),
				true,
				true,
				{year, 1, 25},
				{0, 17},
				false
			}
		});

	// Some listings with uncommon date/time format
	// --------------------------------------------

	m_entries.push_back((t_entry){
			"-rw-r--r--   1 root     other        531 09-26 2000 05-unix-date file",
			{
				_T("05-unix-date file"),
				531,
				_T("-rw-r--r--"),
				_T("root other"),
				false,
				false,
				_T(""),
				true,
				false,
				{2000, 9, 26},
				{0, 0},
				false
			}
		});

	m_entries.push_back((t_entry){
			"-rw-r--r--   1 root     other        531 09-26 13:45 06-unix-date file",
			{
				_T("06-unix-date file"),
				531,
				_T("-rw-r--r--"),
				_T("root other"),
				false,
				false,
				_T(""),
				true,
				true,
				{(9 > month) ? year - 1 : year, 9, 26},
				{13, 45},
				false
			}
		});

	m_entries.push_back((t_entry){
			"-rw-r--r--   1 root     other        531 2005-06-07 21:22 07-unix-date file",
			{
				_T("07-unix-date file"),
				531,
				_T("-rw-r--r--"),
				_T("root other"),
				false,
				false,
				_T(""),
				true,
				true,
				{2005, 6, 7},
				{21, 22},
				false
			}
		});


	// Unix style with size information in kilobytes
	m_entries.push_back((t_entry){
			"-rw-r--r--   1 root     other  33.5k Oct 5 21:22 08-unix-namedsize file",
			{
				_T("08-unix-namedsize file"),
				335 * 1024 / 10,
				_T("-rw-r--r--"),
				_T("root other"),
				false,
				false,
				_T(""),
				true,
				true,
				{(10 > month) ? year - 1 : year, 10, 5},
				{21, 22},
				false
			}
		});

	// NetWare style listings
	// ----------------------

	m_entries.push_back((t_entry){
			"d [R----F--] supervisor            512       Jan 16 18:53    09-netware dir",
			{
				_T("09-netware dir"),
				512,
				_T("d [R----F--]"),
				_T("supervisor"),
				true,
				false,
				_T(""),
				true,
				true,
				{(1 > month) ? year - 1 : year, 1, 16},
				{18, 53},
				false
			}
		});

	m_entries.push_back((t_entry){
			"- [R----F--] rhesus             214059       Oct 20 15:27    10-netware file",
			{
				_T("10-netware file"),
				214059,
				_T("- [R----F--]"),
				_T("rhesus"),
				false,
				false,
				_T(""),
				true,
				true,
				{(10 > month) ? year - 1 : year, 10, 20},
				{15, 27},
				false
			}
		});
		
	// NetPresenz for the Mac
	// ----------------------

	// Actually this one isn't parse properly:
	// The numerical username is mistaken as size. However,
	// this is ambiguous to the normal unix style listing.
	// It's not possible to recognize both formats the right way.
	m_entries.push_back((t_entry){
			"-------r--         326  1391972  1392298 Nov 22  1995 11-netpresenz file",
			{
				_T("11-netpresenz file"),
				1392298,
				_T("-------r--"),
				_T("1391972"),
				false,
				false,
				_T(""),
				true,
				false,
				{1995, 11, 22},
				{0, 0},
				false
			}
		});

	m_entries.push_back((t_entry){
			"drwxrwxr-x               folder        2 May 10  1996 12-netpresenz dir",
			{
				_T("12-netpresenz dir"),
				2,
				_T("drwxrwxr-x"),
				_T("folder"),
				true,
				false,
				_T(""),
				true,
				false,
				{1996, 5, 10},
				{0, 0},
				false
			}
		});

	// A format with domain field some windows servers send
	m_entries.push_back((t_entry){
			"-rw-r--r--   1 group domain user 531 Jan 29 03:26 13-unix-domain file",
			{
				_T("13-unix-domain file"),
				531,
				_T("-rw-r--r--"),
				_T("group domain user"),
				false,
				false,
				_T(""),
				true,
				true,
				{(1 > month) ? year - 1 : year, 1, 29},
				{3, 26},
				false
			}
		});

	// EPLF directory listings
	// -----------------------

	m_entries.push_back((t_entry){
			"+i8388621.48594,m825718503,r,s280,up755\t14-eplf file",
			{
				_T("14-eplf file"),
				280,
				_T("755"),
				_T(""),
				false,
				false,
				_T(""),
				true,
				true,
				{1996, 2, 1},
				{23, 15},
				false
			}
		});

	m_entries.push_back((t_entry){
        	"+i8388621.50690,m824255907,/,\t15-eplf dir",
			{
				_T("15-eplf dir"),
				-1,
				_T(""),
				_T(""),
				true,
				false,
				_T(""),
				true,
				true,
				{1996, 1, 14},
				{0, 58},
				false
			}
		});

	// MSDOS type listing used by old IIS
	// ----------------------------------

	m_entries.push_back((t_entry){
			"04-27-00  12:09PM       <DIR>          16-dos-dateambiguous dir",
			{
				_T("16-dos-dateambiguous dir"),
				-1,
				_T(""),
				_T(""),
				true,
				false,
				_T(""),
				true,
				true,
				{2000, 4, 27},
				{12, 9},
				false
			}
		});
	
	// Ambiguous date and AM/PM crap. Some evil manager must have forced the poor devs to implement this
	m_entries.push_back((t_entry){
			"04-06-00  03:47PM                  589 17-dos-dateambiguous file",
			{
				_T("17-dos-dateambiguous file"),
				589,
				_T(""),
				_T(""),
				false,
				false,
				_T(""),
				true,
				true,
				{2000, 4, 6},
				{15, 47},
				false
			}
		});

	m_entries.push_back((t_entry){
			"2002-09-02  18:48       <DIR>          18-dos-longyear dir",
			{
				_T("18-dos-longyear dir"),
				-1,
				_T(""),
				_T(""),
				true,
				false,
				_T(""),
				true,
				true,
				{2002, 9, 2},
				{18, 48},
				false
			}
		});

	m_entries.push_back((t_entry){
			"2002-09-02  19:06                9,730 19-dos-longyear file",
			{
				_T("19-dos-longyear file"),
				9730,
				_T(""),
				_T(""),
				false,
				false,
				_T(""),
				true,
				true,
				{2002, 9, 2},
				{19, 6},
				false
			}
		});

	// Numerical unix style listing
	m_entries.push_back((t_entry){
			"0100644   500  101   12345    123456789       20-unix-numerical file",
			{
				_T("20-unix-numerical file"),
				12345,
				_T("0100644"),
				_T("500 101"),
				false,
				false,
				_T(""),
				true,
				true,
				{1973, 10, 29},
				{22, 33},
				false
			}
		});

	// VShell servers
	// --------------

	m_entries.push_back((t_entry){
			"206876  Apr 04, 2000 21:06 21-vshell-old file",
			{
				_T("21-vshell-old file"),
				206876,
				_T(""),
				_T(""),
				false,
				false,
				_T(""),
				true,
				true,
				{2000, 4, 4},
				{21, 6},
				false
			}
		});
	
	m_entries.push_back((t_entry){
			"0  Dec 12, 2002 02:13 22-vshell-old dir/",
			{
				_T("22-vshell-old dir"),
				0,
				_T(""),
				_T(""),
				true,
				false,
				_T(""),
				true,
				true,
				{2002, 12, 12},
				{2, 13},
				false
			}
		});
	
	/* This type of directory listings is sent by some newer versions of VShell
	 * both year and time in one line is uncommon. */
	m_entries.push_back((t_entry){
			"-rwxr-xr-x    1 user group        9 Oct 08, 2002 09:47 23-vshell-new file",
			{
				_T("23-vshell-new file"),
				9,
				_T("-rwxr-xr-x"),
				_T("user group"),
				false,
				false,
				_T(""),
				true,
				true,
				{2002, 10, 8},
				{9, 47},
				false
			}
		});

	// OS/2 server format
	// ------------------
	
	// This server obviously isn't Y2K aware
	m_entries.push_back((t_entry){
			"36611      A    04-23-103  10:57  24-os2 file",
			{
				_T("24-os2 file"),
				36611,
				_T(""),
				_T(""),
				false,
				false,
				_T(""),
				true,
				true,
				{2003, 4, 23},
				{10, 57},
				false
			}
		});

	m_entries.push_back((t_entry){
			" 1123      A    07-14-99   12:37  25-os2 file",
			{
				_T("25-os2 file"),
				1123,
				_T(""),
				_T(""),
				false,
				false,
				_T(""),
				true,
				true,
				{1999, 7, 14},
				{12, 37},
				false
			}
		});

	m_entries.push_back((t_entry){
			"    0 DIR       02-11-103  16:15  26-os2 dir",
			{
				_T("26-os2 dir"),
				0,
				_T(""),
				_T(""),
				true,
				false,
				_T(""),
				true,
				true,
				{2003, 2, 11},
				{16, 15},
				false
			}
		});

	m_entries.push_back((t_entry){
			" 1123 DIR  A    10-05-100  23:38  27-os2 dir",
			{
				_T("27-os2 dir"),
				1123,
				_T(""),
				_T(""),
				true,
				false,
				_T(""),
				true,
				true,
				{2000, 10, 5},
				{23, 38},
				false
			}
		});

	// Localized date formats
	// ----------------------

	m_entries.push_back((t_entry){
			"dr-xr-xr-x   2 root     other      2235 26. Juli, 20:10 28-datetest-ger dir",
			{
				_T("28-datetest-ger dir"),
				2235,
				_T("dr-xr-xr-x"),
				_T("root other"),
				true,
				false,
				_T(""),
				true,
				true,
				{(7 > month) ? year - 1 : year, 7, 26},
				{20, 10},
				false
			}
		});

	m_entries.push_back((t_entry){
			"-r-xr-xr-x   2 root     other      2235 2.   Okt.  2003 29-datetest-ger file",
			{
				_T("29-datetest-ger file"),
				2235,
				_T("-r-xr-xr-x"),
				_T("root other"),
				false,
				false,
				_T(""),
				true,
				false,
				{2003, 10, 2},
				{0, 0},
				false
			}
		});

	m_entries.push_back((t_entry){
			"-r-xr-xr-x   2 root     other      2235 1999/10/12 17:12 30-datetest file",
			{
				_T("30-datetest file"),
				2235,
				_T("-r-xr-xr-x"),
				_T("root other"),
				false,
				false,
				_T(""),
				true,
				true,
				{1999, 10, 12},
				{17, 12},
				false
			}
		});

	m_entries.push_back((t_entry){
			"-r-xr-xr-x   2 root     other      2235 24-04-2003 17:12 31-datetest file",
			{
				_T("31-datetest file"),
				2235,
				_T("-r-xr-xr-x"),
				_T("root other"),
				false,
				false,
				_T(""),
				true,
				true,
				{2003, 4, 24},
				{17, 12},
				false
			}
		});

	// Japanese listing
	// Remark: I'v no idea in which encoding the foreign characters are, but
	// it's not valid UTF-8. Parser has to be able to cope with it somehow.
	m_entries.push_back((t_entry){
			"-rw-r--r--   1 root       sys           8473  4\x8c\x8e 18\x93\xfa 2003\x94\x4e 32-datatest-japanese file",
			{
				_T("32-datatest-japanese file"),
				8473,
				_T("-rw-r--r--"),
				_T("root sys"),
				false,
				false,
				_T(""),
				true,
				false,
				{2003, 4, 18},
				{0, 0},
				false
			}
		});


/*
	wxString name;
	wxLongLong size;
	wxString permissions;
	wxString ownerGroup;
	bool dir;
	bool link;
	wxString target; // Set to linktarget it link is true

	bool hasDate;
	bool hasTime;

	struct t_date
	{
		int year;
		int month;
		int day;
	} date;

	struct t_time
	{
		int hour;
		int minute;
	} time;

	bool unsure;
*/

	// Fix line endings
	for (std::vector<t_entry>::iterator iter = m_entries.begin(); iter != m_entries.end(); iter++)
		iter->data += "\r\n";
}

void CDirectoryListingParserTest::testIndividual()
{
	m_sync.Enter();

	static int index = 0;
	const t_entry &entry = m_entries[index++];

	m_sync.Leave();

	const CServer server;

	CDirectoryListingParser parser(0, server);

	const char* str = entry.data.c_str();
	const int len = strlen(str);
	char* data = new char[len];
	memcpy(data, str, len);
	parser.AddData(data, len);

	CDirectoryListing* pListing = parser.Parse(CServerPath());

	wxString msg = wxString::Format(_T("Data: %s, count: %d"), wxString(entry.data.c_str(), wxConvUTF8).c_str(), pListing->m_entryCount);
	msg.Replace(_T("\r"), _T(""));
	msg.Replace(_T("\n"), _T(""));
	CPPUNIT_ASSERT_MESSAGE((const char*)msg.mb_str(), pListing->m_entryCount == 1);

	msg = wxString::Format(_T("Data: %s  Expected:\n%s\n  Got:\n%s"), wxString(entry.data.c_str(), wxConvUTF8).c_str(), entry.reference.dump().c_str(), pListing->m_pEntries[0].dump().c_str());

	CPPUNIT_ASSERT_MESSAGE((const char*)msg.mb_str(), pListing->m_pEntries[0] == entry.reference);

	delete pListing;
}

void CDirectoryListingParserTest::testAll()
{
	const CServer server;
	CDirectoryListingParser parser(0, server);
	for (std::vector<t_entry>::const_iterator iter = m_entries.begin(); iter != m_entries.end(); iter++)
	{
		const char* str = iter->data.c_str();
		const int len = strlen(str);
		char* data = new char[len];
		memcpy(data, str, len);
		parser.AddData(data, len);
	}
	CDirectoryListing* pListing = parser.Parse(CServerPath());

	CPPUNIT_ASSERT(pListing->m_entryCount == m_entries.size());

	unsigned int i = 0;
	for (std::vector<t_entry>::const_iterator iter = m_entries.begin(); iter != m_entries.end(); iter++, i++)
	{
		wxString msg = wxString::Format(_T("Data: %s  Expected:\n%s\n  Got:\n%s"), wxString(iter->data.c_str(), wxConvUTF8).c_str(), iter->reference.dump().c_str(), pListing->m_pEntries[i].dump().c_str());

		CPPUNIT_ASSERT_MESSAGE((const char*)msg.mb_str(), pListing->m_pEntries[i] == iter->reference);
	}


	delete pListing;
}

void CDirectoryListingParserTest::setUp()
{
}