File: ClaimsPrincipalTest.cs

package info (click to toggle)
mono-reference-assemblies 3.12.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 604,240 kB
  • ctags: 625,505
  • sloc: cs: 3,967,741; xml: 2,793,081; ansic: 418,042; java: 60,435; sh: 14,833; makefile: 11,576; sql: 7,956; perl: 1,467; cpp: 1,446; yacc: 1,203; python: 598; asm: 422; sed: 16; php: 1
file content (256 lines) | stat: -rw-r--r-- 7,514 bytes parent folder | download | duplicates (2)
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
//
//
// ClaimsPrincipalTest.cs - NUnit Test Cases for System.Security.Claims.ClaimsPrincipal
//

#if NET_4_5

using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Security.Principal;

namespace MonoTests.System.Security.Claims
{
	[TestFixture]
	public class ClaimsPrincipalTest
	{
		#region Ctor Empty

		[Test]
		public void EmptyCtorWorks ()
		{
			var p = new ClaimsPrincipal ();
			Assert.IsNotNull (p.Identities, "#1");
			Assert.AreEqual (p.Identities.ToArray ().Length, 0, "#2");

			Assert.IsNotNull (p.Claims, "#3");
			Assert.AreEqual (p.Claims.ToArray ().Length, 0, "#4");

			Assert.IsNull (p.Identity, "#5");
		}

		#endregion

		#region Ctor IIdentity

		[Test]
		[ExpectedException (typeof (ArgumentNullException))]
		public void IIdentityCtorNullThrows ()
		{
			var p = new ClaimsPrincipal ((IIdentity)null);
		}

		[Test]
		public void IIdentityCtorClaimsIdentityWorks ()
		{
			var id = new ClaimsIdentity (
				       new List<Claim> { new Claim ("claim_type", "claim_value") },
				       "");
			var p = new ClaimsPrincipal (id);

			Assert.IsNotNull (p.Identities, "#1");
			Assert.AreEqual (1, p.Identities.Count (), "#2");

			Assert.AreEqual (id, p.Identities.First (), "#3");

			Assert.AreEqual (id, p.Identity, "#4");

			Assert.IsNotNull (p.Claims, "#5");
			Assert.AreEqual (1, p.Claims.Count (), "#6");
			Assert.IsTrue (p.Claims.Any (claim => claim.Type == "claim_type" && claim.Value == "claim_value"), "#7");
		}

		[Test]
		public void IIdentityCtorNonClaimsIdentityWorks ()
		{
			var id = new TestIdentity {
				Name = "test_name", 
				AuthenticationType = "test_auth"
			};
			var p = new ClaimsPrincipal (id);

			Assert.IsNotNull (p.Identities, "#1");
			Assert.AreEqual (1, p.Identities.Count (), "#2");

			Assert.AreNotEqual (id, p.Identities.First (), "#3");
			Assert.AreNotEqual (id, p.Identity, "#4");
			Assert.AreEqual (id.Name, p.Identity.Name, "#5");

			Assert.IsNotNull (p.Claims, "#6");
			Assert.AreEqual (1, p.Claims.Count (), "#7");
			Assert.IsTrue (p.Claims.Any (claim => claim.Type == ClaimsIdentity.DefaultNameClaimType && claim.Value == "test_name"), "#8");
		}

		#endregion

		#region Ctor IPrincipal

		[Test]
		[ExpectedException (typeof (ArgumentNullException))]
		public void IPrincipalCtorNullThrows ()
		{
			var p = new ClaimsPrincipal ((IPrincipal)null);
		}

		[Test]
		public void IPrincipalCtorClaimsPrincipalWorks ()
		{
			var baseId = new ClaimsIdentity (
				           new List<Claim> { new Claim ("claim_type", "claim_value") },
				           "");
			var basePrincipal = new ClaimsPrincipal ();
			basePrincipal.AddIdentity (baseId);

			var p = new ClaimsPrincipal (basePrincipal);

			Assert.IsNotNull (p.Identities, "#1");
			Assert.AreEqual (1, p.Identities.Count (), "#2");

			Assert.AreEqual (baseId, p.Identities.First (), "#3");

			Assert.AreEqual (baseId, p.Identity, "#4");

			Assert.IsNotNull (p.Claims, "$5");
			Assert.AreEqual (1, p.Claims.Count (), "#6");
			Assert.IsTrue (p.Claims.Any (claim => claim.Type == "claim_type" && claim.Value == "claim_value"), "#7");
		}

		[Test]
		public void IPrincipalCtorNonClaimsPrincipalWithNonClaimsIdentityWorks ()
		{
			var id = new TestIdentity {
				Name = "test_name", 
				AuthenticationType = "test_auth"
			};
			var basePrincipal = new TestPrincipal { Identity = id };
			var p = new ClaimsPrincipal (basePrincipal);

			Assert.IsNotNull (p.Identities, "#1");
			Assert.AreEqual (1, p.Identities.Count (), "#2");

			Assert.AreNotEqual (id, p.Identities.First (), "#3");
			Assert.AreNotEqual (id, p.Identity, "#4");
			Assert.AreEqual (id.Name, p.Identity.Name, "#5");

			Assert.IsNotNull (p.Claims, "#6");
			Assert.AreEqual (1, p.Claims.Count (), "#7");
			Assert.IsTrue (p.Claims.Any (claim => claim.Type == ClaimsIdentity.DefaultNameClaimType && claim.Value == "test_name"), "#8");
		}

		[Test]
		public void IPrincipalCtorNonClaimsPrincipalWithoutIdentityWorks ()
		{
			var p = new ClaimsPrincipal (new TestPrincipal ());
			Assert.IsNotNull (p.Identities, "#1");
			Assert.AreEqual (p.Identities.ToArray ().Length, 1, "#2");

			Assert.IsNotNull (p.Claims, "#3");
			Assert.AreEqual (p.Claims.ToArray ().Length, 0, "#4");

			Assert.IsNotNull (p.Identity, "#5");
			Assert.IsFalse (p.Identity.IsAuthenticated, "#6");
		}

		[Test]
		[Category ("Ctor_IPrincipal")]
		public void IPrincipalCtorClaimsPrincipalWithoutIdentityWorks ()
		{
			var p = new ClaimsPrincipal (new ClaimsPrincipal ());
			Assert.IsNotNull (p.Identities, "#1");
			Assert.AreEqual (p.Identities.ToArray ().Length, 0, "#2");

			Assert.IsNotNull (p.Claims, "#3");
			Assert.AreEqual (p.Claims.ToArray ().Length, 0, "#4");

			Assert.IsNull (p.Identity, "#5");
		}

		[Test]
		public void IPrincipalCtorClaimsPrincipalWithMultipleIdentitiesWorks ()
		{
			var baseId1 = new ClaimsIdentity ("baseId1");
			var baseId2 = new GenericIdentity ("generic_name", "baseId2");
			var baseId3 = WindowsIdentity.GetAnonymous ();

			var basePrincipal = new ClaimsPrincipal (baseId1);
			basePrincipal.AddIdentity (baseId2);
			basePrincipal.AddIdentity (baseId3);

			var p = new ClaimsPrincipal (basePrincipal);
			Assert.IsNotNull (p.Identities, "#1");
			Assert.AreEqual (3, p.Identities.Count (), "#2");

			Assert.IsNotNull (p.Claims, "#3");
			Assert.AreEqual (1, p.Claims.Count (), "#4");

			// The Identity property favours WindowsIdentity
			Assert.AreEqual (baseId3, p.Identity, "#5");

			Assert.IsTrue (p.Claims.Any (claim => claim.Type == ClaimsIdentity.DefaultNameClaimType && claim.Value == "generic_name"), "#6");

			Assert.AreEqual (baseId2.Claims.First (), p.Claims.First (), "#7");
		}

		#endregion

		#region Ctor IEnumerable<ClaimsIdentity>

		[Test]
		[ExpectedException (typeof (ArgumentNullException))]
		public void IEnumClaimsIdCtorNullThrows ()
		{
			var p = new ClaimsPrincipal ((IEnumerable<ClaimsIdentity>)null);
		}

		[Test]
		public void IEnumClaimsIdCtorEmptyWorks ()
		{
			var p = new ClaimsPrincipal (new ClaimsIdentity [0]);
			Assert.IsNotNull (p.Identities, "#1");
			Assert.AreEqual (p.Identities.ToArray ().Length, 0, "#2");
			Assert.IsNotNull (p.Claims, "#3");
			Assert.AreEqual (p.Claims.ToArray ().Length, 0, "#4");
			Assert.IsNull (p.Identity, "#5");
		}

		[Test]
		public void IEnumClaimsIdCtorMultipleIdentitiesWorks ()
		{
			var baseId1 = new ClaimsIdentity ("baseId1");
			var baseId2 = new GenericIdentity ("generic_name2", "baseId2");
			var baseId3 = new GenericIdentity ("generic_name3", "baseId3");

			var p = new ClaimsPrincipal (new List<ClaimsIdentity> { baseId1, baseId2, baseId3 });
			Assert.IsNotNull (p.Identities, "#1");
			Assert.AreEqual (3, p.Identities.Count (), "#2");

			Assert.IsNotNull (p.Claims, "#3");
			Assert.AreEqual (2, p.Claims.Count (), "#4");

			Assert.AreEqual (baseId1, p.Identity, "#5");

			Assert.IsTrue (p.Claims.Any (claim => claim.Type == ClaimsIdentity.DefaultNameClaimType && claim.Value == "generic_name2"), "#6");
			Assert.IsTrue (p.Claims.Any (claim => claim.Type == ClaimsIdentity.DefaultNameClaimType && claim.Value == "generic_name3"), "#7");

			Assert.AreEqual (baseId2.Claims.First (), p.Claims.First (), "#7");
			Assert.AreEqual (baseId3.Claims.Last (), p.Claims.Last (), "#8");
		}

		#endregion

		internal class TestPrincipal : IPrincipal
		{
			public IIdentity Identity { get; set; }

			public bool IsInRole (string role)
			{
				throw new NotImplementedException ();
			}
		}
	}
}

#endif