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

XsltSettings.cs « XslCompiledTransformApi « Xslt « tests « System.Private.Xml « src - github.com/mono/corefx.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6e5c003ce79d98541492813b4c7591ca1bbf5ba6 (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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Xunit;
using Xunit.Abstractions;
using System.IO;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;

namespace System.Xml.Tests
{
    //[TestCase(Name = "XsltSettings-Retail", Desc = "This testcase tests the different settings on XsltSettings and the corresponding behavior in retail mode", Param = "Retail")]
    //[TestCase(Name = "XsltSettings-Debug", Desc = "This testcase tests the different settings on XsltSettings and the corresponding behavior in debug mode", Param = "Debug")]
    public class CXsltSettings : XsltApiTestCaseBase2
    {
        private ITestOutputHelper _output;
        public CXsltSettings(ITestOutputHelper output) : base(output)
        {
            _output = output;
        }

        private XslCompiledTransform _xsl = null;
        private string _xmlFile = string.Empty;
        private string _xslFile = string.Empty;

        private bool _debug = false;

        private void Init(string xmlFile, string xslFile)
        {
            //if (Param.ToString() == "Debug")
            //{
            //    xsl = new XslCompiledTransform(true);
            //    _debug = true;
            //}
            //else
            _xsl = new XslCompiledTransform();

            _xmlFile = FullFilePath(xmlFile);
            _xslFile = FullFilePath(xslFile);
        }

        private StringWriter Transform()
        {
            StringWriter sw = new StringWriter();
            _xsl.Transform(_xmlFile, null, sw);
            return sw;
        }

        private void VerifyResult(object actual, object expected, string message)
        {
            _output.WriteLine("Expected : {0}", expected);
            _output.WriteLine("Actual : {0}", actual);

            Assert.Equal(actual, expected);
        }

        [ActiveIssue(9873)]
        //[Variation(id = 1, Desc = "Test the script block with EnableScript, should work", Pri = 0, Params = new object[] { "XsltSettings.xml", "XsltSettings1.xsl", false, true })]
        [InlineData(1, "XsltSettings.xml", "XsltSettings1.xsl", false, true)]
        //[Variation(id = 4, Desc = "Test the script block with TrustedXslt, should work", Pri = 1, Params = new object[] { "XsltSettings.xml", "XsltSettings1.xsl", true, true })]
        [InlineData(4, "XsltSettings.xml", "XsltSettings1.xsl", true, true)]
        //[Variation(id = 9, Desc = "Test the combination of script and document function with TrustedXslt, should work", Pri = 0, Params = new object[] { "XsltSettings.xml", "XsltSettings3.xsl", true, true })]
        [InlineData(9, "XsltSettings.xml", "XsltSettings3.xsl", true, true)]
        //[Variation(id = 11, Desc = "Test the combination of script and document function with EnableScript, only script should work", Pri = 2, Params = new object[] { "XsltSettings.xml", "XsltSettings3.xsl", false, true })]
        [InlineData(11, "XsltSettings.xml", "XsltSettings3.xsl", false, true)]
        [Theory]
        public void XsltSettings1__1ActiveIssue9873(object param0, object param1, object param2, object param3, object param4)
        {
            XsltSettings1_1(param0, param1, param2, param3, param4);
        }

        //[Variation(id = 15, Desc = "Test 1 with Default settings, should fail", Pri = 1, Params = new object[] { "XsltSettings.xml", "XsltSettings1.xsl", false, true, false, false })]
        [InlineData(15, "XsltSettings.xml", "XsltSettings1.xsl", false, true, false, false)]
        //[Variation(id = 16, Desc = "Test 2 with EnableScript override, should work", Pri = 1, Params = new object[] { "XsltSettings.xml", "XsltSettings1.xsl", false, false, false, true })]
        [InlineData(16, "XsltSettings.xml", "XsltSettings1.xsl", false, false, false, true)]
        //[Variation(id = 19, Desc = "Test 9 with Default settings override, should fail", Pri = 1, Params = new object[] { "XsltSettings.xml", "XsltSettings3.xsl", true, true, false, false })]
        [InlineData(19, "XsltSettings.xml", "XsltSettings3.xsl", true, true, false, false)]
        //[Variation(id = 20, Desc = "Test 10 with TrustedXslt override, should work", Pri = 1, Params = new object[] { "XsltSettings.xml", "XsltSettings3.xsl", false, false, true, true })]
        [InlineData(20, "XsltSettings.xml", "XsltSettings3.xsl", false, false, true, true)]
        public void XsltSettings1_2_ActiveIssue9873(object param0, object param1, object param2, object param3, object param4, object param5, object param6)
        {
            XsltSettings1_2(param0, param1, param2, param3, param4, param5, param6);
        }

        [ActiveIssue(9876)]
        //[Variation(id = 5, Desc = "Test the document function with EnableDocumentFunction, should work", Pri = 0, Params = new object[] { "XsltSettings.xml", "XsltSettings2.xsl", true, false })]
        [InlineData(5, "XsltSettings.xml", "XsltSettings2.xsl", true, false)]
        //[Variation(id = 8, Desc = "Test the document function with TrustedXslt, should work", Pri = 1, Params = new object[] { "XsltSettings.xml", "XsltSettings2.xsl", true, true })]
        [InlineData(8, "XsltSettings.xml", "XsltSettings2.xsl", true, true)]
        [Theory]
        public void XsltSettings1_1_ActiveIssue9876(object param0, object param1, object param2, object param3, object param4)
        {
            XsltSettings1_1(param0, param1, param2, param3, param4);
        }

        [ActiveIssue(9876)]
        //[Variation(id = 18, Desc = "Test 6 with EnableDocumentFunction override, should work", Pri = 1, Params = new object[] { "XsltSettings.xml", "XsltSettings2.xsl", false, false, true, false })]
        [InlineData(18, "XsltSettings.xml", "XsltSettings2.xsl", false, false, true, false)]
        [Theory]
        public void XsltSettings1_2_ActiveIssue9876(object param0, object param1, object param2, object param3, object param4, object param5, object param6)
        {
            XsltSettings1_2(param0, param1, param2, param3, param4, param5, param6);
        }

        //[Variation(id = 2, Desc = "Test the script block with Default Settings, should fail", Pri = 0, Params = new object[] { "XsltSettings.xml", "XsltSettings1.xsl", false, false })]
        [InlineData(2, "XsltSettings.xml", "XsltSettings1.xsl", false, false)]
        //[Variation(id = 3, Desc = "Test the script block with EnableDocumentFunction, should fail", Pri = 2, Params = new object[] { "XsltSettings.xml", "XsltSettings1.xsl", true, false })]
        [InlineData(3, "XsltSettings.xml", "XsltSettings1.xsl", true, false)]
        //[Variation(id = 6, Desc = "Test the document function with Default Settings, should fail", Pri = 0, Params = new object[] { "XsltSettings.xml", "XsltSettings2.xsl", false, false })]
        [InlineData(6, "XsltSettings.xml", "XsltSettings2.xsl", false, false)]
        //[Variation(id = 7, Desc = "Test the document function with EnableScript, should fail", Pri = 2, Params = new object[] { "XsltSettings.xml", "XsltSettings2.xsl", false, true })]
        [InlineData(7, "XsltSettings.xml", "XsltSettings2.xsl", false, true)]
        //[Variation(id = 10, Desc = "Test the combination of script and document function with Default Settings, should fail", Pri = 0, Params = new object[] { "XsltSettings.xml", "XsltSettings3.xsl", false, false })]
        [InlineData(10, "XsltSettings.xml", "XsltSettings3.xsl", false, false)]
        //[Variation(id = 12, Desc = "Test the combination of script and document function with EnableDocumentFunction, only document should work", Pri = 2, Params = new object[] { "XsltSettings.xml", "XsltSettings3.xsl", true, false })]
        [InlineData(12, "XsltSettings.xml", "XsltSettings3.xsl", true, false)]
        //[Variation(id = 13, Desc = "Test the stylesheet with no script and document function with Default Settings", Pri = 1, Params = new object[] { "XsltSettings.xml", "XsltSettings4.xsl", false, false })]
        [InlineData(13, "XsltSettings.xml", "XsltSettings4.xsl", false, false)]
        //[Variation(id = 14, Desc = "Test the stylesheet with no script and document function with TrustedXslt", Pri = 1, Params = new object[] { "XsltSettings.xml", "XsltSettings4.xsl", true, true })]
        [InlineData(14, "XsltSettings.xml", "XsltSettings4.xsl", true, true)]
        /*
         * enable all disable scripting
         * enable all disable document()
         * enable all disable none
         * enable all disable all

         */
        [Theory]
        public void XsltSettings1_1(object param0, object param1, object param2, object param3, object param4)
        {
            Init(param1.ToString(), param2.ToString());

            // In Proc Skip
            // XsltSettings - Debug (1-20)
            // XsltSettings - Retail (1,4,9,11,15,16,19,20)
            if (_isInProc)
            {
                if (_debug)
                    return; //TEST_SKIPPED;
                else
                {
                    switch ((int)param0)
                    {
                        case 1:
                        case 4:
                        case 9:
                        case 11:
                            return; //TEST_SKIPPED;
                                    //                  default:
                                    //just continue;
                    }
                }
            }

            XsltSettings xs = new XsltSettings((bool)param3, (bool)param4);
            _xsl.Load(_xslFile, xs, new XmlUrlResolver());

            try
            {
                StringWriter sw = Transform();

                switch ((int)param0)
                {
                    case 1:
                    case 4:
                        VerifyResult(sw.ToString(), "30", "Unexpected result, expected 30");
                        return;

                    case 5:
                    case 8:
                        VerifyResult(sw.ToString(), "7", "Unexpected result, expected 7");
                        return;

                    case 9:
                        VerifyResult(sw.ToString(), "17", "Unexpected result, expected 17");
                        return;

                    case 13:
                    case 14:
                        VerifyResult(sw.ToString(), "PASS", "Unexpected result, expected PASS");
                        return;

                    default:
                        Assert.True(false);
                        return;
                }
            }
            catch (XsltException ex)
            {
                switch ((int)param0)
                {
                    case 2:
                    case 3:
                    case 6:
                    case 7:
                    case 10:
                    case 11:
                    case 12:
                        _output.WriteLine(ex.ToString());
                        _output.WriteLine(ex.Message);
                        return;

                    default:
                        Assert.True(false);
                        return;
                }
            }
        }

        //[Variation(id = 17, Desc = "Test 5 with Default settings override, should fail", Pri = 1, Params = new object[] { "XsltSettings.xml", "XsltSettings2.xsl", true, false, false, false })]
        [InlineData(17, "XsltSettings.xml", "XsltSettings2.xsl", true, false, false, false)]
        /*
         * enable all disable scripting
         * enable all disable document()
         * enable all disable none
         * enable all disable all

         */
        [Theory]
        public void XsltSettings1_2(object param0, object param1, object param2, object param3, object param4, object param5, object param6)
        {
            Init(param1.ToString(), param2.ToString());

            // In Proc Skip
            // XsltSettings - Debug (1-20)
            // XsltSettings - Retail (1,4,9,11,15,16,19,20)
            if (_isInProc)
            {
                if (_debug)
                    return; //TEST_SKIPPED;
                else
                {
                    switch ((int)param0)
                    {
                        case 15:
                        case 16:
                        case 19:
                        case 20:
                            return; //TEST_SKIPPED;
                                    //                  default:
                                    //just continue;
                    }
                }
            }

            XsltSettings xs = new XsltSettings((bool)param3, (bool)param4);
            _xsl.Load(_xslFile, xs, new XmlUrlResolver());

            xs.EnableDocumentFunction = (bool)param5;
            xs.EnableScript = (bool)param6;
            _xsl.Load(_xslFile, xs, new XmlUrlResolver());

            try
            {
                StringWriter sw = Transform();

                switch ((int)param0)
                {
                    case 16:
                        VerifyResult(sw.ToString(), "30", "Unexpected result, expected 30");
                        return;

                    case 18:
                        VerifyResult(sw.ToString(), "7", "Unexpected result, expected 7");
                        return;

                    case 20:
                        VerifyResult(sw.ToString(), "17", "Unexpected result, expected 17");
                        return;

                    default:
                        Assert.True(false);
                        return;
                }
            }
            catch (XsltException ex)
            {
                switch ((int)param0)
                {
                    case 15:
                    case 17:
                    case 19:
                        _output.WriteLine(ex.ToString());
                        _output.WriteLine(ex.Message);
                        return;

                    default:
                        Assert.True(false);
                        return;
                }
            }
        }

        //[Variation(id = 21, Desc = "Disable Scripting and load a stylesheet which includes another sytlesheet with script block", Pri = 1, Params = new object[] { "XsltSettings.xml", "XsltSettings5.xsl", false, false })]
        [InlineData("XsltSettings.xml", "XsltSettings5.xsl", false, false)]
        //[Variation(id = 22, Desc = "Disable Scripting and load a stylesheet which imports XSLT which includes another XSLT with script block", Pri = 1, Params = new object[] { "XsltSettings.xml", "XsltSettings6.xsl", false, false })]
        [InlineData("XsltSettings.xml", "XsltSettings6.xsl", false, false)]
        //[Variation(id = 23, Desc = "Disable Scripting and load a stylesheet which has an entity expanding to a script block", Pri = 1, Params = new object[] { "XsltSettings.xml", "XsltSettings7.xsl", false, false })]
        [InlineData("XsltSettings.xml", "XsltSettings7.xsl", false, false)]
        //[Variation(id = 24, Desc = "Disable Scripting and load a stylesheet with multiple script blocks with different languages", Pri = 1, Params = new object[] { "XsltSettings.xml", "XsltSettings8.xsl", false, false })]
        [InlineData("XsltSettings.xml", "XsltSettings8.xsl", false, false)]
        [Theory]
        public void XsltSettings2(object param0, object param1, object param2, object param3)
        {
            Init(param0.ToString(), param1.ToString());

            if (_isInProc)
                if (_debug)
                    return; //TEST_SKIPPED;

            XsltSettings xs = new XsltSettings((bool)param2, (bool)param3);
            XPathDocument doc = new XPathDocument(_xslFile);
            _xsl.Load(doc, xs, new XmlUrlResolver());

            try
            {
                StringWriter sw = Transform();
                _output.WriteLine("Execution of the scripts was allowed even when XsltSettings.EnableScript is false");
                Assert.True(false);
            }
            catch (XsltException ex)
            {
                _output.WriteLine(ex.ToString());
                return;
            }
        }

        //[Variation(id = 25, Desc = "Disable DocumentFunction and Malicious stylesheet has document(url) opening a URL to an external system", Pri = 1, Params = new object[] { "XsltSettings.xml", "XsltSettings9.xsl", false, false })]
        //[InlineData("XsltSettings.xml", "XsltSettings9.xsl", false, false)]
        //[Variation(id = 26, Desc = "Disable DocumentFunction and Malicious stylesheet has document(nodeset) opens union of all URLs referenced", Pri = 1, Params = new object[] { "XsltSettings.xml", "XsltSettings10.xsl", false, false })]
        [InlineData("XsltSettings.xml", "XsltSettings10.xsl", false, false)]
        //[Variation(id = 27, Desc = "Disable DocumentFunction and Malicious stylesheet has document(url, nodeset) nodeset is a base URL to 1st arg", Pri = 1, Params = new object[] { "XsltSettings.xml", "XsltSettings11.xsl", false, false })]
        //[InlineData("XsltSettings.xml", "XsltSettings11.xsl", false, false)]
        //[Variation(id = 28, Desc = "Disable DocumentFunction and Malicious stylesheet has document(nodeset, nodeset) 2nd arg is a base URL to 1st arg", Pri = 1, Params = new object[] { "XsltSettings.xml", "XsltSettings12.xsl", false, false })]
        [InlineData("XsltSettings.xml", "XsltSettings12.xsl", false, false)]
        //[Variation(id = 29, Desc = "Disable DocumentFunction and Malicious stylesheet has document(''), no threat but just to verify if its considered", Pri = 1, Params = new object[] { "XsltSettings.xml", "XsltSettings13.xsl", false, false })]
        [InlineData("XsltSettings.xml", "XsltSettings13.xsl", false, false)]
        //[Variation(id = 30, Desc = "Disable DocumentFunction and Stylesheet includes another stylesheet with document() function", Pri = 1, Params = new object[] { "XsltSettings.xml", "XsltSettings14.xsl", false, false })]
        //[InlineData("XsltSettings.xml", "XsltSettings14.xsl", false, false)]
        //[Variation(id = 31, Desc = "Disable DocumentFunction and Stylesheet has an entity reference to doc(), ENTITY s document('foo.xml')", Pri = 1, Params = new object[] { "XsltSettings.xml", "XsltSettings15.xsl", false, false })]
        //[InlineData("XsltSettings.xml", "XsltSettings15.xsl", false, false)]
        [Theory]
        public void XsltSettings3(object param0, object param1, object param2, object param3)
        {
            Init(param0.ToString(), param1.ToString());

            if (_isInProc)
                if (_debug)
                    return; //TEST_SKIPPED;

            XsltSettings xs = new XsltSettings((bool)param2, (bool)param3);
            XPathDocument doc = new XPathDocument(_xslFile);
            _xsl.Load(doc, xs, new XmlUrlResolver());

            try
            {
                StringWriter sw = Transform();
                _output.WriteLine("Execution of the document function was allowed even when XsltSettings.EnableDocumentFunction is false");
                Assert.True(false);
            }
            catch (XsltException ex)
            {
                _output.WriteLine(ex.ToString());
                return;
            }
        }
    }
}