**** Merged from MCS ****
[mono-project.git] / mcs / class / Microsoft.Web.Services / Test / Microsoft.Web.Services.Timestamp / TimestampTest.cs
blob27ca423edd937791d38c7b9f44a4bec538079265
1 //
2 // TimestampTest.cs - NUnit Test Cases for Timestamp
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.Timestamp;
12 using System;
13 using System.Xml;
15 // note: due to compiler confusion between classes and namespace (like Timestamp)
16 // I renamed the test namespace from "MonoTests.Microsoft.Web.Services.Timestamp"
17 // to "MonoTests.MS.Web.Services.Timestamp".
18 namespace MonoTests.MS.Web.Services.Timestamp {
20 [TestFixture]
21 public class TimestampTest : Assertion {
23 // compiler conflict between namespace Timestamp and class Timestamp
24 private Microsoft.Web.Services.Timestamp.Timestamp ts;
26 private static string empty = "<wsu:Timestamp xmlns:wsu=\"http://schemas.xmlsoap.org/ws/2002/07/utility\" />";
27 private static string test1 = "<wsu:Timestamp xmlns:wsu=\"http://schemas.xmlsoap.org/ws/2002/07/utility\"><wsu:Created>2001-09-13T08:42:00Z</wsu:Created><wsu:Expires>2001-10-13T09:00:00Z</wsu:Expires></wsu:Timestamp>";
28 private static string test2 = "<wsu:Timestamp xmlns:wsu=\"http://schemas.xmlsoap.org/ws/2002/07/utility\"><wsu:Created>2001-09-13T08:42:00Z</wsu:Created><wsu:Expires>2001-10-13T09:00:00Z</wsu:Expires><wsu:Received Actor=\"http://x.com/\" Delay=\"60000\">2001-09-13T08:44:00Z</wsu:Received></wsu:Timestamp>";
29 private static string test3 = "<wsu:Timestamp xmlns:wsu=\"http://schemas.xmlsoap.org/ws/2002/07/utility\"><wsu:Created wsu:Id=\"createdId\">2001-09-13T08:42:00Z</wsu:Created><wsu:Expires wsu:Id=\"expiresId\">2001-10-13T09:00:00Z</wsu:Expires><wsu:Received Actor=\"http://x.com/\" Delay=\"60000\">2001-09-13T08:44:00Z</wsu:Received></wsu:Timestamp>";
30 private static string test4 = "<wsu:Timestamp wsu:Id=\"timestampId\" xmlns:wsu=\"http://schemas.xmlsoap.org/ws/2002/07/utility\"><wsu:Created wsu:Id=\"createdId\">2001-09-13T08:42:00Z</wsu:Created><wsu:Expires wsu:Id=\"expiresId\">2001-10-13T09:00:00Z</wsu:Expires><wsu:Received Actor=\"http://x.com/\" Delay=\"60000\" wsu:Id=\"receivedId\">2001-09-13T08:44:00Z</wsu:Received></wsu:Timestamp>";
32 [SetUp]
33 void SetUp ()
35 ts = new Microsoft.Web.Services.Timestamp.Timestamp ();
38 [Test]
39 public void Properties ()
41 // test values
42 DateTime testTime = DateTime.UtcNow;
43 long testTTL = 1;
44 // default values
45 AssertEquals ("Created (default)", DateTime.MinValue, ts.Created);
46 AssertEquals ("Expires (default)", DateTime.MaxValue, ts.Expires);
47 AllTests.AssertEquals ("TTL (default)", 300000, ts.Ttl);
48 // new ttl values
49 ts.Ttl = testTTL;
50 AllTests.AssertEquals ("TTL (test)", 1, ts.Ttl);
51 ts.Ttl = 0;
52 AllTests.AssertEquals ("TTL (0)", 0, ts.Ttl);
53 AssertEquals ("Expires (0)", DateTime.MaxValue, ts.Expires);
54 AssertNotNull ("Receivers", ts.Receivers);
55 AssertEquals ("Receivers.Count", 0, ts.Receivers.Count);
58 [Test]
59 [ExpectedException (typeof (ArgumentException))]
60 public void Ttl_Negative ()
62 ts.Ttl = -1;
65 [Test]
66 [ExpectedException (typeof (TimestampFormatException))]
67 public void CheckInvalid ()
69 // default object is incomplete
70 ts.CheckValid ();
73 // Note: All valid (no exception are thrown) but all EXPIRED !
74 [Test]
75 public void CheckValid ()
77 XmlDocument doc = new XmlDocument ();
78 doc.LoadXml (test1);
79 ts.LoadXml (doc.DocumentElement);
80 ts.CheckValid ();
82 doc.LoadXml (test2);
83 ts.LoadXml (doc.DocumentElement);
84 ts.CheckValid ();
86 doc.LoadXml (test3);
87 ts.LoadXml (doc.DocumentElement);
88 ts.CheckValid ();
90 doc.LoadXml (test4);
91 ts.LoadXml (doc.DocumentElement);
92 ts.CheckValid ();
95 [Test]
96 public void GetXml ()
98 XmlDocument doc = new XmlDocument ();
99 XmlElement xel = ts.GetXml (doc);
100 // minimal XML
101 AssertEquals ("Xml", empty, xel.OuterXml);
103 ts.Ttl = 60000; // one minute
104 xel = ts.GetXml (doc);
105 // TTL has no change in XML
106 AssertEquals ("Xml", empty, xel.OuterXml);
109 [Test]
110 [ExpectedException (typeof (ArgumentNullException))]
111 public void LoadXml_Null ()
113 ts.LoadXml (null);
116 [Test]
117 [ExpectedException (typeof (ArgumentException))]
118 public void LoadXml_BadElement ()
120 XmlDocument doc = new XmlDocument ();
121 // bad element (Timestamp case is invalid)
122 doc.LoadXml ("<wsu:timeStamp xmlns:wsu=\"http://schemas.xmlsoap.org/ws/2002/07/utility\" />");
123 ts.LoadXml (doc.DocumentElement);
126 [Test]
127 [ExpectedException (typeof (ArgumentException))]
128 public void LoadXml_BadNamespace ()
130 XmlDocument doc = new XmlDocument ();
131 // bad namespace (invalid year in URL)
132 doc.LoadXml ("<wsu:Timestamp xmlns:wsu=\"http://schemas.xmlsoap.org/ws/2003/07/utility\" />");
133 ts.LoadXml (doc.DocumentElement);
136 // sample taken from http://msdn.microsoft.com/library/en-us/dnglobspec/html/ws-security.asp
137 [Test]
138 public void LoadXml ()
140 XmlDocument doc = new XmlDocument ();
141 doc.LoadXml (test1);
142 ts.LoadXml (doc.DocumentElement);
144 XmlElement xel = ts.GetXml (doc);
145 AssertEquals ("Xml", test1, xel.OuterXml);
146 AssertEquals ("Xml Actor", "", ts.Actor);
147 AssertEquals ("Xml Created", "2001-09-13T08:42:00Z", ts.Created.ToString (WSTimestamp.TimeFormat));
148 AssertEquals ("Xml Expires", "2001-10-13T09:00:00Z", ts.Expires.ToString (WSTimestamp.TimeFormat));
149 AssertEquals ("Xml Receivers.Count", 0, ts.Receivers.Count);
150 AllTests.AssertEquals ("Xml Ttl", 300000, ts.Ttl);
153 // sample taken from http://msdn.microsoft.com/library/en-us/dnglobspec/html/ws-security.asp
154 [Test]
155 public void LoadXmlWithOneIntermediary ()
157 XmlDocument doc = new XmlDocument ();
158 doc.LoadXml (test2);
159 ts.LoadXml (doc.DocumentElement);
161 XmlElement xel = ts.GetXml (doc);
162 AssertEquals ("Xml", test2, xel.OuterXml);
163 AssertEquals ("Xml Actor", "", ts.Actor); // Actor is part of <Received> element
164 AssertEquals ("Xml Created", "2001-09-13T08:42:00Z", ts.Created.ToString (WSTimestamp.TimeFormat));
165 AssertEquals ("Xml Expires", "2001-10-13T09:00:00Z", ts.Expires.ToString (WSTimestamp.TimeFormat));
166 AssertEquals ("Xml Receivers.Count", 1, ts.Receivers.Count);
167 AllTests.AssertEquals ("Xml Ttl", 300000, ts.Ttl);
170 // WSE supports wsu:Id for Created and Expires elements
171 [Test]
172 public void LoadXmlWithCreatedAndExpiresIds ()
174 XmlDocument doc = new XmlDocument ();
175 doc.LoadXml (test3);
176 ts.LoadXml (doc.DocumentElement);
178 XmlElement xel = ts.GetXml (doc);
179 AssertEquals ("Xml", test3, xel.OuterXml);
180 AssertEquals ("Xml Actor", "", ts.Actor); // Actor is part of <Received> element
181 AssertEquals ("Xml Created", "2001-09-13T08:42:00Z", ts.Created.ToString (WSTimestamp.TimeFormat));
182 AssertEquals ("Xml Expires", "2001-10-13T09:00:00Z", ts.Expires.ToString (WSTimestamp.TimeFormat));
183 AssertEquals ("Xml Receivers.Count", 1, ts.Receivers.Count);
184 AllTests.AssertEquals ("Xml Ttl", 300000, ts.Ttl);
187 // WSE _doesn't_ support wsu:Id for Timestamp and Received elements
188 [Test]
189 public void LoadXmlWithAllIds ()
191 XmlDocument doc = new XmlDocument ();
192 doc.LoadXml (test4);
193 ts.LoadXml (doc.DocumentElement);
195 XmlElement xel = ts.GetXml (doc);
196 // FIXME (WSE) AssertEquals ("Xml", test4, xel.OuterXml);
197 AssertEquals ("Xml Actor", "", ts.Actor); // Actor is part of <Received> element
198 AssertEquals ("Xml Created", "2001-09-13T08:42:00Z", ts.Created.ToString (WSTimestamp.TimeFormat));
199 AssertEquals ("Xml Expires", "2001-10-13T09:00:00Z", ts.Expires.ToString (WSTimestamp.TimeFormat));
200 AssertEquals ("Xml Receivers.Count", 1, ts.Receivers.Count);
201 AllTests.AssertEquals ("Xml Ttl", 300000, ts.Ttl);