forked from microsoft/winget-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDownloader.cpp
More file actions
171 lines (145 loc) · 5.31 KB
/
Copy pathDownloader.cpp
File metadata and controls
171 lines (145 loc) · 5.31 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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#include "pch.h"
#include "TestCommon.h"
#include "AppInstallerDownloader.h"
#include "AppInstallerSHA256.h"
#include "HttpStream/HttpLocalCache.h"
using namespace AppInstaller;
using namespace AppInstaller::Utility;
using namespace std::string_literals;
TEST_CASE("DownloadValidFileAndVerifyHash", "[Downloader]")
{
TestCommon::TempFile tempFile("downloader_test"s, ".test"s);
INFO("Using temporary file named: " << tempFile.GetPath());
// Todo: point to files from our repo when the repo goes public
ProgressCallback callback;
auto result = Download("https://raw.githubusercontent.com/microsoft/msix-packaging/master/LICENSE", tempFile.GetPath(), DownloadType::Manifest, callback);
REQUIRE(!result.Sha256Hash.empty());
auto resultHash = result.Sha256Hash;
auto expectedHash = SHA256::ConvertToBytes("d2a45116709136462ee7a1c42f0e75f0efa258fe959b1504dc8ea4573451b759");
REQUIRE(std::equal(
expectedHash.begin(),
expectedHash.end(),
resultHash.begin()));
uint64_t expectedFileSize = 1119;
REQUIRE(result.SizeInBytes == expectedFileSize);
REQUIRE(std::filesystem::file_size(tempFile.GetPath()) == expectedFileSize);
REQUIRE(result.ContentType);
REQUIRE(!result.ContentType.value().empty());
// Verify motw content
std::filesystem::path motwFile(tempFile);
motwFile += ":Zone.Identifier:$data";
std::ifstream motwStream(motwFile);
std::stringstream motwContent;
motwContent << motwStream.rdbuf();
std::string motwContentStr = motwContent.str();
REQUIRE(motwContentStr.find("ZoneId=3") != std::string::npos);
}
TEST_CASE("DownloadValidFileAndCancel", "[Downloader]")
{
TestCommon::TempFile tempFile("downloader_test"s, ".test"s);
INFO("Using temporary file named: " << tempFile.GetPath());
ProgressCallback callback;
DownloadResult waitResult;
std::thread waitThread([&]
{
waitResult = Download("https://aka.ms/win32-x64-user-stable", tempFile.GetPath(), DownloadType::Installer, callback);
});
callback.Cancel();
waitThread.join();
REQUIRE(waitResult.Sha256Hash.empty());
}
TEST_CASE("DownloadInvalidUrl", "[Downloader]")
{
TestCommon::TempFile tempFile("downloader_test"s, ".test"s);
INFO("Using temporary file named: " << tempFile.GetPath());
ProgressCallback callback;
REQUIRE_THROWS(Download("blargle-flargle-fluff", tempFile.GetPath(), DownloadType::Installer, callback));
}
TEST_CASE("HttpStream_ReadLastFullPage", "[HttpStream]")
{
Microsoft::WRL::ComPtr<IStream> stream;
STATSTG stat = { 0 };
for (size_t i = 0; i < 10; ++i)
{
stream = GetReadOnlyStreamFromURI("https://aka.ms/win32-x64-user-stable");
stat = { 0 };
REQUIRE(stream->Stat(&stat, STATFLAG_NONAME) == S_OK);
if (stat.cbSize.QuadPart > 0)
{
break;
}
Sleep(500);
}
{
INFO("https://aka.ms/win32-x64-user-stable gave back a 0 byte file");
REQUIRE(stream);
}
LARGE_INTEGER seek;
seek.QuadPart = (stat.cbSize.QuadPart / HttpStream::HttpLocalCache::PAGE_SIZE) * HttpStream::HttpLocalCache::PAGE_SIZE;
REQUIRE(stream->Seek(seek, STREAM_SEEK_SET, nullptr) == S_OK);
std::unique_ptr<BYTE[]> buffer = std::make_unique<BYTE[]>(HttpStream::HttpLocalCache::PAGE_SIZE);
ULONG read = 0;
REQUIRE(stream->Read(buffer.get(), static_cast<ULONG>(HttpStream::HttpLocalCache::PAGE_SIZE), &read) >= S_OK);
REQUIRE(read == (stat.cbSize.QuadPart % HttpStream::HttpLocalCache::PAGE_SIZE));
}
TEST_CASE("CacheControl", "[Downloader]")
{
SECTION("Empty")
{
CacheControlPolicy test{ L"" };
REQUIRE(!test.Present);
}
SECTION("Space")
{
CacheControlPolicy test{ L" " };
REQUIRE(!test.Present);
}
SECTION("Standard")
{
CacheControlPolicy test{ L"public, max-age=77287" };
REQUIRE(test.Present);
REQUIRE(test.Public);
REQUIRE(!test.NoCache);
REQUIRE(!test.NoStore);
REQUIRE(test.MaxAge == 77287);
}
SECTION("All")
{
CacheControlPolicy test{ L"public, no-cache, no-store, max-age = 77" };
REQUIRE(test.Present);
REQUIRE(test.Public);
REQUIRE(test.NoCache);
REQUIRE(test.NoStore);
REQUIRE(test.MaxAge == 77);
}
SECTION("Casing")
{
CacheControlPolicy test{ L"Public, Max-Age=42" };
REQUIRE(test.Present);
REQUIRE(test.Public);
REQUIRE(!test.NoCache);
REQUIRE(!test.NoStore);
REQUIRE(test.MaxAge == 42);
}
SECTION("Unknown")
{
CacheControlPolicy test{ L"public, max-age=77287, not-real" };
REQUIRE(test.Present);
REQUIRE(test.Public);
REQUIRE(!test.NoCache);
REQUIRE(!test.NoStore);
REQUIRE(test.MaxAge == 77287);
}
SECTION("MaxAge Negative")
{
CacheControlPolicy test{ L"max-age=-1" };
REQUIRE(test.MaxAge == CacheControlPolicy::MaximumMaxAge);
}
SECTION("MaxAge not a number")
{
CacheControlPolicy test{ L"max-age=FOO" };
REQUIRE(test.MaxAge == 0);
}
}