Welcome to mirror list, hosted at ThFree Co, Russian Federation.

XmlDsigXPathTransformTest.cs « System.Security.Cryptography.Xml « Test « System.Security « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a3fa421560920f42d7fdcaa204e4ba206f574ae8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
//
// XmlDsigXPathTransformTest.cs - NUnit Test Cases for XmlDsigXPathTransform
//
// Author:
//	Sebastien Pouliot <sebastien@ximian.com>
//
// (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
// Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
//

using System;
using System.IO;
using System.Security.Cryptography;
using System.Security.Cryptography.Xml;
using System.Text;
using System.Xml;
using System.Xml.Xsl;
using System.Xml.XPath;

using NUnit.Framework;

namespace MonoTests.System.Security.Cryptography.Xml {

	// Note: GetInnerXml is protected in XmlDsigXPathTransform making it
	// difficult to test properly. This class "open it up" :-)
	public class UnprotectedXmlDsigXPathTransform : XmlDsigXPathTransform {

		public XmlNodeList UnprotectedGetInnerXml () 
		{
			return base.GetInnerXml ();
		}
	}

	[TestFixture]
	public class XmlDsigXPathTransformTest {

		protected UnprotectedXmlDsigXPathTransform transform;

		[SetUp]
		protected void SetUp () 
		{
			transform = new UnprotectedXmlDsigXPathTransform ();
		}

		[Test]
		public void Properties () 
		{
			Assert.AreEqual ("http://www.w3.org/TR/1999/REC-xpath-19991116", transform.Algorithm, "Algorithm");

			Type[] input = transform.InputTypes;
			Assert.IsTrue ((input.Length == 3), "Input #");
			// check presence of every supported input types
			bool istream = false;
			bool ixmldoc = false;
			bool ixmlnl = false;
			foreach (Type t in input) {
				if (t.ToString () == "System.IO.Stream")
					istream = true;
				if (t.ToString () == "System.Xml.XmlDocument")
					ixmldoc = true;
				if (t.ToString () == "System.Xml.XmlNodeList")
					ixmlnl = true;
			}
			Assert.IsTrue (istream, "Input Stream");
			Assert.IsTrue (ixmldoc, "Input XmlDocument");
			Assert.IsTrue (ixmlnl, "Input XmlNodeList");

			Type[] output = transform.OutputTypes;
			Assert.IsTrue ((output.Length == 1), "Output #");
			// check presence of every supported output types
			bool oxmlnl = false;
			foreach (Type t in output) {
				if (t.ToString () == "System.Xml.XmlNodeList")
					oxmlnl = true;
			}
			Assert.IsTrue (oxmlnl, "Output XmlNodeList");
		}

		protected void AreEqual (string msg, XmlNodeList expected, XmlNodeList actual) 
		{
			for (int i=0; i < expected.Count; i++) {
				if (expected [i].OuterXml != actual [i].OuterXml)
					Assert.Fail (msg + " [" + i + "] expected " + expected[i].OuterXml + " bug got " + actual[i].OuterXml);
			}
			Assert.AreEqual (expected.Count, actual.Count);
		}

		[Test]
#if NET_2_0
		[Ignore ("throws a NullReferenceException - but it's (kind of internal)")]
#endif
		public void GetInnerXml () 
		{
			XmlNodeList xnl = transform.UnprotectedGetInnerXml ();
			Assert.AreEqual (1, xnl.Count, "Default InnerXml.Count");
			Assert.AreEqual ("<XPath xmlns=\"http://www.w3.org/2000/09/xmldsig#\"></XPath>", xnl [0].OuterXml, "Default InnerXml.OuterXml");
		}

		[Test]
#if ONLY_1_1
		[ExpectedException (typeof (NullReferenceException))]
#endif
		public void OnlyInner () 
		{
			XmlNodeList inner = InnerXml (""); // empty
			transform.LoadInnerXml (inner);
			XmlNodeList xnl = (XmlNodeList) transform.GetOutput ();
#if NET_2_0
			Assert.AreEqual (0, xnl.Count, "Count");
#endif
		}

		private XmlDocument GetDoc () 
		{
			string test = "<catalog><cd><title>Empire Burlesque</title><artist>Bob Dylan</artist><price>10.90</price>";
			test += "<year>1985</year></cd><cd><title>Hide your heart</title><artist>Bonnie Tyler</artist><price>9.90</price>";
			test += "<year>1988</year></cd><cd><title>Greatest Hits</title><artist>Dolly Parton</artist><price>9.90</price>";
			test += "<year>1982</year></cd><cd><title>Still got the blues</title><artist>Gary Moore</artist><price>10.20</price>";
			test += "<year>1990</year></cd><cd><title>Eros</title><artist>Eros Ramazzotti</artist><price>9.90</price>";
			test += "<year>1997</year></cd></catalog>";
			XmlDocument doc = new XmlDocument ();
			doc.LoadXml (test);
			return doc;
		}

		private XmlNodeList InnerXml (string xpathExpr) 
		{
			string xpath = "<XPath xmlns=\"http://www.w3.org/2000/09/xmldsig#\">" + xpathExpr + "</XPath>";
			XmlDocument doc = new XmlDocument ();
			doc.LoadXml (xpath);
			return doc.ChildNodes;
		}

		[Test]
#if NET_2_0
		[Category ("NotWorking")]
#endif
		public void LoadInputAsXmlDocument () 
		{
			XmlDocument doc = GetDoc ();
			transform.LoadInput (doc);
			XmlNodeList inner = InnerXml ("//*/title");
			transform.LoadInnerXml (inner);
			XmlNodeList xnl = (XmlNodeList) transform.GetOutput ();
#if NET_2_0
			Assert.AreEqual (73, xnl.Count);
#else
			Assert.AreEqual (47, xnl.Count);
#endif
		}

		[Test]
		public void LoadInputAsXmlDocument_EmptyXPath () 
		{
			XmlDocument doc = GetDoc ();
			transform.LoadInput (doc);
			// empty means no LoadInnerXml
			XmlNodeList xnl = (XmlNodeList) transform.GetOutput ();
			Assert.AreEqual (0, xnl.Count, "Empy Result");
		}

		[Test]
		[Category ("NotWorking")]
		public void LoadInputAsXmlNodeList () 
		{
			XmlDocument doc = GetDoc ();
			transform.LoadInput (doc.ChildNodes);
			XmlNodeList inner = InnerXml ("//*/title");
			transform.LoadInnerXml (inner);
			XmlNodeList xnl = (XmlNodeList) transform.GetOutput ();
			Assert.AreEqual (1, xnl.Count);
		}

		[Test]
		public void LoadInputAsXmlNodeList_EmptyXPath () 
		{
			XmlDocument doc = GetDoc ();
			transform.LoadInput (doc.ChildNodes);
			// empty means no LoadInnerXml
			XmlNodeList xnl = (XmlNodeList) transform.GetOutput ();
			Assert.AreEqual (0, xnl.Count, "Empy Result");
		}

		[Test]
#if NET_2_0
		[Category ("NotWorking")]
#endif
		public void LoadInputAsStream () 
		{
			XmlDocument doc = GetDoc ();
			doc.PreserveWhitespace = true;
			MemoryStream ms = new MemoryStream ();
			doc.Save (ms);
			ms.Position = 0;
			transform.LoadInput (ms);
			XmlNodeList inner = InnerXml ("//*/title");
			transform.LoadInnerXml (inner);
			XmlNodeList xnl = (XmlNodeList) transform.GetOutput ();
#if NET_2_0
			Assert.AreEqual (73, xnl.Count);
#else
			Assert.AreEqual (47, xnl.Count);
#endif
		}

		[Test]
		public void LoadInputAsStream_EmptyXPath () 
		{
			XmlDocument doc = GetDoc ();
			MemoryStream ms = new MemoryStream ();
			doc.Save (ms);
			ms.Position = 0;
			transform.LoadInput (ms);
			// empty means no LoadInnerXml
			XmlNodeList xnl = (XmlNodeList) transform.GetOutput ();
			Assert.AreEqual (0, xnl.Count, "Empy Result");
		}

		[Test]
		public void LoadInnerXml () 
		{
			XmlNodeList inner = InnerXml ("//*");
			transform.LoadInnerXml (inner);
			XmlNodeList xnl = transform.UnprotectedGetInnerXml ();
			Assert.AreEqual (inner, xnl, "LoadInnerXml");
		}

		[Test]
		public void UnsupportedInput () 
		{
			byte[] bad = { 0xBA, 0xD };
			// LAMESPEC: input MUST be one of InputType - but no exception is thrown (not documented)
			transform.LoadInput (bad);
		}

		[Test]
		[ExpectedException (typeof (ArgumentException))]
		public void UnsupportedOutput () 
		{
			XmlDocument doc = new XmlDocument ();
			object o = transform.GetOutput (doc.GetType ());
		}

		[Test]
		public void TransformSimple ()
		{
			XmlDsigXPathTransform t = new XmlDsigXPathTransform ();
			XmlDocument xpdoc = new XmlDocument ();
			string ns = "http://www.w3.org/2000/09/xmldsig#";
			string xpath = "<XPath xmlns='" + ns + "' xmlns:x='urn:foo'>*|@*|namespace::*</XPath>"; // not absolute path.. so @* and namespace::* does not make sense.
			xpdoc.LoadXml (xpath);
			t.LoadInnerXml (xpdoc.ChildNodes);
			XmlDocument doc = new XmlDocument ();
			doc.LoadXml ("<element xmlns='urn:foo'><foo><bar>test</bar></foo></element>");
			t.LoadInput (doc);
			XmlNodeList nl = (XmlNodeList) t.GetOutput ();
			Assert.AreEqual (XmlNodeType.Document, nl [0].NodeType);
			Assert.AreEqual (XmlNodeType.Element, nl [1].NodeType);
			Assert.AreEqual ("element", nl [1].LocalName);
			Assert.AreEqual (XmlNodeType.Element, nl [2].NodeType);
			Assert.AreEqual ("foo", nl [2].LocalName);
			Assert.AreEqual (XmlNodeType.Element, nl [3].NodeType);
			Assert.AreEqual ("bar", nl [3].LocalName);
			// MS.NET bug - ms.net returns ns node even when the
			// current node is ns node (it is like returning
			// attribute from attribute nodes).
//			Assert.AreEqual (XmlNodeType.Attribute, nl [4].NodeType);
//			Assert.AreEqual ("xmlns", nl [4].LocalName);
		}

		[Test]
		[Category ("NotWorking")]
		// MS.NET looks incorrect, or something incorrect in this test code; It turned out nothing to do with function here()
		public void FunctionHereObsolete ()
		{
			XmlDsigXPathTransform t = new XmlDsigXPathTransform ();
			XmlDocument xpdoc = new XmlDocument ();
			string ns = "http://www.w3.org/2000/09/xmldsig#";
//			string xpath = "<XPath xmlns='" + ns + "' xmlns:x='urn:foo'>here()</XPath>";
			string xpath = "<XPath xmlns='" + ns + "' xmlns:x='urn:foo'></XPath>";
			xpdoc.LoadXml (xpath);
			t.LoadInnerXml (xpdoc.ChildNodes);
			XmlDocument doc = new XmlDocument ();

			doc.LoadXml ("<element a='b'><foo><bar>test</bar></foo></element>");
			t.LoadInput (doc);

			XmlNodeList nl = (XmlNodeList) t.GetOutput ();
			Assert.AreEqual (0, nl.Count, "0");

			doc.LoadXml ("<element xmlns='urn:foo'><foo><bar>test</bar></foo></element>");
			t.LoadInput (doc);
			nl = (XmlNodeList) t.GetOutput ();
#if NET_2_0
			Assert.AreEqual (0, nl.Count, "1");
#else
			Assert.AreEqual (1, nl.Count, "1");
#endif

			doc.LoadXml ("<element xmlns='urn:foo'><foo xmlns='urn:bar'><bar>test</bar></foo></element>");
			t.LoadInput (doc);
			nl = (XmlNodeList) t.GetOutput ();
#if NET_2_0
			Assert.AreEqual (0, nl.Count, "2");
#else
			Assert.AreEqual (2, nl.Count, "2");
#endif

			doc.LoadXml ("<element xmlns='urn:foo' xmlns:x='urn:x'><foo xmlns='urn:bar'><bar>test</bar></foo></element>");
			t.LoadInput (doc);
			nl = (XmlNodeList) t.GetOutput ();
#if NET_2_0
			Assert.AreEqual (0, nl.Count, "3");
#else
			Assert.AreEqual (3, nl.Count, "3");
#endif

			doc.LoadXml ("<envelope><Signature xmlns='http://www.w3.org/2000/09/xmldsig#'><XPath>blah</XPath></Signature></envelope>");
			t.LoadInput (doc);
			nl = (XmlNodeList) t.GetOutput ();
#if NET_2_0
			Assert.AreEqual (0, nl.Count, "4");
#else
			Assert.AreEqual (1, nl.Count, "4");
			Assert.AreEqual (XmlNodeType.Attribute, nl [0].NodeType, "NodeType");
#endif
		}
	}
}