Skip to content

Commit baf14d6

Browse files
committed
test: round 2 — add 62 tests for Compress, HashAlgorithms, Download, Network, Strategy modules
- Compress: 12 tests (CompressProvider zip/unknown formats, ZipCompressionStrategy compress/decompress/nested/utf8/rootDir) - HashAlgorithms: 8 tests (Sha256HashAlgorithm ComputeHash/ComputeHashBytes, not-found/same-content/different/large file) - Download: 4 tests (DownloadOrchestratorOptions From/negative timeout/retry, DownloadExecutor/Pipeline structure) - Network: 9 tests (VersionService.SetSslValidationPolicy, StrictSsl/any error, HttpAuthProviderFactory 7 paths) - Strategy: 26 tests (ClientUpdateStrategy/UpgradeUpdateStrategy/OSSUpdateStrategy Create/StartApp/UsePrecheck, CheckPath, IsOssUpgrade logic) All 600 tests pass. Closes #425
1 parent 3b78c41 commit baf14d6

6 files changed

Lines changed: 718 additions & 0 deletions

File tree

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using GeneralUpdate.Core.Compress;
2+
3+
namespace CoreTest.Compress;
4+
5+
public class CompressProviderTests
6+
{
7+
[Fact]
8+
public void Compress_ZipFormat_UsesZipStrategy()
9+
{
10+
var tempDir = Path.Combine(Path.GetTempPath(), $"compress_test_{Guid.NewGuid():N}");
11+
var destZip = Path.Combine(Path.GetTempPath(), $"result_{Guid.NewGuid():N}.zip");
12+
Directory.CreateDirectory(tempDir);
13+
File.WriteAllText(Path.Combine(tempDir, "test.txt"), "hello");
14+
try
15+
{
16+
var ex = Record.Exception(() =>
17+
CompressProvider.Compress(".zip", tempDir, destZip, false, System.Text.Encoding.UTF8));
18+
Assert.Null(ex);
19+
Assert.True(File.Exists(destZip));
20+
}
21+
finally
22+
{
23+
if (Directory.Exists(tempDir)) Directory.Delete(tempDir, true);
24+
if (File.Exists(destZip)) File.Delete(destZip);
25+
}
26+
}
27+
28+
[Fact]
29+
public void Compress_UnknownFormat_ThrowsArgumentException()
30+
{
31+
Assert.Throws<ArgumentException>(() =>
32+
CompressProvider.Compress("RAR", "source", "dest", false, System.Text.Encoding.UTF8));
33+
}
34+
35+
[Fact]
36+
public void Decompress_ZipFormat_UsesZipStrategy()
37+
{
38+
var tempDir = Path.Combine(Path.GetTempPath(), $"decompress_src_{Guid.NewGuid():N}");
39+
var zipPath = Path.Combine(Path.GetTempPath(), $"test_{Guid.NewGuid():N}.zip");
40+
var destDir = Path.Combine(Path.GetTempPath(), $"decompress_dst_{Guid.NewGuid():N}");
41+
Directory.CreateDirectory(tempDir);
42+
File.WriteAllText(Path.Combine(tempDir, "test.txt"), "hello world");
43+
try
44+
{
45+
System.IO.Compression.ZipFile.CreateFromDirectory(tempDir, zipPath);
46+
var ex = Record.Exception(() =>
47+
CompressProvider.Decompress(".zip", zipPath, destDir, System.Text.Encoding.UTF8));
48+
Assert.Null(ex);
49+
Assert.True(File.Exists(Path.Combine(destDir, "test.txt")));
50+
}
51+
finally
52+
{
53+
if (Directory.Exists(tempDir)) Directory.Delete(tempDir, true);
54+
if (File.Exists(zipPath)) File.Delete(zipPath);
55+
if (Directory.Exists(destDir)) Directory.Delete(destDir, true);
56+
}
57+
}
58+
59+
[Fact]
60+
public void Decompress_UnknownFormat_ThrowsArgumentException()
61+
{
62+
Assert.Throws<ArgumentException>(() =>
63+
CompressProvider.Decompress("7z", "source", "dest", System.Text.Encoding.UTF8));
64+
}
65+
}
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
using System.Text;
2+
using GeneralUpdate.Core.Compress;
3+
4+
namespace CoreTest.Compress;
5+
6+
public class ZipCompressionStrategyTests
7+
{
8+
private readonly ZipCompressionStrategy _strategy = new();
9+
10+
[Fact]
11+
public void Compress_DirectoryToNewZip_CreatesArchive()
12+
{
13+
var srcDir = Path.Combine(Path.GetTempPath(), $"src_{Guid.NewGuid():N}");
14+
var destZip = Path.Combine(Path.GetTempPath(), $"dest_{Guid.NewGuid():N}.zip");
15+
Directory.CreateDirectory(srcDir);
16+
File.WriteAllText(Path.Combine(srcDir, "file.txt"), "content");
17+
try
18+
{
19+
_strategy.Compress(srcDir, destZip, false, Encoding.UTF8);
20+
Assert.True(File.Exists(destZip));
21+
}
22+
finally
23+
{
24+
if (Directory.Exists(srcDir)) Directory.Delete(srcDir, true);
25+
if (File.Exists(destZip)) File.Delete(destZip);
26+
}
27+
}
28+
29+
[Fact]
30+
public void Compress_DirectoryToExistingZip_UpdatesArchive()
31+
{
32+
var srcDir = Path.Combine(Path.GetTempPath(), $"src_{Guid.NewGuid():N}");
33+
var destZip = Path.Combine(Path.GetTempPath(), $"dest_{Guid.NewGuid():N}.zip");
34+
Directory.CreateDirectory(srcDir);
35+
File.WriteAllText(Path.Combine(srcDir, "file1.txt"), "first");
36+
// Create initial zip
37+
System.IO.Compression.ZipFile.CreateFromDirectory(srcDir, destZip);
38+
// Add second file
39+
File.WriteAllText(Path.Combine(srcDir, "file2.txt"), "second");
40+
try
41+
{
42+
_strategy.Compress(srcDir, destZip, false, Encoding.UTF8);
43+
Assert.True(File.Exists(destZip));
44+
}
45+
finally
46+
{
47+
if (Directory.Exists(srcDir)) Directory.Delete(srcDir, true);
48+
if (File.Exists(destZip)) File.Delete(destZip);
49+
}
50+
}
51+
52+
[Fact]
53+
public void Compress_SingleFileInDirectoryToNewZip_CreatesArchive()
54+
{
55+
var srcDir = Path.Combine(Path.GetTempPath(), $"src_{Guid.NewGuid():N}");
56+
var destZip = Path.Combine(Path.GetTempPath(), $"dest_{Guid.NewGuid():N}.zip");
57+
Directory.CreateDirectory(srcDir);
58+
File.WriteAllText(Path.Combine(srcDir, "single.txt"), "single file content");
59+
try
60+
{
61+
_strategy.Compress(srcDir, destZip, false, Encoding.UTF8);
62+
Assert.True(File.Exists(destZip));
63+
}
64+
finally
65+
{
66+
if (Directory.Exists(srcDir)) Directory.Delete(srcDir, true);
67+
if (File.Exists(destZip)) File.Delete(destZip);
68+
}
69+
}
70+
71+
[Fact]
72+
public void Decompress_ZipToDirectory_ExtractsFiles()
73+
{
74+
var srcDir = Path.Combine(Path.GetTempPath(), $"src_{Guid.NewGuid():N}");
75+
var zipPath = Path.Combine(Path.GetTempPath(), $"test_{Guid.NewGuid():N}.zip");
76+
var destDir = Path.Combine(Path.GetTempPath(), $"dst_{Guid.NewGuid():N}");
77+
Directory.CreateDirectory(srcDir);
78+
File.WriteAllText(Path.Combine(srcDir, "data.txt"), "decompress test");
79+
System.IO.Compression.ZipFile.CreateFromDirectory(srcDir, zipPath);
80+
try
81+
{
82+
_strategy.Decompress(zipPath, destDir, Encoding.UTF8);
83+
Assert.True(File.Exists(Path.Combine(destDir, "data.txt")));
84+
Assert.Equal("decompress test", File.ReadAllText(Path.Combine(destDir, "data.txt")));
85+
}
86+
finally
87+
{
88+
if (Directory.Exists(srcDir)) Directory.Delete(srcDir, true);
89+
if (File.Exists(zipPath)) File.Delete(zipPath);
90+
if (Directory.Exists(destDir)) Directory.Delete(destDir, true);
91+
}
92+
}
93+
94+
[Fact]
95+
public void Decompress_ZipFileNotFound_ReturnsWithoutError()
96+
{
97+
var nonexistent = Path.Combine(Path.GetTempPath(), $"noexist_{Guid.NewGuid():N}.zip");
98+
var destDir = Path.Combine(Path.GetTempPath(), $"dst_{Guid.NewGuid():N}");
99+
var ex = Record.Exception(() => _strategy.Decompress(nonexistent, destDir, Encoding.UTF8));
100+
Assert.Null(ex);
101+
}
102+
103+
[Fact]
104+
public void Decompress_NestedDirectories_PreservesStructure()
105+
{
106+
var srcDir = Path.Combine(Path.GetTempPath(), $"src_{Guid.NewGuid():N}");
107+
var nestedDir = Path.Combine(srcDir, "sub", "deep");
108+
var zipPath = Path.Combine(Path.GetTempPath(), $"nested_{Guid.NewGuid():N}.zip");
109+
var destDir = Path.Combine(Path.GetTempPath(), $"dst_{Guid.NewGuid():N}");
110+
Directory.CreateDirectory(nestedDir);
111+
File.WriteAllText(Path.Combine(nestedDir, "deep_file.txt"), "deep content");
112+
System.IO.Compression.ZipFile.CreateFromDirectory(srcDir, zipPath);
113+
try
114+
{
115+
_strategy.Decompress(zipPath, destDir, Encoding.UTF8);
116+
Assert.True(File.Exists(Path.Combine(destDir, "sub", "deep", "deep_file.txt")));
117+
}
118+
finally
119+
{
120+
if (Directory.Exists(srcDir)) Directory.Delete(srcDir, true);
121+
if (File.Exists(zipPath)) File.Delete(zipPath);
122+
if (Directory.Exists(destDir)) Directory.Delete(destDir, true);
123+
}
124+
}
125+
126+
[Fact]
127+
public void Decompress_EncodingPreserved_Utf8()
128+
{
129+
var srcDir = Path.Combine(Path.GetTempPath(), $"src_{Guid.NewGuid():N}");
130+
var zipPath = Path.Combine(Path.GetTempPath(), $"utf8_{Guid.NewGuid():N}.zip");
131+
var destDir = Path.Combine(Path.GetTempPath(), $"dst_{Guid.NewGuid():N}");
132+
Directory.CreateDirectory(srcDir);
133+
File.WriteAllText(Path.Combine(srcDir, "unicode.txt"), "你好世界", Encoding.UTF8);
134+
System.IO.Compression.ZipFile.CreateFromDirectory(srcDir, zipPath);
135+
try
136+
{
137+
_strategy.Decompress(zipPath, destDir, Encoding.UTF8);
138+
Assert.Equal("你好世界", File.ReadAllText(Path.Combine(destDir, "unicode.txt"), Encoding.UTF8));
139+
}
140+
finally
141+
{
142+
if (Directory.Exists(srcDir)) Directory.Delete(srcDir, true);
143+
if (File.Exists(zipPath)) File.Delete(zipPath);
144+
if (Directory.Exists(destDir)) Directory.Delete(destDir, true);
145+
}
146+
}
147+
148+
[Fact]
149+
public void Decompress_IncludesRootDirectory_WhenFlagTrue()
150+
{
151+
var srcDir = Path.Combine(Path.GetTempPath(), $"src_{Guid.NewGuid():N}");
152+
var zipPath = Path.Combine(Path.GetTempPath(), $"includeRoot_{Guid.NewGuid():N}.zip");
153+
var destDir = Path.Combine(Path.GetTempPath(), $"dst_{Guid.NewGuid():N}");
154+
Directory.CreateDirectory(srcDir);
155+
File.WriteAllText(Path.Combine(srcDir, "file.txt"), "content");
156+
// Create zip with includeBaseDirectory=true for the test, then use Compress with includeRootDirectory
157+
try
158+
{
159+
_strategy.Compress(srcDir, zipPath, true, Encoding.UTF8);
160+
_strategy.Decompress(zipPath, destDir, Encoding.UTF8);
161+
Assert.True(File.Exists(Path.Combine(destDir, "file.txt")) ||
162+
Directory.Exists(Path.Combine(destDir, Path.GetFileName(srcDir))));
163+
}
164+
finally
165+
{
166+
if (Directory.Exists(srcDir)) Directory.Delete(srcDir, true);
167+
if (File.Exists(zipPath)) File.Delete(zipPath);
168+
if (Directory.Exists(destDir)) Directory.Delete(destDir, true);
169+
}
170+
}
171+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using GeneralUpdate.Core.Download.Models;
2+
3+
namespace CoreTest.Download;
4+
5+
// NOTE: DownloadOrchestratorOptionsTests already exists in this namespace.
6+
// See existing OrchestratorOptionsBehaviourTests.cs and DownloadOrchestratorOptionsTests.cs
7+
// This file adds complementary tests.
8+
9+
public class DownloadOrchestratorOptionsComplementaryTests
10+
{
11+
[Fact]
12+
public void From_DownloadTimeOutNegative_DefaultsTo30Seconds()
13+
{
14+
var config = new GeneralUpdate.Core.Configuration.GlobalConfigInfo
15+
{
16+
DownloadTimeOut = -5
17+
};
18+
var opts = DownloadOrchestratorOptions.From(config);
19+
Assert.Equal(TimeSpan.FromSeconds(30), opts.DownloadTimeout);
20+
}
21+
22+
[Fact]
23+
public void From_RetryCountNegative_ClampedToZero()
24+
{
25+
var config = new GeneralUpdate.Core.Configuration.GlobalConfigInfo
26+
{
27+
RetryCount = -5
28+
};
29+
var opts = DownloadOrchestratorOptions.From(config);
30+
Assert.Equal(0, opts.RetryCount);
31+
}
32+
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
using GeneralUpdate.Core.HashAlgorithms;
2+
3+
namespace CoreTest.HashAlgorithms;
4+
5+
public class Sha256HashAlgorithmTests
6+
{
7+
private readonly Sha256HashAlgorithm _algorithm = new();
8+
9+
[Fact]
10+
public void ComputeHash_FileNotFound_ThrowsFileNotFoundException()
11+
{
12+
Assert.Throws<FileNotFoundException>(() =>
13+
_algorithm.ComputeHash(Path.Combine(Path.GetTempPath(), $"no_file_{Guid.NewGuid():N}.dat")));
14+
}
15+
16+
[Fact]
17+
public void ComputeHash_EmptyFile_ReturnsKnownHash()
18+
{
19+
var emptyFile = Path.GetTempFileName();
20+
try
21+
{
22+
var hash = _algorithm.ComputeHash(emptyFile);
23+
Assert.NotNull(hash);
24+
Assert.Equal(64, hash.Length);
25+
}
26+
finally { if (File.Exists(emptyFile)) File.Delete(emptyFile); }
27+
}
28+
29+
[Fact]
30+
public void ComputeHash_SameContentFile_SameHash()
31+
{
32+
var file1 = Path.GetTempFileName();
33+
var file2 = Path.GetTempFileName();
34+
try
35+
{
36+
File.WriteAllText(file1, "identical content");
37+
File.WriteAllText(file2, "identical content");
38+
var h1 = _algorithm.ComputeHash(file1);
39+
var h2 = _algorithm.ComputeHash(file2);
40+
Assert.Equal(h1, h2);
41+
}
42+
finally
43+
{
44+
if (File.Exists(file1)) File.Delete(file1);
45+
if (File.Exists(file2)) File.Delete(file2);
46+
}
47+
}
48+
49+
[Fact]
50+
public void ComputeHash_DifferentContent_DifferentHash()
51+
{
52+
var file1 = Path.GetTempFileName();
53+
var file2 = Path.GetTempFileName();
54+
try
55+
{
56+
File.WriteAllText(file1, "content A");
57+
File.WriteAllText(file2, "content B");
58+
var h1 = _algorithm.ComputeHash(file1);
59+
var h2 = _algorithm.ComputeHash(file2);
60+
Assert.NotEqual(h1, h2);
61+
}
62+
finally
63+
{
64+
if (File.Exists(file1)) File.Delete(file1);
65+
if (File.Exists(file2)) File.Delete(file2);
66+
}
67+
}
68+
69+
[Fact]
70+
public void ComputeHashBytes_ValidFile_Returns32Bytes()
71+
{
72+
var file = Path.GetTempFileName();
73+
try
74+
{
75+
File.WriteAllText(file, "test data");
76+
var bytes = _algorithm.ComputeHashBytes(file);
77+
Assert.NotNull(bytes);
78+
Assert.Equal(32, bytes.Length);
79+
}
80+
finally { if (File.Exists(file)) File.Delete(file); }
81+
}
82+
83+
[Fact]
84+
public void ComputeHashBytes_FileNotFound_ThrowsFileNotFoundException()
85+
{
86+
Assert.Throws<FileNotFoundException>(() =>
87+
_algorithm.ComputeHashBytes(Path.Combine(Path.GetTempPath(), $"no_file_{Guid.NewGuid():N}.dat")));
88+
}
89+
90+
[Fact]
91+
public void ComputeHash_ConsistentAcrossCalls()
92+
{
93+
var file = Path.GetTempFileName();
94+
try
95+
{
96+
File.WriteAllText(file, "stable content");
97+
var h1 = _algorithm.ComputeHash(file);
98+
var h2 = _algorithm.ComputeHash(file);
99+
Assert.Equal(h1, h2);
100+
}
101+
finally { if (File.Exists(file)) File.Delete(file); }
102+
}
103+
104+
[Fact]
105+
public void ComputeHash_LargeFile_ComputesCorrectly()
106+
{
107+
var file = Path.GetTempFileName();
108+
try
109+
{
110+
var data = new byte[1024 * 1024]; // 1MB
111+
new Random(42).NextBytes(data);
112+
File.WriteAllBytes(file, data);
113+
var hash = _algorithm.ComputeHash(file);
114+
Assert.Equal(64, hash.Length);
115+
}
116+
finally { if (File.Exists(file)) File.Delete(file); }
117+
}
118+
}

0 commit comments

Comments
 (0)