Repository navigation
Expand file tree
/
Copy pathctest_runner.cpp
More file actions
901 lines (737 loc) · 30.4 KB
/
Copy pathctest_runner.cpp
File metadata and controls
901 lines (737 loc) · 30.4 KB
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
#include "database.hpp"
#include "error.hpp"
#include "file_binder.hpp"
#include "governance.hpp"
#include "link_service.hpp"
#include <cstdlib>
#include <algorithm>
#include <filesystem>
#include <chrono>
#include <fstream>
#include <iostream>
#include <set>
#include <system_error>
#include <string>
#include <vector>
#if defined(_WIN32)
#include <windows.h>
#endif
namespace {
using TestFunction = bool (*)();
struct TestCase {
const char* name;
TestFunction function;
};
bool Expect(bool condition, const char* message)
{
if (!condition) {
std::cerr << message << '\n';
}
return condition;
}
int ReadEnvInt(const char* name, int defaultValue)
{
const char* value = std::getenv(name);
if (value == nullptr || *value == '\0') {
return defaultValue;
}
try {
const int parsed = std::stoi(value);
return parsed > 0 ? parsed : defaultValue;
} catch (...) {
return defaultValue;
}
}
std::string WideToUtf8(const std::wstring& value)
{
#if defined(_WIN32)
if (value.empty()) {
return {};
}
const int requiredSize = WideCharToMultiByte(CP_UTF8, 0, value.c_str(), -1, nullptr, 0, nullptr, nullptr);
if (requiredSize <= 1) {
return {};
}
std::string result(static_cast<std::size_t>(requiredSize - 1), '\0');
WideCharToMultiByte(CP_UTF8, 0, value.c_str(), -1, result.data(), requiredSize, nullptr, nullptr);
return result;
#else
return std::string(value.begin(), value.end());
#endif
}
std::filesystem::path MakeUniqueTempDir(const std::string& name)
{
const auto base = std::filesystem::temp_directory_path() / ("dynamic_file_binder_" + name + "_" + std::to_string(static_cast<unsigned long long>(std::rand())));
std::error_code errorCode;
std::filesystem::remove_all(base, errorCode);
std::filesystem::create_directories(base, errorCode);
return base;
}
bool TestErrorManager()
{
using namespace dynamic_file_binder;
ErrorManager::Instance().Clear();
ErrorManager::Instance().Report({
ErrorDomain::Governance,
ErrorSeverity::Warning,
"ERR_TEST",
"test message",
"sample.txt",
});
const auto last = ErrorManager::Instance().Last();
if (!Expect(last.has_value(), "Expected an error record")) {
return false;
}
if (!Expect(last->code == "ERR_TEST", "Unexpected error code")) {
return false;
}
const std::string formatted = ErrorManager::Instance().Format(*last);
return Expect(formatted.find("test message") != std::string::npos, "Formatted error message missing payload");
}
bool TestGovernance()
{
using namespace dynamic_file_binder;
FileInfo file;
file.sourcePath = "video.mp4";
file.fileSize = 5ull * 1024ull * 1024ull;
GovernanceStack governance;
const GovernanceDecision deferDecision = governance.Evaluate(file, 90.0f, false);
if (!Expect(deferDecision.action == GovernanceAction::Defer, "Expected high-load defer decision")) {
return false;
}
const GovernanceDecision forceDecision = governance.Evaluate(file, 10.0f, true);
if (!Expect(forceDecision.action == GovernanceAction::BindPhysical, "Expected forced physical binding")) {
return false;
}
const GovernanceDecision symbolicDecision = governance.Evaluate(file, 10.0f, false);
return Expect(symbolicDecision.action == GovernanceAction::BindSymbolic, "Expected large file to prefer symbolic binding");
}
bool TestGovernanceRejectPhysicalDisabled()
{
using namespace dynamic_file_binder;
ErrorManager::Instance().Clear();
GovernanceRules rules;
rules.allowPhysicalCopies = false;
FileInfo file;
file.sourcePath = "doc.txt";
file.fileSize = 1024;
GovernanceStack governance(rules);
const GovernanceDecision decision = governance.Evaluate(file, 10.0f, true);
if (!Expect(decision.action == GovernanceAction::Reject, "Expected reject when physical copy is disabled and forcePhysical=true")) {
return false;
}
const auto last = ErrorManager::Instance().Last();
return Expect(last.has_value() && last->code == "GOVERNANCE_PHYSICAL_DISABLED", "Expected GOVERNANCE_PHYSICAL_DISABLED error");
}
bool TestDatabase()
{
using namespace dynamic_file_binder;
const std::filesystem::path tempDir = MakeUniqueTempDir("database");
const std::filesystem::path dbPath = tempDir / "usage.db";
Database database(dbPath);
DbFile file;
file.sourcePath = tempDir / "source.txt";
file.localPath = tempDir / "local.txt";
file.size = 12;
file.lastUsed = 42;
file.type = ".txt";
file.isSymbolicLink = false;
if (!Expect(database.UpsertFile(file), "Failed to insert database row")) {
std::filesystem::remove_all(tempDir);
return false;
}
const auto files = database.GetAllFiles();
if (!Expect(files.size() == 1, "Database row count mismatch")) {
std::filesystem::remove_all(tempDir);
return false;
}
if (!Expect(database.RemoveFile(file.localPath), "Failed to remove database row")) {
std::filesystem::remove_all(tempDir);
return false;
}
const auto emptyFiles = database.GetAllFiles();
std::filesystem::remove_all(tempDir);
return Expect(emptyFiles.empty(), "Database should be empty after remove");
}
bool TestFileBinderCrud()
{
using namespace dynamic_file_binder;
const std::filesystem::path tempDir = MakeUniqueTempDir("filebinder");
const std::filesystem::path sourceFile = tempDir / "source.txt";
{
std::ofstream output(sourceFile);
output << "alpha beta gamma";
}
FileBinder binder(tempDir / "workspace");
FileInfo info;
info.sourcePath = sourceFile;
info.localPath = std::filesystem::path("docs") / "sample.txt";
info.fileSize = static_cast<std::size_t>(std::filesystem::file_size(sourceFile));
info.fileType = ".txt";
info.lastUsed = 1;
if (!Expect(binder.BindFile(info, true), "Expected physical bind to succeed")) {
std::filesystem::remove_all(tempDir);
return false;
}
const std::filesystem::path firstTarget = tempDir / "workspace" / "docs" / "sample.txt";
if (!Expect(std::filesystem::exists(firstTarget), "Bound target file missing")) {
std::filesystem::remove_all(tempDir);
return false;
}
if (!Expect(binder.ListBindings().size() == 1, "Expected one binding after create")) {
std::filesystem::remove_all(tempDir);
return false;
}
if (!Expect(binder.MarkFileAsUsed(info.localPath), "Expected mark-as-used to succeed")) {
std::filesystem::remove_all(tempDir);
return false;
}
const std::filesystem::path newLocalPath = std::filesystem::path("docs") / "renamed.txt";
if (!Expect(binder.UpdateBinding(info.localPath, newLocalPath, true), "Expected update binding to succeed")) {
std::filesystem::remove_all(tempDir);
return false;
}
const std::filesystem::path secondTarget = tempDir / "workspace" / "docs" / "renamed.txt";
if (!Expect(std::filesystem::exists(secondTarget), "Updated target file missing")) {
std::filesystem::remove_all(tempDir);
return false;
}
if (!Expect(!std::filesystem::exists(firstTarget), "Old target file should be removed after update")) {
std::filesystem::remove_all(tempDir);
return false;
}
if (!Expect(binder.RemoveBinding(newLocalPath), "Expected binding removal to succeed")) {
std::filesystem::remove_all(tempDir);
return false;
}
const bool removed = !std::filesystem::exists(secondTarget) && binder.ListBindings().empty();
std::filesystem::remove_all(tempDir);
return Expect(removed, "Binding should be removed from disk and database");
}
bool TestLinkServiceHardLink()
{
using namespace dynamic_file_binder;
const std::filesystem::path tempDir = MakeUniqueTempDir("hardlink");
const std::filesystem::path sourceFile = tempDir / "source.txt";
const std::filesystem::path linkFile = tempDir / "linked.txt";
{
std::ofstream output(sourceFile);
output << "hard link content";
}
LinkService linkService;
const LinkResult result = linkService.create({sourceFile, linkFile, LinkKind::Hard});
if (!Expect(result.success, result.message.empty() ? "Hardlink creation failed" : "Hardlink creation failed")) {
std::filesystem::remove_all(tempDir);
return false;
}
const bool sameContent = std::filesystem::exists(sourceFile) && std::filesystem::exists(linkFile) && std::filesystem::file_size(sourceFile) == std::filesystem::file_size(linkFile);
const auto hardLinkCount = std::filesystem::hard_link_count(sourceFile);
std::filesystem::remove_all(tempDir);
return Expect(sameContent && hardLinkCount >= 2, "Hardlink should share inode and exist twice");
}
bool TestLinkServiceSymbolicLink()
{
using namespace dynamic_file_binder;
const std::filesystem::path tempDir = MakeUniqueTempDir("symlink");
const std::filesystem::path sourceFile = tempDir / "source.txt";
const std::filesystem::path linkFile = tempDir / "linked.txt";
{
std::ofstream output(sourceFile);
output << "symbolic link content";
}
LinkService linkService;
const LinkResult result = linkService.create({sourceFile, linkFile, LinkKind::Symbolic});
if (!result.success) {
#if defined(_WIN32)
if (result.error_code == ERROR_PRIVILEGE_NOT_HELD || result.error_code == ERROR_NOT_SUPPORTED) {
std::cout << "symlink: SKIP (not available in this environment)\n";
std::filesystem::remove_all(tempDir);
return true;
}
#endif
std::cerr << "Symlink creation failed: " << (result.message.empty() ? std::string("unknown") : WideToUtf8(result.message)) << '\n';
std::filesystem::remove_all(tempDir);
return false;
}
const bool linked = std::filesystem::exists(linkFile) && std::filesystem::is_symlink(linkFile);
const bool readable = std::filesystem::read_symlink(linkFile) == sourceFile;
std::filesystem::remove_all(tempDir);
return Expect(linked && readable, "Symbolic link should exist and point to source");
}
bool TestPhysicalCopyFlow()
{
using namespace dynamic_file_binder;
std::cout << "copyflow: setup" << std::endl;
const std::filesystem::path tempDir = MakeUniqueTempDir("copyflow");
const std::filesystem::path sourceFile = tempDir / "source.txt";
{
std::ofstream output(sourceFile);
output << "copy flow content";
}
std::cout << "copyflow: binder create" << std::endl;
FileBinder binder(tempDir / "workspace");
FileInfo info;
info.sourcePath = sourceFile;
info.localPath = std::filesystem::path("copies") / "sample.txt";
info.fileSize = static_cast<std::size_t>(std::filesystem::file_size(sourceFile));
info.fileType = ".txt";
info.lastUsed = 1;
std::cout << "copyflow: bind" << std::endl;
if (!Expect(binder.BindFile(info, true), "Expected physical copy bind to succeed")) {
std::filesystem::remove_all(tempDir);
return false;
}
const std::filesystem::path target = tempDir / "workspace" / "copies" / "sample.txt";
std::cout << "copyflow: check target" << std::endl;
if (!Expect(std::filesystem::exists(target), "Physical copy target missing")) {
std::filesystem::remove_all(tempDir);
return false;
}
std::cout << "copyflow: check symlink flag" << std::endl;
if (!Expect(!std::filesystem::is_symlink(target), "Physical copy must not be symlink")) {
std::filesystem::remove_all(tempDir);
return false;
}
std::cout << "copyflow: read content" << std::endl;
std::string content;
{
std::ifstream input(target);
std::getline(input, content);
}
std::cout << "copyflow: cleanup" << std::endl;
std::error_code errorCode;
std::filesystem::remove_all(tempDir, errorCode);
return Expect(content == "copy flow content", "Physical copy content mismatch");
}
bool TestBindFailurePreservesExistingTarget()
{
using namespace dynamic_file_binder;
ErrorManager::Instance().Clear();
const std::filesystem::path tempDir = MakeUniqueTempDir("bind_preserve");
const std::filesystem::path workspace = tempDir / "workspace";
const std::filesystem::path existingTarget = workspace / "docs" / "sample.txt";
std::error_code errorCode;
std::filesystem::create_directories(existingTarget.parent_path(), errorCode);
{
std::ofstream output(existingTarget);
output << "must-stay";
}
FileBinder binder(workspace);
FileInfo info;
info.sourcePath = tempDir / "missing_source.txt";
info.localPath = std::filesystem::path("docs") / "sample.txt";
info.fileSize = 100;
info.fileType = ".txt";
info.lastUsed = 5;
if (!Expect(!binder.BindFile(info, true), "Bind with missing source must fail")) {
std::filesystem::remove_all(tempDir, errorCode);
return false;
}
std::string content;
{
std::ifstream input(existingTarget);
std::getline(input, content);
}
const auto last = ErrorManager::Instance().Last();
const bool hasExpectedError = last.has_value() && last->code == "SOURCE_NOT_FOUND";
std::filesystem::remove_all(tempDir, errorCode);
return Expect(content == "must-stay" && hasExpectedError, "Target must remain unchanged when bind fails");
}
bool TestDatabaseVersionMismatch()
{
using namespace dynamic_file_binder;
ErrorManager::Instance().Clear();
const std::filesystem::path tempDir = MakeUniqueTempDir("db_version");
const std::filesystem::path dbPath = tempDir / "usage.db";
{
std::ofstream output(dbPath, std::ios::trunc);
output << "#DFBDB\t999\n";
output << "source\tlocal\t1\t1\t.txt\t0\n";
}
Database database(dbPath);
const auto files = database.GetAllFiles();
if (!Expect(files.empty(), "Version mismatch DB must not be loaded")) {
std::error_code errorCode;
std::filesystem::remove_all(tempDir, errorCode);
return false;
}
DbFile record;
record.sourcePath = "source";
record.localPath = "local";
record.size = 1;
record.lastUsed = 1;
record.type = ".txt";
record.isSymbolicLink = false;
if (!Expect(!database.UpsertFile(record), "Upsert must fail on DB version mismatch")) {
std::error_code errorCode;
std::filesystem::remove_all(tempDir, errorCode);
return false;
}
const auto last = ErrorManager::Instance().Last();
const bool hasMismatch = last.has_value() && last->code == "DB_VERSION_MISMATCH";
std::error_code errorCode;
std::filesystem::remove_all(tempDir, errorCode);
return Expect(hasMismatch, "Expected DB_VERSION_MISMATCH error record");
}
bool TestRevertDetectsVersionMismatch()
{
using namespace dynamic_file_binder;
ErrorManager::Instance().Clear();
const std::filesystem::path tempDir = MakeUniqueTempDir("revert_mismatch");
const std::filesystem::path sourceFile = tempDir / "source.txt";
const std::filesystem::path workspace = tempDir / "workspace";
const std::filesystem::path localFile = workspace / "docs" / "local.txt";
std::error_code errorCode;
std::filesystem::create_directories(localFile.parent_path(), errorCode);
{
std::ofstream sourceOut(sourceFile, std::ios::trunc);
sourceOut << "new-source-content";
}
{
std::ofstream localOut(localFile, std::ios::trunc);
localOut << "old-managed-copy";
}
FileBinder binder(workspace);
Database db(binder.GetDatabasePath());
DbFile dbFile;
dbFile.sourcePath = sourceFile;
dbFile.localPath = localFile;
dbFile.size = static_cast<std::size_t>(std::filesystem::file_size(localFile));
dbFile.lastUsed = 1;
dbFile.type = ".txt";
dbFile.isSymbolicLink = false;
if (!Expect(db.UpsertFile(dbFile), "Failed to setup DB record for mismatch test")) {
std::filesystem::remove_all(tempDir, errorCode);
return false;
}
if (!Expect(!binder.RevertConnection(), "Revert must fail on source/local mismatch")) {
std::filesystem::remove_all(tempDir, errorCode);
return false;
}
std::string sourceContent;
{
std::ifstream sourceIn(sourceFile);
std::getline(sourceIn, sourceContent);
}
const auto last = ErrorManager::Instance().Last();
const bool hasMismatch = last.has_value() && last->code == "VERSION_MISMATCH";
std::filesystem::remove_all(tempDir, errorCode);
return Expect(sourceContent == "new-source-content" && hasMismatch, "Source must not be overwritten on mismatch");
}
bool TestTwoFolderSyncLifecycle()
{
using namespace dynamic_file_binder;
ErrorManager::Instance().Clear();
const std::filesystem::path tempRoot = MakeUniqueTempDir("sync_lifecycle");
const std::filesystem::path sourceRepo = tempRoot / "source_repo";
const std::filesystem::path workspace = tempRoot / "managed_workspace";
std::error_code errorCode;
std::filesystem::create_directories(sourceRepo, errorCode);
std::filesystem::create_directories(workspace, errorCode);
const std::filesystem::path sourceA = sourceRepo / "a.txt";
const std::filesystem::path sourceB = sourceRepo / "b.txt";
{
std::ofstream outA(sourceA, std::ios::trunc);
outA << "alpha";
}
{
std::ofstream outB(sourceB, std::ios::trunc);
outB << "beta";
}
FileBinder binder(workspace);
FileInfo fileA;
fileA.sourcePath = sourceA;
fileA.localPath = std::filesystem::path("sync") / "a_local.txt";
fileA.fileSize = static_cast<std::size_t>(std::filesystem::file_size(sourceA));
fileA.fileType = ".txt";
fileA.lastUsed = 1;
FileInfo fileB;
fileB.sourcePath = sourceB;
fileB.localPath = std::filesystem::path("sync") / "b_local.txt";
fileB.fileSize = static_cast<std::size_t>(std::filesystem::file_size(sourceB));
fileB.fileType = ".txt";
fileB.lastUsed = 1;
if (!Expect(binder.BindFile(fileA, true), "Bind A failed")) {
std::filesystem::remove_all(tempRoot, errorCode);
return false;
}
if (!Expect(binder.BindFile(fileB, true), "Bind B failed")) {
std::filesystem::remove_all(tempRoot, errorCode);
return false;
}
const std::filesystem::path localA = workspace / "sync" / "a_local.txt";
const std::filesystem::path localB = workspace / "sync" / "b_local.txt";
if (!Expect(std::filesystem::exists(localA) && std::filesystem::exists(localB), "Local files missing after bind")) {
std::filesystem::remove_all(tempRoot, errorCode);
return false;
}
const auto bindingsAfterCreate = binder.ListBindings();
if (!Expect(bindingsAfterCreate.size() == 2, "Expected two bindings after create")) {
std::filesystem::remove_all(tempRoot, errorCode);
return false;
}
if (!Expect(binder.MarkFileAsUsed(fileA.localPath), "MarkFileAsUsed for A failed")) {
std::filesystem::remove_all(tempRoot, errorCode);
return false;
}
const std::filesystem::path renamedA = std::filesystem::path("sync") / "a_renamed.txt";
if (!Expect(binder.UpdateBinding(fileA.localPath, renamedA, true), "UpdateBinding for A failed")) {
std::filesystem::remove_all(tempRoot, errorCode);
return false;
}
const std::filesystem::path localARenamed = workspace / "sync" / "a_renamed.txt";
if (!Expect(std::filesystem::exists(localARenamed) && !std::filesystem::exists(localA), "Rename state invalid")) {
std::filesystem::remove_all(tempRoot, errorCode);
return false;
}
binder.OptimizeStorage(1024);
if (!Expect(binder.RemoveBinding(fileB.localPath), "RemoveBinding for B failed")) {
std::filesystem::remove_all(tempRoot, errorCode);
return false;
}
if (!Expect(!std::filesystem::exists(localB), "Local B still exists after remove")) {
std::filesystem::remove_all(tempRoot, errorCode);
return false;
}
const auto bindingsAfterRemove = binder.ListBindings();
if (!Expect(bindingsAfterRemove.size() == 1, "Expected one binding after remove")) {
std::filesystem::remove_all(tempRoot, errorCode);
return false;
}
if (!Expect(binder.Cleanup(), "Cleanup should succeed")) {
std::filesystem::remove_all(tempRoot, errorCode);
return false;
}
if (!Expect(!std::filesystem::exists(localARenamed), "Local renamed file should be removed after cleanup")) {
std::filesystem::remove_all(tempRoot, errorCode);
return false;
}
std::string sourceAContent;
{
std::ifstream inA(sourceA);
std::getline(inA, sourceAContent);
}
const bool sourceStillValid = sourceAContent == "alpha" && std::filesystem::exists(sourceB);
std::filesystem::remove_all(tempRoot, errorCode);
return Expect(sourceStillValid, "Source repository content must stay consistent after full lifecycle");
}
bool TestStateStormErrorMatrix()
{
using namespace dynamic_file_binder;
ErrorManager::Instance().Clear();
const std::filesystem::path tempRoot = MakeUniqueTempDir("state_storm");
const std::filesystem::path sourceRepo = tempRoot / "source_repo";
const std::filesystem::path workspace = tempRoot / "workspace";
std::error_code errorCode;
std::filesystem::create_directories(sourceRepo, errorCode);
std::filesystem::create_directories(workspace, errorCode);
const std::filesystem::path sourceFile = sourceRepo / "ok.txt";
{
std::ofstream out(sourceFile, std::ios::trunc);
out << "ok-content";
}
FileBinder binder(workspace);
FileInfo ok;
ok.sourcePath = sourceFile;
ok.localPath = std::filesystem::path("docs") / "ok.txt";
ok.fileSize = static_cast<std::size_t>(std::filesystem::file_size(sourceFile));
ok.fileType = ".txt";
ok.lastUsed = 1;
if (!Expect(binder.BindFile(ok, true), "Initial bind for state-storm setup failed")) {
std::filesystem::remove_all(tempRoot, errorCode);
return false;
}
if (!Expect(binder.RemoveBinding(ok.localPath), "First remove should succeed")) {
std::filesystem::remove_all(tempRoot, errorCode);
return false;
}
if (!Expect(!binder.RemoveBinding(ok.localPath), "Second remove should fail")) {
std::filesystem::remove_all(tempRoot, errorCode);
return false;
}
if (!Expect(!binder.UpdateBinding(ok.localPath, std::filesystem::path("docs") / "missing.txt", true), "Update on missing binding should fail")) {
std::filesystem::remove_all(tempRoot, errorCode);
return false;
}
FileInfo missing;
missing.sourcePath = sourceRepo / "missing_source.txt";
missing.localPath = std::filesystem::path("docs") / "missing_source.txt";
missing.fileSize = 123;
missing.fileType = ".txt";
missing.lastUsed = 1;
if (!Expect(!binder.BindFile(missing, true), "Bind with missing source should fail")) {
std::filesystem::remove_all(tempRoot, errorCode);
return false;
}
const std::filesystem::path sourceMismatch = sourceRepo / "source_mismatch.txt";
const std::filesystem::path localMismatch = workspace / "docs" / "local_mismatch.txt";
std::filesystem::create_directories(localMismatch.parent_path(), errorCode);
{
std::ofstream out(sourceMismatch, std::ios::trunc);
out << "source-new";
}
{
std::ofstream out(localMismatch, std::ios::trunc);
out << "local-old";
}
Database db(binder.GetDatabasePath());
DbFile mismatchRecord;
mismatchRecord.sourcePath = sourceMismatch;
mismatchRecord.localPath = localMismatch;
mismatchRecord.size = static_cast<std::size_t>(std::filesystem::file_size(localMismatch));
mismatchRecord.lastUsed = 7;
mismatchRecord.type = ".txt";
mismatchRecord.isSymbolicLink = false;
mismatchRecord.contentHash = "deadbeef";
if (!Expect(db.UpsertFile(mismatchRecord), "Failed to insert mismatch record")) {
std::filesystem::remove_all(tempRoot, errorCode);
return false;
}
if (!Expect(!binder.RevertConnection(), "Revert should fail in state-storm mismatch phase")) {
std::filesystem::remove_all(tempRoot, errorCode);
return false;
}
const std::vector<ErrorRecord> errors = ErrorManager::Instance().Snapshot();
std::set<std::string> codes;
for (const auto& error : errors) {
codes.insert(error.code);
}
const bool hasBindingNotFound = codes.find("BINDING_NOT_FOUND") != codes.end();
const bool hasSourceNotFound = codes.find("SOURCE_NOT_FOUND") != codes.end();
const bool hasHashMismatch = codes.find("MANAGED_COPY_HASH_MISMATCH") != codes.end();
std::filesystem::remove_all(tempRoot, errorCode);
return Expect(hasBindingNotFound && hasSourceNotFound && hasHashMismatch, "State-storm must cover binding/source/hash mismatch errors");
}
bool TestLongRunMultiFolderSoak()
{
using namespace dynamic_file_binder;
ErrorManager::Instance().Clear();
const int runSeconds = ReadEnvInt("DFB_LONGRUN_SECONDS", 5);
const auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds(runSeconds);
const std::filesystem::path tempRoot = MakeUniqueTempDir("longrun_multifolder");
const std::filesystem::path sourceRoot = tempRoot / "source_repo";
const std::filesystem::path workspace = tempRoot / "managed_workspace";
std::error_code errorCode;
std::filesystem::create_directories(sourceRoot, errorCode);
std::filesystem::create_directories(workspace, errorCode);
struct TrackedFile {
std::filesystem::path source;
std::filesystem::path local;
};
const std::vector<std::string> folderNames = {"folder A", "folder B", "folder C"};
std::vector<TrackedFile> tracked;
tracked.reserve(folderNames.size() * 2);
for (const std::string& folderName : folderNames) {
const std::filesystem::path sourceFolder = sourceRoot / folderName;
std::filesystem::create_directories(sourceFolder, errorCode);
for (int i = 0; i < 2; ++i) {
const std::filesystem::path sourceFile = sourceFolder / ("seed_" + std::to_string(i) + ".txt");
std::ofstream out(sourceFile, std::ios::trunc);
out << "seed:" << folderName << ':' << i << '\n';
const std::filesystem::path localPath = std::filesystem::path("sync") / folderName / ("seed_" + std::to_string(i) + ".txt");
tracked.push_back({sourceFile, localPath});
}
}
FileBinder binder(workspace);
for (const auto& entry : tracked) {
FileInfo info;
info.sourcePath = entry.source;
info.localPath = entry.local;
info.fileSize = static_cast<std::size_t>(std::filesystem::file_size(entry.source));
info.fileType = ".txt";
info.lastUsed = std::time(nullptr);
if (!Expect(binder.BindFile(info, true), "Longrun: initial bind failed")) {
std::filesystem::remove_all(tempRoot, errorCode);
return false;
}
}
int operations = 0;
std::size_t cursor = 0;
while (std::chrono::steady_clock::now() < deadline) {
TrackedFile& entry = tracked[cursor % tracked.size()];
++cursor;
{
std::ofstream out(entry.source, std::ios::app);
out << "tick:" << operations << '\n';
}
if (!Expect(binder.MarkFileAsUsed(entry.local), "Longrun: mark-as-used failed")) {
std::filesystem::remove_all(tempRoot, errorCode);
return false;
}
if ((operations % 7) == 0) {
const std::filesystem::path renamed = entry.local.parent_path() /
(entry.local.stem().string() + "_r" + std::to_string(operations) + entry.local.extension().string());
if (!Expect(binder.UpdateBinding(entry.local, renamed, true), "Longrun: update-binding failed")) {
std::filesystem::remove_all(tempRoot, errorCode);
return false;
}
entry.local = renamed;
}
if ((operations % 11) == 0) {
if (!Expect(binder.RemoveBinding(entry.local), "Longrun: remove-binding failed")) {
std::filesystem::remove_all(tempRoot, errorCode);
return false;
}
FileInfo rebinding;
rebinding.sourcePath = entry.source;
rebinding.localPath = entry.local;
rebinding.fileSize = static_cast<std::size_t>(std::filesystem::file_size(entry.source));
rebinding.fileType = ".txt";
rebinding.lastUsed = std::time(nullptr);
if (!Expect(binder.BindFile(rebinding, true), "Longrun: rebind failed")) {
std::filesystem::remove_all(tempRoot, errorCode);
return false;
}
}
if ((operations % 13) == 0) {
binder.OptimizeStorage(128ull * 1024ull * 1024ull);
}
++operations;
}
const auto bindings = binder.ListBindings();
const bool countOk = bindings.size() == tracked.size();
const bool opOk = operations >= 20;
std::filesystem::remove_all(tempRoot, errorCode);
return Expect(countOk && opOk, "Longrun: unexpected final binding count or too few operations");
}
const TestCase kTests[] = {
{"error", TestErrorManager},
{"governance", TestGovernance},
{"governance_reject_physical", TestGovernanceRejectPhysicalDisabled},
{"database", TestDatabase},
{"filebinder", TestFileBinderCrud},
{"hardlink", TestLinkServiceHardLink},
{"symlink", TestLinkServiceSymbolicLink},
{"copyflow", TestPhysicalCopyFlow},
{"bind_preserve", TestBindFailurePreservesExistingTarget},
{"db_version", TestDatabaseVersionMismatch},
{"revert_mismatch", TestRevertDetectsVersionMismatch},
{"sync_lifecycle", TestTwoFolderSyncLifecycle},
{"state_storm", TestStateStormErrorMatrix},
{"longrun", TestLongRunMultiFolderSoak},
};
} // namespace
int main(int argc, char* argv[])
{
std::cout.setf(std::ios::unitbuf);
std::cerr.setf(std::ios::unitbuf);
std::srand(1337);
if (argc < 2) {
std::cerr << "Usage: ctest_runner <test-name|all>\n";
return 1;
}
const std::string requested = argv[1];
bool allPassed = true;
for (const TestCase& testCase : kTests) {
if (requested != "all" && requested != testCase.name) {
continue;
}
const bool passed = testCase.function();
std::cout << testCase.name << ": " << (passed ? "PASS" : "FAIL") << '\n';
allPassed = allPassed && passed;
if (!passed && requested != "all") {
return 1;
}
}
return allPassed ? 0 : 1;
}