-
Notifications
You must be signed in to change notification settings - Fork 17
/
testbackupstore.cpp
3342 lines (2845 loc) · 111 KB
/
testbackupstore.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
// --------------------------------------------------------------------------
//
// File
// Name: testbackupstore.cpp
// Purpose: Test backup store server
// Created: 2003/08/20
//
// --------------------------------------------------------------------------
#include "Box.h"
#include <stdlib.h>
#include <string.h>
#include "Archive.h"
#include "BackupClientCryptoKeys.h"
#include "BackupClientFileAttributes.h"
#include "BackupProtocol.h"
#include "BackupStoreAccountDatabase.h"
#include "BackupStoreAccounts.h"
#include "BackupStoreConfigVerify.h"
#include "BackupStoreConstants.h"
#include "BackupStoreDirectory.h"
#include "BackupStoreException.h"
#include "BackupStoreFile.h"
#include "BackupStoreFilenameClear.h"
#include "BackupStoreFileEncodeStream.h"
#include "BackupStoreInfo.h"
#include "BackupStoreObjectMagic.h"
#include "BackupStoreRefCountDatabase.h"
#include "BoxPortsAndFiles.h"
#include "CollectInBufferStream.h"
#include "Configuration.h"
#include "FileStream.h"
#include "HousekeepStoreAccount.h"
#include "MemBlockStream.h"
#include "RaidFileController.h"
#include "RaidFileException.h"
#include "RaidFileRead.h"
#include "RaidFileWrite.h"
#include "SSLLib.h"
#include "ServerControl.h"
#include "Socket.h"
#include "SocketStreamTLS.h"
#include "StoreStructure.h"
#include "StoreTestUtils.h"
#include "TLSContext.h"
#include "Test.h"
#include "ZeroStream.h"
#include "MemLeakFindOn.h"
#define ENCFILE_SIZE 2765
// Make some test attributes
#define ATTR1_SIZE 245
#define ATTR2_SIZE 23
#define ATTR3_SIZE 122
#define SHORT_TIMEOUT 5000
int attr1[ATTR1_SIZE];
int attr2[ATTR2_SIZE];
int attr3[ATTR3_SIZE];
typedef struct
{
BackupStoreFilenameClear fn;
box_time_t mod;
int64_t id;
int64_t size;
int16_t flags;
box_time_t attrmod;
} dirtest;
static dirtest ens[] =
{
{BackupStoreFilenameClear(), 324324, 3432, 324, BackupStoreDirectory::Entry::Flags_File, 458763243422LL},
{BackupStoreFilenameClear(), 3432, 32443245645LL, 78, BackupStoreDirectory::Entry::Flags_Dir, 3248972347LL},
{BackupStoreFilenameClear(), 544435, 234234, 23324, BackupStoreDirectory::Entry::Flags_File | BackupStoreDirectory::Entry::Flags_Deleted, 2348974782LL},
{BackupStoreFilenameClear(), 234, 235436, 6523, BackupStoreDirectory::Entry::Flags_File, 32458923175634LL},
{BackupStoreFilenameClear(), 0x3242343532144LL, 8978979789LL, 21345, BackupStoreDirectory::Entry::Flags_File, 329483243432LL},
{BackupStoreFilenameClear(), 324265765734LL, 12312312321LL, 324987324329874LL, BackupStoreDirectory::Entry::Flags_File | BackupStoreDirectory::Entry::Flags_Deleted, 32489747234LL},
{BackupStoreFilenameClear(), 3452134, 7868578768LL, 324243, BackupStoreDirectory::Entry::Flags_Dir, 34786457432LL},
{BackupStoreFilenameClear(), 43543543, 324234, 21432, BackupStoreDirectory::Entry::Flags_Dir | BackupStoreDirectory::Entry::Flags_Deleted, 3489723478327LL},
{BackupStoreFilenameClear(), 325654765874324LL, 4353543, 1, BackupStoreDirectory::Entry::Flags_File | BackupStoreDirectory::Entry::Flags_OldVersion, 32489734789237LL},
{BackupStoreFilenameClear(), 32144325, 436547657, 9, BackupStoreDirectory::Entry::Flags_File | BackupStoreDirectory::Entry::Flags_OldVersion, 234897347234LL}
};
static const char *ens_filenames[] = {"obj1ertewt", "obj2", "obj3", "obj4dfedfg43", "obj5", "obj6dfgs", "obj7", "obj8xcvbcx", "obj9", "obj10fgjhfg"};
#define DIR_NUM 10
#define DIR_DIRS 3
#define DIR_FILES 7
#define DIR_OLD 2
#define DIR_DELETED 3
typedef struct
{
const char *fnextra;
BackupStoreFilenameClear name;
int seed;
int size;
box_time_t mod_time;
int64_t allocated_objid;
bool should_be_old_version;
bool delete_file;
} uploadtest;
#define TEST_FILE_FOR_PATCHING "testfiles/test2"
// a few bytes will be inserted at this point:
#define TEST_FILE_FOR_PATCHING_PATCH_AT ((64*1024)-128)
#define TEST_FILE_FOR_PATCHING_SIZE ((128*1024)+2564)
#define UPLOAD_PATCH_EN 2
uploadtest uploads[] =
{
{"0", BackupStoreFilenameClear(), 324, 455, 0, 0, false, false},
{"1", BackupStoreFilenameClear(), 3232432, 2674, 0, 0, true, false}, // old ver
{"2", BackupStoreFilenameClear(), 234, TEST_FILE_FOR_PATCHING_SIZE, 0, 0, false, false},
{"3", BackupStoreFilenameClear(), 324324, 6763, 0, 0, false, false},
{"4", BackupStoreFilenameClear(), 23456, 124, 0, 0, true, false}, // old ver
{"5", BackupStoreFilenameClear(), 675745, 1, 0, 0, false, false}, // will upload new attrs for this one!
{"6", BackupStoreFilenameClear(), 345213, 0, 0, 0, false, false},
{"7", BackupStoreFilenameClear(), 12313, 3246, 0, 0, true, true}, // old ver, will get deleted
{"8", BackupStoreFilenameClear(), 457, 3434, 0, 0, false, false}, // overwrites
{"9", BackupStoreFilenameClear(), 12315, 446, 0, 0, false, false},
{"a", BackupStoreFilenameClear(), 3476, 2466, 0, 0, false, false},
{"b", BackupStoreFilenameClear(), 124334, 4562, 0, 0, false, false},
{"c", BackupStoreFilenameClear(), 45778, 234, 0, 0, false, false}, // overwrites
{"d", BackupStoreFilenameClear(), 2423425, 435, 0, 0, false, true} // overwrites, will be deleted
};
static const char *uploads_filenames[] = {"49587fds", "cvhjhj324", "sdfcscs324", "dsfdsvsdc3214", "XXsfdsdf2342", "dsfdsc232",
"sfdsdce2345", "YYstfbdtrdf76", "cvhjhj324", "fbfd098.ycy", "dfs98732hj", "svd987kjsad", "XXsfdsdf2342", "YYstfbdtrdf76"};
#define UPLOAD_NUM 14
#define UPLOAD_LATEST_FILES 12
// file we'll upload some new attributes for
#define UPLOAD_ATTRS_EN 5
#define UPLOAD_DELETE_EN 13
// file which will be moved (as well as it's old version)
#define UPLOAD_FILE_TO_MOVE 8
#define UNLINK_IF_EXISTS(filename) \
if (FileExists(filename)) { TEST_THAT(EMU_UNLINK(filename) == 0); }
//! Simplifies calling setUp() with the current function name in each test.
#define SETUP_TEST_BACKUPSTORE() \
SETUP(); \
if (ServerIsAlive(bbstored_pid)) \
TEST_THAT_OR(StopServer(), FAIL); \
ExpectedRefCounts.resize(BACKUPSTORE_ROOT_DIRECTORY_ID + 1); \
set_refcount(BACKUPSTORE_ROOT_DIRECTORY_ID, 1); \
TEST_THAT_OR(create_account(10000, 20000), FAIL);
//! Checks account for errors and shuts down daemons at end of every test.
bool teardown_test_backupstore()
{
bool status = true;
if (FileExists("testfiles/0_0/backup/01234567/info.rf"))
{
TEST_THAT_OR(check_reference_counts(), status = false);
TEST_THAT_OR(check_account(), status = false);
}
return status;
}
#define TEARDOWN_TEST_BACKUPSTORE() \
if (ServerIsAlive(bbstored_pid)) \
StopServer(); \
TEST_THAT(teardown_test_backupstore()); \
TEARDOWN();
// Nice random data for testing written files
class R250 {
public:
// Set up internal state table with 32-bit random numbers.
// The bizarre bit-twiddling is because rand() returns 16 bits of which
// the bottom bit is always zero! Hence, I use only some of the bits.
// You might want to do something better than this....
R250(int seed) : posn1(0), posn2(103)
{
// populate the state and incr tables
srand(seed);
for (int i = 0; i != stateLen; ++i) {
state[i] = ((rand() >> 2) << 19) ^ ((rand() >> 2) << 11) ^ (rand() >> 2);
incrTable[i] = i == stateLen - 1 ? 0 : i + 1;
}
// stir up the numbers to ensure they're random
for (int j = 0; j != stateLen * 4; ++j)
(void) next();
}
// Returns the next random number. Xor together two elements separated
// by 103 mod 250, replacing the first element with the result. Then
// increment the two indices mod 250.
inline int next()
{
int ret = (state[posn1] ^= state[posn2]); // xor and replace element
posn1 = incrTable[posn1]; // increment indices using lookup table
posn2 = incrTable[posn2];
return ret;
}
private:
enum { stateLen = 250 }; // length of the state table
int state[stateLen]; // holds the random number state
int incrTable[stateLen]; // lookup table: maps i to (i+1) % stateLen
int posn1, posn2; // indices into the state table
};
int SkipEntries(int e, int16_t FlagsMustBeSet, int16_t FlagsNotToBeSet)
{
if(e >= DIR_NUM) return e;
bool skip = false;
do
{
skip = false;
if(FlagsMustBeSet != BackupStoreDirectory::Entry::Flags_INCLUDE_EVERYTHING)
{
if((ens[e].flags & FlagsMustBeSet) != FlagsMustBeSet)
{
skip = true;
}
}
if((ens[e].flags & FlagsNotToBeSet) != 0)
{
skip = true;
}
if(skip)
{
++e;
}
} while(skip && e < DIR_NUM);
return e;
}
void CheckEntries(BackupStoreDirectory &rDir, int16_t FlagsMustBeSet, int16_t FlagsNotToBeSet)
{
int e = 0;
BackupStoreDirectory::Iterator i(rDir);
BackupStoreDirectory::Entry *en = 0;
while((en = i.Next()) != 0)
{
TEST_THAT(e < DIR_NUM);
// Skip to entry in the ens array which matches
e = SkipEntries(e, FlagsMustBeSet, FlagsNotToBeSet);
// Does it match?
TEST_THAT(en->GetName() == ens[e].fn && en->GetModificationTime() == ens[e].mod && en->GetObjectID() == ens[e].id && en->GetFlags() == ens[e].flags && en->GetSizeInBlocks() == ens[e].size);
// next
++e;
}
// Got them all?
TEST_THAT(en == 0);
TEST_THAT(DIR_NUM == SkipEntries(e, FlagsMustBeSet, FlagsNotToBeSet));
}
bool test_filename_encoding()
{
SETUP_TEST_BACKUPSTORE();
// test some basics -- encoding and decoding filenames
{
// Make some filenames in various ways
BackupStoreFilenameClear fn1;
fn1.SetClearFilename(std::string("filenameXYZ"));
BackupStoreFilenameClear fn2(std::string("filenameXYZ"));
BackupStoreFilenameClear fn3(fn1);
TEST_THAT(fn1 == fn2);
TEST_THAT(fn1 == fn3);
// Check that it's been encrypted
std::string name(fn2.GetEncodedFilename());
TEST_THAT(name.find("name") == name.npos);
// Bung it in a stream, get it out in a Clear filename
{
CollectInBufferStream stream;
fn1.WriteToStream(stream);
stream.SetForReading();
BackupStoreFilenameClear fn4;
fn4.ReadFromStream(stream, IOStream::TimeOutInfinite);
TEST_THAT(fn4.GetClearFilename() == "filenameXYZ");
TEST_THAT(fn4 == fn1);
}
// Bung it in a stream, get it out in a server non-Clear filename (two of them into the same var)
{
BackupStoreFilenameClear fno("pinglet dksfnsf jksjdf ");
CollectInBufferStream stream;
fn1.WriteToStream(stream);
fno.WriteToStream(stream);
stream.SetForReading();
BackupStoreFilename fn5;
fn5.ReadFromStream(stream, IOStream::TimeOutInfinite);
TEST_THAT(fn5 == fn1);
fn5.ReadFromStream(stream, IOStream::TimeOutInfinite);
TEST_THAT(fn5 == fno);
}
// Same again with clear strings
{
BackupStoreFilenameClear fno("pinglet dksfnsf jksjdf ");
CollectInBufferStream stream;
fn1.WriteToStream(stream);
fno.WriteToStream(stream);
stream.SetForReading();
BackupStoreFilenameClear fn5;
fn5.ReadFromStream(stream, IOStream::TimeOutInfinite);
TEST_THAT(fn5.GetClearFilename() == "filenameXYZ");
fn5.ReadFromStream(stream, IOStream::TimeOutInfinite);
TEST_THAT(fn5.GetClearFilename() == "pinglet dksfnsf jksjdf ");
}
// Test a very big filename
{
const char *fnr = "01234567890123456789012345678901234567890123456789"
"01234567890123456789012345678901234567890123456789"
"01234567890123456789012345678901234567890123456789"
"01234567890123456789012345678901234567890123456789"
"01234567890123456789012345678901234567890123456789"
"01234567890123456789012345678901234567890123456789"
"01234567890123456789012345678901234567890123456789"
"01234567890123456789012345678901234567890123456789";
BackupStoreFilenameClear fnLong(fnr);
CollectInBufferStream stream;
fnLong.WriteToStream(stream);
stream.SetForReading();
BackupStoreFilenameClear fn9;
fn9.ReadFromStream(stream, IOStream::TimeOutInfinite);
TEST_THAT(fn9.GetClearFilename() == fnr);
TEST_THAT(fn9 == fnLong);
}
// Test a filename which went wrong once
{
BackupStoreFilenameClear dodgy("content-negotiation.html");
}
}
TEARDOWN_TEST_BACKUPSTORE();
}
bool test_backupstore_directory()
{
SETUP_TEST_BACKUPSTORE();
{
// Now play with directories
// Fill in...
BackupStoreDirectory dir1(12, 98);
for(int e = 0; e < DIR_NUM; ++e)
{
dir1.AddEntry(ens[e].fn, ens[e].mod, ens[e].id, ens[e].size, ens[e].flags, ens[e].attrmod);
}
// Got the right number
TEST_THAT(dir1.GetNumberOfEntries() == DIR_NUM);
// Stick it into a stream and get it out again
{
CollectInBufferStream stream;
dir1.WriteToStream(stream);
stream.SetForReading();
BackupStoreDirectory dir2(stream);
TEST_THAT(dir2.GetNumberOfEntries() == DIR_NUM);
TEST_THAT(dir2.GetObjectID() == 12);
TEST_THAT(dir2.GetContainerID() == 98);
CheckEntries(dir2, BackupStoreDirectory::Entry::Flags_INCLUDE_EVERYTHING, BackupStoreDirectory::Entry::Flags_EXCLUDE_NOTHING);
}
// Then do selective writes and reads
{
CollectInBufferStream stream;
dir1.WriteToStream(stream, BackupStoreDirectory::Entry::Flags_File);
stream.SetForReading();
BackupStoreDirectory dir2(stream);
TEST_THAT(dir2.GetNumberOfEntries() == DIR_FILES);
CheckEntries(dir2, BackupStoreDirectory::Entry::Flags_File, BackupStoreDirectory::Entry::Flags_EXCLUDE_NOTHING);
}
{
CollectInBufferStream stream;
dir1.WriteToStream(stream, BackupStoreDirectory::Entry::Flags_INCLUDE_EVERYTHING, BackupStoreDirectory::Entry::Flags_File);
stream.SetForReading();
BackupStoreDirectory dir2(stream);
TEST_THAT(dir2.GetNumberOfEntries() == DIR_DIRS);
CheckEntries(dir2, BackupStoreDirectory::Entry::Flags_Dir, BackupStoreDirectory::Entry::Flags_EXCLUDE_NOTHING);
}
{
CollectInBufferStream stream;
dir1.WriteToStream(stream, BackupStoreDirectory::Entry::Flags_File, BackupStoreDirectory::Entry::Flags_OldVersion);
stream.SetForReading();
BackupStoreDirectory dir2(stream);
TEST_THAT(dir2.GetNumberOfEntries() == DIR_FILES - DIR_OLD);
CheckEntries(dir2, BackupStoreDirectory::Entry::Flags_File, BackupStoreDirectory::Entry::Flags_OldVersion);
}
// Finally test deleting items
{
dir1.DeleteEntry(12312312321LL);
// Verify
TEST_THAT(dir1.GetNumberOfEntries() == DIR_NUM - 1);
CollectInBufferStream stream;
dir1.WriteToStream(stream, BackupStoreDirectory::Entry::Flags_File);
stream.SetForReading();
BackupStoreDirectory dir2(stream);
TEST_THAT(dir2.GetNumberOfEntries() == DIR_FILES - 1);
}
// Check attributes
{
int attrI[4] = {1, 2, 3, 4};
StreamableMemBlock attr(attrI, sizeof(attrI));
BackupStoreDirectory d1(16, 546);
d1.SetAttributes(attr, 56234987324232LL);
TEST_THAT(d1.GetAttributes() == attr);
TEST_THAT(d1.GetAttributesModTime() == 56234987324232LL);
CollectInBufferStream stream;
d1.WriteToStream(stream);
stream.SetForReading();
BackupStoreDirectory d2(stream);
TEST_THAT(d2.GetAttributes() == attr);
TEST_THAT(d2.GetAttributesModTime() == 56234987324232LL);
}
}
TEARDOWN_TEST_BACKUPSTORE();
}
void write_test_file(int t)
{
std::string filename("testfiles/test");
filename += uploads[t].fnextra;
BOX_TRACE("Writing " << filename);
FileStream write(filename.c_str(), O_WRONLY | O_CREAT);
R250 r(uploads[t].seed);
unsigned char *data = (unsigned char*)malloc(uploads[t].size);
for(int l = 0; l < uploads[t].size; ++l)
{
data[l] = r.next() & 0xff;
}
write.Write(data, uploads[t].size);
free(data);
}
void test_test_file(int t, IOStream &rStream)
{
// Decode to a file
BackupStoreFile::DecodeFile(rStream, "testfiles/test_download", SHORT_TIMEOUT);
// Compare...
FileStream in("testfiles/test_download");
TEST_THAT(in.BytesLeftToRead() == uploads[t].size);
R250 r(uploads[t].seed);
unsigned char *data = (unsigned char*)malloc(uploads[t].size);
TEST_THAT(in.ReadFullBuffer(data, uploads[t].size, 0 /* not interested in bytes read if this fails */));
for(int l = 0; l < uploads[t].size; ++l)
{
TEST_THAT(data[l] == (r.next() & 0xff));
}
free(data);
in.Close();
TEST_THAT(EMU_UNLINK("testfiles/test_download") == 0);
}
void assert_everything_deleted(BackupProtocolCallable &protocol, int64_t DirID)
{
BOX_TRACE("Test for del: " << BOX_FORMAT_OBJECTID(DirID));
// Command
std::auto_ptr<BackupProtocolSuccess> dirreply(protocol.QueryListDirectory(
DirID,
BackupProtocolListDirectory::Flags_INCLUDE_EVERYTHING,
BackupProtocolListDirectory::Flags_EXCLUDE_NOTHING, false /* no attributes */));
// Stream
BackupStoreDirectory dir(protocol.ReceiveStream(), SHORT_TIMEOUT);
BackupStoreDirectory::Iterator i(dir);
BackupStoreDirectory::Entry *en = 0;
int files = 0;
int dirs = 0;
while((en = i.Next()) != 0)
{
if(en->GetFlags() & BackupProtocolListDirectory::Flags_Dir)
{
dirs++;
// Recurse
assert_everything_deleted(protocol, en->GetObjectID());
}
else
{
files++;
}
// Check it's deleted
TEST_THAT(en->IsDeleted());
}
// Check there were the right number of files and directories
TEST_THAT(files == 3);
TEST_THAT(dirs == 0 || dirs == 2);
}
void create_file_in_dir(std::string name, std::string source, int64_t parentId,
BackupProtocolCallable &protocol, BackupStoreRefCountDatabase* pRefCount)
{
BackupStoreFilenameClear name_encoded("file_One");
std::auto_ptr<IOStream> upload(BackupStoreFile::EncodeFile(
source.c_str(), parentId, name_encoded));
std::auto_ptr<BackupProtocolSuccess> stored(
protocol.QueryStoreFile(
parentId,
0x123456789abcdefLL, /* modification time */
0x7362383249872dfLL, /* attr hash */
0, /* diff from ID */
name_encoded,
upload));
int64_t objectId = stored->GetObjectID();
if(pRefCount)
{
TEST_EQUAL(objectId, pRefCount->GetLastObjectIDUsed());
TEST_EQUAL(1, pRefCount->GetRefCount(objectId))
}
set_refcount(objectId, 1);
}
const box_time_t FAKE_MODIFICATION_TIME = 0xfeedfacedeadbeefLL;
const box_time_t FAKE_ATTR_MODIFICATION_TIME = 0xdeadbeefcafebabeLL;
int64_t create_test_data_subdirs(BackupProtocolCallable &protocol,
int64_t indir, const char *name, int depth,
BackupStoreRefCountDatabase* pRefCount)
{
// Create a directory
int64_t subdirid = 0;
BackupStoreFilenameClear dirname(name);
{
// Create with dummy attributes
int attrS = 0;
std::auto_ptr<IOStream> attr(new MemBlockStream(&attrS, sizeof(attrS)));
std::auto_ptr<BackupProtocolSuccess> dirCreate(protocol.QueryCreateDirectory(
indir, FAKE_ATTR_MODIFICATION_TIME, dirname, attr));
subdirid = dirCreate->GetObjectID();
}
BOX_TRACE("Creating subdirs, depth = " << depth << ", dirid = " <<
BOX_FORMAT_OBJECTID(subdirid));
if(pRefCount)
{
TEST_EQUAL(subdirid, pRefCount->GetLastObjectIDUsed());
TEST_EQUAL(1, pRefCount->GetRefCount(subdirid))
}
set_refcount(subdirid, 1);
// Put more directories in it, if we haven't gone down too far
if(depth > 0)
{
create_test_data_subdirs(protocol, subdirid, "dir_One",
depth - 1, pRefCount);
create_test_data_subdirs(protocol, subdirid, "dir_Two",
depth - 1, pRefCount);
}
// Stick some files in it
create_file_in_dir("file_One", "testfiles/test1", subdirid, protocol,
pRefCount);
create_file_in_dir("file_Two", "testfiles/test1", subdirid, protocol,
pRefCount);
create_file_in_dir("file_Three", "testfiles/test1", subdirid, protocol,
pRefCount);
return subdirid;
}
void check_dir_after_uploads(BackupProtocolCallable &protocol,
const StreamableMemBlock &Attributes)
{
// Command
std::auto_ptr<BackupProtocolSuccess> dirreply(protocol.QueryListDirectory(
BACKUPSTORE_ROOT_DIRECTORY_ID,
BackupProtocolListDirectory::Flags_INCLUDE_EVERYTHING,
BackupProtocolListDirectory::Flags_EXCLUDE_NOTHING, false /* no attributes */));
TEST_THAT(dirreply->GetObjectID() == BACKUPSTORE_ROOT_DIRECTORY_ID);
// Stream
BackupStoreDirectory dir(protocol.ReceiveStream(), SHORT_TIMEOUT);
TEST_EQUAL(UPLOAD_NUM, dir.GetNumberOfEntries());
TEST_THAT(!dir.HasAttributes());
// Check them!
BackupStoreDirectory::Iterator i(dir);
BackupStoreDirectory::Entry *en;
for(int t = 0; t < UPLOAD_NUM; ++t)
{
en = i.Next();
TEST_THAT(en != 0);
if (en == 0) continue;
TEST_LINE(uploads[t].name == en->GetName(),
"uploaded file " << t << " name");
BackupStoreFilenameClear clear(en->GetName());
TEST_EQUAL_LINE(uploads[t].name.GetClearFilename(),
clear.GetClearFilename(),
"uploaded file " << t << " name");
TEST_EQUAL_LINE(uploads[t].allocated_objid, en->GetObjectID(),
"uploaded file " << t << " ID");
TEST_EQUAL_LINE(uploads[t].mod_time, en->GetModificationTime(),
"uploaded file " << t << " modtime");
int correct_flags = BackupProtocolListDirectory::Flags_File;
if(uploads[t].should_be_old_version) correct_flags |= BackupProtocolListDirectory::Flags_OldVersion;
if(uploads[t].delete_file) correct_flags |= BackupProtocolListDirectory::Flags_Deleted;
TEST_EQUAL_LINE(correct_flags, en->GetFlags(),
"uploaded file " << t << " flags");
if(t == UPLOAD_ATTRS_EN)
{
TEST_THAT(en->HasAttributes());
TEST_THAT(en->GetAttributesHash() == 32498749832475LL);
TEST_THAT(en->GetAttributes() == Attributes);
}
else
{
// No attributes on this one
TEST_THAT(!en->HasAttributes());
}
}
en = i.Next();
TEST_THAT(en == 0);
}
typedef struct
{
int objectsNotDel;
int deleted;
int old;
} recursive_count_objects_results;
void recursive_count_objects_r(BackupProtocolCallable &protocol, int64_t id,
recursive_count_objects_results &results)
{
// Command
std::auto_ptr<BackupProtocolSuccess> dirreply(protocol.QueryListDirectory(
id,
BackupProtocolListDirectory::Flags_INCLUDE_EVERYTHING,
BackupProtocolListDirectory::Flags_EXCLUDE_NOTHING, false /* no attributes */));
// Stream
BackupStoreDirectory dir(protocol.ReceiveStream(), SHORT_TIMEOUT);
// Check them!
BackupStoreDirectory::Iterator i(dir);
// Discard first
BackupStoreDirectory::Entry *en = 0;
while((en = i.Next()) != 0)
{
if((en->GetFlags() & (BackupStoreDirectory::Entry::Flags_Deleted | BackupStoreDirectory::Entry::Flags_OldVersion)) == 0) results.objectsNotDel++;
if(en->GetFlags() & BackupStoreDirectory::Entry::Flags_Deleted) results.deleted++;
if(en->GetFlags() & BackupStoreDirectory::Entry::Flags_OldVersion) results.old++;
if(en->GetFlags() & BackupStoreDirectory::Entry::Flags_Dir)
{
recursive_count_objects_r(protocol, en->GetObjectID(), results);
}
}
}
TLSContext context;
void recursive_count_objects(int64_t id, recursive_count_objects_results &results)
{
// Get a connection
BackupProtocolLocal2 protocolReadOnly(0x01234567, "test",
"backup/01234567/", 0, false);
// Count objects
recursive_count_objects_r(protocolReadOnly, id, results);
// Close it
protocolReadOnly.QueryFinished();
}
bool check_block_index(const char *encoded_file, IOStream &rBlockIndex)
{
// Open file, and move to the right position
FileStream enc(encoded_file);
BackupStoreFile::MoveStreamPositionToBlockIndex(enc);
bool same = true;
// Now compare the two...
while(enc.StreamDataLeft())
{
char buffer1[2048];
char buffer2[2048];
int s = enc.Read(buffer1, sizeof(buffer1), SHORT_TIMEOUT);
if(rBlockIndex.Read(buffer2, s, SHORT_TIMEOUT) != s)
{
same = false;
break;
}
if(::memcmp(buffer1, buffer2, s) != 0)
{
same = false;
break;
}
}
if(rBlockIndex.StreamDataLeft())
{
same = false;
// Absorb all this excess data so procotol is in the first state
char buffer[2048];
while(rBlockIndex.StreamDataLeft())
{
rBlockIndex.Read(buffer, sizeof(buffer), SHORT_TIMEOUT);
}
}
return same;
}
bool check_files_same(const char *f1, const char *f2)
{
// Open file, and move to the right position
FileStream f1s(f1);
FileStream f2s(f2);
bool same = true;
// Now compare the two...
while(f1s.StreamDataLeft())
{
char buffer1[2048];
char buffer2[2048];
int s = f1s.Read(buffer1, sizeof(buffer1));
if(f2s.Read(buffer2, s) != s)
{
same = false;
break;
}
if(::memcmp(buffer1, buffer2, s) != 0)
{
same = false;
break;
}
}
if(f2s.StreamDataLeft())
{
same = false;
}
return same;
}
std::auto_ptr<RaidFileRead> get_raid_file(int64_t ObjectID)
{
std::string filename;
StoreStructure::MakeObjectFilename(ObjectID,
"backup/01234567/" /* mStoreRoot */, 0 /* mStoreDiscSet */,
filename, false /* EnsureDirectoryExists */);
return RaidFileRead::Open(0, filename);
}
int64_t create_directory(BackupProtocolCallable& protocol,
int64_t parent_dir_id = BACKUPSTORE_ROOT_DIRECTORY_ID);
int64_t create_file(BackupProtocolCallable& protocol, int64_t subdirid,
const std::string& remote_filename = "");
bool run_housekeeping_and_check_account(BackupProtocolLocal2& protocol)
{
protocol.QueryFinished();
bool check_account_status;
TEST_THAT(check_account_status = run_housekeeping_and_check_account());
bool check_refcount_status;
TEST_THAT(check_refcount_status = check_reference_counts());
protocol.Reopen();
return check_account_status & check_refcount_status;
}
bool test_temporary_refcount_db_is_independent()
{
SETUP_TEST_BACKUPSTORE();
std::auto_ptr<BackupStoreAccountDatabase> apAccounts(
BackupStoreAccountDatabase::Read("testfiles/accounts.txt"));
std::auto_ptr<BackupStoreRefCountDatabase> temp(
BackupStoreRefCountDatabase::Create(
apAccounts->GetEntry(0x1234567)));
std::auto_ptr<BackupStoreRefCountDatabase> perm(
BackupStoreRefCountDatabase::Load(
apAccounts->GetEntry(0x1234567),
true // ReadOnly
));
TEST_CHECK_THROWS(temp->GetRefCount(2),
BackupStoreException, UnknownObjectRefCountRequested);
TEST_CHECK_THROWS(perm->GetRefCount(2),
BackupStoreException, UnknownObjectRefCountRequested);
temp->AddReference(2);
TEST_EQUAL(1, temp->GetRefCount(2));
TEST_CHECK_THROWS(perm->GetRefCount(2),
BackupStoreException, UnknownObjectRefCountRequested);
temp->Discard();
// Need to delete perm object so it doesn't keep a filehandle open,
// preventing tearDown from rewriting the refcount DB and thus causing
// test failure.
perm.reset();
TEARDOWN_TEST_BACKUPSTORE();
}
bool test_server_housekeeping()
{
SETUP_TEST_BACKUPSTORE();
int encfile[ENCFILE_SIZE];
{
for(int l = 0; l < ENCFILE_SIZE; ++l)
{
encfile[l] = l * 173;
}
// Write this to a file
{
FileStream f("testfiles/file1", O_WRONLY | O_CREAT);
f.Write(encfile, sizeof(encfile));
}
}
// We need complete control over housekeeping, so use a local client
// instead of a network client + daemon.
BackupProtocolLocal2 protocol(0x01234567, "test", "backup/01234567/",
0, false);
int root_dir_blocks = get_raid_file(BACKUPSTORE_ROOT_DIRECTORY_ID)->GetDiscUsageInBlocks();
TEST_THAT(check_num_files(0, 0, 0, 1));
TEST_THAT(check_num_blocks(protocol, 0, 0, 0, root_dir_blocks,
root_dir_blocks));
TEST_THAT(check_reference_counts());
// Store a file -- first make the encoded file
BackupStoreFilenameClear store1name("file1");
{
FileStream out("testfiles/file1_upload1", O_WRONLY | O_CREAT);
std::auto_ptr<IOStream> encoded(
BackupStoreFile::EncodeFile("testfiles/file1",
BACKUPSTORE_ROOT_DIRECTORY_ID, store1name));
encoded->CopyStreamTo(out);
}
// TODO FIXME move most of the code immediately below into
// test_server_commands.
// TODO FIXME use COMMAND macro for all commands to check the returned
// object ID.
#define COMMAND(command, objectid) \
TEST_EQUAL(objectid, protocol.command->GetObjectID());
// Then send it
int64_t store1objid;
{
std::auto_ptr<IOStream> upload(new FileStream("testfiles/file1_upload1"));
std::auto_ptr<BackupProtocolSuccess> stored(protocol.QueryStoreFile(
BACKUPSTORE_ROOT_DIRECTORY_ID,
0x123456789abcdefLL, /* modification time */
0x7362383249872dfLL, /* attr hash */
0, /* diff from ID */
store1name,
upload));
store1objid = stored->GetObjectID();
TEST_EQUAL_LINE(2, store1objid, "wrong ObjectID for newly "
"uploaded file");
}
// Update expected reference count of this new object
set_refcount(store1objid, 1);
// And retrieve it
{
// Retrieve as object
COMMAND(QueryGetObject(store1objid), store1objid);
// BLOCK
{
// Get stream
std::auto_ptr<IOStream> filestream(protocol.ReceiveStream());
// Need to put it in another stream, because it's not in stream order
CollectInBufferStream f;
filestream->CopyStreamTo(f);
f.SetForReading();
// Get and decode
UNLINK_IF_EXISTS("testfiles/file1_upload_retrieved");
BackupStoreFile::DecodeFile(f, "testfiles/file1_upload_retrieved", IOStream::TimeOutInfinite);
}
// Retrieve as file
COMMAND(QueryGetFile(BACKUPSTORE_ROOT_DIRECTORY_ID, store1objid), store1objid);
// BLOCK
{
UNLINK_IF_EXISTS("testfiles/file1_upload_retrieved_str");
// Get stream
std::auto_ptr<IOStream> filestream(protocol.ReceiveStream());
// Get and decode
BackupStoreFile::DecodeFile(*filestream, "testfiles/file1_upload_retrieved_str", IOStream::TimeOutInfinite);
}
// Read in rebuilt original, and compare contents
{
FileStream in("testfiles/file1_upload_retrieved");
int encfile_i[ENCFILE_SIZE];
in.Read(encfile_i, sizeof(encfile_i));
TEST_THAT(memcmp(encfile, encfile_i, sizeof(encfile)) == 0);
}
{
FileStream in("testfiles/file1_upload_retrieved_str");
int encfile_i[ENCFILE_SIZE];
in.Read(encfile_i, sizeof(encfile_i));
TEST_THAT(memcmp(encfile, encfile_i, sizeof(encfile)) == 0);
}
// Retrieve the block index, by ID
{
std::auto_ptr<BackupProtocolSuccess> getblockindex(protocol.QueryGetBlockIndexByID(store1objid));
TEST_THAT(getblockindex->GetObjectID() == store1objid);
std::auto_ptr<IOStream> blockIndexStream(protocol.ReceiveStream());
// Check against uploaded file
TEST_THAT(check_block_index("testfiles/file1_upload1", *blockIndexStream));
}
// and again, by name
{
std::auto_ptr<BackupProtocolSuccess> getblockindex(protocol.QueryGetBlockIndexByName(BACKUPSTORE_ROOT_DIRECTORY_ID, store1name));
TEST_THAT(getblockindex->GetObjectID() == store1objid);
std::auto_ptr<IOStream> blockIndexStream(protocol.ReceiveStream());
// Check against uploaded file
TEST_THAT(check_block_index("testfiles/file1_upload1", *blockIndexStream));
}
}
// Get the directory again, and see if the entry is in it
{
// Command
std::auto_ptr<BackupProtocolSuccess> dirreply(protocol.QueryListDirectory(
BACKUPSTORE_ROOT_DIRECTORY_ID,
BackupProtocolListDirectory::Flags_INCLUDE_EVERYTHING,
BackupProtocolListDirectory::Flags_EXCLUDE_NOTHING, false /* no attributes */));
// Stream
BackupStoreDirectory dir(protocol.ReceiveStream(), SHORT_TIMEOUT);
TEST_THAT(dir.GetNumberOfEntries() == 1);
BackupStoreDirectory::Iterator i(dir);
BackupStoreDirectory::Entry *en = i.Next();
TEST_THAT(en != 0);
TEST_THAT(i.Next() == 0);
if(en != 0)
{
TEST_THAT(en->GetName() == store1name);
TEST_THAT(en->GetModificationTime() == 0x123456789abcdefLL);
TEST_THAT(en->GetAttributesHash() == 0x7362383249872dfLL);
TEST_THAT(en->GetObjectID() == store1objid);
TEST_EQUAL(6, en->GetSizeInBlocks());
TEST_THAT(en->GetFlags() == BackupStoreDirectory::Entry::Flags_File);
}
}
int file1_blocks = get_raid_file(store1objid)->GetDiscUsageInBlocks();
TEST_THAT(check_num_files(1, 0, 0, 1));
TEST_THAT(check_num_blocks(protocol, file1_blocks, 0, 0, root_dir_blocks,
file1_blocks + root_dir_blocks));
TEST_THAT(check_reference_counts());
// Upload again, as a patch to the original file.
int64_t patch1_id = BackupStoreFile::QueryStoreFileDiff(protocol,
"testfiles/file1", // LocalFilename