-
Notifications
You must be signed in to change notification settings - Fork 7
/
datastore_test.cpp
1078 lines (858 loc) · 30.1 KB
/
datastore_test.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
/*
* Copyright (c) 2018, Psiphon Inc.
* All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <cstdlib>
#include <ctime>
#include <thread>
#include <filesystem>
#include "gtest/gtest.h"
#include "test_helpers.hpp"
#include "datastore.hpp"
using namespace std;
using namespace psicash;
using json = nlohmann::json;
constexpr auto ds_suffix = ".test";
class TestDatastore : public ::testing::Test, public TempDir
{
public:
TestDatastore() = default;
};
TEST_F(TestDatastore, InitSimple)
{
Datastore ds;
auto err = ds.Init(GetTempDir(), ds_suffix);
ASSERT_FALSE(err) << err.ToString();
}
TEST_F(TestDatastore, InitCorrupt)
{
const auto k = "/k"_json_pointer;
// Note that the underlying implementation of std::hash differs between platforms, so we can't just hardcode known datastore values and checksums. We'll need to compute a real value.
string kv_checksum;
{
auto temp_dir = GetTempDir();
Datastore ds;
auto err = ds.Init(temp_dir, GetSuffix(true));
ASSERT_FALSE(err);
ASSERT_FALSE(ds.Set(k, "v"s));
auto ds_contents = ReadFile(DatastoreFilepath(temp_dir, true));
ASSERT_TRUE(ds_contents);
auto close_brace = ds_contents->find_last_of("}");
kv_checksum = utils::TrimCopy(ds_contents->substr(close_brace+1));
}
const string checksum_match = R"({"k":"v"})"s + "\n\n" + kv_checksum;
const string checksum_mismatch = R"({"k":"z"})"s + "\n\n" + kv_checksum;
// Both the main and the backup datastore files are empty
{
auto temp_dir = GetTempDir();
auto ds_file = DatastoreFilepath(temp_dir, true);
auto ok = WriteFile(ds_file, "");
ASSERT_TRUE(ok);
ok = WriteFile(BackupDatastoreFile(ds_file), "");
ASSERT_TRUE(ok);
Datastore ds;
auto err = ds.Init(temp_dir, GetSuffix(true));
ASSERT_TRUE(err);
ASSERT_GT(err.ToString().length(), 0);
}
// Both the main and the backup datastore files have checksum but empty content
{
auto temp_dir = GetTempDir();
auto ds_file = DatastoreFilepath(temp_dir, true);
auto ok = WriteFile(ds_file, "\n\nfakechecksum");
ASSERT_TRUE(ok);
ok = WriteFile(BackupDatastoreFile(ds_file), "\n\nfakechecksum");
ASSERT_TRUE(ok);
Datastore ds;
auto err = ds.Init(temp_dir, GetSuffix(true));
ASSERT_TRUE(err);
ASSERT_GT(err.ToString().length(), 0);
}
// The main datastore file is corrupt
{
auto temp_dir = GetTempDir();
auto ds_file = DatastoreFilepath(temp_dir, true);
auto ok = WriteFile(ds_file, checksum_mismatch);
ASSERT_TRUE(ok);
ok = WriteFile(BackupDatastoreFile(ds_file), checksum_match);
ASSERT_TRUE(ok);
Datastore ds;
auto err = ds.Init(temp_dir, GetSuffix(true));
ASSERT_FALSE(err);
auto val = ds.Get<string>("/k"_json_pointer);
ASSERT_TRUE(val);
ASSERT_EQ(*val, "v");
}
// The backup datastore file is corrupt
{
auto temp_dir = GetTempDir();
auto ds_file = DatastoreFilepath(temp_dir, true);
auto ok = WriteFile(ds_file, checksum_match);
ASSERT_TRUE(ok);
ok = WriteFile(BackupDatastoreFile(ds_file), checksum_mismatch);
ASSERT_TRUE(ok);
Datastore ds;
auto err = ds.Init(temp_dir, GetSuffix(true));
ASSERT_FALSE(err);
auto val = ds.Get<string>("/k"_json_pointer);
ASSERT_TRUE(val);
ASSERT_EQ(*val, "v");
}
// Both the main and backup datastore files are corrupt
{
auto temp_dir = GetTempDir();
auto ds_file = DatastoreFilepath(temp_dir, true);
auto ok = WriteFile(ds_file, checksum_mismatch);
ASSERT_TRUE(ok);
ok = WriteFile(BackupDatastoreFile(ds_file), checksum_mismatch);
ASSERT_TRUE(ok);
Datastore ds;
auto err = ds.Init(temp_dir, GetSuffix(true));
ASSERT_TRUE(err);
}
// The main datastore file has bad JSON and no checksum, but the backup is fine
{
auto temp_dir = GetTempDir();
auto ds_file = DatastoreFilepath(temp_dir, true);
auto ok = WriteFile(ds_file, "bad json file data");
ASSERT_TRUE(ok);
ok = WriteFile(BackupDatastoreFile(ds_file), checksum_match);
ASSERT_TRUE(ok);
Datastore ds;
auto err = ds.Init(temp_dir, GetSuffix(true));
ASSERT_FALSE(err);
auto val = ds.Get<string>("/k"_json_pointer);
ASSERT_TRUE(val);
ASSERT_EQ(*val, "v");
}
// The backup datastore file has bad JSON and no checksum, but the main is fine
{
auto temp_dir = GetTempDir();
auto ds_file = DatastoreFilepath(temp_dir, true);
auto ok = WriteFile(ds_file, checksum_match);
ASSERT_TRUE(ok);
ok = WriteFile(BackupDatastoreFile(ds_file), "bad json file data");
ASSERT_TRUE(ok);
Datastore ds;
auto err = ds.Init(temp_dir, GetSuffix(true));
ASSERT_FALSE(err);
auto val = ds.Get<string>("/k"_json_pointer);
ASSERT_TRUE(val);
ASSERT_EQ(*val, "v");
}
}
TEST_F(TestDatastore, InitBadDir)
{
auto bad_dir = GetTempDir() + "/a/b/c/d/f/g";
Datastore ds1;
auto err = ds1.Init(bad_dir.c_str(), ds_suffix);
ASSERT_TRUE(err);
bad_dir = "/";
Datastore ds2;
err = ds2.Init(bad_dir.c_str(), ds_suffix);
// This fails when running under Docker (via act) but not as a GitHub Action
ASSERT_TRUE(err) << "Expected to fail under local Docker (act)";
}
TEST_F(TestDatastore, InitMigrateChecksum)
{
auto temp_dir = GetTempDir();
auto ds_file = DatastoreFilepath(temp_dir, true);
// Write the main file with no checksum
auto ok = WriteFile(ds_file, R"({"k":"v"})");
ASSERT_TRUE(ok);
// Don't write the backup file at all
Datastore ds;
auto err = ds.Init(temp_dir, GetSuffix(true));
ASSERT_FALSE(err);
auto val = ds.Get<string>("/k"_json_pointer);
ASSERT_TRUE(val);
ASSERT_EQ(*val, "v");
}
TEST_F(TestDatastore, InitPromoteCommitFile)
{
// When reading, the .commit file takes precedence over an existing datastore file.
// .commit exists, no datastore file
{
auto temp_dir = GetTempDir();
auto ds_file = DatastoreFilepath(temp_dir, true);
// Write the main commit file (the lack of checksum doesn't matter for this test)
auto ok = WriteFile(ds_file+".commit", R"({"k":"v"})");
ASSERT_TRUE(ok);
// We won't bother with the backup commit file, as it doesn't change this test
Datastore ds;
auto err = ds.Init(temp_dir, GetSuffix(true));
ASSERT_FALSE(err);
auto val = ds.Get<string>("/k"_json_pointer);
ASSERT_TRUE(val);
ASSERT_EQ(*val, "v");
}
// .commit and datastore file both exist
{
auto temp_dir = GetTempDir();
auto ds_file = DatastoreFilepath(temp_dir, true);
// Write the main commit file (the lack of checksum doesn't matter for this test)
auto ok = WriteFile(ds_file+".commit", R"({"k":"v"})");
ASSERT_TRUE(ok);
// Write the main file, with different values
ok = WriteFile(ds_file, R"({"k":"z"})");
ASSERT_TRUE(ok);
Datastore ds;
auto err = ds.Init(temp_dir, GetSuffix(true));
ASSERT_FALSE(err);
// The .commit file should win
auto val = ds.Get<string>("/k"_json_pointer);
ASSERT_TRUE(val);
ASSERT_EQ(*val, "v");
}
// A .temp file should be ignored when reading
{
auto temp_dir = GetTempDir();
auto ds_file = DatastoreFilepath(temp_dir, true);
// Write the main commit file (the lack of checksum doesn't matter for this test)
auto ok = WriteFile(ds_file+".temp", R"({"k":"v"})");
ASSERT_TRUE(ok);
// Write the main file, with different values
ok = WriteFile(ds_file, R"({"k":"z"})");
ASSERT_TRUE(ok);
Datastore ds;
auto err = ds.Init(temp_dir, GetSuffix(true));
ASSERT_FALSE(err);
// The .commit file should win
auto val = ds.Get<string>("/k"_json_pointer);
ASSERT_TRUE(val);
ASSERT_EQ(*val, "z");
}
}
TEST_F(TestDatastore, CheckPersistence)
{
// We will create an instance, destroy it, create a new one with the same data directory, and
// check that it contains our previous data.
auto temp_dir = GetTempDir();
auto ds = new Datastore();
auto err = ds->Init(temp_dir, ds_suffix);
ASSERT_FALSE(err);
string want = "v";
auto k = "/k"_json_pointer;
err = ds->Set(k, want);
ASSERT_FALSE(err);
auto got = ds->Get<string>(k);
ASSERT_TRUE(got);
ASSERT_EQ(*got, want);
// Destroy/close the datastore
delete ds;
// Create a new one and check that it has the same data.
ds = new Datastore();
err = ds->Init(temp_dir, ds_suffix);
ASSERT_FALSE(err);
got = ds->Get<string>(k);
ASSERT_TRUE(got);
ASSERT_EQ(*got, want);
delete ds;
}
TEST_F(TestDatastore, Reset)
{
auto temp_dir = GetTempDir();
auto ds = new Datastore();
auto err = ds->Init(temp_dir, ds_suffix);
ASSERT_FALSE(err);
string want = "v";
auto k = "/k"_json_pointer;
err = ds->Set(k, want);
ASSERT_FALSE(err);
auto got = ds->Get<string>(k);
ASSERT_TRUE(got);
ASSERT_EQ(*got, want);
// Destroy/close the datastore
delete ds;
// Create a new one and check that it has the same data.
ds = new Datastore();
err = ds->Init(temp_dir, ds_suffix);
ASSERT_FALSE(err);
got = ds->Get<string>(k);
ASSERT_TRUE(got);
ASSERT_EQ(*got, want);
delete ds;
// Reset with arguments
ds = new Datastore();
err = ds->Reset(temp_dir, ds_suffix, {});
ASSERT_FALSE(err) << err.ToString();
// First Get without calling Init; should get "not initialized" error
got = ds->Get<string>(k);
ASSERT_FALSE(got);
ASSERT_EQ(got.error(), psicash::Datastore::DatastoreGetError::kDatastoreUninitialized);
// Then initialize and try again
err = ds->Init(temp_dir, ds_suffix);
ASSERT_FALSE(err);
// Key should not be found, since we haven't set it
got = ds->Get<string>(k);
ASSERT_FALSE(got);
ASSERT_EQ(got.error(), psicash::Datastore::DatastoreGetError::kNotFound);
// Set it
err = ds->Set(k, want);
ASSERT_FALSE(err);
// Get it for real
got = ds->Get<string>(k);
ASSERT_TRUE(got);
ASSERT_EQ(*got, want);
delete ds;
// Reset without arguments
ds = new Datastore();
err = ds->Init(temp_dir, ds_suffix);
ASSERT_FALSE(err);
got = ds->Get<string>(k);
ASSERT_TRUE(got);
ASSERT_EQ(*got, want);
err = ds->Reset({});
ASSERT_FALSE(err);
got = ds->Get<string>(k);
ASSERT_FALSE(got);
ASSERT_EQ(got.error(), psicash::Datastore::DatastoreGetError::kNotFound);
delete ds;
// Reset with non-empty new value
temp_dir = GetTempDir(); // use a fresh dir to avoid pollution
ds = new Datastore();
err = ds->Init(temp_dir, ds_suffix);
ASSERT_FALSE(err);
got = ds->Get<string>("/k"_json_pointer);
ASSERT_FALSE(got);
err = ds->Reset({{"k"s, want}});
ASSERT_FALSE(err);
got = ds->Get<string>("/k"_json_pointer);
ASSERT_TRUE(got);
ASSERT_EQ(*got, want);
delete ds;
}
TEST_F(TestDatastore, Transactions)
{
auto temp_dir = GetTempDir();
auto ds = new Datastore();
auto err = ds->Init(temp_dir, ds_suffix);
ASSERT_FALSE(err);
// This should persist
auto trans_want1 = "/trans_want1"_json_pointer;
err = ds->Set(trans_want1, trans_want1.to_string());
ASSERT_FALSE(err);
// This should persist, as we're committing
ds->BeginTransaction();
auto trans_want2 = "/trans_want2"_json_pointer;
err = ds->Set(trans_want2, trans_want2.to_string());
ASSERT_FALSE(err);
err = ds->EndTransaction(/*commit=*/true);
ASSERT_FALSE(err);
// This should NOT persist, as we're rolling back
ds->BeginTransaction();
auto trans_want3 = "/trans_want3"_json_pointer;
err = ds->Set(trans_want3, trans_want3.to_string());
ASSERT_FALSE(err);
err = ds->EndTransaction(/*commit=*/false);
ASSERT_FALSE(err);
// Another committed value, to make sure the order of things doesn't matter
ds->BeginTransaction();
auto trans_want4 = "/trans_want4"_json_pointer;
err = ds->Set(trans_want4, trans_want4.to_string());
ASSERT_FALSE(err);
err = ds->EndTransaction(/*commit=*/true);
ASSERT_FALSE(err);
// This should also NOT persist, since we're hitting the dtor
ds->BeginTransaction();
auto trans_want5 = "/trans_want5"_json_pointer;
err = ds->Set(trans_want5, trans_want5.to_string());
ASSERT_FALSE(err);
// Close
delete ds;
// Reopen
ds = new Datastore();
err = ds->Init(temp_dir, ds_suffix);
ASSERT_FALSE(err);
auto got = ds->Get<string>(trans_want1);
ASSERT_TRUE(got);
ASSERT_EQ(*got, trans_want1.to_string());
got = ds->Get<string>(trans_want2);
ASSERT_TRUE(got);
ASSERT_EQ(*got, trans_want2.to_string());
got = ds->Get<string>(trans_want3);
ASSERT_FALSE(got);
got = ds->Get<string>(trans_want4);
ASSERT_TRUE(got);
ASSERT_EQ(*got, trans_want4.to_string());
got = ds->Get<string>(trans_want5);
ASSERT_FALSE(got);
delete ds;
}
TEST_F(TestDatastore, NestedTransactions)
{
auto temp_dir = GetTempDir();
auto ds = new Datastore();
auto err = ds->Init(temp_dir, ds_suffix);
ASSERT_FALSE(err);
// Nest then commit
ds->BeginTransaction();
auto trans_want1 = "/trans_want1"_json_pointer;
err = ds->Set(trans_want1, trans_want1.to_string());
ASSERT_FALSE(err);
ds->BeginTransaction();
auto trans_want2 = "/trans_want2"_json_pointer;
err = ds->Set(trans_want2, trans_want2.to_string());
ASSERT_FALSE(err);
err = ds->EndTransaction(/*commit=*/true);
ASSERT_FALSE(err);
auto trans_want3 = "/trans_want3"_json_pointer;
err = ds->Set(trans_want3, trans_want3.to_string());
ASSERT_FALSE(err);
err = ds->EndTransaction(/*commit=*/true);
ASSERT_FALSE(err);
auto got = ds->Get<string>(trans_want1);
ASSERT_TRUE(got);
ASSERT_EQ(*got, trans_want1.to_string());
got = ds->Get<string>(trans_want2);
ASSERT_TRUE(got);
ASSERT_EQ(*got, trans_want2.to_string());
got = ds->Get<string>(trans_want3);
ASSERT_TRUE(got);
ASSERT_EQ(*got, trans_want3.to_string());
// Nest then roll back
ds->BeginTransaction();
auto trans_want4 = "/trans_want4"_json_pointer;
err = ds->Set(trans_want4, trans_want4.to_string());
ASSERT_FALSE(err);
// Also set one of the previous set values
err = ds->Set(trans_want3, "nope");
ASSERT_FALSE(err);
ds->BeginTransaction();
auto trans_want5 = "/trans_want5"_json_pointer;
err = ds->Set(trans_want5, trans_want5.to_string());
ASSERT_FALSE(err);
// We're going to commit the inner transaction...
err = ds->EndTransaction(/*commit=*/true);
ASSERT_FALSE(err);
auto trans_want6 = "/trans_want6"_json_pointer;
err = ds->Set(trans_want6, trans_want6.to_string());
ASSERT_FALSE(err);
// ...and roll back the outer transaction
err = ds->EndTransaction(/*commit=*/false);
ASSERT_FALSE(err);
got = ds->Get<string>(trans_want3);
ASSERT_TRUE(got);
ASSERT_EQ(*got, trans_want3.to_string());
got = ds->Get<string>(trans_want4);
ASSERT_FALSE(got) << *got;
got = ds->Get<string>(trans_want5);
ASSERT_FALSE(got);
got = ds->Get<string>(trans_want6);
ASSERT_FALSE(got);
}
TEST_F(TestDatastore, TransactionRaceConditionBug)
{
// Before we added "transactions" to the datastore, it only provided the ability to
// "pause" writing. But this still left the possibility that a set of updates that
// are only coherent together (like setting "is account" and the "account username")
// could be read when only partially updated (each individual Set is threadsafe, but
// multiple of them could be read separately).
Datastore ds;
auto err = ds.Init(GetTempDir(), ds_suffix);
ASSERT_FALSE(err);
const auto k = "/k"_json_pointer;
const string k_want1 = "kv";
err = ds.Set(k, k_want1);
ASSERT_FALSE(err);
const auto j = "/j"_json_pointer;
const string j_want1 = "jv";
err = ds.Set(j, j_want1);
ASSERT_FALSE(err);
const string k_want2 = "kv2", j_want2 = "jv2";
std::thread t([&](){
ds.BeginTransaction();
err = ds.Set(k, k_want2);
ASSERT_FALSE(err);
// Give the main-thread code a chance to read k
std::this_thread::sleep_for(std::chrono::milliseconds(200));
err = ds.Set(j, j_want2);
ASSERT_FALSE(err);
ds.EndTransaction(true);
});
// Give the thread a chance to start and set k
std::this_thread::sleep_for(std::chrono::milliseconds(100));
// With proper transaction isolation, this Get should wait on the mutex until the thread is done
auto k_got = ds.Get<string>(k);
ASSERT_TRUE(k_got);
auto j_got = ds.Get<string>(j);
ASSERT_TRUE(j_got) << (int)j_got.error();
// If we have a race condition, k will have been updated, but j won't be
ASSERT_EQ(*k_got, k_want2);
ASSERT_EQ(*j_got, j_want2) << "transaction isolation fail!";
t.join();
}
TEST_F(TestDatastore, SetSimple)
{
Datastore ds;
auto err = ds.Init(GetTempDir(), ds_suffix);
ASSERT_FALSE(err);
auto k = "/k"_json_pointer;
string want = "v";
err = ds.Set(k, want);
ASSERT_FALSE(err);
auto got = ds.Get<string>(k);
ASSERT_TRUE(got);
ASSERT_EQ(*got, want);
}
TEST_F(TestDatastore, SetDeep)
{
Datastore ds;
auto err = ds.Init(GetTempDir(), ds_suffix);
ASSERT_FALSE(err);
auto key1 = "/key1"_json_pointer, key2 = "/key2"_json_pointer;
string want = "want";
err = ds.Set(key1/key2, want);
ASSERT_FALSE(err);
// Try to get key1 and then get key2 from it
auto gotShallow = ds.Get<json>(key1);
ASSERT_TRUE(gotShallow);
string gotDeep = gotShallow->at(key2).get<string>();
ASSERT_EQ(gotDeep, want);
// Then try to get /key1/key2 directly
auto gotDeep2 = ds.Get<string>(key1/key2);
ASSERT_TRUE(gotDeep2);
ASSERT_EQ(gotDeep2, want);
}
TEST_F(TestDatastore, SetAndClear)
{
Datastore ds;
auto err = ds.Init(GetTempDir(), ds_suffix);
ASSERT_FALSE(err);
map<string, string> want = {{"a", "a"}, {"b", "b"}};
auto k = "/k"_json_pointer;
err = ds.Set(k, want);
ASSERT_FALSE(err);
auto got = ds.Get<map<string, string>>(k);
ASSERT_TRUE(got);
ASSERT_EQ(got->size(), want.size());
want.clear();
err = ds.Set(k, want);
ASSERT_FALSE(err);
got = ds.Get<map<string, string>>(k);
ASSERT_TRUE(got);
ASSERT_EQ(got->size(), 0);
}
TEST_F(TestDatastore, SetTypes)
{
// Test some types other than just string
Datastore ds;
auto err = ds.Init(GetTempDir(), ds_suffix);
ASSERT_FALSE(err);
// Start with string
string wantString = "v";
auto wantStringKey = "/wantStringKey"_json_pointer;
err = ds.Set(wantStringKey, wantString);
ASSERT_FALSE(err);
auto gotString = ds.Get<string>(wantStringKey);
ASSERT_TRUE(gotString);
ASSERT_EQ(*gotString, wantString);
// bool
bool wantBool = true;
auto wantBoolKey = "/wantBoolKey"_json_pointer;
err = ds.Set(wantBoolKey, wantBool);
ASSERT_FALSE(err);
auto gotBool = ds.Get<bool>(wantBoolKey);
ASSERT_TRUE(gotBool);
ASSERT_EQ(*gotBool, wantBool);
// int
int wantInt = 5273482;
auto wantIntKey = "/wantIntKey"_json_pointer;
err = ds.Set(wantIntKey, wantInt);
ASSERT_FALSE(err);
auto gotInt = ds.Get<int>(wantIntKey);
ASSERT_TRUE(gotInt);
ASSERT_EQ(*gotInt, wantInt);
}
TEST_F(TestDatastore, SetNoStore)
{
auto temp_dir = GetTempDir();
auto ds_path = DatastoreFilepath(temp_dir, ds_suffix);
auto k = "/k"_json_pointer;
auto v = "a";
{
Datastore ds;
auto err = ds.Init(temp_dir, ds_suffix);
ASSERT_FALSE(err);
// Set without writing to disk
err = ds.Set(k, v, false);
ASSERT_FALSE(err);
// Should be accessible
auto got = ds.Get<string>(k);
ASSERT_TRUE(got);
ASSERT_EQ(*got, v);
}
{
Datastore ds;
auto err = ds.Init(temp_dir, ds_suffix);
ASSERT_FALSE(err);
// Value should not have been stored
auto got = ds.Get<string>(k);
ASSERT_FALSE(got);
// Set without writing to disk
err = ds.Set(k, v, false);
ASSERT_FALSE(err);
// Should be accessible
got = ds.Get<string>(k);
ASSERT_TRUE(got);
ASSERT_EQ(*got, v);
// Write another value that is stored, prompting the storage of k
err = ds.Set("/x"_json_pointer, "y"s, true);
ASSERT_FALSE(err);
}
{
Datastore ds;
auto err = ds.Init(temp_dir, ds_suffix);
ASSERT_FALSE(err);
// Value should have been stored this time
auto got = ds.Get<string>(k);
ASSERT_TRUE(got);
ASSERT_EQ(*got, v);
}
}
TEST_F(TestDatastore, SetWriteDedup)
{
auto temp_dir = GetTempDir();
auto ds_path = DatastoreFilepath(temp_dir, ds_suffix);
Datastore ds;
auto err = ds.Init(temp_dir, ds_suffix);
ASSERT_FALSE(err);
auto k = "/k"_json_pointer;
err = ds.Set(k, "a"s);
ASSERT_FALSE(err);
auto file_time1 = std::filesystem::last_write_time(ds_path);
// Make sure there could be a file time difference, regardless of last_write_time resolution.
// This is necessary when running under Docker or GitHub Actions.
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
// Try setting the same value again
err = ds.Set(k, "a"s);
ASSERT_FALSE(err);
auto file_time2 = std::filesystem::last_write_time(ds_path);
// The file time should not have changed after setting the same value
ASSERT_EQ(file_time1, file_time2);
// Make sure there could be a file time difference, regardless of last_write_time resolution.
// This is necessary when running under Docker or GitHub Actions.
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
// Change to a different value
err = ds.Set(k, "b"s);
ASSERT_FALSE(err);
auto file_time3 = std::filesystem::last_write_time(ds_path);
// The file should have been updated, so its time should be newer
ASSERT_GT(file_time3, file_time1);
}
TEST_F(TestDatastore, TransactionWriteDedup)
{
auto temp_dir = GetTempDir();
auto ds_path = DatastoreFilepath(temp_dir, ds_suffix);
Datastore ds;
auto err = ds.Init(temp_dir, ds_suffix);
ASSERT_FALSE(err);
auto k1 = "/k1"_json_pointer;
auto k2 = "/k2"_json_pointer;
err = ds.Set(k1, "a"s);
ASSERT_FALSE(err);
err = ds.Set(k2, "a"s);
ASSERT_FALSE(err);
auto file_time1 = std::filesystem::last_write_time(ds_path);
// Make sure there could be a file time difference, regardless of last_write_time resolution.
// This is necessary when running under Docker or GitHub Actions.
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
ds.BeginTransaction();
// Try setting the same values again
err = ds.Set(k1, "a"s);
ASSERT_FALSE(err);
err = ds.Set(k2, "a"s);
ASSERT_FALSE(err);
err = ds.EndTransaction(true);
ASSERT_FALSE(err);
auto file_time2 = std::filesystem::last_write_time(ds_path);
// The file time should not have changed after setting the same values
ASSERT_EQ(file_time1, file_time2);
// Make sure there could be a file time difference, regardless of last_write_time resolution.
// This is necessary when running under Docker or GitHub Actions.
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
ds.BeginTransaction();
// Change to a different value
err = ds.Set(k1, "b"s);
ASSERT_FALSE(err);
err = ds.Set(k2, "b"s);
ASSERT_FALSE(err);
err = ds.EndTransaction(true);
ASSERT_FALSE(err);
auto file_time3 = std::filesystem::last_write_time(ds_path);
// The file should have been updated, so its time should be newer
ASSERT_GT(file_time3, file_time1);
// Make sure there could be a file time difference, regardless of last_write_time resolution.
// This is necessary when running under Docker or GitHub Actions.
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
ds.BeginTransaction();
// Changing and then rolling back should still work as expected
err = ds.Set(k1, "c"s);
ASSERT_FALSE(err);
err = ds.Set(k2, "c"s);
ASSERT_FALSE(err);
err = ds.EndTransaction(false);
ASSERT_FALSE(err);
auto file_time4 = std::filesystem::last_write_time(ds_path);
// Should not have changed
ASSERT_EQ(file_time3, file_time4);
}
TEST_F(TestDatastore, TypeMismatch)
{
Datastore ds;
auto err = ds.Init(GetTempDir(), ds_suffix);
ASSERT_FALSE(err);
// string
string wantString = "v";
auto wantStringKey = "/wantStringKey"_json_pointer;
err = ds.Set(wantStringKey, wantString);
ASSERT_FALSE(err);
auto gotString = ds.Get<string>(wantStringKey);
ASSERT_TRUE(gotString);
ASSERT_EQ(*gotString, wantString);
// bool
bool wantBool = true;
auto wantBoolKey = "/wantBoolKey"_json_pointer;
err = ds.Set(wantBoolKey, wantBool);
ASSERT_FALSE(err);
auto gotBool = ds.Get<bool>(wantBoolKey);
ASSERT_TRUE(gotBool);
ASSERT_EQ(*gotBool, wantBool);
// int
int wantInt = 5273482;
auto wantIntKey = "/wantIntKey"_json_pointer;
err = ds.Set(wantIntKey, wantInt);
ASSERT_FALSE(err);
auto gotInt = ds.Get<int>(wantIntKey);
ASSERT_TRUE(gotInt);
ASSERT_EQ(*gotInt, wantInt);
// It's an error to set one type and then try to get another
auto got_fail_1 = ds.Get<bool>(wantStringKey);
ASSERT_FALSE(got_fail_1);
ASSERT_EQ(got_fail_1.error(), psicash::Datastore::DatastoreGetError::kTypeMismatch);
auto got_fail_2 = ds.Get<string>(wantIntKey);
ASSERT_FALSE(got_fail_2);
ASSERT_EQ(got_fail_2.error(), psicash::Datastore::DatastoreGetError::kTypeMismatch);
auto got_fail_3 = ds.Get<int>(wantStringKey);
ASSERT_FALSE(got_fail_3);
ASSERT_EQ(got_fail_3.error(), psicash::Datastore::DatastoreGetError::kTypeMismatch);
auto got_fail_4 = ds.Get<int>(wantBoolKey);
//ASSERT_FALSE(got_fail_4); // NOTE: This doesn't actually fail. There must be a successful implicit conversion.
// It's not an error to set one type to a key and then replace it with another type
err = ds.Set(wantStringKey, wantBool);
ASSERT_FALSE(err);
}
TEST_F(TestDatastore, GetSimple)
{
Datastore ds;
auto err = ds.Init(GetTempDir(), ds_suffix);
ASSERT_FALSE(err);
string want = "v";
err = ds.Set("/k"_json_pointer, want);
ASSERT_FALSE(err);
auto got = ds.Get<string>("/k"_json_pointer);
ASSERT_TRUE(got);
ASSERT_EQ(*got, want);
}
TEST_F(TestDatastore, GetDeep)
{
// This is a copy of SetDeep
Datastore ds;
auto err = ds.Init(GetTempDir(), ds_suffix);
ASSERT_FALSE(err);
auto key1 = "/key1"_json_pointer, key2 = "/key2"_json_pointer;
string want = "want";
err = ds.Set(key1/key2, want);
ASSERT_FALSE(err);
// Try to get key1 and then get key2 from it
auto gotShallow = ds.Get<json>(key1);
ASSERT_TRUE(gotShallow);
string gotDeep = gotShallow->at(key2).get<string>();
ASSERT_EQ(gotDeep, want);
// Then try to get /key1/key2 directly
auto gotDeep2 = ds.Get<string>(key1/key2);
ASSERT_TRUE(gotDeep2);
ASSERT_EQ(gotDeep2, want);
}
TEST_F(TestDatastore, GetNotFound)
{
Datastore ds;
auto err = ds.Init(GetTempDir(), ds_suffix);
ASSERT_FALSE(err);
string want = "v";
auto k = "/k"_json_pointer;
err = ds.Set(k, want);