forked from Psiphon-Inc/psicash-lib-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuserdata_test.cpp
716 lines (598 loc) · 24 KB
/
userdata_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
#include "gtest/gtest.h"
#include "gmock/gmock.h"
#include "test_helpers.hpp"
#include "userdata.hpp"
#include "vendor/nlohmann/json.hpp"
using json = nlohmann::json;
using namespace std;
using namespace nonstd;
using namespace psicash;
using namespace testing;
constexpr auto dev = true;
class TestUserData : public ::testing::Test, public TempDir
{
public:
TestUserData() {}
};
TEST_F(TestUserData, InitSimple)
{
UserData ud;
auto err = ud.Init(GetTempDir().c_str(), dev);
ASSERT_FALSE(err);
}
TEST_F(TestUserData, InitFail)
{
auto bad_dir = GetTempDir() + "/a/b/c/d/f/g";
UserData ud;
auto err = ud.Init(bad_dir.c_str(), dev);
ASSERT_TRUE(err);
}
TEST_F(TestUserData, InitUpgrade)
{
auto dsDir = GetTempDir();
// Write a v1 file.
auto ok = TempDir::Write(dsDir, dev, R"({"IsAccount":false,"authTokens":{"earner":"earnertoken","indicator":"indicatortoken","spender":"spendertoken"},"balance":125000000000,"lastTransactionID":"boosttransid","purchasePrices":[{"class":"speed-boost","distinguisher":"1hr","price":100000000000},{"class":"speed-boost","distinguisher":"2hr","price":200000000000},{"class":"speed-boost","distinguisher":"3hr","price":300000000000},{"class":"speed-boost","distinguisher":"4hr","price":400000000000},{"class":"speed-boost","distinguisher":"5hr","price":500000000000},{"class":"speed-boost","distinguisher":"6hr","price":600000000000},{"class":"speed-boost","distinguisher":"7hr","price":700000000000},{"class":"speed-boost","distinguisher":"8hr","price":800000000000},{"class":"speed-boost","distinguisher":"9hr","price":900000000000},{"class":"speed-boost","distinguisher":"24hr","price":800000000000}],"purchases":[{"authorization":{"AccessType":"speed-boost-test","Encoded":"boostauth","Expires":"2020-07-27T15:14:30.986Z","ID":"boostauthid"},"class":"speed-boost","distinguisher":"1hr","id":"boosttransid","localTimeExpiry":"2020-07-27T15:14:32.878Z","serverTimeExpiry":"2020-07-27T15:14:30.986Z"}],"requestMetadata":{"client_region":"CA","client_version":"999","propagation_channel_id":"ABCD1234","sponsor_id":"ABCD1234"},"serverTimeDiff":-2584,"v":1})");
ASSERT_TRUE(ok) << errno;
// Now load the file and upgrade
UserData ud;
auto err = ud.Init(dsDir.c_str(), dev);
ASSERT_FALSE(err);
ASSERT_GT(ud.GetInstanceID().length(), 0);
ASSERT_FALSE(ud.GetIsLoggedOutAccount());
ASSERT_EQ(ud.GetServerTimeDiff().count(), -2584);
AuthTokens want_tokens = {{"earner",{"earnertoken",nullopt}},{"indicator",{"indicatortoken",nullopt}},{"spender",{"spendertoken",nullopt}}};
ASSERT_TRUE(AuthTokenSetsEqual(ud.GetAuthTokens(), want_tokens));
ASSERT_FALSE(ud.GetIsAccount());
ASSERT_EQ(ud.GetAccountUsername(), "");
ASSERT_EQ(ud.GetBalance(), 125000000000L);
ASSERT_EQ(ud.GetPurchasePrices().size(), 10);
ASSERT_EQ(ud.GetPurchases().size(), 1);
ASSERT_EQ(ud.GetLastTransactionID(), "boosttransid");
ASSERT_EQ(ud.GetRequestMetadata().size(), 4);
}
TEST_F(TestUserData, InitBadVersion)
{
auto dsDir = GetTempDir();
// Write a datastore file with a bad (too future) version: 999999.
auto ok = TempDir::Write(dsDir, dev, R"({"IsAccount":false,"authTokens":{"earner":"earnertoken","indicator":"indicatortoken","spender":"spendertoken"},"balance":125000000000,"lastTransactionID":"boosttransid","purchasePrices":[{"class":"speed-boost","distinguisher":"1hr","price":100000000000},{"class":"speed-boost","distinguisher":"2hr","price":200000000000},{"class":"speed-boost","distinguisher":"3hr","price":300000000000},{"class":"speed-boost","distinguisher":"4hr","price":400000000000},{"class":"speed-boost","distinguisher":"5hr","price":500000000000},{"class":"speed-boost","distinguisher":"6hr","price":600000000000},{"class":"speed-boost","distinguisher":"7hr","price":700000000000},{"class":"speed-boost","distinguisher":"8hr","price":800000000000},{"class":"speed-boost","distinguisher":"9hr","price":900000000000},{"class":"speed-boost","distinguisher":"24hr","price":800000000000}],"purchases":[{"authorization":{"AccessType":"speed-boost-test","Encoded":"boostauth","Expires":"2020-07-27T15:14:30.986Z","ID":"boostauthid"},"class":"speed-boost","distinguisher":"1hr","id":"boosttransid","localTimeExpiry":"2020-07-27T15:14:32.878Z","serverTimeExpiry":"2020-07-27T15:14:30.986Z","serverTimeCreated":"2020-07-27T15:14:30.986Z"}],"requestMetadata":{"client_region":"CA","client_version":"999","propagation_channel_id":"ABCD1234","sponsor_id":"ABCD1234"},"serverTimeDiff":-2584,"v":999999})");
ASSERT_TRUE(ok) << errno;
// Now load the file and upgrade
UserData ud;
auto err = ud.Init(dsDir.c_str(), dev);
ASSERT_TRUE(err);
}
TEST_F(TestUserData, Persistence)
{
auto want_server_time_diff_ms = 54321;
auto want_server_time_diff = datetime::Duration(want_server_time_diff_ms);
auto future = datetime::DateTime::Now().Add(datetime::Duration(want_server_time_diff_ms*2)); // if this is less than want_server_time_diff_ms, the tokens will be expired already
auto past = datetime::DateTime::Now().Sub(datetime::Duration(want_server_time_diff_ms*2));
AuthTokens want_auth_tokens = {{"k1", {"v1", nullopt}}, {"k2", {"v2", future}}, {"k3", {"v3", past}}};
bool want_is_account = true;
string want_account_username = "account-username"s;
int64_t want_balance = 12345;
PurchasePrices want_purchase_prices = {{"tc1", "d1", 123}, {"tc2", "d2", 321}};
Purchases want_purchases = {
{"id1", datetime::DateTime(), "tc1", "d1", nullopt, nullopt, nullopt},
{"id2", datetime::DateTime(), "tc2", "d2", nullopt, nullopt, nullopt}};
string req_metadata_key = "req_metadata_key";
string want_req_metadata_value = "want_req_metadata_value";
auto temp_dir = GetTempDir();
{
// Set a bunch of values to persist
UserData ud;
auto err = ud.Init(temp_dir.c_str(), dev);
ASSERT_FALSE(err);
auto shifted_now = datetime::DateTime::Now().Add(want_server_time_diff);
err = ud.SetServerTimeDiff(shifted_now);
ASSERT_FALSE(err);
err = ud.SetAuthTokens(want_auth_tokens, want_is_account, want_account_username);
ASSERT_FALSE(err);
err = ud.SetBalance(want_balance);
ASSERT_FALSE(err);
err = ud.SetPurchasePrices(want_purchase_prices);
ASSERT_FALSE(err);
err = ud.SetPurchases(want_purchases);
ASSERT_FALSE(err);
err = ud.SetRequestMetadataItem(req_metadata_key, want_req_metadata_value);
ASSERT_FALSE(err);
// close the datastore
}
{
// Re-open the datastore and check values
UserData ud;
auto err = ud.Init(temp_dir.c_str(), dev);
ASSERT_FALSE(err);
auto got_server_time_diff = ud.GetServerTimeDiff();
ASSERT_NEAR(want_server_time_diff.count(), got_server_time_diff.count(), 10);
auto got_auth_tokens = ud.GetAuthTokens();
ASSERT_TRUE(AuthTokenSetsEqual(got_auth_tokens, want_auth_tokens));
auto got_is_account = ud.GetIsAccount();
ASSERT_EQ(got_is_account, want_is_account);
auto got_account_username = ud.GetAccountUsername();
ASSERT_EQ(got_account_username, want_account_username);
auto got_balance = ud.GetBalance();
ASSERT_EQ(got_balance, want_balance);
auto got_purchase_prices = ud.GetPurchasePrices();
ASSERT_EQ(got_purchase_prices, want_purchase_prices);
auto got_purchases = ud.GetPurchases();
ASSERT_EQ(got_purchases, want_purchases);
auto got_request_metadata = ud.GetRequestMetadata();
ASSERT_EQ(want_req_metadata_value, got_request_metadata[req_metadata_key]);
}
}
TEST_F(TestUserData, GetInstanceID)
{
UserData ud;
auto err = ud.Init(GetTempDir().c_str(), dev);
ASSERT_FALSE(err);
string prefix = "instanceid_";
auto v1 = ud.GetInstanceID();
ASSERT_EQ(v1.length(), prefix.length() + 48);
ASSERT_EQ(v1.substr(0, prefix.length()), prefix);
auto v1again = ud.GetInstanceID();
ASSERT_EQ(v1, v1again);
// Clear and expect to get a different ID
ud.Clear();
auto v2 = ud.GetInstanceID();
ASSERT_EQ(v2.length(), prefix.length() + 48);
ASSERT_EQ(v2.substr(0, prefix.length()), prefix);
ASSERT_NE(v1, v2);
}
TEST_F(TestUserData, IsLoggedOutAccount)
{
UserData ud;
auto err = ud.Init(GetTempDir().c_str(), dev);
ASSERT_FALSE(err);
// Check default value
auto v = ud.GetIsLoggedOutAccount();
ASSERT_EQ(v, false);
// Set then get
bool want = true;
err = ud.SetIsLoggedOutAccount(want);
ASSERT_FALSE(err);
auto got = ud.GetIsLoggedOutAccount();
ASSERT_EQ(got, want);
want = false;
err = ud.SetIsLoggedOutAccount(want);
ASSERT_FALSE(err);
got = ud.GetIsLoggedOutAccount();
ASSERT_EQ(got, want);
}
TEST_F(TestUserData, ServerTimeDiff)
{
UserData ud;
auto err = ud.Init(GetTempDir().c_str(), dev);
ASSERT_FALSE(err);
// Check default value
auto v = ud.GetServerTimeDiff();
ASSERT_EQ(v.count(), 0);
// Set then get
auto want = datetime::Duration(54321);
auto shifted_now = datetime::DateTime::Now().Add(want);
err = ud.SetServerTimeDiff(shifted_now);
ASSERT_FALSE(err);
auto got = ud.GetServerTimeDiff();
ASSERT_NEAR(want.count(), got.count(), 10);
}
TEST_F(TestUserData, UpdatePurchaseLocalTimeExpiry)
{
UserData ud;
auto err = ud.Init(GetTempDir().c_str(), dev);
ASSERT_FALSE(err);
Purchase purchase_noexpiry{
.id = "id",
.server_time_created = datetime::DateTime(),
.transaction_class = "tc",
.distinguisher = "d"
};
datetime::DateTime server_expiry;
ASSERT_TRUE(server_expiry.FromISO8601("2031-02-03T04:05:06.789Z"));
Purchase purchase_expiry{
.id = "id",
.server_time_created = datetime::DateTime(),
.transaction_class = "tc",
.distinguisher = "d",
.server_time_expiry = server_expiry
};
// Zero server time diff
ASSERT_EQ(ud.GetServerTimeDiff().count(), 0);
ud.UpdatePurchaseLocalTimeExpiry(purchase_noexpiry);
ASSERT_FALSE(purchase_noexpiry.local_time_expiry);
ud.UpdatePurchaseLocalTimeExpiry(purchase_expiry);
ASSERT_TRUE(purchase_expiry.local_time_expiry);
ASSERT_EQ(*purchase_expiry.local_time_expiry, server_expiry);
// Nonzero server time diff
auto server_time_diff = datetime::Duration(54321);
ASSERT_FALSE(ud.SetServerTimeDiff(datetime::DateTime::Now().Add(server_time_diff)));
ud.UpdatePurchaseLocalTimeExpiry(purchase_noexpiry);
ASSERT_FALSE(purchase_noexpiry.local_time_expiry);
ud.UpdatePurchaseLocalTimeExpiry(purchase_expiry);
ASSERT_TRUE(purchase_expiry.local_time_expiry);
ASSERT_EQ(*purchase_expiry.local_time_expiry, server_expiry.Sub(server_time_diff));
}
TEST_F(TestUserData, AuthTokens)
{
UserData ud;
auto err = ud.Init(GetTempDir().c_str(), dev);
ASSERT_FALSE(err);
// Check default value
auto tokens = ud.GetAuthTokens();
ASSERT_EQ(tokens.size(), 0);
auto is_account = ud.GetIsAccount();
ASSERT_EQ(is_account, false);
auto past = datetime::DateTime::Now().Sub(datetime::Duration(10000));
auto future = datetime::DateTime::Now().Add(datetime::Duration(10000));
AuthTokens want = {{"k1", {"v1", future}}, {"k2", {"v2", past}}};
err = ud.SetAuthTokens(want, false, "");
ASSERT_FALSE(err);
auto got_tokens = ud.GetAuthTokens();
ASSERT_TRUE(AuthTokenSetsEqual(want, got_tokens));
is_account = ud.GetIsAccount();
ASSERT_EQ(is_account, false);
ASSERT_EQ(ud.GetAccountUsername(), "");
err = ud.SetAuthTokens(want, true, "tokens-username");
ASSERT_FALSE(err);
got_tokens = ud.GetAuthTokens();
ASSERT_TRUE(AuthTokenSetsEqual(want, got_tokens));
is_account = ud.GetIsAccount();
ASSERT_EQ(is_account, true);
ASSERT_EQ(ud.GetAccountUsername(), "tokens-username");
}
TEST_F(TestUserData, CullAuthTokens)
{
UserData ud;
auto err = ud.Init(GetTempDir().c_str(), dev);
ASSERT_FALSE(err);
AuthTokens auth_tokens = {{"k1",{"v1"}},{"k2",{"v2"}},{"k3",{"v3"}},{"k4",{"v4"}},};
// All good
err = ud.SetAuthTokens(auth_tokens, false, "");
ASSERT_FALSE(err);
std::map<std::string, bool> valid_tokens = {{"v1",true},{"v2",true},{"v3",true},{"v4",true}};
err = ud.CullAuthTokens(valid_tokens);
ASSERT_FALSE(err);
ASSERT_TRUE(AuthTokenSetsEqual(auth_tokens, ud.GetAuthTokens()));
// All present, one invalid
err = ud.SetAuthTokens(auth_tokens, false, "");
ASSERT_FALSE(err);
valid_tokens = {{"v1",true},{"v2",false},{"v3",true},{"v4",true}};
err = ud.CullAuthTokens(valid_tokens);
ASSERT_FALSE(err);
ASSERT_THAT(ud.GetAuthTokens(), IsEmpty());
// All present, all invalid
err = ud.SetAuthTokens(auth_tokens, false, "");
ASSERT_FALSE(err);
valid_tokens = {{"v1",false},{"v2",false},{"v3",false},{"v4",false}};
err = ud.CullAuthTokens(valid_tokens);
ASSERT_FALSE(err);
ASSERT_THAT(ud.GetAuthTokens(), IsEmpty());
// All valid, one missing
err = ud.SetAuthTokens(auth_tokens, false, "");
ASSERT_FALSE(err);
valid_tokens = {{"v1",true},{"v3",true},{"v4",true}};
err = ud.CullAuthTokens(valid_tokens);
ASSERT_FALSE(err);
ASSERT_THAT(ud.GetAuthTokens(), IsEmpty());
// All missing
err = ud.SetAuthTokens(auth_tokens, false, "");
ASSERT_FALSE(err);
valid_tokens = {};
err = ud.CullAuthTokens(valid_tokens);
ASSERT_FALSE(err);
ASSERT_THAT(ud.GetAuthTokens(), IsEmpty());
}
TEST_F(TestUserData, ValidTokenTypes) {
UserData ud;
auto err = ud.Init(GetTempDir().c_str(), dev);
ASSERT_FALSE(err);
ASSERT_EQ(ud.ValidTokenTypes().size(), 0);
AuthTokens at = {{"a", {"a"}}, {"b", {"b"}}, {"c", {"c"}}};
err = ud.SetAuthTokens(at, false, "");
auto vtt = ud.ValidTokenTypes();
ASSERT_EQ(vtt.size(), 3);
for (const auto& k : vtt) {
ASSERT_EQ(at.count(k), 1);
at.erase(k);
}
ASSERT_EQ(at.size(), 0); // we should have erased all items
AuthTokens empty;
err = ud.SetAuthTokens(empty, false, "");
vtt = ud.ValidTokenTypes();
ASSERT_EQ(vtt.size(), 0);
}
TEST_F(TestUserData, IsAccount)
{
UserData ud;
auto err = ud.Init(GetTempDir().c_str(), dev);
ASSERT_FALSE(err);
// Check default value
auto v = ud.GetIsAccount();
ASSERT_EQ(v, false);
// Set then get
bool want = true;
err = ud.SetIsAccount(want);
ASSERT_FALSE(err);
auto got = ud.GetIsAccount();
ASSERT_EQ(got, want);
}
TEST_F(TestUserData, AccountUsername)
{
UserData ud;
auto err = ud.Init(GetTempDir().c_str(), dev);
ASSERT_FALSE(err);
// Check default value
auto v = ud.GetAccountUsername();
ASSERT_EQ(v, "");
// Set then get
string want = "account-username";
err = ud.SetAccountUsername(want);
ASSERT_FALSE(err);
auto got = ud.GetAccountUsername();
ASSERT_EQ(got, want);
// Set via SetAuthTokens
want = "account-username";
auto future = datetime::DateTime::Now().Add(datetime::Duration(10000));
AuthTokens at = {{"a", {"a", future}}, {"b", {"b", future}}, {"c", {"c", future}}};
err = ud.SetAuthTokens(at, true, want);
got = ud.GetAccountUsername();
ASSERT_EQ(got, want);
// With tracker tokens -- so no username
want = "";
at = {{"a", {"a"}}, {"b", {"b"}}, {"c", {"c"}}};
err = ud.SetAuthTokens(at, true, want);
got = ud.GetAccountUsername();
ASSERT_EQ(got, want);
}
TEST_F(TestUserData, Balance)
{
UserData ud;
auto err = ud.Init(GetTempDir().c_str(), dev);
ASSERT_FALSE(err);
// Check default value
auto v = ud.GetBalance();
ASSERT_EQ(v, 0);
// Set then get
int64_t want = 54321;
err = ud.SetBalance(want);
ASSERT_FALSE(err);
auto got = ud.GetBalance();
ASSERT_EQ(got, want);
}
TEST_F(TestUserData, PurchasePrices)
{
UserData ud;
auto err = ud.Init(GetTempDir().c_str(), dev);
ASSERT_FALSE(err);
// Check default value
auto v = ud.GetPurchasePrices();
ASSERT_EQ(v.size(), 0);
// Set then get
PurchasePrices want = {{"tc1", "d1", 123}, {"tc2", "d2", 321}};
err = ud.SetPurchasePrices(want);
ASSERT_FALSE(err);
auto got = ud.GetPurchasePrices();
ASSERT_EQ(got, want);
}
TEST_F(TestUserData, Purchases)
{
UserData ud;
auto err = ud.Init(GetTempDir().c_str(), dev);
ASSERT_FALSE(err);
// Check default value
auto v = ud.GetPurchases();
ASSERT_EQ(v.size(), 0);
// Set then get
auto dt1 = datetime::DateTime::Now().Add(datetime::Duration(1));
auto dt2 = datetime::DateTime::Now().Add(datetime::Duration(2));
auto created1 = datetime::DateTime::Now().Sub(datetime::Duration(3));
auto created2 = datetime::DateTime::Now().Sub(datetime::Duration(4));
auto auth_res1 = psicash::DecodeAuthorization("eyJBdXRob3JpemF0aW9uIjp7IklEIjoibFRSWnBXK1d3TFJqYkpzOGxBUFVaQS8zWnhmcGdwNDFQY0dkdlI5a0RVST0iLCJBY2Nlc3NUeXBlIjoic3BlZWQtYm9vc3QtdGVzdCIsIkV4cGlyZXMiOiIyMDE5LTAxLTE0VDIxOjQ2OjMwLjcxNzI2NTkyNFoifSwiU2lnbmluZ0tleUlEIjoiUUNZTzV2clIvZGhjRDZ6M2FMQlVNeWRuZlJyZFNRL1RWYW1IUFhYeTd0TT0iLCJTaWduYXR1cmUiOiJtV1Z5Tm9ZU0pFRDNXU3I3bG1OeEtReEZza1M5ZWlXWG1lcDVvVWZBSHkwVmYrSjZaQW9WajZrN3ZVTDNrakIreHZQSTZyaVhQc3FzWENRNkx0eFdBQT09In0=");
ASSERT_TRUE(auth_res1);
Purchases want = {
{"id1", created1, "tc1", "d1", dt1, dt2, *auth_res1},
{"id2", created2, "tc2", "d2", nullopt, nullopt, nullopt}};
err = ud.SetPurchases(want);
ASSERT_FALSE(err);
auto got = ud.GetPurchases();
ASSERT_EQ(got, want);
// Test populating the local_time_expiry.
auto serverTimeDiff = datetime::Duration(54321);
auto local_now = datetime::DateTime::Now();
auto server_now = local_now.Add(serverTimeDiff);
err = ud.SetServerTimeDiff(server_now);
ASSERT_FALSE(err);
// Supply server time but not local time
want.push_back({"id3", created1, "tc3", "d3", server_now, nullopt, nullopt});
err = ud.SetPurchases(want);
got = ud.GetPurchases();
ASSERT_EQ(got.size(), 3);
ASSERT_TRUE(got[2].local_time_expiry);
ASSERT_NEAR(got[2].local_time_expiry->MillisSinceEpoch(), local_now.MillisSinceEpoch(), 5);
}
TEST_F(TestUserData, AddPurchase)
{
// This also tests Get/SetLastTransactionID (as Set isn't public)
UserData ud;
auto err = ud.Init(GetTempDir().c_str(), dev);
ASSERT_FALSE(err);
// Check default value
auto v = ud.GetPurchases();
ASSERT_EQ(v.size(), 0);
// We need to check the behaviour for duplicates and out-of-chrono-order addition
Purchases final_want = {
{"id0", datetime::DateTime::Now().Sub(datetime::Duration(6)), "tc0", "d0", nullopt, nullopt, nullopt},
{"id1", datetime::DateTime::Now().Sub(datetime::Duration(5)), "tc1", "d1", nullopt, nullopt, nullopt},
{"id2", datetime::DateTime::Now().Sub(datetime::Duration(4)), "tc2", "d2", nullopt, nullopt, nullopt},
{"id3", datetime::DateTime::Now().Sub(datetime::Duration(3)), "tc3", "d3", nullopt, nullopt, nullopt}};
// Start with a subset
Purchases want = {final_want[0], final_want[2]};
err = ud.AddPurchase(want[0]);
ASSERT_FALSE(err);
err = ud.AddPurchase(want[1]);
ASSERT_FALSE(err);
auto got = ud.GetPurchases();
ASSERT_EQ(got, want);
ASSERT_EQ(ud.GetLastTransactionID(), "id2");
// Add a later purchase
want = {final_want[0], final_want[2], final_want[3]};
err = ud.AddPurchase(final_want[3]);
ASSERT_FALSE(err);
got = ud.GetPurchases();
ASSERT_EQ(got, want);
ASSERT_EQ(ud.GetLastTransactionID(), "id3");
// Add a purchase in the middle
want = {final_want[0], final_want[1], final_want[2], final_want[3]};
err = ud.AddPurchase(final_want[1]);
ASSERT_FALSE(err);
got = ud.GetPurchases();
ASSERT_EQ(got, want);
// Even though id1 is not the newest, it was added last and therefore will be the LastTransactionID. See comment in AddPurchase for details.
ASSERT_EQ(ud.GetLastTransactionID(), "id1");
// Add a duplicate purchase
want = {final_want[0], final_want[1], final_want[2], final_want[3]};
err = ud.AddPurchase(final_want[2]);
ASSERT_FALSE(err);
got = ud.GetPurchases();
ASSERT_EQ(got, want);
ASSERT_EQ(ud.GetLastTransactionID(), "id2");
}
TEST_F(TestUserData, Metadata)
{
UserData ud;
auto err = ud.Init(GetTempDir().c_str(), dev);
ASSERT_FALSE(err);
auto v = ud.GetRequestMetadata();
ASSERT_EQ(v, json({}));
err = ud.SetRequestMetadataItem("k", "v");
ASSERT_FALSE(err);
v = ud.GetRequestMetadata();
ASSERT_EQ(v.dump(), json({{"k", "v"}}).dump());
err = ud.SetRequestMetadataItem("kk", 123);
ASSERT_FALSE(err);
v = ud.GetRequestMetadata();
ASSERT_EQ(v.dump(), json({{"k", "v"}, {"kk", 123}}).dump());
err = ud.SetRequestMetadataItem("k", "v2");
ASSERT_FALSE(err);
v = ud.GetRequestMetadata();
ASSERT_EQ(v.dump(), json({{"k", "v2"}, {"kk", 123}}).dump());
// Make sure modifying the result doesn't modify the internal structure
v["temp"] = "temp";
v = ud.GetRequestMetadata();
ASSERT_EQ(v.dump(), json({{"k", "v2"}, {"kk", 123}}).dump());
// Empty key is an error
err = ud.SetRequestMetadataItem("", "v");
ASSERT_TRUE(err);
err = ud.SetRequestMetadataItem("k", nullptr);
ASSERT_FALSE(err);
v = ud.GetRequestMetadata();
ASSERT_TRUE(v["k"].is_null());
}
TEST_F(TestUserData, Locale)
{
UserData ud;
auto err = ud.Init(GetTempDir().c_str(), dev);
ASSERT_FALSE(err);
auto v = ud.GetLocale();
ASSERT_THAT(v, IsEmpty());
err = ud.SetLocale("en-US");
ASSERT_FALSE(err);
v = ud.GetLocale();
ASSERT_EQ(v, "en-US");
err = ud.SetLocale("");
ASSERT_FALSE(err);
v = ud.GetLocale();
ASSERT_EQ(v, "");
}
TEST_F(TestUserData, Transaction)
{
UserData ud;
auto err = ud.Init(GetTempDir().c_str(), dev);
ASSERT_FALSE(err);
// We're only using specific accessors for easy testing
err = ud.SetLocale("1");
ASSERT_FALSE(err);
auto v = ud.GetLocale();
ASSERT_EQ(v, "1");
{
UserData::Transaction udt(ud);
err = ud.SetLocale("2");
ASSERT_FALSE(err);
auto v = ud.GetLocale();
ASSERT_EQ(v, "2");
// dtor rollback
}
v = ud.GetLocale();
ASSERT_EQ(v, "1");
{
UserData::Transaction udt(ud);
err = ud.SetLocale("3");
ASSERT_FALSE(err);
auto err = udt.Commit();
ASSERT_FALSE(err);
}
v = ud.GetLocale();
ASSERT_EQ(v, "3");
{
UserData::Transaction udt(ud);
err = ud.SetLocale("4");
ASSERT_FALSE(err);
auto err = udt.Rollback();
ASSERT_FALSE(err);
}
v = ud.GetLocale();
ASSERT_EQ(v, "3");
{
UserData::Transaction udt(ud);
err = ud.SetLocale("5");
ASSERT_FALSE(err);
auto err = udt.Commit();
ASSERT_FALSE(err);
err = udt.Rollback(); // does nothing
ASSERT_FALSE(err);
err = udt.Commit(); // does nothing
ASSERT_FALSE(err);
}
v = ud.GetLocale();
ASSERT_EQ(v, "5");
{
UserData::Transaction udt(ud);
auto err = udt.Rollback();
ASSERT_FALSE(err);
// Modify _after_ rollback
err = ud.SetLocale("6");
ASSERT_FALSE(err);
// Extra rollback should do nothing
err = udt.Rollback();
ASSERT_FALSE(err);
}
v = ud.GetLocale();
ASSERT_EQ(v, "6");
{
UserData::Transaction udt(ud);
{
UserData::Transaction udt(ud);
{
UserData::Transaction udt(ud);
err = ud.SetLocale("7");
ASSERT_FALSE(err);
// inner commit does nothing
auto err = udt.Commit();
ASSERT_FALSE(err);
}
auto v = ud.GetIsAccount();
ASSERT_EQ(v, false);
err = ud.SetIsAccount(true);
ASSERT_FALSE(err);
// inner rollback does nothing
auto err = udt.Rollback();
ASSERT_FALSE(err);
}
// We have committed one inner transaction and rolled back the another, but they have no effect on the outer.
// Now we're commit the outer transaction.
udt.Commit();
}
v = ud.GetLocale();
ASSERT_EQ(v, "7");
auto b = ud.GetIsAccount();
ASSERT_EQ(b, true);
}