-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathmain.cpp
1074 lines (832 loc) · 37.6 KB
/
main.cpp
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
#include "gtest/gtest.h" // Google Test Framework
#include "test_utils.h" // Helpers for tests
// Test subject (SUT)
#include "FTPClient.h"
#define PRINT_LOG [](const std::string& strLogMsg) { std::cout << strLogMsg << std::endl; }
// Test parameters
extern bool FTP_TEST_ENABLED;
extern bool SFTP_TEST_ENABLED;
extern bool HTTP_PROXY_TEST_ENABLED;
extern std::string CURL_LOG_FOLDER;
extern std::string SSL_CERT_FILE;
extern std::string SSL_KEY_FILE;
extern std::string SSL_KEY_PWD;
extern std::string FTP_SERVER;
extern unsigned FTP_SERVER_PORT;
extern std::string FTP_USERNAME;
extern std::string FTP_PASSWORD;
extern std::string FTP_REMOTE_FILE;
extern std::string FTP_REMOTE_FILE_SHA1SUM;
extern std::string FTP_REMOTE_UPLOAD_FOLDER;
extern std::string FTP_REMOTE_DOWNLOAD_FOLDER;
extern std::string SFTP_SERVER;
extern unsigned SFTP_SERVER_PORT;
extern std::string SFTP_USERNAME;
extern std::string SFTP_PASSWORD;
extern std::string SFTP_REMOTE_FILE;
extern std::string SFTP_REMOTE_FILE_SHA1SUM;
extern std::string SFTP_REMOTE_UPLOAD_FOLDER;
extern std::string SFTP_REMOTE_DOWNLOAD_FOLDER;
extern std::string PROXY_SERVER;
extern std::string PROXY_SERVER_FAKE;
extern std::mutex g_mtxConsoleMutex;
namespace embeddedmz {
// fixture for FTP tests
class FTPClientTest : public ::testing::Test {
protected:
std::unique_ptr<CFTPClient> m_pFTPClient;
FTPClientTest() : m_pFTPClient(nullptr) {
#ifdef DEBUG_CURL
CFTPClient::SetCurlTraceLogDirectory(CURL_LOG_FOLDER);
#endif
}
virtual ~FTPClientTest() {}
virtual void SetUp() {
m_pFTPClient.reset(new CFTPClient(PRINT_LOG));
m_pFTPClient->InitSession(FTP_SERVER, FTP_SERVER_PORT, FTP_USERNAME, FTP_PASSWORD,
CFTPClient::FTP_PROTOCOL::FTP, CFTPClient::SettingsFlag::ENABLE_LOG);
// needed to avoid some rare test failure - though, it seems that libcurl timeout are not working properly...
//m_pFTPClient->SetTimeout(500);
}
virtual void TearDown() {
if (m_pFTPClient.get() != nullptr) {
m_pFTPClient->CleanupSession();
m_pFTPClient.reset();
}
}
};
// fixture for SFTP tests
class SFTPClientTest : public ::testing::Test {
protected:
std::unique_ptr<CFTPClient> m_pSFTPClient;
SFTPClientTest() : m_pSFTPClient(nullptr) {
#ifdef DEBUG_CURL
CFTPClient::SetCurlTraceLogDirectory(CURL_LOG_FOLDER);
#endif
}
virtual ~SFTPClientTest() {}
virtual void SetUp() {
m_pSFTPClient.reset(new CFTPClient(PRINT_LOG));
m_pSFTPClient->InitSession(SFTP_SERVER, SFTP_SERVER_PORT, SFTP_USERNAME, SFTP_PASSWORD, CFTPClient::FTP_PROTOCOL::SFTP,
CFTPClient::SettingsFlag::ENABLE_LOG);
m_pSFTPClient->SetInsecure(true);
// needed to avoid some rare test failure - timeout seems to not working
//m_pSFTPClient->SetTimeout(500);
}
virtual void TearDown() {
if (m_pSFTPClient.get() != nullptr) {
m_pSFTPClient->CleanupSession();
m_pSFTPClient.reset();
}
}
};
// Unit tests
// Tests without a fixture (testing setters/getters and init./cleanup session)
TEST(FTPClient, TestSession) {
CFTPClient FTPClient(PRINT_LOG);
EXPECT_TRUE(FTPClient.GetUsername().empty());
EXPECT_TRUE(FTPClient.GetPassword().empty());
EXPECT_TRUE(FTPClient.GetURL().empty());
EXPECT_TRUE(FTPClient.GetProxy().empty());
EXPECT_TRUE(FTPClient.GetSSLCertFile().empty());
EXPECT_TRUE(FTPClient.GetSSLKeyFile().empty());
EXPECT_TRUE(FTPClient.GetSSLKeyPwd().empty());
EXPECT_FALSE(FTPClient.GetActive());
EXPECT_TRUE(FTPClient.GetNoSignal());
EXPECT_EQ(0, FTPClient.GetTimeout());
EXPECT_TRUE(FTPClient.GetCurlPointer() == nullptr);
EXPECT_EQ(CFTPClient::SettingsFlag::NO_FLAGS, FTPClient.GetSettingsFlags());
EXPECT_EQ(CFTPClient::FTP_PROTOCOL::FTP, FTPClient.GetProtocol());
ASSERT_TRUE(FTPClient.InitSession(FTP_SERVER, FTP_SERVER_PORT, FTP_USERNAME, FTP_PASSWORD, CFTPClient::FTP_PROTOCOL::SFTP,
CFTPClient::ENABLE_LOG | CFTPClient::ENABLE_SSH_AGENT));
EXPECT_EQ(CFTPClient::ENABLE_LOG | CFTPClient::ENABLE_SSH_AGENT, FTPClient.GetSettingsFlags());
EXPECT_EQ(CFTPClient::FTP_PROTOCOL::SFTP, FTPClient.GetProtocol());
EXPECT_TRUE(FTPClient.GetCurlPointer() != nullptr);
FTPClient.SetProxy("my_proxy");
FTPClient.SetSSLCertFile("file.cert");
FTPClient.SetSSLKeyFile("key.key");
FTPClient.SetSSLKeyPassword("passphrase");
FTPClient.SetTimeout(10);
FTPClient.SetActive(true);
FTPClient.SetNoSignal(false);
EXPECT_TRUE(FTPClient.GetActive());
EXPECT_FALSE(FTPClient.GetNoSignal());
EXPECT_STREQ(FTP_SERVER.c_str(), FTPClient.GetURL().c_str());
EXPECT_EQ(FTP_SERVER_PORT, FTPClient.GetPort());
EXPECT_STREQ(FTP_USERNAME.c_str(), FTPClient.GetUsername().c_str());
EXPECT_STREQ(FTP_PASSWORD.c_str(), FTPClient.GetPassword().c_str());
EXPECT_STREQ("http://my_proxy", FTPClient.GetProxy().c_str());
EXPECT_STREQ("file.cert", FTPClient.GetSSLCertFile().c_str());
EXPECT_STREQ("key.key", FTPClient.GetSSLKeyFile().c_str());
EXPECT_STREQ("passphrase", FTPClient.GetSSLKeyPwd().c_str());
EXPECT_EQ(10, FTPClient.GetTimeout());
FTPClient.SetProgressFnCallback(reinterpret_cast<void*>(0xFFFFFFFF), &TestDLProgressCallback);
EXPECT_EQ(&TestDLProgressCallback, *FTPClient.GetProgressFnCallback());
EXPECT_EQ(reinterpret_cast<void*>(0xFFFFFFFF), FTPClient.GetProgressFnCallbackOwner());
EXPECT_TRUE(FTPClient.CleanupSession());
}
TEST(FTPClient, TestDoubleInitializingSession) {
CFTPClient FTPClient(PRINT_LOG);
ASSERT_TRUE(FTPClient.InitSession(FTP_SERVER, FTP_SERVER_PORT, "amine", FTP_PASSWORD));
EXPECT_FALSE(FTPClient.InitSession(FTP_SERVER, FTP_SERVER_PORT, FTP_USERNAME, FTP_PASSWORD));
EXPECT_TRUE(FTPClient.CleanupSession());
}
TEST(FTPClient, TestDoubleCleanUp) {
CFTPClient FTPClient(PRINT_LOG);
ASSERT_TRUE(FTPClient.InitSession(FTP_SERVER, FTP_SERVER_PORT, FTP_USERNAME, FTP_PASSWORD));
EXPECT_TRUE(FTPClient.CleanupSession());
EXPECT_FALSE(FTPClient.CleanupSession());
}
TEST(FTPClient, TestCleanUpWithoutInit) {
CFTPClient FTPClient(PRINT_LOG);
ASSERT_FALSE(FTPClient.CleanupSession());
}
TEST(FTPClient, TestMultithreading) {
const char* arrDataArray[3] = {"Thread 1", "Thread 2", "Thread 3"};
auto ThreadFunction = [](const char* pszThreadName) {
CFTPClient FTPClient([](const std::string& strMsg) { std::cout << strMsg << std::endl; });
if (pszThreadName != nullptr) {
std::unique_lock<std::mutex> lock{g_mtxConsoleMutex};
std::cout << pszThreadName << std::endl;
}
};
std::thread FirstThread(ThreadFunction, arrDataArray[0]);
std::thread SecondThread(ThreadFunction, arrDataArray[1]);
std::thread ThirdThread(ThreadFunction, arrDataArray[2]);
// synchronize threads
FirstThread.join(); // pauses until first finishes
SecondThread.join(); // pauses until second finishes
ThirdThread.join(); // pauses until third finishes
}
TEST_F(FTPClientTest, TestDownloadFile) {
if (FTP_TEST_ENABLED) {
// to display a beautiful progress bar on console
m_pFTPClient->SetProgressFnCallback(m_pFTPClient.get(), &TestDLProgressCallback);
#ifdef WINDOWS
// Convert file name from ANSI to UTF8
std::string remoteFileUtf8 = CFTPClient::AnsiToUtf8(FTP_REMOTE_FILE);
#else
std::string remoteFileUtf8 = FTP_REMOTE_FILE;
#endif
ASSERT_TRUE(m_pFTPClient->DownloadFile("downloaded_file", remoteFileUtf8));
/* to properly show the progress bar */
std::cout << std::endl;
/* check the SHA1 sum of the downloaded file if possible */
if (!FTP_REMOTE_FILE_SHA1SUM.empty()) {
std::string ret = sha1sum("downloaded_file");
std::transform(ret.begin(), ret.end(), ret.begin(), ::tolower);
EXPECT_TRUE(FTP_REMOTE_FILE_SHA1SUM == ret);
}
/* delete test file */
EXPECT_TRUE(remove("downloaded_file") == 0);
} else
std::cout << "FTP tests are disabled !" << std::endl;
}
#ifdef WINDOWS
TEST_F(FTPClientTest, TestSaveFileNameWithAccents) {
if (FTP_TEST_ENABLED) {
// to display a beautiful progress bar on console
m_pFTPClient->SetProgressFnCallback(m_pFTPClient.get(), &TestDLProgressCallback);
// Convert file name from ANSI to UTF8
std::string remoteFileUtf8 = CFTPClient::AnsiToUtf8(FTP_REMOTE_FILE);
std::string localFileNameUtf8 = CFTPClient::AnsiToUtf8("fichier_téléchargé");
ASSERT_TRUE(m_pFTPClient->DownloadFile(localFileNameUtf8, remoteFileUtf8));
/* to properly show the progress bar */
std::cout << std::endl;
/* check the SHA1 sum of the downloaded file if possible */
if (!FTP_REMOTE_FILE_SHA1SUM.empty()) {
std::string ret = sha1sum("fichier_téléchargé");
std::transform(ret.begin(), ret.end(), ret.begin(), ::tolower);
EXPECT_TRUE(FTP_REMOTE_FILE_SHA1SUM == ret);
}
/* delete test file */
EXPECT_TRUE(remove("fichier_téléchargé") == 0);
} else
std::cout << "FTP tests are disabled !" << std::endl;
}
#endif
TEST_F(FTPClientTest, TestDownloadFileToMem) {
if (FTP_TEST_ENABLED) {
// stores file content
std::vector<char> output;
// to display a beautiful progress bar on console
m_pFTPClient->SetProgressFnCallback(m_pFTPClient.get(), &TestDLProgressCallback);
#ifdef WINDOWS
// Convert file name from ANSI to UTF8
std::string remoteFileUtf8 = CFTPClient::AnsiToUtf8(FTP_REMOTE_FILE);
#else
std::string remoteFileUtf8 = FTP_REMOTE_FILE;
#endif
ASSERT_TRUE(m_pFTPClient->DownloadFile(remoteFileUtf8, output));
/* to properly show the progress bar */
std::cout << std::endl;
/* check the SHA1 sum of the downloaded file if possible */
if (!FTP_REMOTE_FILE_SHA1SUM.empty()) {
std::string ret = sha1sum(output);
std::transform(ret.begin(), ret.end(), ret.begin(), ::tolower);
EXPECT_TRUE(FTP_REMOTE_FILE_SHA1SUM == ret);
}
} else
std::cout << "FTP tests are disabled !" << std::endl;
}
// this test was created when I was using a buggy FTP server aka FileZilla
TEST_F(FTPClientTest, TestDownloadFile10Times) {
if (FTP_TEST_ENABLED) {
// to display a beautiful progress bar on console
m_pFTPClient->SetProgressFnCallback(m_pFTPClient.get(), &TestDLProgressCallback);
#ifdef WINDOWS
// Convert file name from ANSI to UTF8
std::string remoteFileUtf8 = CFTPClient::AnsiToUtf8(FTP_REMOTE_FILE);
#else
std::string remoteFileUtf8 = FTP_REMOTE_FILE;
#endif
for (unsigned i = 0; i < 10; ++i) {
ASSERT_TRUE(m_pFTPClient->DownloadFile("downloaded_file", remoteFileUtf8));
/* to properly show the progress bar */
std::cout << std::endl;
/* check the SHA1 sum of the downloaded file if possible */
if (!FTP_REMOTE_FILE_SHA1SUM.empty()) {
std::string ret = sha1sum("downloaded_file");
std::transform(ret.begin(), ret.end(), ret.begin(), ::tolower);
EXPECT_TRUE(FTP_REMOTE_FILE_SHA1SUM == ret);
}
}
/* delete test file */
EXPECT_TRUE(remove("downloaded_file") == 0);
} else
std::cout << "FTP tests are disabled !" << std::endl;
}
// Check for failure
TEST_F(FTPClientTest, TestDownloadInexistantFile) {
if (FTP_TEST_ENABLED) {
// inexistant file, expect failure
ASSERT_FALSE(m_pFTPClient->DownloadFile("downloaded_inexistent_file.xxx", "inexistent_file.xxx"));
} else
std::cout << "FTP tests are disabled !" << std::endl;
}
TEST_F(FTPClientTest, TestFileInfo) {
CFTPClient::FileInfo ResFileInfo = {0, 0.0};
if (FTP_TEST_ENABLED) {
#ifdef WINDOWS
// Convert file name from ANSI to UTF8
std::string remoteFileUtf8 = CFTPClient::AnsiToUtf8(FTP_REMOTE_FILE);
#else
std::string remoteFileUtf8 = FTP_REMOTE_FILE;
#endif
ASSERT_TRUE(m_pFTPClient->Info(remoteFileUtf8, ResFileInfo));
EXPECT_GT(ResFileInfo.dFileSize, 0);
EXPECT_GT(ResFileInfo.tFileMTime, 0);
/* TODO : we can check the mtime of the remote file with an epoch value provided in the INI file */
} else
std::cout << "FTP tests are disabled !" << std::endl;
}
// Check for failure
TEST_F(FTPClientTest, TestGetInexistantFileInfo) {
CFTPClient::FileInfo ResFileInfo = {0, 0.0};
if (FTP_TEST_ENABLED) {
ASSERT_FALSE(m_pFTPClient->Info("inexistent_file.xxx", ResFileInfo));
EXPECT_EQ(ResFileInfo.dFileSize, 0.0);
EXPECT_EQ(ResFileInfo.tFileMTime, 0);
} else
std::cout << "FTP tests are disabled !" << std::endl;
}
// TODO : this unit test can't be executed elsewhere
#if 0
TEST_F(FTPClientTest, TestAppend/*AndRemoveFile*/) {
if (FTP_TEST_ENABLED) {
// to display a beautiful progress bar on console
m_pFTPClient->SetProgressFnCallback(m_pFTPClient.get(), &TestUPProgressCallback);
// Upload file
ASSERT_TRUE(m_pFTPClient->UploadFile("C:\\Users\\mmzoughi\\Desktop\\test_upload_part1.txt", FTP_REMOTE_UPLOAD_FOLDER + "test_append.txt"));
std::cout << std::endl;
ASSERT_TRUE(m_pFTPClient->AppendFile("C:\\Users\\mmzoughi\\Desktop\\test_upload.txt", 84, FTP_REMOTE_UPLOAD_FOLDER + "test_append.txt"));
std::cout << std::endl;
// Download the uploaded file into a vector of bytes
{
std::vector<char> uploadedFileBytes;
EXPECT_TRUE(m_pFTPClient->DownloadFile(FTP_REMOTE_UPLOAD_FOLDER + "test_append.txt", uploadedFileBytes));
std::cout << std::endl;
/* check the SHA1 sum of the uploaded file */
std::string expectedSha1Sum = sha1sum("C:\\Users\\mmzoughi\\Desktop\\test_upload.txt");
std::string resultSha1Sum = sha1sum(uploadedFileBytes);
EXPECT_TRUE(expectedSha1Sum == resultSha1Sum);
}
// Remove file
//ASSERT_TRUE(m_pFTPClient->RemoveFile(FTP_REMOTE_UPLOAD_FOLDER + "test_upload.txt"));
// delete test file
//EXPECT_TRUE(remove("test_append.txt") == 0);
} else
std::cout << "FTP tests are disabled !" << std::endl;
}
#endif
// Upload Tests
// check return code
TEST_F(FTPClientTest, TestUploadAndRemoveFile) {
if (FTP_TEST_ENABLED) {
// to display a beautiful progress bar on console
m_pFTPClient->SetProgressFnCallback(m_pFTPClient.get(), &TestUPProgressCallback);
std::ostringstream ssTimestamp;
TimeStampTest(ssTimestamp);
// create dummy test file
std::ofstream ofTestUpload("test_upload.txt");
ASSERT_TRUE(static_cast<bool>(ofTestUpload));
ofTestUpload << "Unit Test TestUploadFile executed on " + ssTimestamp.str() + "\n" +
"This file is uploaded via FTPClient-C++ API.\n" +
"If this file exists, that means that the unit test is passed.\n";
ASSERT_TRUE(static_cast<bool>(ofTestUpload));
ofTestUpload.close();
// Upload file and create a directory "upload_test"
ASSERT_TRUE(m_pFTPClient->UploadFile("test_upload.txt", FTP_REMOTE_UPLOAD_FOLDER + "upload_test/test_upload.txt", true));
/* to properly show the progress bar */
std::cout << std::endl;
// Upload file
ASSERT_TRUE(m_pFTPClient->UploadFile("test_upload.txt", FTP_REMOTE_UPLOAD_FOLDER + "test_upload.txt"));
std::cout << std::endl;
// Download the uploaded file into a vector of bytes
{
std::vector<char> uploadedFileBytes;
EXPECT_TRUE(m_pFTPClient->DownloadFile(FTP_REMOTE_UPLOAD_FOLDER + "test_upload.txt", uploadedFileBytes));
std::cout << std::endl;
/* check the SHA1 sum of the uploaded file */
std::string expectedSha1Sum = sha1sum("test_upload.txt");
std::string resultSha1Sum = sha1sum(uploadedFileBytes);
EXPECT_TRUE(expectedSha1Sum == resultSha1Sum);
}
// Remove file
ASSERT_TRUE(m_pFTPClient->RemoveFile(FTP_REMOTE_UPLOAD_FOLDER + "test_upload.txt"));
// delete test file
EXPECT_TRUE(remove("test_upload.txt") == 0);
} else
std::cout << "FTP tests are disabled !" << std::endl;
}
TEST_F(FTPClientTest, TestUploadStreamAndRemove) {
if (FTP_TEST_ENABLED) {
// to display a beautiful progress bar on console
m_pFTPClient->SetProgressFnCallback(m_pFTPClient.get(), &TestUPProgressCallback);
std::ostringstream ssTimestamp;
TimeStampTest(ssTimestamp);
// create dummy string stream
std::stringstream ofTestUpload;
ASSERT_TRUE(static_cast<bool>(ofTestUpload));
ofTestUpload << "Unit Test 'TestUploadStreamAndRemove' executed on " + ssTimestamp.str() + "\n" +
"This file is uploaded via FTPClient-C++ API.\n" +
"If this file exists, that means that the unit test is passed.\n";
ASSERT_TRUE(static_cast<bool>(ofTestUpload));
// Upload steam
ASSERT_TRUE(static_cast<bool>(ofTestUpload));
ASSERT_TRUE(m_pFTPClient->UploadFile(ofTestUpload, SFTP_REMOTE_UPLOAD_FOLDER + "test_upload_stream.txt"));
std::cout << std::endl;
// Download the uploaded stream into a vector of bytes
{
std::vector<char> uploadedFileBytes;
EXPECT_TRUE(m_pFTPClient->DownloadFile(SFTP_REMOTE_UPLOAD_FOLDER + "test_upload_stream.txt", uploadedFileBytes));
std::cout << std::endl;
/* check the SHA1 sum of the uploaded file */
std::string contentStr = ofTestUpload.str();
std::vector<char> contentBytes(contentStr.begin(), contentStr.end());
std::string expectedSha1Sum = sha1sum(contentBytes);
std::string resultSha1Sum = sha1sum(uploadedFileBytes);
EXPECT_TRUE(expectedSha1Sum == resultSha1Sum);
}
// Remove file
ASSERT_TRUE(m_pFTPClient->RemoveFile(SFTP_REMOTE_UPLOAD_FOLDER + "test_upload_stream.txt"));
} else
std::cout << "FTP tests are disabled !" << std::endl;
}
#ifdef WINDOWS
TEST_F(FTPClientTest, TestUploadFileNameWithAccents) {
if (FTP_TEST_ENABLED) {
// to display a beautiful progress bar on console
m_pFTPClient->SetProgressFnCallback(m_pFTPClient.get(), &TestUPProgressCallback);
std::ostringstream ssTimestamp;
TimeStampTest(ssTimestamp);
// Convert file name from ANSI to UTF8
std::string fileNameUtf8 = CFTPClient::AnsiToUtf8("fichier_à_téléverser.txt");
// create dummy test file
std::ofstream ofTestUpload("fichier_à_téléverser.txt");
ASSERT_TRUE(static_cast<bool>(ofTestUpload));
ofTestUpload << "Unit Test TestUploadFile executed on " + ssTimestamp.str() + "\n" +
"This file is uploaded via FTPClient-C++ API.\n" +
"If this file exists, that means that the unit test is passed.\n";
ASSERT_TRUE(static_cast<bool>(ofTestUpload));
ofTestUpload.close();
// Upload file and create a directory "upload_test"
ASSERT_TRUE(m_pFTPClient->UploadFile(fileNameUtf8, FTP_REMOTE_UPLOAD_FOLDER + "upload_test_accents/" + fileNameUtf8, true));
/* to properly show the progress bar */
std::cout << std::endl;
// Upload file
ASSERT_TRUE(m_pFTPClient->UploadFile(fileNameUtf8, FTP_REMOTE_UPLOAD_FOLDER + fileNameUtf8));
std::cout << std::endl;
// Download the uploaded file into a vector of bytes
{
std::vector<char> uploadedFileBytes;
EXPECT_TRUE(m_pFTPClient->DownloadFile(FTP_REMOTE_UPLOAD_FOLDER + fileNameUtf8, uploadedFileBytes));
std::cout << std::endl;
/* check the SHA1 sum of the uploaded file */
std::string expectedSha1Sum = sha1sum("fichier_à_téléverser.txt");
std::string resultSha1Sum = sha1sum(uploadedFileBytes);
EXPECT_TRUE(expectedSha1Sum == resultSha1Sum);
}
// Remove file
ASSERT_TRUE(m_pFTPClient->RemoveFile(FTP_REMOTE_UPLOAD_FOLDER + fileNameUtf8));
// delete test file
EXPECT_TRUE(remove("fichier_à_téléverser.txt") == 0);
} else
std::cout << "FTP tests are disabled !" << std::endl;
}
#endif
TEST_F(FTPClientTest, TestUploadAndRemoveFile10Times) {
if (FTP_TEST_ENABLED) {
// to display a beautiful progress bar on console
m_pFTPClient->SetProgressFnCallback(m_pFTPClient.get(), &TestUPProgressCallback);
std::ostringstream ssTimestamp;
TimeStampTest(ssTimestamp);
// create dummy test file
std::ofstream ofTestUpload("test_upload.txt");
ASSERT_TRUE(static_cast<bool>(ofTestUpload));
ofTestUpload << "Unit Test TestUploadFile executed on " + ssTimestamp.str() + "\n" +
"This file is uploaded via FTPClient-C++ API.\n" +
"If this file exists, that means that the unit test is passed.\n";
ASSERT_TRUE(static_cast<bool>(ofTestUpload));
ofTestUpload.close();
for (unsigned i = 0; i < 10; ++i) {
// Upload file and create a directory "upload_test"
ASSERT_TRUE(m_pFTPClient->UploadFile("test_upload.txt", FTP_REMOTE_UPLOAD_FOLDER + "upload_test/test_upload.txt", true));
/* to properly show the progress bar */
std::cout << std::endl;
// Upload file
ASSERT_TRUE(m_pFTPClient->UploadFile("test_upload.txt", FTP_REMOTE_UPLOAD_FOLDER + "test_upload.txt"));
std::cout << std::endl;
// Download the uploaded file into a vector of bytes
{
std::vector<char> uploadedFileBytes;
EXPECT_TRUE(m_pFTPClient->DownloadFile(FTP_REMOTE_UPLOAD_FOLDER + "test_upload.txt", uploadedFileBytes));
std::cout << std::endl;
/* check the SHA1 sum of the uploaded file */
std::string expectedSha1Sum = sha1sum("test_upload.txt");
std::string resultSha1Sum = sha1sum(uploadedFileBytes);
EXPECT_TRUE(expectedSha1Sum == resultSha1Sum);
}
// Remove file
ASSERT_TRUE(m_pFTPClient->RemoveFile(FTP_REMOTE_UPLOAD_FOLDER + "test_upload.txt"));
}
// delete test file
EXPECT_TRUE(remove("test_upload.txt") == 0);
} else
std::cout << "FTP tests are disabled !" << std::endl;
}
TEST_F(FTPClientTest, TestUploadFailure) {
if (FTP_TEST_ENABLED) {
// upload of an inexistant file must fail.
ASSERT_FALSE(m_pFTPClient->UploadFile("inexistant_file.doc", FTP_REMOTE_UPLOAD_FOLDER + "inexistant_file.doc"));
} else
std::cout << "FTP tests are disabled !" << std::endl;
}
TEST_F(FTPClientTest, TestList) {
if (FTP_TEST_ENABLED) {
std::string strList;
/* list root directory */
ASSERT_TRUE(m_pFTPClient->List("/", strList, false));
EXPECT_FALSE(strList.empty());
} else
std::cout << "FTP tests are disabled !" << std::endl;
}
TEST_F(FTPClientTest, TestWildcardedURL) {
#ifdef LINUX
mkdir("Wildcard", ACCESSPERMS);
#else
_mkdir("Wildcard");
#endif
if (FTP_TEST_ENABLED) {
// all the content of FTP_REMOTE_DOWNLOAD_FOLDER/*
ASSERT_TRUE(m_pFTPClient->DownloadWildcard("Wildcard", FTP_REMOTE_DOWNLOAD_FOLDER));
/* TODO : check the existence of the downloaded items (Helpers_cpp project can be useful) */
/* TODO : delete recusively the files (Helpers_cpp project can be useful) */
} else
std::cout << "FTP tests are disabled !" << std::endl;
}
// check for failure
TEST_F(FTPClientTest, TestWildcardedURLFailure) {
if (FTP_TEST_ENABLED) {
ASSERT_FALSE(m_pFTPClient->DownloadWildcard("InexistentDir", "*"));
} else
std::cout << "FTP tests are disabled !" << std::endl;
}
TEST_F(FTPClientTest, TestCreateAndRemoveDirectory) {
if (FTP_TEST_ENABLED) {
ASSERT_TRUE(m_pFTPClient->CreateDir(FTP_REMOTE_UPLOAD_FOLDER + "bookmarks"));
EXPECT_TRUE(m_pFTPClient->RemoveDir(FTP_REMOTE_UPLOAD_FOLDER + "bookmarks"));
} else
std::cout << "FTP tests are disabled !" << std::endl;
}
// Proxy Tests
TEST_F(FTPClientTest, TestProxyList) {
if (HTTP_PROXY_TEST_ENABLED && FTP_TEST_ENABLED) {
std::string strList;
m_pFTPClient->SetProxy(PROXY_SERVER);
ASSERT_TRUE(m_pFTPClient->List("/", strList));
EXPECT_FALSE(strList.empty());
} else
std::cout << "HTTP Proxy tests are disabled !" << std::endl;
}
// check for failure
TEST_F(FTPClientTest, TestInexistantProxy) {
if (HTTP_PROXY_TEST_ENABLED && FTP_TEST_ENABLED) {
std::string strList;
/* fake proxy address */
m_pFTPClient->SetProxy(PROXY_SERVER_FAKE);
/* to avoid long waitings - 5 seconds timeout */
m_pFTPClient->SetTimeout(5);
ASSERT_FALSE(m_pFTPClient->List("/", strList, true));
EXPECT_TRUE(strList.empty());
} else
std::cout << "HTTP Proxy tests are disabled !" << std::endl;
}
/* SFTP Unit Tests */
TEST_F(SFTPClientTest, TestDownloadFile) {
if (SFTP_TEST_ENABLED) {
// to display a beautiful progress bar on console
m_pSFTPClient->SetProgressFnCallback(m_pSFTPClient.get(), &TestDLProgressCallback);
ASSERT_TRUE(m_pSFTPClient->DownloadFile("downloaded_file", SFTP_REMOTE_FILE));
/* to properly show the progress bar */
std::cout << std::endl;
/* check the SHA1 sum of the downloaded file if possible */
if (!SFTP_REMOTE_FILE_SHA1SUM.empty()) {
std::string ret = sha1sum("downloaded_file");
std::transform(ret.begin(), ret.end(), ret.begin(), ::tolower);
EXPECT_TRUE(SFTP_REMOTE_FILE_SHA1SUM == ret);
}
/* delete test file */
EXPECT_TRUE(remove("downloaded_file") == 0);
} else
std::cout << "SFTP tests are disabled !" << std::endl;
}
TEST_F(SFTPClientTest, TestDownloadFileToMem) {
if (SFTP_TEST_ENABLED) {
// stores file content
std::vector<char> output;
// to display a beautiful progress bar on console
m_pSFTPClient->SetProgressFnCallback(m_pSFTPClient.get(), &TestDLProgressCallback);
ASSERT_TRUE(m_pSFTPClient->DownloadFile(SFTP_REMOTE_FILE, output));
/* to properly show the progress bar */
std::cout << std::endl;
/* check the SHA1 sum of the downloaded file if possible */
if (!SFTP_REMOTE_FILE_SHA1SUM.empty()) {
std::string ret = sha1sum(output);
std::transform(ret.begin(), ret.end(), ret.begin(), ::tolower);
EXPECT_TRUE(SFTP_REMOTE_FILE_SHA1SUM == ret);
}
} else
std::cout << "SFTP tests are disabled !" << std::endl;
}
TEST_F(SFTPClientTest, TestDownloadFile10Times) {
if (SFTP_TEST_ENABLED) {
// to display a beautiful progress bar on console
m_pSFTPClient->SetProgressFnCallback(m_pSFTPClient.get(), &TestDLProgressCallback);
for (unsigned i = 0; i < 10; ++i) {
ASSERT_TRUE(m_pSFTPClient->DownloadFile("downloaded_file", SFTP_REMOTE_FILE));
/* to properly show the progress bar */
std::cout << std::endl;
/* check the SHA1 sum of the downloaded file if possible */
if (!SFTP_REMOTE_FILE_SHA1SUM.empty()) {
std::string ret = sha1sum("downloaded_file");
std::transform(ret.begin(), ret.end(), ret.begin(), ::tolower);
EXPECT_TRUE(SFTP_REMOTE_FILE_SHA1SUM == ret);
}
}
/* delete test file */
EXPECT_TRUE(remove("downloaded_file") == 0);
} else
std::cout << "SFTP tests are disabled !" << std::endl;
}
// Check for failure
TEST_F(SFTPClientTest, TestDownloadInexistantFile) {
if (SFTP_TEST_ENABLED) {
// inexistant file, expect failure
ASSERT_FALSE(m_pSFTPClient->DownloadFile("downloaded_inexistent_file.xxx", "inexistent_file.xxx"));
} else
std::cout << "SFTP tests are disabled !" << std::endl;
}
TEST_F(SFTPClientTest, TestFileInfo) {
CFTPClient::FileInfo ResFileInfo = {0, 0.0};
if (SFTP_TEST_ENABLED) {
ASSERT_TRUE(m_pSFTPClient->Info(SFTP_REMOTE_FILE, ResFileInfo));
EXPECT_GT(ResFileInfo.dFileSize, 0);
EXPECT_GT(ResFileInfo.tFileMTime, 0);
/* TODO : we can check the mtime of the remote file with an epoch value provided in the INI file */
} else
std::cout << "SFTP tests are disabled !" << std::endl;
}
// Check for failure
TEST_F(SFTPClientTest, TestGetInexistantFileInfo) {
CFTPClient::FileInfo ResFileInfo = {0, 0.0};
if (SFTP_TEST_ENABLED) {
ASSERT_FALSE(m_pSFTPClient->Info("inexistent_file.xxx", ResFileInfo));
EXPECT_EQ(ResFileInfo.dFileSize, 0.0);
EXPECT_EQ(ResFileInfo.tFileMTime, 0);
} else
std::cout << "SFTP tests are disabled !" << std::endl;
}
// Upload Tests
// check return code
TEST_F(SFTPClientTest, TestUploadAndRemoveFile) {
if (SFTP_TEST_ENABLED) {
// to display a beautiful progress bar on console
m_pSFTPClient->SetProgressFnCallback(m_pSFTPClient.get(), &TestUPProgressCallback);
std::ostringstream ssTimestamp;
TimeStampTest(ssTimestamp);
// create dummy test file
std::ofstream ofTestUpload("test_upload.txt");
ASSERT_TRUE(static_cast<bool>(ofTestUpload));
ofTestUpload << "Unit Test TestUploadFile executed on " + ssTimestamp.str() + "\n" +
"This file is uploaded via FTPClient-C++ API.\n" +
"If this file exists, that means that the unit test is passed.\n";
ASSERT_TRUE(static_cast<bool>(ofTestUpload));
ofTestUpload.close();
// Upload file and create a directory "upload_test"
ASSERT_TRUE(m_pSFTPClient->UploadFile("test_upload.txt", SFTP_REMOTE_UPLOAD_FOLDER + "upload_test/test_upload.txt", true));
/* to properly show the progress bar */
std::cout << std::endl;
// Upload file
ASSERT_TRUE(m_pSFTPClient->UploadFile("test_upload.txt", SFTP_REMOTE_UPLOAD_FOLDER + "test_upload.txt"));
std::cout << std::endl;
// Download the uploaded file into a vector of bytes
{
std::vector<char> uploadedFileBytes;
EXPECT_TRUE(m_pSFTPClient->DownloadFile(SFTP_REMOTE_UPLOAD_FOLDER + "test_upload.txt", uploadedFileBytes));
std::cout << std::endl;
/* check the SHA1 sum of the uploaded file */
std::string expectedSha1Sum = sha1sum("test_upload.txt");
std::string resultSha1Sum = sha1sum(uploadedFileBytes);
EXPECT_TRUE(expectedSha1Sum == resultSha1Sum);
}
// Remove file
ASSERT_TRUE(m_pSFTPClient->RemoveFile(SFTP_REMOTE_UPLOAD_FOLDER + "test_upload.txt"));
// delete test file
EXPECT_TRUE(remove("test_upload.txt") == 0);
} else
std::cout << "SFTP tests are disabled !" << std::endl;
}
TEST_F(SFTPClientTest, TestUploadStreamAndRemove) {
if (SFTP_TEST_ENABLED) {
// to display a beautiful progress bar on console
m_pSFTPClient->SetProgressFnCallback(m_pSFTPClient.get(), &TestUPProgressCallback);
std::ostringstream ssTimestamp;
TimeStampTest(ssTimestamp);
// create dummy string stream
std::stringstream ofTestUpload;
ASSERT_TRUE(static_cast<bool>(ofTestUpload));
ofTestUpload << "Unit Test 'TestUploadStreamAndRemove' executed on " + ssTimestamp.str() + "\n" +
"This file is uploaded via FTPClient-C++ API.\n" +
"If this file exists, that means that the unit test is passed.\n";
ASSERT_TRUE(static_cast<bool>(ofTestUpload));
// Upload steam
ASSERT_TRUE(static_cast<bool>(ofTestUpload));
ASSERT_TRUE(m_pSFTPClient->UploadFile(ofTestUpload, SFTP_REMOTE_UPLOAD_FOLDER + "test_upload_stream.txt"));
std::cout << std::endl;
// Download the uploaded stream into a vector of bytes
{
std::vector<char> uploadedFileBytes;
EXPECT_TRUE(m_pSFTPClient->DownloadFile(SFTP_REMOTE_UPLOAD_FOLDER + "test_upload_stream.txt", uploadedFileBytes));
std::cout << std::endl;
/* check the SHA1 sum of the uploaded file */
std::string contentStr = ofTestUpload.str();
std::vector<char> contentBytes(contentStr.begin(), contentStr.end());
std::string expectedSha1Sum = sha1sum(contentBytes);
std::string resultSha1Sum = sha1sum(uploadedFileBytes);
EXPECT_TRUE(expectedSha1Sum == resultSha1Sum);
}
// Remove file
ASSERT_TRUE(m_pSFTPClient->RemoveFile(SFTP_REMOTE_UPLOAD_FOLDER + "test_upload_stream.txt"));
} else
std::cout << "SFTP tests are disabled !" << std::endl;
}
// TODO : this unit test can't be executed elsewhere
#if 0
TEST_F(SFTPClientTest, TestAppend /*AndRemoveFile*/) {
if (SFTP_TEST_ENABLED) {
// to display a beautiful progress bar on console
m_pSFTPClient->SetProgressFnCallback(m_pSFTPClient.get(), &TestUPProgressCallback);
// Upload file
ASSERT_TRUE(
m_pSFTPClient->UploadFile("C:\\Users\\mmzoughi\\Desktop\\test_upload_part1.txt", SFTP_REMOTE_UPLOAD_FOLDER + "test_append.txt"));
std::cout << std::endl;
ASSERT_TRUE(
m_pSFTPClient->AppendFile("C:\\Users\\mmzoughi\\Desktop\\test_upload.txt", 84, SFTP_REMOTE_UPLOAD_FOLDER + "test_append.txt"));
std::cout << std::endl;
// Download the uploaded file into a vector of bytes
{
std::vector<char> uploadedFileBytes;
EXPECT_TRUE(m_pSFTPClient->DownloadFile(SFTP_REMOTE_UPLOAD_FOLDER + "test_append.txt", uploadedFileBytes));
std::cout << std::endl;
/* check the SHA1 sum of the uploaded file */
std::string expectedSha1Sum = sha1sum("C:\\Users\\mmzoughi\\Desktop\\test_upload.txt");
std::string resultSha1Sum = sha1sum(uploadedFileBytes);
EXPECT_TRUE(expectedSha1Sum == resultSha1Sum);
}
// Remove file
// ASSERT_TRUE(m_pFTPClient->RemoveFile(FTP_REMOTE_UPLOAD_FOLDER + "test_upload.txt"));
// delete test file
//EXPECT_TRUE(remove("test_append.txt") == 0);
} else
std::cout << "FTP tests are disabled !" << std::endl;
}
#endif
TEST_F(SFTPClientTest, TestUploadAndRemoveFile10Times) {
if (SFTP_TEST_ENABLED) {
// to display a beautiful progress bar on console
m_pSFTPClient->SetProgressFnCallback(m_pSFTPClient.get(), &TestUPProgressCallback);
std::ostringstream ssTimestamp;
TimeStampTest(ssTimestamp);
// create dummy test file
std::ofstream ofTestUpload("test_upload.txt");
ASSERT_TRUE(static_cast<bool>(ofTestUpload));
ofTestUpload << "Unit Test TestUploadFile executed on " + ssTimestamp.str() + "\n" +
"This file is uploaded via FTPClient-C++ API.\n" +
"If this file exists, that means that the unit test is passed.\n";
ASSERT_TRUE(static_cast<bool>(ofTestUpload));
ofTestUpload.close();
for (unsigned i = 0; i < 10; ++i) {
// Upload file and create a directory "upload_test"
ASSERT_TRUE(m_pSFTPClient->UploadFile("test_upload.txt", SFTP_REMOTE_UPLOAD_FOLDER + "upload_test/test_upload.txt", true));
/* to properly show the progress bar */
std::cout << std::endl;
// Upload file
ASSERT_TRUE(m_pSFTPClient->UploadFile("test_upload.txt", SFTP_REMOTE_UPLOAD_FOLDER + "test_upload.txt"));
std::cout << std::endl;
// Download the uploaded file into a vector of bytes
{
std::vector<char> uploadedFileBytes;
EXPECT_TRUE(m_pSFTPClient->DownloadFile(SFTP_REMOTE_UPLOAD_FOLDER + "test_upload.txt", uploadedFileBytes));
std::cout << std::endl;
/* check the SHA1 sum of the uploaded file */
std::string expectedSha1Sum = sha1sum("test_upload.txt");
std::string resultSha1Sum = sha1sum(uploadedFileBytes);
EXPECT_TRUE(expectedSha1Sum == resultSha1Sum);
}
// Remove file
ASSERT_TRUE(m_pSFTPClient->RemoveFile(SFTP_REMOTE_UPLOAD_FOLDER + "test_upload.txt"));
}
// delete test file
EXPECT_TRUE(remove("test_upload.txt") == 0);
} else