-
-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathFirmwarePackageTests.cs
405 lines (330 loc) · 18.2 KB
/
FirmwarePackageTests.cs
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using nanoFirmwareFlasher.Tests.Helpers;
using nanoFramework.Tools.FirmwareFlasher;
namespace nanoFirmwareFlasher.Tests
{
[TestClass]
public class FirmwarePackageTests
{
public TestContext TestContext { get; set; } = null!;
[TestMethod]
[TestCategory("CloudSmith")]
public void FirmwarePackage_ListReferenceTargets()
{
#region Get the stable packages
List<CloudSmithPackageDetail> stable = FirmwarePackage.GetTargetList(
false,
false,
null,
VerbosityLevel.Diagnostic);
Assert.IsNotNull(stable);
Assert.AreNotEqual(0, stable.Count);
#endregion
#region Get the preview packages
List<CloudSmithPackageDetail> preview = FirmwarePackage.GetTargetList(
false,
true,
null,
VerbosityLevel.Quiet);
Assert.IsNotNull(preview);
// Assert that the preview packages are not part of the stable package list
foreach (CloudSmithPackageDetail previewPackage in preview)
{
Assert.IsFalse((from s in stable
where s.Name == previewPackage.Name && s.Version == previewPackage.Version
select s).Any());
}
#endregion
#region Get the stable esp32 packages
List<CloudSmithPackageDetail> stableEsp32 = FirmwarePackage.GetTargetList(
false,
false,
SupportedPlatform.esp32,
VerbosityLevel.Diagnostic);
Assert.IsNotNull(stableEsp32);
Assert.AreNotEqual(0, stableEsp32.Count);
// Assert that there are more stable packages than for the esp32
Assert.IsTrue(stableEsp32.Count < stable.Count);
// Assert that all esp32 packages are in the stable list
foreach (CloudSmithPackageDetail esp32Package in stableEsp32)
{
Assert.IsTrue(stable.Any(s => s.Name == esp32Package.Name && s.Version == esp32Package.Version),
$"Package {esp32Package.Name} with version {esp32Package.Version} is not in the stable list.");
}
#endregion
#region Get the stable ti_simplelink packages
List<CloudSmithPackageDetail> stableTiSimpleLink = FirmwarePackage.GetTargetList(
false,
false,
SupportedPlatform.ti_simplelink,
VerbosityLevel.Quiet);
Assert.IsNotNull(stableTiSimpleLink);
Assert.AreNotEqual(0, stableTiSimpleLink.Count);
// Assert that there are more stable packages than for the ti_simplelink
Assert.IsTrue(stableTiSimpleLink.Count < stable.Count);
// Assert that all ti_simplelink packages are in the stable list
foreach (CloudSmithPackageDetail tiSimpleLinkPackage in stableTiSimpleLink)
{
Assert.IsTrue(stable.Any(s => s.Name == tiSimpleLinkPackage.Name && s.Version == tiSimpleLinkPackage.Version),
$"Package {tiSimpleLinkPackage.Name} with version {tiSimpleLinkPackage.Version} is not in the stable list.");
}
#endregion
#region Get the stable stm32 packages
List<CloudSmithPackageDetail> stableStm32 = FirmwarePackage.GetTargetList(
false,
false,
SupportedPlatform.stm32,
VerbosityLevel.Quiet);
Assert.IsNotNull(stableStm32);
Assert.AreNotEqual(0, stableStm32.Count);
// Assert that there are more stable packages than for the stm32
Assert.IsTrue(stableStm32.Count < stable.Count);
// Assert that all stm32 packages are in the stable list
foreach (CloudSmithPackageDetail stm32Package in stableStm32)
{
Assert.IsTrue(stable.Any(s => s.Name == stm32Package.Name && s.Version == stm32Package.Version),
$"Package {stm32Package.Name} with version {stm32Package.Version} is not in the stable list.");
}
#endregion
#region Get the stable efm32 packages
List<CloudSmithPackageDetail> stableEfm32 = FirmwarePackage.GetTargetList(
false,
false,
SupportedPlatform.efm32,
VerbosityLevel.Quiet);
Assert.IsNotNull(stableEfm32);
Assert.AreNotEqual(0, stableEfm32.Count);
// Assert that there are more stable packages than for the efm32
Assert.IsTrue(stableEfm32.Count < stable.Count);
// Assert that all efm32 packages are in the stable list
foreach (CloudSmithPackageDetail efm32Package in stableEfm32)
{
Assert.IsTrue(stable.Any(s => s.Name == efm32Package.Name && s.Version == efm32Package.Version),
$"Package {efm32Package.Name} with version {efm32Package.Version} is not in the stable list.");
}
#endregion
}
[TestMethod]
[TestCategory("CloudSmith")]
public void FirmwarePackage_ListCommunityTargets()
{
#region Get the stable packages
List<CloudSmithPackageDetail> stable = FirmwarePackage.GetTargetList(
true,
false,
null,
VerbosityLevel.Diagnostic);
Assert.IsNotNull(stable);
Assert.AreNotEqual(0, stable.Count);
#endregion
#region Get the stable esp32 packages
List<CloudSmithPackageDetail> stableEsp32 = FirmwarePackage.GetTargetList(
true,
false,
SupportedPlatform.esp32,
VerbosityLevel.Quiet);
Assert.AreNotEqual(0, stableEsp32.Count);
// Assert that there are more stable packages than for the esp32
Assert.IsTrue(stableEsp32.Count < stable.Count);
// Assert that all esp32 packages are in the stable list
foreach (CloudSmithPackageDetail esp32Package in stableEsp32)
{
Assert.IsTrue((from s in stable
where s.Name == esp32Package.Name && s.Version == esp32Package.Version
select s).Any());
}
#endregion
#region Get the stable stm32 packages
List<CloudSmithPackageDetail> stableStm32 = FirmwarePackage.GetTargetList(
true,
false,
SupportedPlatform.stm32,
VerbosityLevel.Quiet);
Assert.AreNotEqual(0, stableStm32.Count);
// Assert that there are more stable packages than for the stm32
Assert.IsTrue(stableStm32.Count < stable.Count);
// Assert that all stm32 packages are in the stable list
foreach (CloudSmithPackageDetail stm32Package in stableStm32)
{
Assert.IsTrue((from s in stable
where s.Name == stm32Package.Name && s.Version == stm32Package.Version
select s).Any());
}
#endregion
}
[TestMethod]
[TestCategory("CloudSmith")]
[DataRow(true)]
[DataRow(false)]
public void FirmwarePackage_Download(bool isReferenceTarget)
{
#region Setup
using var output = new OutputWriterHelper();
string testDirectory = TestDirectoryHelper.GetTestDirectory(TestContext);
string cacheDirectory = Path.Combine(testDirectory, TestDirectoryHelper.LocationPathBase_RelativePath);
List<CloudSmithPackageDetail> stable = GetTargetListHelper.GetTargetList(!isReferenceTarget, false, SupportedPlatform.esp32, false);
CloudSmithPackageDetail? newerPackage = null;
CloudSmithPackageDetail? package = null;
for (int i = 0; i < stable.Count; i++)
{
for (int j = i + 1; j < stable.Count; j++)
{
if (stable[i].Name == stable[j].Name && stable[i].Version != stable[j].Version)
{
if (new Version(stable[i].Version) > new Version(stable[j].Version))
{
newerPackage = stable[i];
package = stable[j];
}
else
{
newerPackage = stable[j];
package = stable[i];
}
break;
}
}
if (newerPackage is not null)
{
break;
}
}
if (newerPackage is null || package is null)
{
Assert.Inconclusive("No ESP32 package available with two versions???");
}
#endregion
#region Download older version
var actual = new Esp32Firmware(package!.Name, package.Version, false, null);
ExitCodes exitCode = actual.DownloadAndExtractAsync(null).GetAwaiter().GetResult();
Assert.AreEqual(ExitCodes.OK, exitCode);
Assert.IsTrue(Directory.Exists(Path.Combine(cacheDirectory, package.Name)));
Assert.IsTrue(File.Exists(Path.Combine(cacheDirectory, package.Name, $"{package.Name}-{package.Version}.zip")));
Assert.IsTrue(File.Exists(Path.Combine(cacheDirectory, package.Name, "nanoCLR.bin")));
#endregion
#region Download newer version
DateTime modified = File.GetLastWriteTimeUtc(Path.Combine(cacheDirectory, package.Name, "nanoCLR.bin"));
actual = new Esp32Firmware(package!.Name, newerPackage.Version, false, null);
exitCode = actual.DownloadAndExtractAsync(null).GetAwaiter().GetResult();
Assert.AreEqual(ExitCodes.OK, exitCode);
Assert.IsTrue(Directory.Exists(Path.Combine(cacheDirectory, package.Name)));
Assert.IsTrue(File.Exists(Path.Combine(cacheDirectory, package.Name, $"{newerPackage.Name}-{newerPackage.Version}.zip")));
Assert.IsTrue(File.Exists(Path.Combine(cacheDirectory, package.Name, "nanoCLR.bin")));
Assert.AreNotEqual(modified, File.GetLastWriteTimeUtc(Path.Combine(cacheDirectory, package.Name, "nanoCLR.bin")));
#endregion
}
[TestMethod]
[TestCategory("CloudSmith")]
public void FirmwarePackage_VirtualDevice_Download()
{
#region Setup
using var output = new OutputWriterHelper();
string testDirectory = TestDirectoryHelper.GetTestDirectory(TestContext);
string cacheDirectory = Path.Combine(testDirectory, TestDirectoryHelper.LocationPathBase_RelativePath);
string targetName = "WIN_DLL_nanoCLR";
#endregion
#region Download Virtual Device firmware
var actual = new Esp32Firmware(targetName, null, false, null);
ExitCodes exitCode = actual.DownloadAndExtractAsync(null).GetAwaiter().GetResult();
Assert.AreEqual(ExitCodes.OK, exitCode);
Assert.IsTrue(Directory.Exists(Path.Combine(cacheDirectory, targetName)));
string[] directories = (from d in Directory.GetDirectories(Path.Combine(cacheDirectory, targetName))
select Path.GetFileName(d)).ToArray();
Assert.AreEqual(1, (from d in directories
where d.StartsWith(targetName + "-")
select d).Count());
Assert.IsTrue(File.Exists(Path.Combine(cacheDirectory, targetName, directories[0], "nanoFramework.nanoCLR.dll")));
Assert.AreEqual(1, directories.Length);
#endregion
}
[TestMethod]
[TestCategory("CloudSmith")]
public void FirmwarePackage_LegacyVirtualDevice_Download()
{
#region Setup
using var output = new OutputWriterHelper();
string testDirectory = TestDirectoryHelper.GetTestDirectory(TestContext);
string cacheDirectory = Path.Combine(testDirectory, TestDirectoryHelper.LocationPathBase_RelativePath);
string targetName = "WIN32_nanoCLR";
#endregion
#region Download Virtual Device firmware
var actual = new Esp32Firmware(targetName, null, false, null);
ExitCodes exitCode = actual.DownloadAndExtractAsync(null).GetAwaiter().GetResult();
Assert.AreEqual(ExitCodes.OK, exitCode);
Assert.IsTrue(Directory.Exists(Path.Combine(cacheDirectory, targetName)));
string[] directories = (from d in Directory.GetDirectories(Path.Combine(cacheDirectory, targetName))
select Path.GetFileName(d)).ToArray();
Assert.AreEqual(1, (from d in directories
where d.StartsWith(targetName + "-")
select d).Count());
Assert.IsTrue(File.Exists(Path.Combine(cacheDirectory, targetName, directories[0], "nanoFramework.nanoCLR.dll")));
Assert.AreEqual(1, directories.Length);
#endregion
}
[TestMethod]
[TestCategory("Firmware archive")]
public void FirmwarePackage_FromArchive()
{
#region Setup
using var output = new OutputWriterHelper();
string testDirectory = TestDirectoryHelper.GetTestDirectory(TestContext);
string archiveDirectory = Path.Combine(testDirectory, "archive");
Directory.CreateDirectory(archiveDirectory);
string cacheDirectory = Path.Combine(testDirectory, TestDirectoryHelper.LocationPathBase_RelativePath);
CloudSmithPackageDetail package = GetTargetListHelper.GetTargetList(false, false, SupportedPlatform.ti_simplelink)[0];
var firmware = new Esp32Firmware(package.Name, package.Version, false, null);
ExitCodes exitCode = firmware.DownloadAndExtractAsync(null).GetAwaiter().GetResult();
if (ExitCodes.OK != exitCode)
{
Assert.Inconclusive("Cannot download the ESP32 package.");
}
string testTarget1Name = "TARGET1_IN_ARCHIVE_ONLY";
string testTarget2Name = "TARGET2_IN_ARCHIVE_ONLY";
string testVersion = "1.23.4.5";
string testOldVersion = "1.6.7.8";
// Copy the package to the archive directory, modified for TARGET1_IN_ARCHIVE_ONLY and TARGET2_IN_ARCHIVE_ONLY
File.Copy(Path.Combine(cacheDirectory, package.Name, $"{package.Name}-{package.Version}.zip"), Path.Combine(archiveDirectory, $"{testTarget1Name}-{testVersion}.zip"));
File.WriteAllText(Path.Combine(archiveDirectory, $"{testTarget1Name}-{testVersion}.zip.json"), $@"{{ ""Name"": ""{testTarget1Name}"", ""Version"": ""{testVersion}"", ""Platform"": ""esp32"" }}");
File.Copy(Path.Combine(cacheDirectory, package.Name, $"{package.Name}-{package.Version}.zip"), Path.Combine(archiveDirectory, $"{testTarget2Name}-{testVersion}.zip"));
File.WriteAllText(Path.Combine(archiveDirectory, $"{testTarget2Name}-{testVersion}.zip.json"), $@"{{ ""Name"": ""{testTarget2Name}"", ""Version"": ""{testVersion}"", ""Platform"": ""esp32"" }}");
// Create an invalid package for an earlier version of TARGET2_IN_ARCHIVE_ONLY - this should not be used
File.WriteAllText(Path.Combine(archiveDirectory, $"{testTarget2Name}-{testOldVersion}.zip"), "");
File.WriteAllText(Path.Combine(archiveDirectory, $"{testTarget2Name}-{testOldVersion}.zip.json"), $@"{{ ""Name"": ""{testTarget2Name}"", ""Version"": ""{testOldVersion}"", ""Platform"": ""esp32"" }}");
#endregion
#region Get package that exist in the archive directory but not in the repository; include version
output.Reset();
var actual = new Esp32Firmware(testTarget1Name, testVersion, false, null);
exitCode = actual.DownloadAndExtractAsync(archiveDirectory).GetAwaiter().GetResult();
Assert.AreEqual(ExitCodes.OK, exitCode);
output.AssertAreEqual("");
Assert.IsTrue(Directory.Exists(Path.Combine(cacheDirectory, testTarget1Name)));
Assert.IsTrue(File.Exists(Path.Combine(cacheDirectory, testTarget1Name, $"{testTarget1Name}-{testVersion}.zip")));
Assert.IsTrue(File.Exists(Path.Combine(cacheDirectory, testTarget1Name, "nanoCLR.bin")));
#endregion
#region Get package that exist in the archive directory but not in the repository; do not include version
output.Reset();
actual = new Esp32Firmware(testTarget2Name, null, false, null);
exitCode = actual.DownloadAndExtractAsync(archiveDirectory).GetAwaiter().GetResult();
Assert.AreEqual(ExitCodes.OK, exitCode);
output.AssertAreEqual("");
Assert.IsTrue(Directory.Exists(Path.Combine(cacheDirectory, testTarget2Name)));
Assert.IsTrue(File.Exists(Path.Combine(cacheDirectory, testTarget2Name, $"{testTarget2Name}-{testVersion}.zip")));
Assert.IsTrue(File.Exists(Path.Combine(cacheDirectory, testTarget2Name, "nanoCLR.bin")));
#endregion
#region Get package that does not exist in the archive directory
testTarget1Name = "MISSING_TARGET";
actual = new Esp32Firmware(testTarget1Name, testVersion, false, null);
exitCode = actual.DownloadAndExtractAsync(archiveDirectory).GetAwaiter().GetResult();
Assert.AreEqual(ExitCodes.E9015, exitCode);
output.AssertAreEqual("");
Assert.IsFalse(File.Exists(Path.Combine(cacheDirectory, testTarget1Name, $"{testTarget1Name}-{testVersion}.zip")));
#endregion
}
}
}