forked from robinrodricks/FluentFTP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tests.cs
1292 lines (1053 loc) · 36.6 KB
/
Tests.cs
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
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using FluentFTP;
using System.Threading;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Linq;
#if !NOASYNC
using System.Threading.Tasks;
#endif
using System.IO.Compression;
using System.Text;
using FluentFTP.Proxy;
using System.Security.Authentication;
using Xunit;
using FluentFTP.Helpers;
namespace Tests {
public class Tests {
private const string Category_Code = "Code";
private const string Category_PublicFTP = "PublicFTP";
private const string Category_CustomFTP = "CustomerFTP";
// SET THESE BEFORE RUNNING ANY TESTS!
private const string m_host = "";
private const string m_user = "";
private const string m_pass = "";
private static readonly int[] connectionTypes = new[] {
(int) FtpDataConnectionType.EPSV,
(int) FtpDataConnectionType.EPRT,
(int) FtpDataConnectionType.PASV,
(int) FtpDataConnectionType.PORT
};
#region Helpers
private void OnValidateCertificate(FtpClient control, FtpSslValidationEventArgs e) {
e.Accept = true;
}
private static FtpClient NewFtpClient(string host = m_host, string username = m_user, string password = m_pass) {
return new FtpClient(host, new NetworkCredential(username, password));
}
private static FtpClient NewFtpClient_NetBsd() {
return NewFtpClient("ftp.netbsd.org", "ftp", "ftp");
}
private static FtpClient NewFtpClient_Tele2SpeedTest() {
return new FtpClient("ftp://speedtest.tele2.net/");
}
private static FtpClient NewFtpClient_Inacessible() {
return new FtpClient("ftp://bad.unknown.com/");
}
private static FtpClient NewFtpClient_Inacessible2() {
return new FtpClient("ftp://192.168.0.0/");
}
#endregion
//[Fact]
public void TestListPathWithHttp11Proxy() {
using (FtpClient cl = new FtpClientHttp11Proxy(new ProxyInfo {Host = "127.0.0.1", Port = 3128,})) // Credential = new NetworkCredential()
{
FtpTrace.WriteLine("FTPClient::ConnectionType = '" + cl.ConnectionType + "'");
cl.Credentials = new NetworkCredential(m_user, m_pass);
cl.Host = m_host;
cl.ValidateCertificate += OnValidateCertificate;
cl.DataConnectionType = FtpDataConnectionType.PASV;
cl.Connect();
foreach (var item in cl.GetListing(null, FtpListOption.SizeModify | FtpListOption.ForceNameList)) {
FtpTrace.WriteLine(item.Modified.Kind);
FtpTrace.WriteLine(item.Modified);
}
}
}
//[Fact]
public void TestListPath() {
using (var cl = NewFtpClient()) {
cl.EncryptionMode = FtpEncryptionMode.None;
cl.GetListing();
FtpTrace.WriteLine("Path listing succeeded");
cl.GetListing(null, FtpListOption.NoPath);
FtpTrace.WriteLine("No path listing succeeded");
}
}
//[Fact]
public void TestCheckCapabilities() {
using (var cl = NewFtpClient()) {
cl.CheckCapabilities = false;
Debug.Assert(cl.HasFeature(FtpCapability.NONE), "Excepted FTP capabilities to be NONE.");
}
}
#if !NOASYNC
//[Fact]
public async Task TestListPathAsync() {
using (var cl = NewFtpClient()) {
cl.EncryptionMode = FtpEncryptionMode.None;
await cl.GetListingAsync();
FtpTrace.WriteLine("Path listing succeeded");
await cl.GetListingAsync(null, FtpListOption.NoPath);
FtpTrace.WriteLine("No path listing succeeded");
}
}
#endif
//[Fact]
public void StreamResponses() {
using (var cl = NewFtpClient()) {
cl.EncryptionMode = FtpEncryptionMode.None;
cl.ValidateCertificate += OnValidateCertificate;
using (var s = (FtpDataStream) cl.OpenWrite("test.txt")) {
var r = s.CommandStatus;
FtpTrace.WriteLine("");
FtpTrace.WriteLine("Response to STOR:");
FtpTrace.WriteLine("Code: " + r.Code);
FtpTrace.WriteLine("Message: " + r.Message);
FtpTrace.WriteLine("Informational: " + r.InfoMessages);
r = s.Close();
FtpTrace.WriteLine("");
FtpTrace.WriteLine("Response after close:");
FtpTrace.WriteLine("Code: " + r.Code);
FtpTrace.WriteLine("Message: " + r.Message);
FtpTrace.WriteLine("Informational: " + r.InfoMessages);
}
}
}
#if !NOASYNC
//[Fact]
public async Task StreamResponsesAsync() {
using (var cl = NewFtpClient()) {
cl.EncryptionMode = FtpEncryptionMode.None;
cl.ValidateCertificate += OnValidateCertificate;
using (var s = (FtpDataStream) await cl.OpenWriteAsync("test.txt")) {
var r = s.CommandStatus;
FtpTrace.WriteLine("");
FtpTrace.WriteLine("Response to STOR:");
FtpTrace.WriteLine("Code: " + r.Code);
FtpTrace.WriteLine("Message: " + r.Message);
FtpTrace.WriteLine("Informational: " + r.InfoMessages);
r = s.Close();
FtpTrace.WriteLine("");
FtpTrace.WriteLine("Response after close:");
FtpTrace.WriteLine("Code: " + r.Code);
FtpTrace.WriteLine("Message: " + r.Message);
FtpTrace.WriteLine("Informational: " + r.InfoMessages);
}
}
}
#endif
//[Fact]
public void TestUnixListing() {
using (var cl = NewFtpClient()) {
if (!cl.FileExists("test.txt")) {
using (var s = cl.OpenWrite("test.txt")) {
}
}
foreach (var i in cl.GetListing(null, FtpListOption.ForceList)) {
FtpTrace.WriteLine(i);
}
}
}
[Fact]
[Trait("Category", Category_Code)]
public void TestFtpPath() {
var path = "/home/sigurdhj/errors/16.05.2014/asdasd/asd asd asd aa asd/Kooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo::asdasd";
FtpTrace.WriteLine(path.GetFtpDirectoryName());
FtpTrace.WriteLine("/foobar/boo".GetFtpDirectoryName());
FtpTrace.WriteLine("./foobar/boo".GetFtpDirectoryName());
FtpTrace.WriteLine("./foobar".GetFtpDirectoryName());
FtpTrace.WriteLine("/foobar".GetFtpDirectoryName());
FtpTrace.WriteLine("foobar".GetFtpDirectoryName());
FtpTrace.WriteLine(path.GetFtpFileName());
FtpTrace.WriteLine("/foo/bar".GetFtpFileName());
FtpTrace.WriteLine("./foo/bar".GetFtpFileName());
FtpTrace.WriteLine("./bar".GetFtpFileName());
FtpTrace.WriteLine("/bar".GetFtpFileName());
FtpTrace.WriteLine("bar".GetFtpFileName());
}
//[Fact]
public void TestGetObjectInfo() {
using (var client = NewFtpClient()) {
FtpTrace.WriteLine(client.GetObjectInfo("/public_html/temp/README.md"));
}
}
#if !NOASYNC && !CORE
//[Fact]
public async Task TestGetObjectInfoAsync() {
using (var cl = NewFtpClient()) {
cl.Encoding = Encoding.UTF8;
var item = await cl.GetObjectInfoAsync("/Examples/OpenRead.cs");
FtpTrace.WriteLine(item.ToString());
}
}
#endif
//[Fact]
public void TestManualEncoding() {
using (var cl = NewFtpClient()) {
cl.Encoding = Encoding.UTF8;
using (var s = cl.OpenWrite("test.txt")) {
}
}
}
//[Fact]
public void TestServer() {
using (var cl = NewFtpClient()) {
cl.EncryptionMode = FtpEncryptionMode.Explicit;
cl.ValidateCertificate += OnValidateCertificate;
foreach (var i in cl.GetListing("/")) {
FtpTrace.WriteLine(i.FullName);
}
}
}
private void TestServerDownload(FtpClient client, string path) {
foreach (var i in client.GetListing(path)) {
switch (i.Type) {
case FtpFileSystemObjectType.Directory:
TestServerDownload(client, i.FullName);
break;
case FtpFileSystemObjectType.File:
using (var s = client.OpenRead(i.FullName)) {
var b = new byte[8192];
var read = 0;
long total = 0;
try {
while ((read = s.Read(b, 0, b.Length)) > 0) {
total += read;
Console.Write("\r{0}/{1} {2:p} ",
total, s.Length, (double) total / (double) s.Length);
}
Console.Write("\r{0}/{1} {2:p} ",
total, s.Length, (double) total / (double) s.Length);
}
finally {
FtpTrace.WriteLine("");
}
}
break;
}
}
}
#if !NOASYNC
private async Task TestServerDownloadAsync(FtpClient client, string path) {
foreach (var i in await client.GetListingAsync(path)) {
switch (i.Type) {
case FtpFileSystemObjectType.Directory:
await TestServerDownloadAsync(client, i.FullName);
break;
case FtpFileSystemObjectType.File:
using (var s = await client.OpenReadAsync(i.FullName)) {
var b = new byte[8192];
var read = 0;
long total = 0;
try {
while ((read = await s.ReadAsync(b, 0, b.Length)) > 0) {
total += read;
Console.Write("\r{0}/{1} {2:p} ",
total, s.Length, (double) total / (double) s.Length);
}
Console.Write("\r{0}/{1} {2:p} ",
total, s.Length, (double) total / (double) s.Length);
}
finally {
FtpTrace.WriteLine("");
}
}
break;
}
}
}
#endif
#if !CORE14
[Fact]
[Trait("Category", Category_PublicFTP)]
public void TestDisposeWithMultipleThreads() {
using (var cl = NewFtpClient_NetBsd()) {
var t1 = new Thread(() => { cl.GetListing(); });
var t2 = new Thread(() => { cl.Dispose(); });
t1.Start();
Thread.Sleep(2000);
t2.Start();
t1.Join();
t2.Join();
}
}
#endif
[Fact]
[Trait("Category", Category_Code)]
public void TestConnectionFailure() {
try {
using (var cl = NewFtpClient("somefakehost")) {
cl.ConnectTimeout = 5000;
cl.Connect();
}
}
catch (System.Net.Sockets.SocketException) {
} // Expecting this
#if !NOASYNC
catch (AggregateException ex) when (ex.InnerException is System.Net.Sockets.SocketException) {
}
#endif
}
[Fact]
[Trait("Category", Category_PublicFTP)]
public void TestNetBSDServer() {
using (var client = NewFtpClient_NetBsd()) {
foreach (var item in client.GetListing(null,
FtpListOption.ForceList | FtpListOption.Modify | FtpListOption.DerefLinks)) {
FtpTrace.WriteLine(item);
if (item.Type == FtpFileSystemObjectType.Link && item.LinkObject != null) {
FtpTrace.WriteLine(item.LinkObject);
}
}
}
}
//[Fact]
public void TestGetListing() {
using (var client = NewFtpClient()) {
client.Connect();
foreach (var i in client.GetListing("/public_html/temp/", FtpListOption.ForceList | FtpListOption.Recursive)) {
//FtpTrace.WriteLine(i);
}
}
}
[Fact]
[Trait("Category", Category_PublicFTP)]
public void TestGetListingBSDServer() {
using (var client = NewFtpClient_NetBsd()) {
// machine listing
var listing = client.GetListing();
// unix listing
var listing2 = client.GetListing("/", FtpListOption.ForceList);
}
}
#if !NOASYNC
[Fact]
[Trait("Category", Category_PublicFTP)]
public async void TestGetListingBSDServerAsync() {
using (var client = NewFtpClient_NetBsd()) {
// machine listing
var listing = await client.GetListingAsync();
// unix listing
var listing2 = await client.GetListingAsync("/", FtpListOption.ForceList);
}
}
#endif
[Fact]
[Trait("Category", Category_PublicFTP)]
public void TestGetListingTeleServer() {
using (var client = NewFtpClient_Tele2SpeedTest()) {
// machine listing
var listing = client.GetListing();
// unix listing
var listing2 = client.GetListing("/", FtpListOption.ForceList);
}
}
#if !NOASYNC
[Fact]
[Trait("Category", Category_PublicFTP)]
public async void TestGetListingTeleServerAsync() {
using (var client = NewFtpClient_Tele2SpeedTest()) {
// machine listing
var listing = await client.GetListingAsync();
// unix listing
var listing2 = await client.GetListingAsync("/", FtpListOption.ForceList);
}
}
#endif
[Fact]
[Trait("Category", Category_PublicFTP)]
public void TestGetListingTeleServerTimezone() {
using (var client = NewFtpClient_Tele2SpeedTest()) {
// original date = 19/Feb/2020 00:00 (12 am)
// convert to UTC (assume Tokyo to UTC)
client.TimeConversion = FtpDate.UTC;
client.TimeZone = 9;
var listing = client.GetListing();
if (listing[0].Modified != DateTime.Parse("18-Feb-2016 3:00:00 PM")) {
throw new Exception("Timezone conversion failed!");
}
// convert to local time (assume Tokyo to Mumbai)
client.TimeConversion = FtpDate.LocalTime;
client.TimeZone = 9;
var listing2 = client.GetListing();
if (listing2[0].Modified == DateTime.Parse("18-Feb-2016 12:00:00 AM")) {
throw new Exception("Timezone conversion failed!");
}
}
}
#if !CORE
//[Fact]
public void TestGetListingCCC() {
using (var client = NewFtpClient()) {
client.EncryptionMode = FtpEncryptionMode.Explicit;
client.PlainTextEncryption = true;
client.SslProtocols = SslProtocols.Tls;
client.ValidateCertificate += OnValidateCertificate;
client.Connect();
foreach (var i in client.GetListing("/public_html/temp/", FtpListOption.ForceList | FtpListOption.Recursive)) {
//FtpTrace.WriteLine(i);
}
// 100 K file
client.UploadFile(@"D:\Github\FluentFTP\README.md", "/public_html/temp/README.md");
client.DownloadFile(@"D:\Github\FluentFTP\README2.md", "/public_html/temp/README.md");
}
}
#endif
//[Fact]
public void TestGetMachineListing() {
using (var client = NewFtpClient()) {
client.ListingParser = FtpParser.Machine;
client.Connect();
foreach (var i in client.GetListing("/public_html/temp/", FtpListOption.Recursive)) {
//FtpTrace.WriteLine(i);
}
}
}
//[Fact]
public void TestFileZillaKick() {
using (var cl = NewFtpClient()) {
cl.EnableThreadSafeDataConnections = false;
if (cl.FileExists("TestFile.txt")) {
cl.DeleteFile("TestFile.txt");
}
try {
var s = cl.OpenWrite("TestFile.txt");
for (var i = 0; true; i++) {
s.WriteByte((byte) i);
#if CORE14
Task.Delay(100).Wait();
#else
Thread.Sleep(100);
#endif
}
//s.Close();
}
catch (FtpCommandException ex) {
FtpTrace.WriteLine("Exception caught!");
FtpTrace.WriteLine(ex.ToString());
}
}
}
[Fact]
[Trait("Category", Category_Code)]
public void TestUnixListParser() {
var parser = new FtpListParser(new FtpClient());
parser.Init(FtpOperatingSystem.Unix);
//parser.parser = FtpParser.Legacy;
var sample = new[] {
"drwxr-xr-x 7 user1 user1 512 Sep 27 2011 .",
"drwxr-xr-x 31 user1 user1 1024 Sep 27 2011 ..",
"lrwxrwxrwx 1 user1 user1 9 Sep 27 2011 data.0000 -> data.6460",
"drwxr-xr-x 10 user1 user1 512 Jun 29 2012 data.6460",
"lrwxrwxrwx 1 user1 user1 8 Sep 27 2011 sys.0000 -> sys.6460",
"drwxr-xr-x 133 user1 user1 4096 Jun 25 16:26 sys.6460"
};
foreach (var s in sample) {
var item = parser.ParseSingleLine("/", s, new List<FtpCapability>(), false);
if (item != null) {
FtpTrace.WriteLine(item);
}
}
}
[Fact]
[Trait("Category", Category_Code)]
public void TestIISParser() {
var parser = new FtpListParser(new FtpClient());
parser.Init(FtpOperatingSystem.Windows);
//parser.parser = FtpParser.Legacy;
var sample = new[] {
"03-07-13 10:02AM 901 File01.xml",
"03-07-13 10:03AM 921 File02.xml",
"03-07-13 10:04AM 904 File03.xml",
"03-07-13 10:04AM 912 File04.xml",
"03-08-13 11:10AM 912 File05.xml",
"03-15-13 02:38PM 912 File06.xml",
"03-07-13 10:16AM 909 File07.xml",
"03-07-13 10:16AM 899 File08.xml",
"03-08-13 10:22AM 904 File09.xml",
"03-25-13 07:27AM 895 File10.xml",
"03-08-13 10:22AM 6199 File11.txt",
"03-25-13 07:22AM 31444 File12.txt",
"03-25-13 07:24AM 24537 File13.txt"
};
foreach (var s in sample) {
var item = parser.ParseSingleLine("/", s, new List<FtpCapability>(), false);
if (item != null) {
FtpTrace.WriteLine(item);
}
}
}
[Fact]
[Trait("Category", Category_Code)]
public void TestOpenVMSParser() {
var parser = new FtpListParser(new FtpClient());
parser.Init(FtpOperatingSystem.VMS);
var sample = new[] {
"411_4114.TXT;1 11 21-MAR-2012 15:17 [TBMS,TBMS_BOSS] (RWED,RWED,,RE)",
"ACT_CC_NAME_4114.TXT;1 30 21-MAR-2012 15:17 [TBMS,TBMS_BOSS] (RWED,RWED,,RE)",
"ACT_CC_NUM_4114.TXT;1 30 21-MAR-2012 15:17 [TBMS,TBMS_BOSS] (RWED,RWED,,RE)",
"ACT_CELL_NAME_4114.TXT;1 113 21-MAR-2012 15:17 [TBMS,TBMS_BOSS] (RWED,RWED,,RE)",
"ACT_CELL_NUM_4114.TXT;1 113 21-MAR-2012 15:17 [TBMS,TBMS_BOSS] (RWED,RWED,,RE)",
"AGCY_BUDG_4114.TXT;1 63 21-MAR-2012 15:17 [TBMS,TBMS_BOSS] (RWED,RWED,,RE)",
"CELL_SUMM_4114.TXT;1 125 21-MAR-2012 15:17 [TBMS,TBMS_BOSS] (RWED,RWED,,RE)",
"CELL_SUMM_CHART_4114.PDF;2 95 21-MAR-2012 10:58 [TBMS,TBMS_BOSS] (RWED,RWED,,RE)",
"DET_4114.TXT;1 17472 21-MAR-2012 15:17 [TBMS,TBMS_BOSS] (RWED,RWED,,RE)",
"DET_4114_000.TXT;1 777 21-MAR-2012 15:18 [TBMS,TBMS_BOSS] (RWED,RWED,,RE)",
"DET_4114_001.TXT;1 254 21-MAR-2012 15:18 [TBMS,TBMS_BOSS] (RWED,RWED,,RE)",
"DET_4114_003.TXT;1 21 21-MAR-2012 15:18 [TBMS,TBMS_BOSS] (RWED,RWED,,RE)",
"DET_4114_006.TXT;1 22 21-MAR-2012 15:18 [TBMS,TBMS_BOSS] (RWED,RWED,,RE)",
"DET_4114_101.TXT;1 431 21-MAR-2012 15:18 [TBMS,TBMS_BOSS] (RWED,RWED,,RE)",
"DET_4114_121.TXT;1 2459 21-MAR-2012 15:18 [TBMS,TBMS_BOSS] (RWED,RWED,,RE)",
"DET_4114_124.TXT;1 4610 21-MAR-2012 15:18 [TBMS,TBMS_BOSS] (RWED,RWED,,RE)",
"DET_4114_200.TXT;1 936 21-MAR-2012 15:18 [TBMS,TBMS_BOSS] (RWED,RWED,,RE)",
"TEL_4114.TXT;1 1178 21-MAR-2012 15:19 [TBMS,TBMS_BOSS] (RWED,RWED,,RE)"
};
foreach (var s in sample) {
var item = parser.ParseSingleLine("disk$user520:[4114.2012.Jan]", s, new List<FtpCapability>(), false);
if (item != null) {
FtpTrace.WriteLine(item);
}
}
}
//[Fact]
public void TestDirectoryWithDots() {
using (var cl = NewFtpClient()) {
cl.Connect();
// FTP server set to timeout after 5 seconds.
//Thread.Sleep(6000);
cl.GetListing("Test.Directory", FtpListOption.ForceList);
cl.SetWorkingDirectory("Test.Directory");
cl.GetListing(null, FtpListOption.ForceList);
}
}
//[Fact]
public void TestDispose() {
using (var cl = NewFtpClient()) {
cl.Connect();
// FTP server set to timeout after 5 seconds.
//Thread.Sleep(6000);
foreach (var item in cl.GetListing()) {
}
}
}
//[Fact]
public void TestHash() {
using (var cl = NewFtpClient()) {
cl.Connect();
FtpTrace.WriteLine("Supported HASH algorithms: " + cl.HashAlgorithms);
FtpTrace.WriteLine("Current HASH algorithm: " + cl.GetHashAlgorithm());
foreach (FtpHashAlgorithm alg in Enum.GetValues(typeof(FtpHashAlgorithm))) {
if (alg != FtpHashAlgorithm.NONE && cl.HashAlgorithms.HasFlag(alg)) {
FtpHash hash = null;
cl.SetHashAlgorithm(alg);
hash = cl.GetHash("LICENSE.TXT");
if (hash.IsValid) {
Debug.Assert(hash.Verify(@"C:\FTPTEST\LICENSE.TXT"), "The computed hash didn't match or the hash object was invalid!");
}
}
}
}
}
#if !NOASYNC
//[Fact]
public async Task TestHashAsync() {
using (var cl = NewFtpClient()) {
await cl.ConnectAsync();
FtpTrace.WriteLine("Supported HASH algorithms: " + cl.HashAlgorithms);
FtpTrace.WriteLine("Current HASH algorithm: " + await cl.GetHashAlgorithmAsync());
foreach (FtpHashAlgorithm alg in Enum.GetValues(typeof(FtpHashAlgorithm))) {
if (alg != FtpHashAlgorithm.NONE && cl.HashAlgorithms.HasFlag(alg)) {
FtpHash hash = null;
await cl.SetHashAlgorithmAsync(alg);
hash = await cl.GetHashAsync("LICENSE.TXT");
if (hash.IsValid) {
Debug.Assert(hash.Verify(@"C:\FTPTEST\LICENSE.TXT"), "The computed hash didn't match or the hash object was invalid!");
}
}
}
}
}
#endif
//[Fact]
public void TestReset() {
using (var cl = NewFtpClient()) {
cl.Connect();
using (var istream = cl.OpenRead("LICENSE.TXT", 10)) {
}
}
}
[Fact]
[Trait("Category", Category_PublicFTP)]
public void GetPublicFTPServerListing() {
using (var cl = NewFtpClient_Tele2SpeedTest()) {
cl.Connect();
FtpTrace.WriteLine(cl.Capabilities);
foreach (var item in cl.GetListing(null, FtpListOption.Recursive)) {
FtpTrace.WriteLine(item);
}
}
}
[Fact]
[Trait("Category", Category_Code)]
public void TestMODCOMP_PWD_Parser() {
var response = "PWD = ~TNA=AMP,VNA=VOL03,FNA=U-ED-B2-USL";
Match m;
if ((m = Regex.Match(response, "PWD = (?<pwd>.*)")).Success) {
FtpTrace.WriteLine("PWD: " + m.Groups["pwd"].Value);
}
}
//[Fact]
public void TestNameListing() {
using (var cl = NewFtpClient()) {
cl.ValidateCertificate += OnValidateCertificate;
cl.DataConnectionType = FtpDataConnectionType.PASV;
//cl.EncryptionMode = FtpEncryptionMode.Explicit;
//cl.SocketPollInterval = 5000;
cl.Connect();
//FtpTrace.WriteLine("Sleeping for 10 seconds to force timeout.");
//Thread.Sleep(10000);
var items = cl.GetListing();
foreach (var item in items) {
//FtpTrace.WriteLine(item.FullName);
//FtpTrace.WriteLine(item.Modified.Kind);
//FtpTrace.WriteLine(item.Modified);
}
}
}
//[Fact]
public void TestNameListingFTPS() {
using (var cl = NewFtpClient()) {
cl.ValidateCertificate += OnValidateCertificate;
//cl.DataConnectionType = FtpDataConnectionType.PASV;
cl.EncryptionMode = FtpEncryptionMode.Explicit;
cl.Connect();
//FtpTrace.WriteLine("Sleeping for 10 seconds to force timeout.");
//Thread.Sleep(10000);
foreach (var item in cl.GetListing()) {
FtpTrace.WriteLine(item.FullName);
//FtpTrace.WriteLine(item.Modified.Kind);
//FtpTrace.WriteLine(item.Modified);
}
}
}
#if !CORE14
//[Fact] // Beware: Completely ignores thrown exceptions - Doesn't actually test anything!
public FtpClient Connect() {
var threads = new List<Thread>();
var cl = new FtpClient();
cl.ValidateCertificate += OnValidateCertificate;
//cl.EncryptionMode = FtpEncryptionMode.Explicit;
for (var i = 0; i < 1; i++) {
var count = i;
var t = new Thread(new ThreadStart(delegate {
cl.Credentials = new NetworkCredential(m_user, m_pass);
cl.Host = m_host;
cl.Connect();
for (var j = 0; j < 10; j++) {
cl.Execute("NOOP");
}
if (count % 2 == 0) {
cl.Disconnect();
}
}));
t.Start();
threads.Add(t);
}
while (threads.Count > 0) {
threads[0].Join();
threads.RemoveAt(0);
}
return cl;
}
public void Upload(FtpClient cl) {
var root = @"..\..\..";
var threads = new List<Thread>();
foreach (var s in Directory.GetFiles(root, "*", SearchOption.AllDirectories)) {
var file = s;
if (file.Contains(@"\.git")) {
continue;
}
var t = new Thread(new ThreadStart(delegate { DoUpload(cl, root, file); }));
t.Start();
threads.Add(t);
}
while (threads.Count > 0) {
threads[0].Join();
threads.RemoveAt(0);
}
}
#endif
public void DoUpload(FtpClient cl, string root, string s) {
var type = FtpDataType.Binary;
var path = Path.GetDirectoryName(s).Replace(root, "");
var name = Path.GetFileName(s);
if (Path.GetExtension(s).ToLower() == ".cs" || Path.GetExtension(s).ToLower() == ".txt") {
type = FtpDataType.ASCII;
}
if (!cl.DirectoryExists(path)) {
cl.CreateDirectory(path, true);
}
else if (cl.FileExists(string.Format("{0}/{1}", path, name))) {
cl.DeleteFile(string.Format("{0}/{1}", path, name));
}
using (
Stream istream = new FileStream(s, FileMode.Open, FileAccess.Read),
ostream = cl.OpenWrite(s.Replace(root, ""), type)) {
var buf = new byte[8192];
var read = 0;
while ((read = istream.Read(buf, 0, buf.Length)) > 0) {
ostream.Write(buf, 0, read);
}
if (cl.HashAlgorithms != FtpHashAlgorithm.NONE) {
Debug.Assert(cl.GetHash(s.Replace(root, "")).Verify(s), "The computed hashes don't match!");
}
}
/*if (!cl.GetHash(s.Replace(root, "")).Verify(s))
throw new Exception("Hashes didn't match!");*/
}
#if !CORE14
public void Download(FtpClient cl) {
var threads = new List<Thread>();
Download(threads, cl, "/");
while (threads.Count > 0) {
threads[0].Join();
lock (threads) {
threads.RemoveAt(0);
}
}
}
public void Download(List<Thread> threads, FtpClient cl, string path) {
foreach (var item in cl.GetListing(path)) {
if (item.Type == FtpFileSystemObjectType.Directory) {
Download(threads, cl, item.FullName);
}
else if (item.Type == FtpFileSystemObjectType.File) {
var file = item.FullName;
var t = new Thread(new ThreadStart(delegate { DoDownload(cl, file); }));
t.Start();
lock (threads) {
threads.Add(t);
}
}
}
}
#endif
public void DoDownload(FtpClient cl, string file) {
using (var s = cl.OpenRead(file)) {
var buf = new byte[8192];
while (s.Read(buf, 0, buf.Length) > 0) {
;
}
}
}
public void Delete(FtpClient cl) {
DeleteDirectory(cl, "/");
}
public void DeleteDirectory(FtpClient cl, string path) {
foreach (var item in cl.GetListing(path)) {
if (item.Type == FtpFileSystemObjectType.File) {
cl.DeleteFile(item.FullName);
}
else if (item.Type == FtpFileSystemObjectType.Directory) {
DeleteDirectory(cl, item.FullName);
cl.DeleteDirectory(item.FullName);
}
}
}
//[Fact]
public void TestUTF8() {
// the following file name was reported in the discussions as having
// problems:
// https://netftp.codeplex.com/discussions/445090
var filename = "Verbundmörtel Zubehör + Technische Daten DE.pdf";
using (var cl = NewFtpClient()) {
cl.DataConnectionType = FtpDataConnectionType.PASV;
cl.InternetProtocolVersions = FtpIpVersion.ANY;
using (var ostream = cl.OpenWrite(filename))
using (var writer = new StreamWriter(ostream)) {
writer.WriteLine(filename);
}
}
}
//[Fact]
public void TestUploadDownloadFile() {
using (var cl = NewFtpClient()) {
cl.Connect();
// 100 K file
cl.UploadFile(@"D:\Github\FluentFTP\README.md", "/public_html/temp/README.md");
cl.DownloadFile(@"D:\Github\FluentFTP\README2.md", "/public_html/temp/README.md");
/*
// 10 M file
cl.UploadFile(@"D:\Drivers\mb_driver_intel_irst_6series.exe", "/public_html/temp/big.txt");
cl.Rename("/public_html/temp/big.txt", "/public_html/temp/big2.txt");
cl.DownloadFile(@"D:\Drivers\mb_driver_intel_irst_6series_2.exe", "/public_html/temp/big2.txt");
*/
}
}
#if !NOASYNC
//[Fact]
public async Task TestUploadDownloadFileAsync() {
using (var cl = NewFtpClient()) {
// 100 K file
await cl.UploadFileAsync(@"D:\Github\FluentFTP\README.md", "/public_html/temp/README.md");
await cl.DownloadFileAsync(@"D:\Github\FluentFTP\README2.md", "/public_html/temp/README.md");
/*
// 10 M file
await cl.UploadFileAsync(@"D:\Drivers\mb_driver_intel_irst_6series.exe", "/public_html/temp/big.txt");
await cl.RenameAsync("/public_html/temp/big.txt", "/public_html/temp/big2.txt");
await cl.DownloadFileAsync(@"D:\Drivers\mb_driver_intel_irst_6series_2.exe", "/public_html/temp/big2.txt");
*/
}
}
#endif
//[Fact]
public void TestUploadDownloadFile_UTF() {
using (var cl = NewFtpClient()) {
// 100 K file
cl.UploadFile(@"D:\Tests\Caffè.jpg", "/public_html/temp/Caffè.jpg");
cl.DownloadFile(@"D:\Tests\Caffè2.jpg", "/public_html/temp/Caffè.jpg");
/*
// 10 M file
cl.UploadFile(@"D:\Drivers\mb_driver_intel_irst_6series.exe", "/public_html/temp/big.txt");
cl.Rename("/public_html/temp/big.txt", "/public_html/temp/big2.txt");
cl.DownloadFile(@"D:\Drivers\mb_driver_intel_irst_6series_2.exe", "/public_html/temp/big2.txt");
*/
}
}
//[Fact]
public void TestUploadDownloadFile_ANSI() {
using (var cl = NewFtpClient()) {
cl.Encoding = Encoding.GetEncoding(1252);