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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
|
//-----------------------------------------------------------------------
// <copyright file="RealmTests.cs" company="Outercurve Foundation">
// Copyright (c) Outercurve Foundation. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace DotNetOpenAuth.Test {
using System;
using DotNetOpenAuth.Messaging;
using DotNetOpenAuth.Messaging.Reflection;
using DotNetOpenAuth.OpenId;
using DotNetOpenAuth.Test.Mocks;
using NUnit.Framework;
[TestFixture]
public class RealmTests {
[Test]
public void ValidRealmsTest() {
// Just create these. If any are determined to be invalid,
// an exception should be thrown that would fail this test.
new Realm("http://www.myopenid.com");
new Realm("http://www.myopenid.com/");
new Realm("http://www.myopenid.com:5221/");
new Realm("https://www.myopenid.com");
new Realm("http://www.myopenid.com/abc");
new Realm("http://www.myopenid.com/abc/");
new Realm("http://*.myopenid.com/");
new Realm("http://*.com/");
new Realm("http://*.guest.myopenid.com/");
}
[Test]
[ExpectedException(typeof(ArgumentNullException))]
public void InvalidRealmNullString() {
new Realm((string)null);
}
[Test]
[ExpectedException(typeof(ArgumentNullException))]
public void InvalidRealmNullUri() {
new Realm((Uri)null);
}
[Test]
[ExpectedException(typeof(UriFormatException))]
public void InvalidRealmEmpty() {
new Realm(string.Empty);
}
[Test]
[ExpectedException(typeof(UriFormatException))]
public void InvalidRealmBadProtocol() {
new Realm("asdf://www.microsoft.com/");
}
[Test]
[ExpectedException(typeof(UriFormatException))]
public void InvalidRealmNoScheme() {
new Realm("www.guy.com");
}
[Test]
[ExpectedException(typeof(UriFormatException))]
public void InvalidRealmBadWildcard1() {
new Realm("http://*www.my.com");
}
[Test]
[ExpectedException(typeof(UriFormatException))]
public void InvalidRealmBadWildcard2() {
new Realm("http://www.*.com");
}
[Test]
[ExpectedException(typeof(UriFormatException))]
public void InvalidRealmBadWildcard3() {
new Realm("http://www.my.*/");
}
[Test]
[ExpectedException(typeof(UriFormatException))]
public void InvalidRealmTwoWildcards1() {
new Realm("http://**.my.com");
}
[Test]
[ExpectedException(typeof(UriFormatException))]
public void InvalidRealmTwoWildcards2() {
new Realm("http://*.*.my.com");
}
[Test]
public void IsSaneTest() {
Assert.IsTrue(new Realm("http://www.myopenid.com").IsSane);
Assert.IsTrue(new Realm("http://myopenid.com").IsSane);
Assert.IsTrue(new Realm("http://localhost").IsSane);
Assert.IsTrue(new Realm("http://localhost:33532/dab").IsSane);
Assert.IsTrue(new Realm("http://www.myopenid.com").IsSane);
Assert.IsFalse(new Realm("http://*.com").IsSane);
Assert.IsFalse(new Realm("http://*.co.uk").IsSane);
}
[Test]
public void IsUrlWithinRealmTests() {
/*
* The openid.return_to URL MUST descend from the openid.trust_root, or the
* Identity Provider SHOULD return an error. Namely, the URL scheme and port
* MUST match. The path, if present, MUST be equal to or below the value of
* openid.trust_root, and the domains on both MUST match, or, the
* openid.trust_root value contain a wildcard like http://*.example.com.
* The wildcard SHALL only be at the beginning. It is RECOMMENDED Identity
* Provider's protect their End Users from requests for things like
* http://*.com/ or http://*.co.uk/.
*/
// Schemes must match
Assert.IsFalse(new Realm("https://www.my.com/").Contains("http://www.my.com/"));
Assert.IsFalse(new Realm("http://www.my.com/").Contains("https://www.my.com/"));
// Ports must match
Assert.IsTrue(new Realm("http://www.my.com/").Contains("http://www.my.com:80/boo"));
Assert.IsTrue(new Realm("http://www.my.com:80/").Contains("http://www.my.com/boo"));
Assert.IsFalse(new Realm("http://www.my.com:79/").Contains("http://www.my.com/boo"));
Assert.IsFalse(new Realm("https://www.my.com/").Contains("http://www.my.com:79/boo"));
// Path must be (at or) below trust root
Assert.IsTrue(new Realm("http://www.my.com/").Contains("http://www.my.com/"));
Assert.IsTrue(new Realm("http://www.my.com/").Contains("http://www.my.com/boo"));
Assert.IsTrue(new Realm("http://www.my.com/p/").Contains("http://www.my.com/p/l"));
Assert.IsTrue(new Realm("http://www.my.com/bah").Contains("http://www.my.com/bah/bah"));
Assert.IsTrue(new Realm("http://www.my.com/bah").Contains("http://www.my.com/bah/bah"));
Assert.IsTrue(new Realm("http://www.my.com/bah.html").Contains("http://www.my.com/bah.html/bah"));
Assert.IsFalse(new Realm("http://www.my.com/bah").Contains("http://www.my.com/bahbah"));
Assert.IsTrue(new Realm("http://www.my.com/bah").Contains("http://www.my.com/bah?q=a"));
Assert.IsTrue(new Realm("http://www.my.com/bah?q=a").Contains("http://www.my.com/bah?q=a"));
Assert.IsTrue(new Realm("http://www.my.com/bah?a=b&c=d").Contains("http://www.my.com/bah?a=b&c=d&e=f"));
Assert.IsFalse(new Realm("http://www.my.com/bah?a=b&c=d").Contains("http://www.my.com/bah?a=b"));
// Domains MUST match
Assert.IsFalse(new Realm("http://www.my.com/").Contains("http://yours.com/"));
Assert.IsFalse(new Realm("http://www.my.com/").Contains("http://www.yours.com/"));
Assert.IsFalse(new Realm("http://www.my.com/").Contains("http://q.www.my.com/"));
Assert.IsFalse(new Realm("http://www.my.com/").Contains("http://wwww.my.com/"));
Assert.IsFalse(new Realm("http://www.my.com/").Contains("http://www.my.com.uk/"));
Assert.IsFalse(new Realm("http://www.my.com/").Contains("http://www.my.comm/"));
// Allow for wildcards
Assert.IsTrue(new Realm("http://*.www.my.com/").Contains("http://bah.www.my.com/"));
Assert.IsTrue(new Realm("http://*.www.my.com/").Contains("http://bah.WWW.MY.COM/"));
Assert.IsTrue(new Realm("http://*.www.my.com/").Contains("http://bah.www.my.com/boo"));
Assert.IsTrue(new Realm("http://*.my.com/").Contains("http://bah.www.my.com/boo"));
Assert.IsTrue(new Realm("http://*.my.com/").Contains("http://my.com/boo"));
Assert.IsFalse(new Realm("http://*.my.com/").Contains("http://ohmeohmy.com/"));
Assert.IsFalse(new Realm("http://*.my.com/").Contains("http://me.com/"));
Assert.IsFalse(new Realm("http://*.my.com/").Contains("http://my.co/"));
Assert.IsFalse(new Realm("http://*.my.com/").Contains("http://com/"));
Assert.IsFalse(new Realm("http://*.www.my.com/").Contains("http://my.com/"));
Assert.IsFalse(new Realm("http://*.www.my.com/").Contains("http://zzz.my.com/"));
// These are tested against by the constructor test, as these are invalid wildcard positions.
////Assert.IsFalse(new Realm("http://*www.my.com/").ValidateUrl("http://bah.www.my.com/"));
////Assert.IsFalse(new Realm("http://*www.my.com/").ValidateUrl("http://wwww.my.com/"));
// Among those that should return true, mix up character casing to test for case sensitivity.
// Host names should be case INSENSITIVE, but paths should probably be case SENSITIVE,
// because in some systems they are case sensitive and to ignore this would open
// security holes.
Assert.IsTrue(new Realm("http://www.my.com/").Contains("http://WWW.MY.COM/"));
Assert.IsFalse(new Realm("http://www.my.com/abc").Contains("http://www.my.com/ABC"));
}
[Test]
public void ImplicitConversionFromStringTests() {
Realm realm = "http://host";
Assert.AreEqual("host", realm.Host);
realm = (string)null;
Assert.IsNull(realm);
}
[Test]
public void ImplicitConversionToStringTests() {
Realm realm = new Realm("http://host/");
string realmString = realm;
Assert.AreEqual("http://host/", realmString);
realm = null;
realmString = realm;
Assert.IsNull(realmString);
}
[Test]
public void ImplicitConverstionFromUriTests() {
Uri uri = new Uri("http://host");
Realm realm = uri;
Assert.AreEqual(uri.Host, realm.Host);
uri = null;
realm = uri;
Assert.IsNull(realm);
}
[Test]
public void EqualsTest() {
Realm testRealm1a = new Realm("http://www.yahoo.com");
Realm testRealm1b = new Realm("http://www.yahoo.com");
Realm testRealm2 = new Realm("http://www.yahoo.com/b");
Realm testRealm3 = new Realm("http://*.www.yahoo.com");
Assert.AreEqual(testRealm1a, testRealm1b);
Assert.AreNotEqual(testRealm1a, testRealm2);
Assert.AreNotEqual(testRealm1a, null);
Assert.AreNotEqual(testRealm1a, testRealm1a.ToString(), "Although the URLs are equal, different object types shouldn't be equal.");
Assert.AreNotEqual(testRealm3, testRealm1a, "Wildcard difference ignored by Equals");
}
[Test]
public void MessagePartConvertibility() {
var message = new MessageWithRealm();
var messageDescription = new MessageDescription(message.GetType(), new Version(1, 0));
var messageDictionary = new MessageDictionary(message, messageDescription, false);
messageDictionary["Realm"] = OpenId.OpenIdTestBase.RPRealmUri.AbsoluteUri;
Assert.That(messageDictionary["Realm"], Is.EqualTo(OpenId.OpenIdTestBase.RPRealmUri.AbsoluteUri));
}
private class MessageWithRealm : TestMessage {
[MessagePart]
internal Realm Realm { get; set; }
}
}
}
|