Skip to content

Commit a9dc1ed

Browse files
author
Cory Thompson
committed
Code cleanup
1 parent 45e69fa commit a9dc1ed

14 files changed

+334
-328
lines changed

WebPush.Test/ECKeyHelperTest.cs

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
using Org.BouncyCastle.Crypto;
1+
using System.Linq;
22
using Org.BouncyCastle.Crypto.Parameters;
3-
using System.Linq;
43
using WebPush.Util;
54
using Xunit;
65

@@ -14,58 +13,58 @@ public class ECKeyHelperTest
1413
private const string TEST_PRIVATE_KEY = @"on6X5KmLEFIVvPP3cNX9kE0OF6PV9TJQXVbnKU2xEHI";
1514

1615
[Fact]
17-
public void TestGetPublicKey()
16+
public void TestGenerateKeys()
1817
{
19-
byte[] publicKey = UrlBase64.Decode(TEST_PUBLIC_KEY);
20-
ECPublicKeyParameters publicKeyParams = ECKeyHelper.GetPublicKey(publicKey);
18+
var keys = ECKeyHelper.GenerateKeys();
2119

22-
string importedPublicKey = UrlBase64.Encode(publicKeyParams.Q.GetEncoded(false));
20+
var publicKey = ((ECPublicKeyParameters) keys.Public).Q.GetEncoded(false);
21+
var privateKey = ((ECPrivateKeyParameters) keys.Private).D.ToByteArrayUnsigned();
2322

24-
Assert.Equal(TEST_PUBLIC_KEY, importedPublicKey);
23+
var publicKeyLength = publicKey.Length;
24+
var privateKeyLength = privateKey.Length;
25+
26+
Assert.Equal(65, publicKeyLength);
27+
Assert.Equal(32, privateKeyLength);
28+
29+
;
2530
}
2631

2732
[Fact]
28-
public void TestGetPrivateKey()
33+
public void TestGenerateKeysNoCache()
2934
{
30-
byte[] privateKey = UrlBase64.Decode(TEST_PRIVATE_KEY);
31-
ECPrivateKeyParameters privateKeyParams = ECKeyHelper.GetPrivateKey(privateKey);
35+
var keys1 = ECKeyHelper.GenerateKeys();
36+
var keys2 = ECKeyHelper.GenerateKeys();
3237

33-
string importedPrivateKey = UrlBase64.Encode(privateKeyParams.D.ToByteArrayUnsigned());
38+
var publicKey1 = ((ECPublicKeyParameters) keys1.Public).Q.GetEncoded(false);
39+
var privateKey1 = ((ECPrivateKeyParameters) keys1.Private).D.ToByteArrayUnsigned();
3440

35-
Assert.Equal(TEST_PRIVATE_KEY, importedPrivateKey);
41+
var publicKey2 = ((ECPublicKeyParameters) keys2.Public).Q.GetEncoded(false);
42+
var privateKey2 = ((ECPrivateKeyParameters) keys2.Private).D.ToByteArrayUnsigned();
43+
44+
Assert.False(publicKey1.SequenceEqual(publicKey2));
45+
Assert.False(privateKey1.SequenceEqual(privateKey2));
3646
}
3747

3848
[Fact]
39-
public void TestGenerateKeys()
49+
public void TestGetPrivateKey()
4050
{
41-
AsymmetricCipherKeyPair keys = ECKeyHelper.GenerateKeys();
51+
var privateKey = UrlBase64.Decode(TEST_PRIVATE_KEY);
52+
var privateKeyParams = ECKeyHelper.GetPrivateKey(privateKey);
4253

43-
byte[] publicKey = ((ECPublicKeyParameters)keys.Public).Q.GetEncoded(false);
44-
byte[] privateKey = ((ECPrivateKeyParameters)keys.Private).D.ToByteArrayUnsigned();
54+
var importedPrivateKey = UrlBase64.Encode(privateKeyParams.D.ToByteArrayUnsigned());
4555

46-
int publicKeyLength = publicKey.Length;
47-
int privateKeyLength = privateKey.Length;
48-
49-
Assert.Equal(65, publicKeyLength);
50-
Assert.Equal(32, privateKeyLength);
51-
52-
;
56+
Assert.Equal(TEST_PRIVATE_KEY, importedPrivateKey);
5357
}
5458

5559
[Fact]
56-
public void TestGenerateKeysNoCache()
60+
public void TestGetPublicKey()
5761
{
58-
AsymmetricCipherKeyPair keys1 = ECKeyHelper.GenerateKeys();
59-
AsymmetricCipherKeyPair keys2 = ECKeyHelper.GenerateKeys();
60-
61-
byte[] publicKey1 = ((ECPublicKeyParameters)keys1.Public).Q.GetEncoded(false);
62-
byte[] privateKey1 = ((ECPrivateKeyParameters)keys1.Private).D.ToByteArrayUnsigned();
62+
var publicKey = UrlBase64.Decode(TEST_PUBLIC_KEY);
63+
var publicKeyParams = ECKeyHelper.GetPublicKey(publicKey);
6364

64-
byte[] publicKey2 = ((ECPublicKeyParameters)keys2.Public).Q.GetEncoded(false);
65-
byte[] privateKey2 = ((ECPrivateKeyParameters)keys2.Private).D.ToByteArrayUnsigned();
65+
var importedPublicKey = UrlBase64.Encode(publicKeyParams.Q.GetEncoded(false));
6666

67-
Assert.False(publicKey1.SequenceEqual(publicKey2));
68-
Assert.False(privateKey1.SequenceEqual(privateKey2));
67+
Assert.Equal(TEST_PUBLIC_KEY, importedPublicKey);
6968
}
7069
}
7170
}

WebPush.Test/JWSSignerTest.cs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using Org.BouncyCastle.Crypto.Parameters;
2-
using System.Collections.Generic;
1+
using System.Collections.Generic;
32
using System.Text;
43
using WebPush.Util;
54
using Xunit;
@@ -11,38 +10,38 @@ public class JWSSignerTest
1110
[Fact]
1211
public void TestGenerateSignature()
1312
{
14-
ECPrivateKeyParameters privateKey = ECKeyHelper.GetPrivateKey(new byte[32]);
13+
var privateKey = ECKeyHelper.GetPrivateKey(new byte[32]);
1514

16-
Dictionary<string, object> header = new Dictionary<string, object>();
15+
var header = new Dictionary<string, object>();
1716
header.Add("typ", "JWT");
1817
header.Add("alg", "ES256");
1918

20-
Dictionary<string, object> jwtPayload = new Dictionary<string, object>();
19+
var jwtPayload = new Dictionary<string, object>();
2120
jwtPayload.Add("aud", "aud");
2221
jwtPayload.Add("exp", 1);
2322
jwtPayload.Add("sub", "subject");
2423

25-
JWSSigner signer = new JWSSigner(privateKey);
26-
string token = signer.GenerateSignature(header, jwtPayload);
24+
var signer = new JWSSigner(privateKey);
25+
var token = signer.GenerateSignature(header, jwtPayload);
2726

28-
string[] tokenParts = token.Split('.');
27+
var tokenParts = token.Split('.');
2928

3029
Assert.Equal(3, tokenParts.Length);
3130

32-
string encodedHeader = tokenParts[0];
33-
string encodedPayload = tokenParts[1];
34-
string signature = tokenParts[2];
31+
var encodedHeader = tokenParts[0];
32+
var encodedPayload = tokenParts[1];
33+
var signature = tokenParts[2];
3534

36-
string decodedHeader = Encoding.UTF8.GetString(UrlBase64.Decode(encodedHeader));
37-
string decodedPayload = Encoding.UTF8.GetString(UrlBase64.Decode(encodedPayload));
35+
var decodedHeader = Encoding.UTF8.GetString(UrlBase64.Decode(encodedHeader));
36+
var decodedPayload = Encoding.UTF8.GetString(UrlBase64.Decode(encodedPayload));
3837

3938
Assert.Equal(@"{""typ"":""JWT"",""alg"":""ES256""}", decodedHeader);
4039
Assert.Equal(@"{""aud"":""aud"",""exp"":1,""sub"":""subject""}", decodedPayload);
4140

42-
byte[] decodedSignature = UrlBase64.Decode(signature);
43-
int decodedSignatureLength = decodedSignature.Length;
41+
var decodedSignature = UrlBase64.Decode(signature);
42+
var decodedSignatureLength = decodedSignature.Length;
4443

45-
bool isSignatureLengthValid = decodedSignatureLength == 66 || decodedSignatureLength == 64;
44+
var isSignatureLengthValid = decodedSignatureLength == 66 || decodedSignatureLength == 64;
4645
Assert.Equal(true, isSignatureLengthValid);
4746
}
4847
}

WebPush.Test/UrlBase64Test.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@ public class UrlBase64Test
99
[Fact]
1010
public void TestBase64UrlDecode()
1111
{
12-
byte[] expected = new byte[3] { 181, 235, 45 };
13-
byte[] actual = UrlBase64.Decode(@"test");
12+
var expected = new byte[3] {181, 235, 45};
13+
var actual = UrlBase64.Decode(@"test");
1414
Assert.True(actual.SequenceEqual(expected));
1515
}
1616

1717
[Fact]
1818
public void TestBase64UrlEncode()
1919
{
20-
string expected = @"test";
21-
string actual = UrlBase64.Encode(new byte[3] { 181, 235, 45 });
20+
var expected = @"test";
21+
var actual = UrlBase64.Encode(new byte[3] {181, 235, 45});
2222
Assert.Equal(expected, actual);
2323
}
2424
}

WebPush.Test/VapidHelperTest.cs

Lines changed: 32 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Collections.Generic;
32
using WebPush.Util;
43
using Xunit;
54

@@ -14,9 +13,9 @@ public class VapidHelperTest
1413
[Fact]
1514
public void TestGenerateVapidKeys()
1615
{
17-
VapidDetails keys = VapidHelper.GenerateVapidKeys();
18-
byte[] publicKey = UrlBase64.Decode(keys.PublicKey);
19-
byte[] privateKey = UrlBase64.Decode(keys.PrivateKey);
16+
var keys = VapidHelper.GenerateVapidKeys();
17+
var publicKey = UrlBase64.Decode(keys.PublicKey);
18+
var privateKey = UrlBase64.Decode(keys.PrivateKey);
2019

2120
Assert.Equal(32, privateKey.Length);
2221
Assert.Equal(65, publicKey.Length);
@@ -25,8 +24,8 @@ public void TestGenerateVapidKeys()
2524
[Fact]
2625
public void TestGenerateVapidKeysNoCache()
2726
{
28-
VapidDetails keys1 = VapidHelper.GenerateVapidKeys();
29-
VapidDetails keys2 = VapidHelper.GenerateVapidKeys();
27+
var keys1 = VapidHelper.GenerateVapidKeys();
28+
var keys2 = VapidHelper.GenerateVapidKeys();
3029

3130
Assert.NotEqual(keys1.PublicKey, keys2.PublicKey);
3231
Assert.NotEqual(keys1.PrivateKey, keys2.PrivateKey);
@@ -35,76 +34,64 @@ public void TestGenerateVapidKeysNoCache()
3534
[Fact]
3635
public void TestGetVapidHeaders()
3736
{
38-
string publicKey = UrlBase64.Encode(new byte[65]);
39-
string privatekey = UrlBase64.Encode(new byte[32]);
40-
Dictionary<string, string> headers = VapidHelper.GetVapidHeaders(VALID_AUDIENCE, VALID_SUBJECT, publicKey, privatekey);
37+
var publicKey = UrlBase64.Encode(new byte[65]);
38+
var privatekey = UrlBase64.Encode(new byte[32]);
39+
var headers = VapidHelper.GetVapidHeaders(VALID_AUDIENCE, VALID_SUBJECT, publicKey, privatekey);
4140

4241
Assert.True(headers.ContainsKey("Authorization"));
4342
Assert.True(headers.ContainsKey("Crypto-Key"));
4443
}
4544

4645
[Fact]
47-
public void TestGetVapidHeadersWithMailToSubject()
46+
public void TestGetVapidHeadersAudienceNotAUrl()
4847
{
49-
string publicKey = UrlBase64.Encode(new byte[65]);
50-
string privatekey = UrlBase64.Encode(new byte[32]);
51-
Dictionary<string, string> headers = VapidHelper.GetVapidHeaders(VALID_AUDIENCE, VALID_SUBJECT_MAILTO, publicKey,
52-
privatekey);
48+
var publicKey = UrlBase64.Encode(new byte[65]);
49+
var privatekey = UrlBase64.Encode(new byte[32]);
5350

54-
Assert.True(headers.ContainsKey("Authorization"));
55-
Assert.True(headers.ContainsKey("Crypto-Key"));
51+
Assert.Throws(typeof(ArgumentException),
52+
delegate { VapidHelper.GetVapidHeaders("invalid audience", VALID_SUBJECT, publicKey, privatekey); });
5653
}
5754

5855
[Fact]
59-
public void TestGetVapidHeadersAudienceNotAUrl()
56+
public void TestGetVapidHeadersInvalidPrivateKey()
6057
{
61-
string publicKey = UrlBase64.Encode(new byte[65]);
62-
string privatekey = UrlBase64.Encode(new byte[32]);
58+
var publicKey = UrlBase64.Encode(new byte[65]);
59+
var privatekey = UrlBase64.Encode(new byte[1]);
6360

6461
Assert.Throws(typeof(ArgumentException),
65-
delegate
66-
{
67-
VapidHelper.GetVapidHeaders("invalid audience", VALID_SUBJECT, publicKey, privatekey);
68-
});
62+
delegate { VapidHelper.GetVapidHeaders(VALID_AUDIENCE, VALID_SUBJECT, publicKey, privatekey); });
6963
}
7064

7165
[Fact]
72-
public void TestGetVapidHeadersSubjectNotAUrlOrMailTo()
66+
public void TestGetVapidHeadersInvalidPublicKey()
7367
{
74-
string publicKey = UrlBase64.Encode(new byte[65]);
75-
string privatekey = UrlBase64.Encode(new byte[32]);
68+
var publicKey = UrlBase64.Encode(new byte[1]);
69+
var privatekey = UrlBase64.Encode(new byte[32]);
7670

7771
Assert.Throws(typeof(ArgumentException),
78-
delegate
79-
{
80-
VapidHelper.GetVapidHeaders(VALID_AUDIENCE, "invalid subject", publicKey, privatekey);
81-
});
72+
delegate { VapidHelper.GetVapidHeaders(VALID_AUDIENCE, VALID_SUBJECT, publicKey, privatekey); });
8273
}
8374

8475
[Fact]
85-
public void TestGetVapidHeadersInvalidPublicKey()
76+
public void TestGetVapidHeadersSubjectNotAUrlOrMailTo()
8677
{
87-
string publicKey = UrlBase64.Encode(new byte[1]);
88-
string privatekey = UrlBase64.Encode(new byte[32]);
78+
var publicKey = UrlBase64.Encode(new byte[65]);
79+
var privatekey = UrlBase64.Encode(new byte[32]);
8980

9081
Assert.Throws(typeof(ArgumentException),
91-
delegate
92-
{
93-
VapidHelper.GetVapidHeaders(VALID_AUDIENCE, VALID_SUBJECT, publicKey, privatekey);
94-
});
82+
delegate { VapidHelper.GetVapidHeaders(VALID_AUDIENCE, "invalid subject", publicKey, privatekey); });
9583
}
9684

9785
[Fact]
98-
public void TestGetVapidHeadersInvalidPrivateKey()
86+
public void TestGetVapidHeadersWithMailToSubject()
9987
{
100-
string publicKey = UrlBase64.Encode(new byte[65]);
101-
string privatekey = UrlBase64.Encode(new byte[1]);
88+
var publicKey = UrlBase64.Encode(new byte[65]);
89+
var privatekey = UrlBase64.Encode(new byte[32]);
90+
var headers = VapidHelper.GetVapidHeaders(VALID_AUDIENCE, VALID_SUBJECT_MAILTO, publicKey,
91+
privatekey);
10292

103-
Assert.Throws(typeof(ArgumentException),
104-
delegate
105-
{
106-
VapidHelper.GetVapidHeaders(VALID_AUDIENCE, VALID_SUBJECT, publicKey, privatekey);
107-
});
93+
Assert.True(headers.ContainsKey("Authorization"));
94+
Assert.True(headers.ContainsKey("Crypto-Key"));
10895
}
10996
}
11097
}

0 commit comments

Comments
 (0)