forked from microsoft/winget-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQLiteIndex.cpp
More file actions
2132 lines (1674 loc) · 81.6 KB
/
Copy pathSQLiteIndex.cpp
File metadata and controls
2132 lines (1674 loc) · 81.6 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
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) Microsoft Corporation.
// Licensed under the MIT License.
#include "pch.h"
#include "TestCommon.h"
#include <SQLiteWrapper.h>
#include <Microsoft/SQLiteIndex.h>
#include <winget/Manifest.h>
#include <AppInstallerStrings.h>
#include <Microsoft/Schema/1_0/IdTable.h>
#include <Microsoft/Schema/1_0/NameTable.h>
#include <Microsoft/Schema/1_0/MonikerTable.h>
#include <Microsoft/Schema/1_0/VersionTable.h>
#include <Microsoft/Schema/1_0/ChannelTable.h>
#include <Microsoft/Schema/1_0/PathPartTable.h>
#include <Microsoft/Schema/1_0/ManifestTable.h>
#include <Microsoft/Schema/1_0/TagsTable.h>
#include <Microsoft/Schema/1_0/CommandsTable.h>
#include <Microsoft/Schema/1_0/SearchResultsTable.h>
using namespace std::string_literals;
using namespace std::string_view_literals;
using namespace TestCommon;
using namespace AppInstaller::Manifest;
using namespace AppInstaller::Repository;
using namespace AppInstaller::Repository::Microsoft;
using namespace AppInstaller::Repository::SQLite;
using namespace AppInstaller::Utility;
SQLiteIndex CreateTestIndex(const std::string& filePath, std::optional<Schema::Version> version = {})
{
// If no specific version requested, then use generator to run against all versions.
if (!version)
{
version = GENERATE(Schema::Version{ 1, 0 }, Schema::Version{ 1, 1 }, Schema::Version::Latest());
}
return SQLiteIndex::CreateNew(filePath, version.value());
}
Schema::Version TestPrepareForRead(SQLiteIndex& index)
{
// This will only be called for tests that want to support cross version checks.
// Based on the version of the incoming, we only want to generate versions less or equal to it.
if (index.GetVersion() == Schema::Version{ 1, 0 })
{
// Nothing to do here
}
else if (index.GetVersion() == Schema::Version{ 1, 1 })
{
auto changeVersion = GENERATE(false, true);
if (changeVersion)
{
index.ForceVersion(Schema::Version{ 1, 0 });
return { 1, 0 };
}
}
else if (index.GetVersion() == Schema::Version{ 1, 2 })
{
Schema::Version version = GENERATE(Schema::Version{ 1, 0 }, Schema::Version{ 1, 1 }, Schema::Version{ 1, 2 });
if (version != Schema::Version{ 1, 2 })
{
index.ForceVersion(version);
return version;
}
}
return index.GetVersion();
}
SQLiteIndex SimpleTestSetup(const std::string& filePath, Manifest& manifest, std::string& relativePath, std::optional<Schema::Version> version = {})
{
SQLiteIndex index = CreateTestIndex(filePath, version);
manifest.Installers.push_back({});
manifest.Id = "Test.Id";
manifest.DefaultLocalization.Add<Localization::PackageName>("Test Name");
manifest.Moniker = "testmoniker";
manifest.Version = "1.0.0";
manifest.Channel = "test";
manifest.DefaultLocalization.Add<Localization::Tags>({ "t1", "t2" });
manifest.Installers[0].Commands = { "test1", "test2" };
relativePath = "test/id/1.0.0.yaml";
index.AddManifest(manifest, relativePath);
return index;
}
struct IndexFields
{
IndexFields(
std::string id,
std::string name,
std::string moniker,
std::string version,
std::string channel,
std::vector<NormalizedString> tags,
std::vector<NormalizedString> commands,
std::string path
) :
Id(std::move(id)),
Name(std::move(name)),
Moniker(std::move(moniker)),
Version(std::move(version)),
Channel(std::move(channel)),
Tags(std::move(tags)),
Commands(std::move(commands)),
Path(std::move(path))
{}
IndexFields(
std::string id,
std::string name,
std::string moniker,
std::string version,
std::string channel,
std::vector<NormalizedString> tags,
std::vector<NormalizedString> commands,
std::string path,
std::vector<NormalizedString> packageFamilyNames,
std::vector<NormalizedString> productCodes
) :
Id(std::move(id)),
Name(std::move(name)),
Moniker(std::move(moniker)),
Version(std::move(version)),
Channel(std::move(channel)),
Tags(std::move(tags)),
Commands(std::move(commands)),
Path(std::move(path)),
PackageFamilyNames(std::move(packageFamilyNames)),
ProductCodes(std::move(productCodes))
{}
IndexFields(
std::string id,
std::string name,
std::string publisher,
std::string moniker,
std::string version,
std::string channel,
std::vector<NormalizedString> tags,
std::vector<NormalizedString> commands,
std::string path,
std::vector<NormalizedString> packageFamilyNames,
std::vector<NormalizedString> productCodes
) :
Id(std::move(id)),
Name(std::move(name)),
Publisher(std::move(publisher)),
Moniker(std::move(moniker)),
Version(std::move(version)),
Channel(std::move(channel)),
Tags(std::move(tags)),
Commands(std::move(commands)),
Path(std::move(path)),
PackageFamilyNames(std::move(packageFamilyNames)),
ProductCodes(std::move(productCodes))
{}
std::string Id;
std::string Name;
std::string Publisher;
std::string Moniker;
std::string Version;
std::string Channel;
std::vector<NormalizedString> Tags;
std::vector<NormalizedString> Commands;
std::string Path;
std::vector<NormalizedString> PackageFamilyNames;
std::vector<NormalizedString> ProductCodes;
};
SQLiteIndex SearchTestSetup(const std::string& filePath, std::initializer_list<IndexFields> data = {}, std::optional<Schema::Version> version = {})
{
SQLiteIndex index = CreateTestIndex(filePath, version);
Manifest manifest;
auto addFunc = [&](const IndexFields& d)
{
manifest.Id = d.Id;
manifest.DefaultLocalization.Add<Localization::PackageName>(d.Name);
manifest.DefaultLocalization.Add<Localization::Publisher>(d.Publisher);
manifest.Moniker = d.Moniker;
manifest.Version = d.Version;
manifest.DefaultLocalization.Add<Localization::Tags>(d.Tags);
manifest.Installers.resize(std::max(d.PackageFamilyNames.size(), d.ProductCodes.size()));
if (manifest.Installers.size() == 0)
{
manifest.Installers.push_back({});
}
manifest.Channel = d.Channel;
manifest.Installers[0].Commands = d.Commands;
for (size_t i = 0; i < d.PackageFamilyNames.size(); ++i)
{
manifest.Installers[i].PackageFamilyName = d.PackageFamilyNames[i];
}
for (size_t i = 0; i < d.ProductCodes.size(); ++i)
{
manifest.Installers[i].ProductCode = d.ProductCodes[i];
}
index.AddManifest(manifest, d.Path);
};
for (const auto& d : data)
{
addFunc(d);
}
return index;
}
bool ArePackageFamilyNameAndProductCodeSupported(const SQLiteIndex& index, const Schema::Version& testVersion)
{
UNSCOPED_INFO("Index " << index.GetVersion() << " | Test " << testVersion);
return (index.GetVersion() >= Schema::Version{ 1, 1 } && testVersion >= Schema::Version{ 1, 1 });
}
bool AreNormalizedNameAndPublisherSupported(const SQLiteIndex& index, const Schema::Version& testVersion)
{
UNSCOPED_INFO("Index " << index.GetVersion() << " | Test " << testVersion);
return (index.GetVersion() >= Schema::Version{ 1, 2 } && testVersion >= Schema::Version{ 1, 2 });
}
bool IsManifestMetadataSupported(const SQLiteIndex& index, const Schema::Version& testVersion)
{
UNSCOPED_INFO("Index " << index.GetVersion() << " | Test " << testVersion);
return (index.GetVersion() >= Schema::Version{ 1, 1 } && testVersion >= Schema::Version{ 1, 1 });
}
std::string GetPropertyStringByKey(const SQLiteIndex& index, SQLite::rowid_t id, PackageVersionProperty property, std::string_view version, std::string_view channel)
{
auto manifestId = index.GetManifestIdByKey(id, version, channel);
REQUIRE(manifestId);
auto result = index.GetPropertyByManifestId(manifestId.value(), property);
REQUIRE(result);
return result.value();
}
std::string GetPropertyStringById(const SQLiteIndex& index, SQLite::rowid_t id, PackageVersionProperty property)
{
auto versions = index.GetVersionKeysById(id);
REQUIRE(!versions.empty());
return GetPropertyStringByKey(index, id, property, versions[0].GetVersion().ToString(), versions[0].GetChannel().ToString());
}
std::string GetIdStringById(const SQLiteIndex& index, SQLite::rowid_t id)
{
return GetPropertyStringById(index, id, PackageVersionProperty::Id);
}
std::string GetNameStringById(const SQLiteIndex& index, SQLite::rowid_t id)
{
return GetPropertyStringById(index, id, PackageVersionProperty::Name);
}
std::string GetPathStringByKey(const SQLiteIndex& index, SQLite::rowid_t id, std::string_view version, std::string_view channel)
{
return GetPropertyStringByKey(index, id, PackageVersionProperty::RelativePath, version, channel);
}
TEST_CASE("SQLiteIndexCreateLatestAndReopen", "[sqliteindex]")
{
TempFile tempFile{ "repolibtest_tempdb"s, ".db"s };
INFO("Using temporary file named: " << tempFile.GetPath());
Schema::Version versionCreated;
// Create the index
{
SQLiteIndex index = SQLiteIndex::CreateNew(tempFile, Schema::Version::Latest());
versionCreated = index.GetVersion();
}
// Reopen the index for read only
{
INFO("Trying with Read");
SQLiteIndex index = SQLiteIndex::Open(tempFile, SQLiteIndex::OpenDisposition::Read);
Schema::Version versionRead = index.GetVersion();
REQUIRE(versionRead == versionCreated);
}
// Reopen the index for read/write
{
INFO("Trying with ReadWrite");
SQLiteIndex index = SQLiteIndex::Open(tempFile, SQLiteIndex::OpenDisposition::ReadWrite);
Schema::Version versionRead = index.GetVersion();
REQUIRE(versionRead == versionCreated);
}
// Reopen the index for immutable read
{
INFO("Trying with Immutable");
SQLiteIndex index = SQLiteIndex::Open(tempFile, SQLiteIndex::OpenDisposition::Immutable);
Schema::Version versionRead = index.GetVersion();
REQUIRE(versionRead == versionCreated);
}
}
TEST_CASE("SQLiteIndexCreateAndAddManifest", "[sqliteindex]")
{
TempFile tempFile{ "repolibtest_tempdb"s, ".db"s };
INFO("Using temporary file named: " << tempFile.GetPath());
Manifest manifest;
std::string relativePath;
SQLiteIndex index = SimpleTestSetup(tempFile, manifest, relativePath);
}
TEST_CASE("SQLiteIndexCreateAndAddManifestFile", "[sqliteindex]")
{
TempFile tempFile{ "repolibtest_tempdb"s, ".db"s };
INFO("Using temporary file named: " << tempFile.GetPath());
SQLiteIndex index = CreateTestIndex(tempFile);
TestDataFile manifestFile{ "Manifest-Good.yaml" };
std::filesystem::path manifestPath{ "microsoft/msixsdk/microsoft.msixsdk-1.7.32.yaml" };
index.AddManifest(manifestFile, manifestPath);
}
TEST_CASE("SQLiteIndexCreateAndAddManifestDuplicate", "[sqliteindex]")
{
TempFile tempFile{ "repolibtest_tempdb"s, ".db"s };
INFO("Using temporary file named: " << tempFile.GetPath());
Manifest manifest;
std::string relativePath;
SQLiteIndex index = SimpleTestSetup(tempFile, manifest, relativePath);
// Attempting to add the same manifest at a different path should fail.
REQUIRE_THROWS_HR(index.AddManifest(manifest, "differentpath.yaml"), HRESULT_FROM_WIN32(ERROR_ALREADY_EXISTS));
// Attempting to add the same manifest with a differently cased Id at a different path should fail.
manifest.Id = ToLower(manifest.Id);
REQUIRE_THROWS_HR(index.AddManifest(manifest, "differentpath.yaml"), HRESULT_FROM_WIN32(ERROR_ALREADY_EXISTS));
// Attempting to add a different manifest at the same path should fail.
manifest.Id += "-new";
REQUIRE_THROWS_HR(index.AddManifest(manifest, relativePath), HRESULT_FROM_WIN32(ERROR_ALREADY_EXISTS));
}
TEST_CASE("SQLiteIndex_RemoveManifestFile_NotPresent", "[sqliteindex]")
{
SQLiteIndex index = CreateTestIndex(SQLITE_MEMORY_DB_CONNECTION_TARGET);
TestDataFile manifestFile{ "Manifest-Good.yaml" };
std::filesystem::path manifestPath{ "microsoft/msixsdk/microsoft.msixsdk-1.7.32.yaml" };
REQUIRE_THROWS_HR(index.RemoveManifest(manifestFile, manifestPath), E_NOT_SET);
}
TEST_CASE("SQLiteIndex_RemoveManifest", "[sqliteindex][V1_0]")
{
TempFile tempFile{ "repolibtest_tempdb"s, ".db"s };
INFO("Using temporary file named: " << tempFile.GetPath());
std::string manifest1Path = "test/id/test.id-1.0.0.yaml";
Manifest manifest1;
manifest1.Installers.push_back({});
manifest1.Id = "test.id";
manifest1.DefaultLocalization.Add<Localization::PackageName>("Test Name");
manifest1.Moniker = "testmoniker";
manifest1.Version = "1.0.0";
manifest1.Channel = "test";
manifest1.DefaultLocalization.Add<Localization::Tags>({ "t1", "t2" });
manifest1.Installers[0].Commands = { "test1", "test2" };
std::string manifest2Path = "test/woah/test.id-1.0.0.yaml";
Manifest manifest2;
manifest2.Installers.push_back({});
manifest2.Id = "test.woah";
manifest2.DefaultLocalization.Add<Localization::PackageName>("Test Name WOAH");
manifest2.Moniker = "testmoniker";
manifest2.Version = "1.0.0";
manifest2.Channel = "test";
manifest2.DefaultLocalization.Add<Localization::Tags>({});
manifest2.Installers[0].Commands = { "test1", "test2", "test3" };
{
SQLiteIndex index = SQLiteIndex::CreateNew(tempFile, { 1, 0 });
index.AddManifest(manifest1, manifest1Path);
index.AddManifest(manifest2, manifest2Path);
// Now remove manifest1
index.RemoveManifest(manifest1, manifest1Path);
}
{
// Open it directly to directly test table state
Connection connection = Connection::Create(tempFile, Connection::OpenDisposition::ReadWrite);
REQUIRE(!Schema::V1_0::ManifestTable::IsEmpty(connection));
REQUIRE(!Schema::V1_0::IdTable::IsEmpty(connection));
REQUIRE(!Schema::V1_0::NameTable::IsEmpty(connection));
REQUIRE(!Schema::V1_0::MonikerTable::IsEmpty(connection));
REQUIRE(!Schema::V1_0::VersionTable::IsEmpty(connection));
REQUIRE(!Schema::V1_0::ChannelTable::IsEmpty(connection));
REQUIRE(!Schema::V1_0::PathPartTable::IsEmpty(connection));
// Because manifest2 had no tags
REQUIRE(Schema::V1_0::TagsTable::IsEmpty(connection));
REQUIRE(!Schema::V1_0::CommandsTable::IsEmpty(connection));
}
{
SQLiteIndex index = SQLiteIndex::Open(tempFile, SQLiteIndex::OpenDisposition::ReadWrite);
// Now remove manifest2
index.RemoveManifest(manifest2, manifest2Path);
}
// Open it directly to directly test table state
Connection connection = Connection::Create(tempFile, Connection::OpenDisposition::ReadWrite);
REQUIRE(Schema::V1_0::ManifestTable::IsEmpty(connection));
REQUIRE(Schema::V1_0::IdTable::IsEmpty(connection));
REQUIRE(Schema::V1_0::NameTable::IsEmpty(connection));
REQUIRE(Schema::V1_0::MonikerTable::IsEmpty(connection));
REQUIRE(Schema::V1_0::VersionTable::IsEmpty(connection));
REQUIRE(Schema::V1_0::ChannelTable::IsEmpty(connection));
REQUIRE(Schema::V1_0::PathPartTable::IsEmpty(connection));
REQUIRE(Schema::V1_0::TagsTable::IsEmpty(connection));
REQUIRE(Schema::V1_0::CommandsTable::IsEmpty(connection));
}
TEST_CASE("SQLiteIndex_RemoveManifest_EnsureConsistentRowId", "[sqliteindex]")
{
TempFile tempFile{ "repolibtest_tempdb"s, ".db"s };
INFO("Using temporary file named: " << tempFile.GetPath());
std::string manifest1Path = "test/id/test.id-1.0.0.yaml";
Manifest manifest1;
manifest1.Installers.push_back({});
manifest1.Id = "test.id";
manifest1.DefaultLocalization.Add<Localization::PackageName>("Test Name");
manifest1.Moniker = "testmoniker";
manifest1.Version = "1.0.0";
manifest1.Channel = "test";
manifest1.DefaultLocalization.Add<Localization::Tags>({ "t1", "t2" });
manifest1.Installers[0].Commands = { "test1", "test2" };
std::string manifest2Path = "test/woah/test.id-1.0.0.yaml";
Manifest manifest2;
manifest2.Installers.push_back({});
manifest2.Id = "test.woah";
manifest2.DefaultLocalization.Add<Localization::PackageName>("Test Name WOAH");
manifest2.Moniker = "testmoniker";
manifest2.Version = "1.0.0";
manifest2.Channel = "test";
manifest2.DefaultLocalization.Add<Localization::Tags>({});
manifest2.Installers[0].Commands = { "test1", "test2", "test3" };
SQLiteIndex index = CreateTestIndex(tempFile);
index.AddManifest(manifest1, manifest1Path);
index.AddManifest(manifest2, manifest2Path);
// Get the second manifest's id for validating consistency
SearchRequest request;
request.Inclusions.emplace_back(PackageMatchFilter(PackageMatchField::Id, MatchType::Exact, manifest2.Id));
auto result = index.Search(request);
REQUIRE(result.Matches.size() == 1);
auto manifest2IdRowId = result.Matches[0].first;
auto rowId = index.GetManifestIdByKey(manifest2IdRowId, {}, {});
REQUIRE(rowId);
auto manifest2RowId = rowId.value();
// Now remove manifest1 and prepare
index.RemoveManifest(manifest1, manifest1Path);
index.PrepareForPackaging();
// Checking consistency will also uncover issues, but not potentially the same ones as below.
REQUIRE(index.CheckConsistency(true));
// Repeat search to ensure consistent ids
result = index.Search(request);
REQUIRE(result.Matches.size() == 1);
REQUIRE(result.Matches[0].first == manifest2IdRowId);
rowId = index.GetManifestIdByKey(manifest2IdRowId, {}, {});
REQUIRE(rowId);
REQUIRE(rowId.value() == manifest2RowId);
REQUIRE(manifest2.Id == index.GetPropertyByManifestId(manifest2RowId, PackageVersionProperty::Id));
REQUIRE(manifest2.DefaultLocalization.Get<Localization::PackageName>() == index.GetPropertyByManifestId(manifest2RowId, PackageVersionProperty::Name));
REQUIRE(manifest2.Version == index.GetPropertyByManifestId(manifest2RowId, PackageVersionProperty::Version));
REQUIRE(manifest2.Channel == index.GetPropertyByManifestId(manifest2RowId, PackageVersionProperty::Channel));
REQUIRE(manifest2Path == index.GetPropertyByManifestId(manifest2RowId, PackageVersionProperty::RelativePath));
}
TEST_CASE("SQLiteIndex_RemoveManifestFile", "[sqliteindex][V1_0]")
{
TempFile tempFile{ "repolibtest_tempdb"s, ".db"s };
INFO("Using temporary file named: " << tempFile.GetPath());
{
SQLiteIndex index = SQLiteIndex::CreateNew(tempFile, { 1, 0 });
TestDataFile manifestFile{ "Manifest-Good.yaml" };
std::filesystem::path manifestPath{ "microsoft/msixsdk/microsoft.msixsdk-1.7.32.yaml" };
index.AddManifest(manifestFile, manifestPath);
// Now remove that manifest
index.RemoveManifest(manifestFile, manifestPath);
}
// Open it directly to directly test table state
Connection connection = Connection::Create(tempFile, Connection::OpenDisposition::ReadWrite);
REQUIRE(Schema::V1_0::ManifestTable::IsEmpty(connection));
REQUIRE(Schema::V1_0::IdTable::IsEmpty(connection));
REQUIRE(Schema::V1_0::NameTable::IsEmpty(connection));
REQUIRE(Schema::V1_0::MonikerTable::IsEmpty(connection));
REQUIRE(Schema::V1_0::VersionTable::IsEmpty(connection));
REQUIRE(Schema::V1_0::ChannelTable::IsEmpty(connection));
REQUIRE(Schema::V1_0::PathPartTable::IsEmpty(connection));
REQUIRE(Schema::V1_0::TagsTable::IsEmpty(connection));
REQUIRE(Schema::V1_0::CommandsTable::IsEmpty(connection));
}
TEST_CASE("SQLiteIndex_UpdateManifest", "[sqliteindex][V1_0]")
{
TempFile tempFile{ "repolibtest_tempdb"s, ".db"s };
INFO("Using temporary file named: " << tempFile.GetPath());
std::string manifestPath = "test/id/test.id-1.0.0.yaml";
Manifest manifest;
manifest.Installers.push_back({});
manifest.Id = "test.id";
manifest.DefaultLocalization.Add < Localization::PackageName>("Test Name");
manifest.Moniker = "testmoniker";
manifest.Version = "1.0.0";
manifest.Channel = "test";
manifest.DefaultLocalization.Add<Localization::Tags>({ "t1", "t2" });
manifest.Installers[0].Commands = { "test1", "test2" };
{
SQLiteIndex index = SQLiteIndex::CreateNew(tempFile, { 1, 0 });
index.AddManifest(manifest, manifestPath);
}
{
// Open it directly to directly test table state
Connection connection = Connection::Create(tempFile, Connection::OpenDisposition::ReadWrite);
REQUIRE(!Schema::V1_0::ManifestTable::IsEmpty(connection));
REQUIRE(!Schema::V1_0::IdTable::IsEmpty(connection));
REQUIRE(!Schema::V1_0::NameTable::IsEmpty(connection));
REQUIRE(!Schema::V1_0::MonikerTable::IsEmpty(connection));
REQUIRE(!Schema::V1_0::VersionTable::IsEmpty(connection));
REQUIRE(!Schema::V1_0::ChannelTable::IsEmpty(connection));
REQUIRE(!Schema::V1_0::PathPartTable::IsEmpty(connection));
REQUIRE(!Schema::V1_0::TagsTable::IsEmpty(connection));
REQUIRE(!Schema::V1_0::CommandsTable::IsEmpty(connection));
}
{
SQLiteIndex index = SQLiteIndex::Open(tempFile, SQLiteIndex::OpenDisposition::ReadWrite);
// Update with no updates should return false
REQUIRE(!index.UpdateManifest(manifest, manifestPath));
manifest.DefaultLocalization.Add<Localization::Description>("description2");
// Update with no indexed updates should return false
REQUIRE(!index.UpdateManifest(manifest, manifestPath));
// Update with indexed changes
manifest.DefaultLocalization.Add<Localization::PackageName>("Test Name2");
manifest.Moniker = "testmoniker2";
manifest.DefaultLocalization.Add<Localization::Tags>({ "t1", "t2", "t3" });
manifest.Installers[0].Commands = {};
REQUIRE(index.UpdateManifest(manifest, manifestPath));
}
{
// Open it directly to directly test table state
Connection connection = Connection::Create(tempFile, Connection::OpenDisposition::ReadWrite);
REQUIRE(!Schema::V1_0::ManifestTable::IsEmpty(connection));
REQUIRE(!Schema::V1_0::IdTable::IsEmpty(connection));
REQUIRE(!Schema::V1_0::NameTable::IsEmpty(connection));
REQUIRE(!Schema::V1_0::MonikerTable::IsEmpty(connection));
REQUIRE(!Schema::V1_0::VersionTable::IsEmpty(connection));
REQUIRE(!Schema::V1_0::ChannelTable::IsEmpty(connection));
REQUIRE(!Schema::V1_0::PathPartTable::IsEmpty(connection));
REQUIRE(!Schema::V1_0::TagsTable::IsEmpty(connection));
// The update removed all commands
REQUIRE(Schema::V1_0::CommandsTable::IsEmpty(connection));
}
{
SQLiteIndex index = SQLiteIndex::Open(tempFile, SQLiteIndex::OpenDisposition::ReadWrite);
// Now remove manifest2
index.RemoveManifest(manifest, manifestPath);
}
// Open it directly to directly test table state
Connection connection = Connection::Create(tempFile, Connection::OpenDisposition::ReadWrite);
REQUIRE(Schema::V1_0::ManifestTable::IsEmpty(connection));
REQUIRE(Schema::V1_0::IdTable::IsEmpty(connection));
REQUIRE(Schema::V1_0::NameTable::IsEmpty(connection));
REQUIRE(Schema::V1_0::MonikerTable::IsEmpty(connection));
REQUIRE(Schema::V1_0::VersionTable::IsEmpty(connection));
REQUIRE(Schema::V1_0::ChannelTable::IsEmpty(connection));
REQUIRE(Schema::V1_0::PathPartTable::IsEmpty(connection));
REQUIRE(Schema::V1_0::TagsTable::IsEmpty(connection));
REQUIRE(Schema::V1_0::CommandsTable::IsEmpty(connection));
}
TEST_CASE("SQLiteIndex_UpdateManifestChangePath", "[sqliteindex][V1_0]")
{
TempFile tempFile{ "repolibtest_tempdb"s, ".db"s };
INFO("Using temporary file named: " << tempFile.GetPath());
std::string manifestPath = "test/id/test.id-1.0.0.yaml";
Manifest manifest;
manifest.Installers.push_back({});
manifest.Id = "test.id";
manifest.DefaultLocalization.Add<Localization::PackageName>("Test Name");
manifest.Moniker = "testmoniker";
manifest.Version = "1.0.0";
manifest.Channel = "test";
manifest.DefaultLocalization.Add<Localization::Tags>({ "t1", "t2" });
manifest.Installers[0].Commands = { "test1", "test2" };
{
SQLiteIndex index = SQLiteIndex::CreateNew(tempFile, { 1, 0 });
index.AddManifest(manifest, manifestPath);
}
{
// Open it directly to directly test table state
Connection connection = Connection::Create(tempFile, Connection::OpenDisposition::ReadWrite);
REQUIRE(!Schema::V1_0::ManifestTable::IsEmpty(connection));
REQUIRE(!Schema::V1_0::IdTable::IsEmpty(connection));
REQUIRE(!Schema::V1_0::NameTable::IsEmpty(connection));
REQUIRE(!Schema::V1_0::MonikerTable::IsEmpty(connection));
REQUIRE(!Schema::V1_0::VersionTable::IsEmpty(connection));
REQUIRE(!Schema::V1_0::ChannelTable::IsEmpty(connection));
REQUIRE(!Schema::V1_0::PathPartTable::IsEmpty(connection));
REQUIRE(!Schema::V1_0::TagsTable::IsEmpty(connection));
REQUIRE(!Schema::V1_0::CommandsTable::IsEmpty(connection));
}
{
SQLiteIndex index = SQLiteIndex::Open(tempFile, SQLiteIndex::OpenDisposition::ReadWrite);
manifestPath = "test/newid/test.newid-1.0.0.yaml";
// Update with path update should indicate change
REQUIRE(index.UpdateManifest(manifest, manifestPath));
}
{
// Open it directly to directly test table state
Connection connection = Connection::Create(tempFile, Connection::OpenDisposition::ReadWrite);
REQUIRE(!Schema::V1_0::ManifestTable::IsEmpty(connection));
REQUIRE(!Schema::V1_0::IdTable::IsEmpty(connection));
REQUIRE(!Schema::V1_0::NameTable::IsEmpty(connection));
REQUIRE(!Schema::V1_0::MonikerTable::IsEmpty(connection));
REQUIRE(!Schema::V1_0::VersionTable::IsEmpty(connection));
REQUIRE(!Schema::V1_0::ChannelTable::IsEmpty(connection));
REQUIRE(!Schema::V1_0::PathPartTable::IsEmpty(connection));
REQUIRE(!Schema::V1_0::TagsTable::IsEmpty(connection));
REQUIRE(!Schema::V1_0::CommandsTable::IsEmpty(connection));
}
{
SQLiteIndex index = SQLiteIndex::Open(tempFile, SQLiteIndex::OpenDisposition::ReadWrite);
// Now remove manifest, with unknown path
index.RemoveManifest(manifest, "");
}
// Open it directly to directly test table state
Connection connection = Connection::Create(tempFile, Connection::OpenDisposition::ReadWrite);
REQUIRE(Schema::V1_0::ManifestTable::IsEmpty(connection));
REQUIRE(Schema::V1_0::IdTable::IsEmpty(connection));
REQUIRE(Schema::V1_0::NameTable::IsEmpty(connection));
REQUIRE(Schema::V1_0::MonikerTable::IsEmpty(connection));
REQUIRE(Schema::V1_0::VersionTable::IsEmpty(connection));
REQUIRE(Schema::V1_0::ChannelTable::IsEmpty(connection));
REQUIRE(Schema::V1_0::PathPartTable::IsEmpty(connection));
REQUIRE(Schema::V1_0::TagsTable::IsEmpty(connection));
REQUIRE(Schema::V1_0::CommandsTable::IsEmpty(connection));
}
TEST_CASE("SQLiteIndex_UpdateManifestChangeCase", "[sqliteindex][V1_0]")
{
TempFile tempFile{ "repolibtest_tempdb"s, ".db"s };
INFO("Using temporary file named: " << tempFile.GetPath());
std::string manifestPath = "test/id/test.id-1.0.0.yaml";
Manifest manifest;
manifest.Installers.push_back({});
manifest.Id = "test.id";
manifest.DefaultLocalization.Add<Localization::PackageName>("Test Name");
manifest.Moniker = "testmoniker";
manifest.Version = "1.0.0-test";
manifest.Channel = "test";
manifest.DefaultLocalization.Add<Localization::Tags>({ "t1", "t2" });
manifest.Installers[0].Commands = { "test1", "test2" };
{
SQLiteIndex index = SQLiteIndex::CreateNew(tempFile, { 1, 0 });
index.AddManifest(manifest, manifestPath);
}
{
SQLiteIndex index = SQLiteIndex::Open(tempFile, SQLiteIndex::OpenDisposition::ReadWrite);
manifest.Id = "Test.Id";
// Update with path update should indicate change
REQUIRE(index.UpdateManifest(manifest, manifestPath));
}
{
SQLiteIndex index = SQLiteIndex::Open(tempFile, SQLiteIndex::OpenDisposition::ReadWrite);
manifest.Version = "1.0.0-Test";
// Update with path update should indicate change
REQUIRE(index.UpdateManifest(manifest, manifestPath));
}
{
SQLiteIndex index = SQLiteIndex::Open(tempFile, SQLiteIndex::OpenDisposition::ReadWrite);
manifest.Channel = "Test";
// Update with path update should indicate change
REQUIRE(index.UpdateManifest(manifest, manifestPath));
}
{
SQLiteIndex index = SQLiteIndex::Open(tempFile, SQLiteIndex::OpenDisposition::ReadWrite);
manifest.DefaultLocalization.Add<Localization::PackageName>("test name");
// Update with path update should indicate change
REQUIRE(index.UpdateManifest(manifest, manifestPath));
}
{
SQLiteIndex index = SQLiteIndex::Open(tempFile, SQLiteIndex::OpenDisposition::ReadWrite);
// Now remove manifest, with unknown path
index.RemoveManifest(manifest, "");
}
// Open it directly to directly test table state
Connection connection = Connection::Create(tempFile, Connection::OpenDisposition::ReadWrite);
REQUIRE(Schema::V1_0::ManifestTable::IsEmpty(connection));
REQUIRE(Schema::V1_0::IdTable::IsEmpty(connection));
REQUIRE(Schema::V1_0::NameTable::IsEmpty(connection));
REQUIRE(Schema::V1_0::MonikerTable::IsEmpty(connection));
REQUIRE(Schema::V1_0::VersionTable::IsEmpty(connection));
REQUIRE(Schema::V1_0::ChannelTable::IsEmpty(connection));
REQUIRE(Schema::V1_0::PathPartTable::IsEmpty(connection));
REQUIRE(Schema::V1_0::TagsTable::IsEmpty(connection));
REQUIRE(Schema::V1_0::CommandsTable::IsEmpty(connection));
}
TEST_CASE("SQLiteIndex_IdCaseInsensitivity", "[sqliteindex][V1_0]")
{
TempFile tempFile{ "repolibtest_tempdb"s, ".db"s };
INFO("Using temporary file named: " << tempFile.GetPath());
std::string manifest1Path = "test/id/test.id-1.0.0.yaml";
Manifest manifest1;
manifest1.Installers.push_back({});
manifest1.Id = "test.id";
manifest1.DefaultLocalization.Add<Localization::PackageName>("Test Name");
manifest1.Moniker = "testmoniker";
manifest1.Version = "1.0.0";
manifest1.DefaultLocalization.Add<Localization::Tags>({ "t1", "t2" });
manifest1.Installers[0].Commands = { "test1", "test2" };
std::string manifest2Path = "test/id/test.id-2.0.0.yaml";
Manifest manifest2 = manifest1;
manifest2.Id = "Test.Id";
manifest1.Version = "2.0.0";
{
SQLiteIndex index = SQLiteIndex::CreateNew(tempFile, { 1, 0 });
index.AddManifest(manifest1, manifest1Path);
auto results = index.Search({});
REQUIRE(results.Matches.size() == 1);
REQUIRE(manifest1.Id == GetIdStringById(index, results.Matches[0].first));
}
{
SQLiteIndex index = SQLiteIndex::Open(tempFile, SQLiteIndex::OpenDisposition::ReadWrite);
index.AddManifest(manifest2, manifest2Path);
auto results = index.Search({});
REQUIRE(results.Matches.size() == 1);
REQUIRE(manifest2.Id == GetIdStringById(index, results.Matches[0].first));
}
{
SQLiteIndex index = SQLiteIndex::Open(tempFile, SQLiteIndex::OpenDisposition::ReadWrite);
manifest1.Id = "TEST.ID";
REQUIRE(index.UpdateManifest(manifest1, manifest1Path));
auto results = index.Search({});
REQUIRE(results.Matches.size() == 1);
REQUIRE(manifest1.Id == GetIdStringById(index, results.Matches[0].first));
}
{
SQLiteIndex index = SQLiteIndex::Open(tempFile, SQLiteIndex::OpenDisposition::ReadWrite);
index.RemoveManifest(manifest1, manifest1Path);
auto results = index.Search({});
REQUIRE(results.Matches.size() == 1);
REQUIRE(manifest1.Id == GetIdStringById(index, results.Matches[0].first));
}
{
SQLiteIndex index = SQLiteIndex::Open(tempFile, SQLiteIndex::OpenDisposition::ReadWrite);
index.RemoveManifest(manifest2, manifest2Path);
auto results = index.Search({});
REQUIRE(results.Matches.empty());
}
// Open it directly to directly test table state
Connection connection = Connection::Create(tempFile, Connection::OpenDisposition::ReadWrite);
REQUIRE(Schema::V1_0::ManifestTable::IsEmpty(connection));
REQUIRE(Schema::V1_0::IdTable::IsEmpty(connection));
REQUIRE(Schema::V1_0::NameTable::IsEmpty(connection));
REQUIRE(Schema::V1_0::MonikerTable::IsEmpty(connection));
REQUIRE(Schema::V1_0::VersionTable::IsEmpty(connection));
REQUIRE(Schema::V1_0::ChannelTable::IsEmpty(connection));
REQUIRE(Schema::V1_0::PathPartTable::IsEmpty(connection));
REQUIRE(Schema::V1_0::TagsTable::IsEmpty(connection));
REQUIRE(Schema::V1_0::CommandsTable::IsEmpty(connection));
}
TEST_CASE("PathPartTable_EnsurePathExists_Negative_Paths", "[sqliteindex][V1_0]")
{
// Open it directly to directly test pathpart table
Connection connection = Connection::Create(SQLITE_MEMORY_DB_CONNECTION_TARGET, Connection::OpenDisposition::Create);
REQUIRE_THROWS_HR(Schema::V1_0::PathPartTable::EnsurePathExists(connection, R"()", false), E_INVALIDARG);
REQUIRE_THROWS_HR(Schema::V1_0::PathPartTable::EnsurePathExists(connection, R"(\)", false), E_INVALIDARG);
REQUIRE_THROWS_HR(Schema::V1_0::PathPartTable::EnsurePathExists(connection, R"(/)", false), E_INVALIDARG);
REQUIRE_THROWS_HR(Schema::V1_0::PathPartTable::EnsurePathExists(connection, R"(C:)", false), E_INVALIDARG);
REQUIRE_THROWS_HR(Schema::V1_0::PathPartTable::EnsurePathExists(connection, R"(C:\\)", false), E_INVALIDARG);
REQUIRE_THROWS_HR(Schema::V1_0::PathPartTable::EnsurePathExists(connection, R"(C:\temp\path\file.txt)", false), E_INVALIDARG);
REQUIRE_THROWS_HR(Schema::V1_0::PathPartTable::EnsurePathExists(connection, R"(\temp\path\file.txt)", false), E_INVALIDARG);
}
TEST_CASE("PathPartTable_EnsurePathExists", "[sqliteindex][V1_0]")
{
TempFile tempFile{ "repolibtest_tempdb"s, ".db"s };
INFO("Using temporary file named: " << tempFile.GetPath());
// Create the index
{
SQLiteIndex index = SQLiteIndex::CreateNew(tempFile, { 1, 0 });
Schema::Version versionCreated = index.GetVersion();
REQUIRE(versionCreated == Schema::Version{ 1, 0 });
}
// Open it directly to directly test pathpart table
Connection connection = Connection::Create(tempFile, Connection::OpenDisposition::ReadWrite);
// attempt to find path that doesn't exist
auto result0 = Schema::V1_0::PathPartTable::EnsurePathExists(connection, R"(a\b\c.txt)", false);
REQUIRE(!std::get<0>(result0));
// add path
auto result1 = Schema::V1_0::PathPartTable::EnsurePathExists(connection, R"(a\b\c.txt)", true);
REQUIRE(std::get<0>(result1));
// Second time trying to create should return false and same id
auto result2 = Schema::V1_0::PathPartTable::EnsurePathExists(connection, R"(a\b\c.txt)", true);
REQUIRE(!std::get<0>(result2));
REQUIRE(std::get<1>(result1) == std::get<1>(result2));
// Trying to find but not create should return true because it exists
auto result3 = Schema::V1_0::PathPartTable::EnsurePathExists(connection, R"(a\b\c.txt)", false);
REQUIRE(std::get<0>(result3));
REQUIRE(std::get<1>(result1) == std::get<1>(result3));
// attempt to find a different file
auto result4 = Schema::V1_0::PathPartTable::EnsurePathExists(connection, R"(a\b\d.txt)", false);
REQUIRE(!std::get<0>(result4));
// add a different file
auto result5 = Schema::V1_0::PathPartTable::EnsurePathExists(connection, R"(a\b\d.txt)", true);
REQUIRE(std::get<0>(result5));
REQUIRE(std::get<1>(result1) != std::get<1>(result5));
// add the same file but deeper
auto result6 = Schema::V1_0::PathPartTable::EnsurePathExists(connection, R"(a\b\d\c.txt)", true);
REQUIRE(std::get<0>(result6));
REQUIRE(std::get<1>(result1) != std::get<1>(result6));
// get the deeper file with extra separators
auto result7 = Schema::V1_0::PathPartTable::EnsurePathExists(connection, R"(a\\b\d\\c.txt)", true);
REQUIRE(!std::get<0>(result7));
REQUIRE(std::get<1>(result6) == std::get<1>(result7));
}
TEST_CASE("SQLiteIndex_PrepareForPackaging", "[sqliteindex]")
{
TempFile tempFile{ "repolibtest_tempdb"s, ".db"s };
INFO("Using temporary file named: " << tempFile.GetPath());
SQLiteIndex index = CreateTestIndex(tempFile);
TestDataFile manifestFile{ "Manifest-Good.yaml" };
std::filesystem::path manifestPath{ "microsoft/msixsdk/microsoft.msixsdk-1.7.32.yaml" };
index.AddManifest(manifestFile, manifestPath);
index.PrepareForPackaging();
}
TEST_CASE("SQLiteIndex_Search_IdExactMatch", "[sqliteindex]")
{
TempFile tempFile{ "repolibtest_tempdb"s, ".db"s };
INFO("Using temporary file named: " << tempFile.GetPath());
Manifest manifest;
std::string relativePath;
SQLiteIndex index = SimpleTestSetup(tempFile, manifest, relativePath);
TestPrepareForRead(index);
SearchRequest request;
request.Query = RequestMatch(MatchType::Exact, manifest.Id);
auto results = index.Search(request);
REQUIRE(results.Matches.size() == 1);
REQUIRE(results.Matches[0].second.Field == PackageMatchField::Id);
REQUIRE(results.Matches[0].second.Type == MatchType::Exact);
REQUIRE(results.Matches[0].second.Value == manifest.Id);
}
TEST_CASE("SQLiteIndex_Search_MultipleMatch", "[sqliteindex]")
{
TempFile tempFile{ "repolibtest_tempdb"s, ".db"s };
INFO("Using temporary file named: " << tempFile.GetPath());
Manifest manifest;
std::string relativePath;
SQLiteIndex index = SimpleTestSetup(tempFile, manifest, relativePath);
manifest.Version = "2.0.0";
index.AddManifest(manifest, relativePath + "2");
TestPrepareForRead(index);
SearchRequest request;
request.Query = RequestMatch(MatchType::Exact, manifest.Id);
auto results = index.Search(request);
REQUIRE(results.Matches.size() == 1);