2010-04-15 Jb Evain <jbevain@novell.com>
[mcs.git] / class / corlib / Test / System.Security.Principal / GenericPrincipalTest.cs
blob4d04928d4ed77f5a2079a121a4a7dcfc1936d9c9
1 //
2 // GenericPrincipalTest.cs - NUnit Test Cases for GenericPrincipal
3 //
4 // Author:
5 // Sebastien Pouliot <sebastien@ximian.com>
6 //
7 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
8 // Copyright (C) 2006 Novell, Inc (http://www.novell.com)
9 //
11 using NUnit.Framework;
12 using System;
13 using System.IO;
14 using System.Runtime.Serialization;
15 using System.Runtime.Serialization.Formatters.Binary;
16 using System.Security.Principal;
18 namespace MonoTests.System.Security.Principal {
20 [TestFixture]
21 public class GenericPrincipalTest {
23 [Test]
24 [ExpectedException (typeof (ArgumentNullException))]
25 public void NullIdentity ()
27 GenericPrincipal gp = new GenericPrincipal (null, new string [5]);
30 [Test]
31 public void NullRoles ()
33 GenericIdentity gi = new GenericIdentity ("user");
34 GenericPrincipal gp = new GenericPrincipal (gi, null);
35 Assert.AreEqual ("user", gp.Identity.Name, "Identity");
36 Assert.IsFalse (gp.IsInRole ("role 1"), "NoRole.IsInRole(x)");
39 [Test]
40 public void IsInRole ()
42 GenericIdentity gi = new GenericIdentity ("user");
43 string[] roles = new string [5];
44 roles [0] = "role 1";
45 GenericPrincipal gp = new GenericPrincipal (gi, roles);
46 roles [1] = "role 2";
47 Assert.IsTrue (gp.IsInRole ("role 1"), "IsInRole (role added before constructor)");
48 Assert.IsFalse (gp.IsInRole ("role 2"), "IsInRole (role added after constructor)");
51 [Test]
52 public void IsInRole_CaseInsensitive ()
54 GenericIdentity gi = new GenericIdentity ("user");
55 GenericPrincipal gp = new GenericPrincipal (gi, new string[2] { "mono", "hackers" });
56 Assert.IsTrue (gp.IsInRole ("MoNo"), "MoNo");
57 Assert.IsTrue (gp.IsInRole ("hAcKeRs"), "hAcKeRs");
60 [Test]
61 public void SerializationRoundtrip ()
63 GenericIdentity gi = new GenericIdentity ("mono", "dna");
64 GenericPrincipal gp = new GenericPrincipal (gi, new string[2] { "monkey", "hackers" });
65 BinaryFormatter bf = new BinaryFormatter ();
66 MemoryStream ms = new MemoryStream ();
67 bf.Serialize (ms, gp);
69 //Console.WriteLine (BitConverter.ToString (ms.ToArray ()));
71 ms.Position = 0;
72 GenericPrincipal clone = (GenericPrincipal) bf.Deserialize (ms);
73 Assert.AreEqual (gp.Identity.Name, clone.Identity.Name, "Name");
74 Assert.AreEqual (gp.Identity.AuthenticationType, clone.Identity.AuthenticationType, "AuthenticationType");
75 Assert.AreEqual (gp.Identity.IsAuthenticated, clone.Identity.IsAuthenticated, "IsAuthenticated");
76 Assert.IsTrue (gp.IsInRole ("monkey"), "IsInRole-monkey");
77 Assert.IsTrue (gp.IsInRole ("hackers"), "IsInRole-hackers");
78 Assert.IsFalse (gp.IsInRole ("donkey"), "IsInRole-donkey");
81 #if NET_2_0
82 static byte[] principal = { 0x00, 0x01, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x01, 0x00, 0x00, 0x00, 0x2A, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6D, 0x2E, 0x53, 0x65, 0x63, 0x75, 0x72, 0x69, 0x74, 0x79, 0x2E, 0x50, 0x72, 0x69, 0x6E, 0x63, 0x69, 0x70, 0x61, 0x6C, 0x2E, 0x47, 0x65, 0x6E, 0x65, 0x72, 0x69, 0x63, 0x50, 0x72, 0x69, 0x6E, 0x63, 0x69, 0x70, 0x61, 0x6C, 0x02, 0x00, 0x00, 0x00, 0x0A, 0x6D, 0x5F, 0x69, 0x64, 0x65, 0x6E, 0x74, 0x69, 0x74, 0x79, 0x07, 0x6D, 0x5F, 0x72, 0x6F, 0x6C, 0x65, 0x73, 0x03, 0x06, 0x29, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6D, 0x2E, 0x53, 0x65, 0x63, 0x75, 0x72, 0x69, 0x74, 0x79, 0x2E, 0x50, 0x72, 0x69, 0x6E, 0x63, 0x69, 0x70, 0x61, 0x6C, 0x2E, 0x47, 0x65, 0x6E, 0x65, 0x72, 0x69, 0x63, 0x49, 0x64, 0x65, 0x6E, 0x74, 0x69, 0x74, 0x79, 0x09, 0x02, 0x00, 0x00, 0x00, 0x09, 0x03, 0x00, 0x00, 0x00, 0x04, 0x02, 0x00, 0x00, 0x00, 0x29, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6D, 0x2E, 0x53, 0x65, 0x63, 0x75, 0x72, 0x69, 0x74, 0x79, 0x2E, 0x50, 0x72, 0x69, 0x6E, 0x63, 0x69, 0x70, 0x61, 0x6C, 0x2E, 0x47, 0x65, 0x6E, 0x65, 0x72, 0x69, 0x63, 0x49, 0x64, 0x65, 0x6E, 0x74, 0x69, 0x74, 0x79, 0x02, 0x00, 0x00, 0x00, 0x06, 0x6D, 0x5F, 0x6E, 0x61, 0x6D, 0x65, 0x06, 0x6D, 0x5F, 0x74, 0x79, 0x70, 0x65, 0x01, 0x01, 0x06, 0x04, 0x00, 0x00, 0x00, 0x04, 0x6D, 0x6F, 0x6E, 0x6F, 0x06, 0x05, 0x00, 0x00, 0x00, 0x03, 0x64, 0x6E, 0x61, 0x11, 0x03, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x06, 0x06, 0x00, 0x00, 0x00, 0x06, 0x6D, 0x6F, 0x6E, 0x6B, 0x65, 0x79, 0x06, 0x07, 0x00, 0x00, 0x00, 0x07, 0x68, 0x61, 0x63, 0x6B, 0x65, 0x72, 0x73, 0x0B };
83 #else
84 static byte[] principal = { 0x00, 0x01, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x01, 0x00, 0x00, 0x00, 0x2A, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6D, 0x2E, 0x53, 0x65, 0x63, 0x75, 0x72, 0x69, 0x74, 0x79, 0x2E, 0x50, 0x72, 0x69, 0x6E, 0x63, 0x69, 0x70, 0x61, 0x6C, 0x2E, 0x47, 0x65, 0x6E, 0x65, 0x72, 0x69, 0x63, 0x50, 0x72, 0x69, 0x6E, 0x63, 0x69, 0x70, 0x61, 0x6C, 0x02, 0x00, 0x00, 0x00, 0x0A, 0x6D, 0x5F, 0x69, 0x64, 0x65, 0x6E, 0x74, 0x69, 0x74, 0x79, 0x07, 0x6D, 0x5F, 0x72, 0x6F, 0x6C, 0x65, 0x73, 0x03, 0x06, 0x29, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6D, 0x2E, 0x53, 0x65, 0x63, 0x75, 0x72, 0x69, 0x74, 0x79, 0x2E, 0x50, 0x72, 0x69, 0x6E, 0x63, 0x69, 0x70, 0x61, 0x6C, 0x2E, 0x47, 0x65, 0x6E, 0x65, 0x72, 0x69, 0x63, 0x49, 0x64, 0x65, 0x6E, 0x74, 0x69, 0x74, 0x79, 0x09, 0x02, 0x00, 0x00, 0x00, 0x09, 0x03, 0x00, 0x00, 0x00, 0x04, 0x02, 0x00, 0x00, 0x00, 0x29, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6D, 0x2E, 0x53, 0x65, 0x63, 0x75, 0x72, 0x69, 0x74, 0x79, 0x2E, 0x50, 0x72, 0x69, 0x6E, 0x63, 0x69, 0x70, 0x61, 0x6C, 0x2E, 0x47, 0x65, 0x6E, 0x65, 0x72, 0x69, 0x63, 0x49, 0x64, 0x65, 0x6E, 0x74, 0x69, 0x74, 0x79, 0x02, 0x00, 0x00, 0x00, 0x06, 0x6D, 0x5F, 0x6E, 0x61, 0x6D, 0x65, 0x06, 0x6D, 0x5F, 0x74, 0x79, 0x70, 0x65, 0x01, 0x01, 0x06, 0x04, 0x00, 0x00, 0x00, 0x04, 0x6D, 0x6F, 0x6E, 0x6F, 0x06, 0x05, 0x00, 0x00, 0x00, 0x03, 0x64, 0x6E, 0x61, 0x11, 0x03, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x06, 0x06, 0x00, 0x00, 0x00, 0x06, 0x6D, 0x6F, 0x6E, 0x6B, 0x65, 0x79, 0x06, 0x07, 0x00, 0x00, 0x00, 0x07, 0x68, 0x61, 0x63, 0x6B, 0x65, 0x72, 0x73, 0x0B };
85 #endif
87 [Test]
88 public void Deserialize ()
90 BinaryFormatter bf = new BinaryFormatter ();
91 MemoryStream ms = new MemoryStream (principal);
92 GenericPrincipal gp = (GenericPrincipal) bf.Deserialize (ms);
93 Assert.AreEqual ("mono", gp.Identity.Name, "Name");
94 Assert.AreEqual ("dna", gp.Identity.AuthenticationType, "AuthenticationType");
95 Assert.IsTrue (gp.Identity.IsAuthenticated, "IsAuthenticated");
96 Assert.IsTrue (gp.IsInRole ("monkey"), "IsInRole-monkey");
97 Assert.IsTrue (gp.IsInRole ("hackers"), "IsInRole-hackers");
98 Assert.IsFalse (gp.IsInRole ("donkey"), "IsInRole-donkey");