-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathVapidHelperTest.cs
119 lines (99 loc) · 4.13 KB
/
VapidHelperTest.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
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using WebPush.Util;
namespace WebPush.Test
{
[TestClass]
public class VapidHelperTest
{
private const string ValidAudience = @"http://example.com";
private const string ValidSubject = @"http://example.com/example";
private const string ValidSubjectMailto = @"mailto:example@example.com";
private const string TestPublicKey =
@"BCvKwB2lbVUYMFAaBUygooKheqcEU-GDrVRnu8k33yJCZkNBNqjZj0VdxQ2QIZa4kV5kpX9aAqyBKZHURm6eG1A";
private const string TestPrivateKey = @"on6X5KmLEFIVvPP3cNX9kE0OF6PV9TJQXVbnKU2xEHI";
[TestMethod]
public void TestGenerateVapidKeys()
{
var keys = VapidHelper.GenerateVapidKeys();
var publicKey = UrlBase64.Decode(keys.PublicKey);
var privateKey = UrlBase64.Decode(keys.PrivateKey);
Assert.AreEqual(32, privateKey.Length);
Assert.AreEqual(65, publicKey.Length);
}
[TestMethod]
public void TestGenerateVapidKeysNoCache()
{
var keys1 = VapidHelper.GenerateVapidKeys();
var keys2 = VapidHelper.GenerateVapidKeys();
Assert.AreNotEqual(keys1.PublicKey, keys2.PublicKey);
Assert.AreNotEqual(keys1.PrivateKey, keys2.PrivateKey);
}
[TestMethod]
public void TestGetVapidHeaders()
{
var publicKey = TestPublicKey;
var privateKey = TestPrivateKey;
var headers = VapidHelper.GetVapidHeaders(ValidAudience, ValidSubject, publicKey, privateKey);
Assert.IsTrue(headers.ContainsKey(@"Authorization"));
Assert.IsTrue(headers.ContainsKey(@"Crypto-Key"));
}
[TestMethod]
public void TestGetVapidHeadersAudienceNotAUrl()
{
var publicKey = TestPublicKey;
var privateKey = TestPrivateKey;
Assert.ThrowsException<ArgumentException>(
delegate
{
VapidHelper.GetVapidHeaders("invalid audience", ValidSubjectMailto, publicKey, privateKey);
});
}
[TestMethod]
public void TestGetVapidHeadersInvalidPrivateKey()
{
var publicKey = UrlBase64.Encode(new byte[65]);
var privateKey = UrlBase64.Encode(new byte[1]);
Assert.ThrowsException<ArgumentException>(
delegate { VapidHelper.GetVapidHeaders(ValidAudience, ValidSubject, publicKey, privateKey); });
}
[TestMethod]
public void TestGetVapidHeadersInvalidPublicKey()
{
var publicKey = UrlBase64.Encode(new byte[1]);
var privateKey = UrlBase64.Encode(new byte[32]);
Assert.ThrowsException<ArgumentException>(
delegate { VapidHelper.GetVapidHeaders(ValidAudience, ValidSubject, publicKey, privateKey); });
}
[TestMethod]
public void TestGetVapidHeadersSubjectNotAUrlOrMailTo()
{
var publicKey = TestPublicKey;
var privateKey = TestPrivateKey;
Assert.ThrowsException<ArgumentException>(
delegate { VapidHelper.GetVapidHeaders(ValidAudience, @"invalid subject", publicKey, privateKey); });
}
[TestMethod]
public void TestGetVapidHeadersWithMailToSubject()
{
var publicKey = TestPublicKey;
var privateKey = TestPrivateKey;
var headers = VapidHelper.GetVapidHeaders(ValidAudience, ValidSubjectMailto, publicKey,
privateKey);
Assert.IsTrue(headers.ContainsKey(@"Authorization"));
Assert.IsTrue(headers.ContainsKey(@"Crypto-Key"));
}
[TestMethod]
public void TestExpirationInPastExceptions()
{
var publicKey = TestPublicKey;
var privateKey = TestPrivateKey;
Assert.ThrowsException<ArgumentException>(
delegate
{
VapidHelper.GetVapidHeaders(ValidAudience, ValidSubjectMailto, publicKey,
privateKey, 1552715607);
});
}
}
}