Merge pull request #188 from damageboy/exposed-handle-compat
[mono.git] / mcs / class / corlib / Test / System.Security.Principal / GenericIdentityTest.cs
1 //
2 // GenericIdentityTest.cs - NUnit Test Cases for GenericIdentity
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 //
10
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;
17
18 namespace MonoTests.System.Security.Principal {
19
20         [TestFixture]
21         public class GenericIdentityTest {
22
23                 [Test]
24                 [ExpectedException (typeof (ArgumentNullException))]
25                 public void NullName () 
26                 {
27                         GenericIdentity gi = new GenericIdentity (null);
28                 }
29
30                 [Test]
31                 [ExpectedException (typeof (ArgumentNullException))]
32                 public void NullAuthenticationType () 
33                 {
34                         GenericIdentity gi = new GenericIdentity ("user", null);
35                 }
36
37                 [Test]
38                 public void Name () 
39                 {
40                         GenericIdentity gi = new GenericIdentity ("user");
41                         Assert.AreEqual ("user", gi.Name, "Name");
42                         Assert.AreEqual (String.Empty, gi.AuthenticationType, "AuthenticationType");
43                         Assert.IsTrue (gi.IsAuthenticated, "IsAuthenticated");
44                 }
45
46                 [Test]
47                 public void NameAuthenticationType () 
48                 {
49                         GenericIdentity gi = new GenericIdentity ("user", "blood oath");
50                         Assert.AreEqual ("user", gi.Name, "Name");
51                         Assert.AreEqual ("blood oath", gi.AuthenticationType, "AuthenticationType");
52                         Assert.IsTrue (gi.IsAuthenticated, "IsAuthenticated");
53                 }
54
55                 [Test]
56                 public void EmptyName () 
57                 {
58                         GenericIdentity gi = new GenericIdentity ("");
59                         Assert.AreEqual (String.Empty, gi.Name, "Name");
60                         Assert.AreEqual (String.Empty, gi.AuthenticationType, "AuthenticationType");
61                         Assert.IsFalse (gi.IsAuthenticated, "IsAuthenticated");
62                 }
63
64                 [Test]
65                 public void SerializationRoundtrip ()
66                 {
67                         GenericIdentity gi = new GenericIdentity ("mono", "dna");
68                         BinaryFormatter bf = new BinaryFormatter ();
69                         MemoryStream ms = new MemoryStream ();
70                         bf.Serialize (ms, gi);
71
72                         //Console.WriteLine (BitConverter.ToString (ms.ToArray ()));
73
74                         ms.Position = 0;
75                         GenericIdentity clone = (GenericIdentity) bf.Deserialize (ms);
76                         Assert.AreEqual (gi.Name, clone.Name, "Name");
77                         Assert.AreEqual (gi.AuthenticationType, clone.AuthenticationType, "AuthenticationType");
78                         Assert.AreEqual (gi.IsAuthenticated, clone.IsAuthenticated, "IsAuthenticated");
79                 }
80
81 #if NET_2_0
82                 static byte[] identity = { 0x00, 0x01, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x01, 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, 0x02, 0x00, 0x00, 0x00, 0x04, 0x6D, 0x6F, 0x6E, 0x6F, 0x06, 0x03, 0x00, 0x00, 0x00, 0x03, 0x64, 0x6E, 0x61, 0x0B };
83 #else
84                 static byte[] identity = { 0x00, 0x01, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x01, 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, 0x02, 0x00, 0x00, 0x00, 0x04, 0x6D, 0x6F, 0x6E, 0x6F, 0x06, 0x03, 0x00, 0x00, 0x00, 0x03, 0x64, 0x6E, 0x61, 0x0B };
85 #endif
86
87                 [Test]
88                 public void Deserialize ()
89                 {
90                         BinaryFormatter bf = new BinaryFormatter ();
91                         MemoryStream ms = new MemoryStream (identity);
92                         GenericIdentity gi = (GenericIdentity) bf.Deserialize (ms);
93                         Assert.AreEqual ("mono", gi.Name, "Name");
94                         Assert.AreEqual ("dna", gi.AuthenticationType, "AuthenticationType");
95                         Assert.IsTrue (gi.IsAuthenticated, "IsAuthenticated");
96                 }
97         }
98 }