1
//-----------------------------------------------------------------------
2 // <copyright file="PolicyResponseTests.cs" company="Andrew Arnott">
3 // Copyright (c) Andrew Arnott. All rights reserved.
5 //-----------------------------------------------------------------------
7 namespace DotNetOpenAuth
.OpenId
.Test
.Extensions
.ProviderAuthenticationPolicy
{
9 using System
.Collections
.Generic
;
12 using Microsoft
.VisualStudio
.TestTools
.UnitTesting
;
13 using DotNetOpenAuth
.OpenId
.Extensions
.ProviderAuthenticationPolicy
;
16 public class PolicyResponseTests
{
17 private DateTime someLocalTime
= new DateTime(2008, 1, 1, 1, 1, 1, 0, DateTimeKind
.Local
);
18 private DateTime someUtcTime
= new DateTime(2008, 1, 1, 1, 1, 1, 0, DateTimeKind
.Utc
);
19 private DateTime someUnspecifiedTime
= new DateTime(2008, 1, 1, 1, 1, 1, 0, DateTimeKind
.Unspecified
);
23 PolicyResponse resp
= new PolicyResponse();
24 Assert
.IsNotNull(resp
.ActualPolicies
);
25 Assert
.AreEqual(0, resp
.ActualPolicies
.Count
);
26 Assert
.IsNull(resp
.AuthenticationTimeUtc
);
27 Assert
.IsNull(resp
.NistAssuranceLevel
);
31 public void AddPolicies() {
32 PolicyResponse resp
= new PolicyResponse();
33 resp
.ActualPolicies
.Add(AuthenticationPolicies
.MultiFactor
);
34 resp
.ActualPolicies
.Add(AuthenticationPolicies
.PhishingResistant
);
35 Assert
.AreEqual(2, resp
.ActualPolicies
.Count
);
36 Assert
.AreEqual(AuthenticationPolicies
.MultiFactor
, resp
.ActualPolicies
[0]);
37 Assert
.AreEqual(AuthenticationPolicies
.PhishingResistant
, resp
.ActualPolicies
[1]);
41 public void AddPolicyMultipleTimes() {
42 // Although this isn't really the desired behavior (we'd prefer to see an
43 // exception thrown), since we're using a List<string> internally we can't
44 // expect anything better (for now). But if this is ever fixed, by all means
45 // change this test to expect an exception or something else.
46 PolicyResponse resp
= new PolicyResponse();
47 resp
.ActualPolicies
.Add(AuthenticationPolicies
.MultiFactor
);
48 resp
.ActualPolicies
.Add(AuthenticationPolicies
.MultiFactor
);
49 Assert
.AreEqual(2, resp
.ActualPolicies
.Count
);
53 public void AuthenticationTimeUtcConvertsToUtc() {
54 PolicyResponse resp
= new PolicyResponse();
55 resp
.AuthenticationTimeUtc
= someLocalTime
;
56 Assert
.IsNotNull(resp
.AuthenticationTimeUtc
);
57 Assert
.AreEqual(DateTimeKind
.Utc
, resp
.AuthenticationTimeUtc
.Value
.Kind
);
58 Assert
.AreEqual(someLocalTime
.ToUniversalTime(), resp
.AuthenticationTimeUtc
.Value
);
62 public void AuthenticationTimeUtcSetUtc() {
63 PolicyResponse resp
= new PolicyResponse();
64 resp
.AuthenticationTimeUtc
= someUtcTime
;
65 Assert
.AreEqual(someUtcTime
, resp
.AuthenticationTimeUtc
);
68 [TestMethod
, ExpectedException(typeof(ArgumentException
))]
69 public void AuthenticationTimeUtcSetUnspecified() {
70 PolicyResponse resp
= new PolicyResponse();
71 resp
.AuthenticationTimeUtc
= someUnspecifiedTime
;
75 public void AuthenticationTimeUtcSetNull() {
76 PolicyResponse resp
= new PolicyResponse();
77 resp
.AuthenticationTimeUtc
= null;
78 Assert
.IsNull(resp
.AuthenticationTimeUtc
);
79 resp
.AuthenticationTimeUtc
= someUtcTime
;
80 Assert
.IsNotNull(resp
.AuthenticationTimeUtc
);
81 resp
.AuthenticationTimeUtc
= null;
82 Assert
.IsNull(resp
.AuthenticationTimeUtc
);
86 public void NistAssuranceLevelSetVarious() {
87 PolicyResponse resp
= new PolicyResponse();
88 resp
.NistAssuranceLevel
= NistAssuranceLevel
.Level1
;
89 Assert
.AreEqual(NistAssuranceLevel
.Level1
, resp
.NistAssuranceLevel
);
90 resp
.NistAssuranceLevel
= null;
91 Assert
.IsNull(resp
.NistAssuranceLevel
);
92 resp
.NistAssuranceLevel
= NistAssuranceLevel
.InsufficientForLevel1
;
93 Assert
.AreEqual(NistAssuranceLevel
.InsufficientForLevel1
, resp
.NistAssuranceLevel
);
97 public void AssuranceLevels() {
98 PolicyResponse resp
= new PolicyResponse();
99 Assert
.AreEqual(0, resp
.AssuranceLevels
.Count
);
100 resp
.NistAssuranceLevel
= NistAssuranceLevel
.Level2
;
101 Assert
.AreEqual(1, resp
.AssuranceLevels
.Count
);
102 Assert
.AreEqual("2", resp
.AssuranceLevels
[Constants
.AuthenticationLevels
.NistTypeUri
]);
103 resp
.AssuranceLevels
[Constants
.AuthenticationLevels
.NistTypeUri
] = "3";
104 Assert
.AreEqual(NistAssuranceLevel
.Level3
, resp
.NistAssuranceLevel
);
105 resp
.AssuranceLevels
.Clear();
106 Assert
.IsNull(resp
.NistAssuranceLevel
);
110 public void EqualsTest() {
111 PolicyResponse resp
= new PolicyResponse();
112 PolicyResponse resp2
= new PolicyResponse();
113 Assert
.AreEqual(resp
, resp2
);
114 Assert
.AreNotEqual(resp
, null);
115 Assert
.AreNotEqual(null, resp
);
117 // Test ActualPolicies list comparison
118 resp
.ActualPolicies
.Add(AuthenticationPolicies
.PhishingResistant
);
119 Assert
.AreNotEqual(resp
, resp2
);
120 resp2
.ActualPolicies
.Add(AuthenticationPolicies
.MultiFactor
);
121 Assert
.AreNotEqual(resp
, resp2
);
122 resp2
.ActualPolicies
.Clear();
123 resp2
.ActualPolicies
.Add(AuthenticationPolicies
.PhishingResistant
);
124 Assert
.AreEqual(resp
, resp2
);
126 // Test ActualPolicies list comparison when that list is not in the same order.
127 resp
.ActualPolicies
.Add(AuthenticationPolicies
.MultiFactor
);
128 Assert
.AreNotEqual(resp
, resp2
);
129 resp2
.ActualPolicies
.Insert(0, AuthenticationPolicies
.MultiFactor
);
130 Assert
.AreEqual(resp
, resp2
);
132 // Test AuthenticationTimeUtc comparison.
133 resp
.AuthenticationTimeUtc
= DateTime
.Now
;
134 Assert
.AreNotEqual(resp
, resp2
);
135 resp2
.AuthenticationTimeUtc
= resp
.AuthenticationTimeUtc
;
136 Assert
.AreEqual(resp
, resp2
);
137 resp2
.AuthenticationTimeUtc
+= TimeSpan
.FromSeconds(1);
138 Assert
.AreNotEqual(resp
, resp2
);
139 resp2
.AuthenticationTimeUtc
= resp
.AuthenticationTimeUtc
;
140 Assert
.AreEqual(resp
, resp2
);
142 // Test NistAssuranceLevel comparison.
143 resp
.NistAssuranceLevel
= NistAssuranceLevel
.InsufficientForLevel1
;
144 Assert
.AreNotEqual(resp
, resp2
);
145 resp2
.NistAssuranceLevel
= NistAssuranceLevel
.InsufficientForLevel1
;
146 Assert
.AreEqual(resp
, resp2
);
147 resp
.NistAssuranceLevel
= NistAssuranceLevel
.Level2
;
148 Assert
.AreNotEqual(resp
, resp2
);
149 resp2
.NistAssuranceLevel
= NistAssuranceLevel
.Level2
;
150 Assert
.AreEqual(resp
, resp2
);
152 // Test AssuranceLevels comparison.
153 resp
.AssuranceLevels
.Add("custom", "b");
154 Assert
.AreNotEqual(resp
, resp2
);
155 resp2
.AssuranceLevels
.Add("custom", "2");
156 Assert
.AreNotEqual(resp
, resp2
);
157 resp2
.AssuranceLevels
["custom"] = "b";
158 Assert
.AreEqual(resp
, resp2
);
159 resp
.AssuranceLevels
[Constants
.AuthenticationLevels
.NistTypeUri
] = "1";
160 Assert
.AreNotEqual(resp
, resp2
);
161 resp2
.AssuranceLevels
[Constants
.AuthenticationLevels
.NistTypeUri
] = "1";
162 Assert
.AreEqual(resp
, resp2
);
166 public void SerializeRoundTrip() {
167 // This test relies on the PolicyResponse.Equals method. If this and that test
168 // are failing, work on EqualsTest first.
171 PolicyResponse resp
= new PolicyResponse(), resp2
= new PolicyResponse();
172 var fields
= ((IExtensionResponse
)resp
).Serialize(null);
173 Assert
.IsTrue(((IExtensionResponse
)resp2
).Deserialize(fields
, null, Constants
.TypeUri
));
174 Assert
.AreEqual(resp
, resp2
);
176 // Test with all fields set
177 resp2
= new PolicyResponse();
178 resp
.ActualPolicies
.Add(AuthenticationPolicies
.MultiFactor
);
179 resp
.AuthenticationTimeUtc
= someUtcTime
;
180 resp
.NistAssuranceLevel
= NistAssuranceLevel
.Level2
;
181 fields
= ((IExtensionResponse
)resp
).Serialize(null);
182 Assert
.IsTrue(((IExtensionResponse
)resp2
).Deserialize(fields
, null, Constants
.TypeUri
));
183 Assert
.AreEqual(resp
, resp2
);
185 // Test with an extra policy
186 resp2
= new PolicyResponse();
187 resp
.ActualPolicies
.Add(AuthenticationPolicies
.PhishingResistant
);
188 resp
.AssuranceLevels
.Add("customlevel", "ABC");
189 fields
= ((IExtensionResponse
)resp
).Serialize(null);
190 Assert
.IsTrue(((IExtensionResponse
)resp2
).Deserialize(fields
, null, Constants
.TypeUri
));
191 Assert
.AreEqual(resp
, resp2
);
193 // Test with a policy added twice. We should see it intelligently leave one of
194 // the doubled policies out.
195 resp2
= new PolicyResponse();
196 resp
.ActualPolicies
.Add(AuthenticationPolicies
.PhishingResistant
);
197 fields
= ((IExtensionResponse
)resp
).Serialize(null);
198 Assert
.IsTrue(((IExtensionResponse
)resp2
).Deserialize(fields
, null, Constants
.TypeUri
));
199 Assert
.AreNotEqual(resp
, resp2
);
200 // Now go ahead and add the doubled one so we can do our equality test.
201 resp2
.ActualPolicies
.Add(AuthenticationPolicies
.PhishingResistant
);
202 Assert
.AreEqual(resp
, resp2
);
206 public void Serialize() {
207 PolicyResponse resp
= new PolicyResponse(), resp2
= new PolicyResponse();
208 var fields
= ((IExtensionResponse
)resp
).Serialize(null);
209 Assert
.AreEqual(1, fields
.Count
);
210 Assert
.IsTrue(fields
.ContainsKey("auth_policies"));
211 Assert
.AreEqual(AuthenticationPolicies
.None
, fields
["auth_policies"]);
213 resp
.ActualPolicies
.Add(AuthenticationPolicies
.PhishingResistant
);
214 fields
= ((IExtensionResponse
)resp
).Serialize(null);
215 Assert
.AreEqual(1, fields
.Count
);
216 Assert
.AreEqual(AuthenticationPolicies
.PhishingResistant
, fields
["auth_policies"]);
218 resp
.ActualPolicies
.Add(AuthenticationPolicies
.PhysicalMultiFactor
);
219 fields
= ((IExtensionResponse
)resp
).Serialize(null);
220 Assert
.AreEqual(1, fields
.Count
);
222 AuthenticationPolicies
.PhishingResistant
+ " " + AuthenticationPolicies
.PhysicalMultiFactor
,
223 fields
["auth_policies"]);
225 resp
.AuthenticationTimeUtc
= DateTime
.UtcNow
;
226 fields
= ((IExtensionResponse
)resp
).Serialize(null);
227 Assert
.AreEqual(2, fields
.Count
);
228 Assert
.IsTrue(fields
.ContainsKey("auth_time"));
230 resp
.NistAssuranceLevel
= NistAssuranceLevel
.Level3
;
231 fields
= ((IExtensionResponse
)resp
).Serialize(null);
232 Assert
.AreEqual(4, fields
.Count
);
233 Assert
.IsTrue(fields
.ContainsKey("auth_level.ns.nist"));
234 Assert
.AreEqual(Constants
.AuthenticationLevels
.NistTypeUri
, fields
["auth_level.ns.nist"]);
235 Assert
.IsTrue(fields
.ContainsKey("auth_level.nist"));
236 Assert
.AreEqual("3", fields
["auth_level.nist"]);
238 resp
.AssuranceLevels
.Add("custom", "CU");
239 fields
= ((IExtensionResponse
)resp
).Serialize(null);
240 Assert
.AreEqual(6, fields
.Count
);
241 Assert
.IsTrue(fields
.ContainsKey("auth_level.ns.alias2"));
242 Assert
.AreEqual("custom", fields
["auth_level.ns.alias2"]);
243 Assert
.IsTrue(fields
.ContainsKey("auth_level.alias2"));
244 Assert
.AreEqual("CU", fields
["auth_level.alias2"]);
245 // and make sure the NIST is still there.
246 Assert
.IsTrue(fields
.ContainsKey("auth_level.ns.nist"));
247 Assert
.AreEqual(Constants
.AuthenticationLevels
.NistTypeUri
, fields
["auth_level.ns.nist"]);
248 Assert
.IsTrue(fields
.ContainsKey("auth_level.nist"));
249 Assert
.AreEqual("3", fields
["auth_level.nist"]);