markus / MarkusAutoUpdate / src / NetSparkle.Tests.AppCastGenerator / SignatureManagerTests.cs @ d8f5045e
이력 | 보기 | 이력해설 | 다운로드 (3.94 KB)
1 |
using NetSparkleUpdater.AppCastGenerator; |
---|---|
2 |
using Org.BouncyCastle.Crypto; |
3 |
using Org.BouncyCastle.Crypto.Generators; |
4 |
using Org.BouncyCastle.Crypto.Parameters; |
5 |
using Org.BouncyCastle.Security; |
6 |
using System; |
7 |
using System.IO; |
8 |
using System.Linq; |
9 |
using Xunit; |
10 |
|
11 |
namespace NetSparkle.Tests.AppCastGenerator |
12 |
{ |
13 |
public class SignatureManagerTests |
14 |
{ |
15 |
private SignatureManager GetSignatureManager() |
16 |
{ |
17 |
var manager = new SignatureManager(); |
18 |
// make sure we don't overwrite user's NetSparkle keys!! |
19 |
manager.SetStorageDirectory(Path.Combine(Path.GetTempPath(), "netsparkle-tests")); |
20 |
return manager; |
21 |
} |
22 |
|
23 |
[Fact] |
24 |
public void TestKeysExist() |
25 |
{ |
26 |
var manager = GetSignatureManager(); |
27 |
manager.Generate(true); |
28 |
Assert.True(manager.KeysExist()); |
29 |
} |
30 |
|
31 |
[Fact] |
32 |
public void CanGenerateKeys() |
33 |
{ |
34 |
var manager = GetSignatureManager(); |
35 |
manager.Generate(true); |
36 |
|
37 |
var publicKey = manager.GetPublicKey(); |
38 |
Assert.NotNull(publicKey); |
39 |
Assert.NotEmpty(publicKey); |
40 |
var privateKey = manager.GetPrivateKey(); |
41 |
Assert.NotNull(privateKey); |
42 |
Assert.NotEmpty(privateKey); |
43 |
} |
44 |
|
45 |
// https://stackoverflow.com/a/1344242/3938401 |
46 |
private static string RandomString(int length) |
47 |
{ |
48 |
Random random = new SecureRandom(); |
49 |
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; |
50 |
return new string(Enumerable.Repeat(chars, length) |
51 |
.Select(s => s[random.Next(s.Length)]).ToArray()); |
52 |
} |
53 |
|
54 |
[Fact] |
55 |
public void CanGetAndVerifySignature() |
56 |
{ |
57 |
// create tmp file |
58 |
var tempData = RandomString(1024); |
59 |
var path = Path.GetTempFileName(); |
60 |
File.WriteAllText(path, tempData); |
61 |
Assert.True(File.Exists(path)); |
62 |
Assert.Equal(tempData, File.ReadAllText(path)); |
63 |
// get signature of file |
64 |
var manager = GetSignatureManager(); |
65 |
manager.Generate(true); |
66 |
var signature = manager.GetSignatureForFile(path); |
67 |
// verify signature |
68 |
Assert.True(manager.VerifySignature(path, signature)); |
69 |
// get rid of temp file |
70 |
File.Delete(path); |
71 |
} |
72 |
|
73 |
[Fact] |
74 |
public void CanGetAndVerifySignatureFromEnvironment() |
75 |
{ |
76 |
// create tmp file |
77 |
var tempData = RandomString(1024); |
78 |
var path = Path.GetTempFileName(); |
79 |
File.WriteAllText(path, tempData); |
80 |
Assert.True(File.Exists(path)); |
81 |
Assert.Equal(tempData, File.ReadAllText(path)); |
82 |
|
83 |
// create keys |
84 |
var Random = new SecureRandom(); |
85 |
|
86 |
Ed25519KeyPairGenerator kpg = new Ed25519KeyPairGenerator(); |
87 |
kpg.Init(new Ed25519KeyGenerationParameters(Random)); |
88 |
|
89 |
AsymmetricCipherKeyPair kp = kpg.GenerateKeyPair(); |
90 |
Ed25519PrivateKeyParameters privateKey = (Ed25519PrivateKeyParameters)kp.Private; |
91 |
Ed25519PublicKeyParameters publicKey = (Ed25519PublicKeyParameters)kp.Public; |
92 |
|
93 |
var privKeyBase64 = Convert.ToBase64String(privateKey.GetEncoded()); |
94 |
var pubKeyBase64 = Convert.ToBase64String(publicKey.GetEncoded()); |
95 |
|
96 |
var manager = GetSignatureManager(); |
97 |
Environment.SetEnvironmentVariable(SignatureManager.PrivateKeyEnvironmentVariable, privKeyBase64); |
98 |
Environment.SetEnvironmentVariable(SignatureManager.PublicKeyEnvironmentVariable, pubKeyBase64); |
99 |
|
100 |
// get signature of file |
101 |
var signature = manager.GetSignatureForFile(path); |
102 |
manager.Generate(true); // force regeneration of keys to "prove" that we are using environment |
103 |
// verify signature |
104 |
Assert.True(manager.VerifySignature(path, signature)); |
105 |
// get rid of temp file |
106 |
File.Delete(path); |
107 |
} |
108 |
} |
109 |
} |