**** Merged from MCS ****
[mono-project.git] / mcs / class / Microsoft.Web.Services / Test / Microsoft.Web.Services.Security / NonceTest.cs
blob653574da1b32d19c0423ed7786872477db1e9f40
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 //
10 using NUnit.Framework;
11 using Microsoft.Web.Services.Security;
12 using System;
13 using System.Xml;
15 namespace MonoTests.MS.Web.Services.Security {
17 [TestFixture]
18 public class NonceTest : Assertion {
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>";
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 ()
30 Nonce n = new Nonce (16);
33 [Test]
34 public void ConstructorIntZero ()
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);
44 [Test]
45 [ExpectedException (typeof (OverflowException))]
46 public void ConstructorNegativeInt ()
48 Nonce n = new Nonce (-1);
51 [Test]
52 public void ConstructorXmlElement ()
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);
62 [Test]
63 [ExpectedException (typeof (ArgumentNullException))]
64 public void ConstructorXmlElementNull ()
66 Nonce n = new Nonce (null);
69 [Test]
70 public void Value ()
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));
79 [Test]
80 public void GetXml ()
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>"));
89 [Test]
90 [ExpectedException (typeof (ArgumentNullException))]
91 public void GetXmlNull ()
93 Nonce n = new Nonce (16);
94 n.GetXml (null);
97 [Test]
98 public void LoadXml ()
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);
109 [Test]
110 [ExpectedException (typeof (ArgumentException))]
111 public void LoadXml_BadLocalName ()
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);
119 [Test]
120 [ExpectedException (typeof (ArgumentException))]
121 public void LoadXml_BadNamespace ()
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);
129 [Test]
130 [ExpectedException (typeof (ArgumentNullException))]
131 public void LoadXmlNull ()
133 Nonce n = new Nonce (16);
134 n.LoadXml (null);
136 #endif