Fri Nov 24 18:38:31 CET 2006 Paolo Molaro <lupus@ximian.com>
[mono.git] / mcs / class / Microsoft.Web.Services / Test / Microsoft.Web.Services.Security / NonceTest.cs
1 //
2 // NonceTest.cs - NUnit Test Cases for Nonce
3 //
4 // Author:
5 //      Sebastien Pouliot (spouliot@motus.com)
6 //
7 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
8 //
9
10 using NUnit.Framework;
11 using Microsoft.Web.Services.Security;
12 using System;
13 using System.Xml;
14
15 namespace MonoTests.MS.Web.Services.Security {
16
17         [TestFixture]
18         public class NonceTest : Assertion {
19
20                 private const string Sample = "<wsse:Nonce xmlns:wsse=\"http://schemas.xmlsoap.org/ws/2002/12/secext\">GWSRuy1IC4zb2jsA+Sz/rw==</wsse:Nonce>";
21                 private const string Zero = "<wsse:Nonce xmlns:wsse=\"http://schemas.xmlsoap.org/ws/2002/12/secext\"></wsse:Nonce>";
22
23
24                 // NOTE: Nonce doesn't have a public constructor in WSE1 (*) so tests can only be runned with WSE2
25                 //       (*) this is bad because we can't reuse Nonce inside other security protocols
26 #if !WSE1
27                 [Test]
28                 public void ConstructorInt () 
29                 {
30                         Nonce n = new Nonce (16);
31                 }
32
33                 [Test]
34                 public void ConstructorIntZero () 
35                 {
36                         Nonce n = new Nonce (0);
37                         AssertEquals ("Nonce(0).Value", String.Empty, n.Value);
38                         AssertNotNull ("Nonce(0).GetValueBytes()", n.GetValueBytes ());
39                         XmlDocument doc = new XmlDocument ();
40                         XmlElement xel = n.GetXml (doc);
41                         AssertEquals ("Nonce(0).GetXml", Zero, xel.OuterXml);
42                 }
43
44                 [Test]
45                 [ExpectedException (typeof (OverflowException))] 
46                 public void ConstructorNegativeInt () 
47                 {
48                         Nonce n = new Nonce (-1);
49                 }
50
51                 [Test]
52                 public void ConstructorXmlElement () 
53                 {
54                         XmlDocument doc = new XmlDocument ();
55                         doc.LoadXml (Sample);
56                         Nonce n = new Nonce (doc.DocumentElement);
57                         // roundtrip
58                         XmlElement xel = n.GetXml (doc);
59                         AssertEquals ("ConstructorXmlElement", Sample, xel.OuterXml);
60                 }
61
62                 [Test]
63                 [ExpectedException (typeof (ArgumentNullException))] 
64                 public void ConstructorXmlElementNull () 
65                 {
66                         Nonce n = new Nonce (null);
67                 }
68
69                 [Test]
70                 public void Value () 
71                 {
72                         Nonce n = new Nonce (16);
73                         AssertNotNull ("Value", n.Value);
74                         byte[] v = n.GetValueBytes ();
75                         AssertNotNull ("GetValueBytes", v);
76                         AssertEquals ("Value==base64(GetValueBytes)", n.Value, Convert.ToBase64String (v));
77                 }
78
79                 [Test] 
80                 public void GetXml ()
81                 {
82                         Nonce n = new Nonce (16);
83                         XmlDocument doc = new XmlDocument ();
84                         XmlElement xel = n.GetXml (doc);
85                         Assert ("GetXml.StartsWith", xel.OuterXml.StartsWith ("<wsse:Nonce xmlns:wsse=\"http://schemas.xmlsoap.org/ws/2002/12/secext\">"));
86                         Assert ("GetXml.EndsWith", xel.OuterXml.EndsWith ("==</wsse:Nonce>"));
87                 }
88
89                 [Test]
90                 [ExpectedException (typeof (ArgumentNullException))] 
91                 public void GetXmlNull () 
92                 {
93                         Nonce n = new Nonce (16);
94                         n.GetXml (null);
95                 }
96
97                 [Test]
98                 public void LoadXml () 
99                 {
100                         Nonce n = new Nonce (16);
101                         XmlDocument doc = new XmlDocument ();
102                         doc.LoadXml (Sample);
103                         n.LoadXml (doc.DocumentElement);
104                         // roundtrip
105                         XmlElement xel = n.GetXml (doc);
106                         AssertEquals ("LoadXml", Sample, xel.OuterXml);
107                 }
108
109                 [Test]
110                 [ExpectedException (typeof (ArgumentException))] 
111                 public void LoadXml_BadLocalName () 
112                 {
113                         Nonce n = new Nonce (16);
114                         XmlDocument doc = new XmlDocument ();
115                         doc.LoadXml ("<wsse:SecurityTokenRef xmlns:wsse=\"http://schemas.xmlsoap.org/ws/2002/07/secext\" />");
116                         n.LoadXml (doc.DocumentElement);
117                 }
118
119                 [Test]
120                 [ExpectedException (typeof (ArgumentException))] 
121                 public void LoadXml_BadNamespace () 
122                 {
123                         Nonce n = new Nonce (16);
124                         XmlDocument doc = new XmlDocument ();
125                         doc.LoadXml ("<wsse:SecurityTokenRef xmlns:wsse=\"http://schemas.xmlsoap.org/ws/2202/07/secext\" />");
126                         n.LoadXml (doc.DocumentElement);
127                 }
128
129                 [Test]
130                 [ExpectedException (typeof (ArgumentNullException))] 
131                 public void LoadXmlNull () 
132                 {
133                         Nonce n = new Nonce (16);
134                         n.LoadXml (null);
135                 }
136 #endif
137         }
138 }