Bug6483188.java revision 779:2b61bfcaa586
1209233Sjchandra/*
2178580Simp * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
3178580Simp * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4178580Simp *
5178580Simp * This code is free software; you can redistribute it and/or modify it
6178580Simp * under the terms of the GNU General Public License version 2 only, as
7178580Simp * published by the Free Software Foundation.
8178580Simp *
9178580Simp * This code is distributed in the hope that it will be useful, but WITHOUT
10178580Simp * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11178580Simp * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12178580Simp * version 2 for more details (a copy is included in the LICENSE file that
13178580Simp * accompanied this code).
14178580Simp *
15178580Simp * You should have received a copy of the GNU General Public License version
16178580Simp * 2 along with this work; if not, write to the Free Software Foundation,
17178580Simp * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18178580Simp *
19178580Simp * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20178580Simp * or visit www.oracle.com if you need additional information or have any
21178580Simp * questions.
22178580Simp */
23178580Simp
24178580Simppackage validation;
25178580Simp
26178580Simpimport java.net.URL;
27178580Simp
28178580Simpimport javax.xml.XMLConstants;
29178580Simpimport javax.xml.validation.Schema;
30178580Simpimport javax.xml.validation.SchemaFactory;
31178580Simpimport javax.xml.validation.Validator;
32178580Simp
33178580Simpimport org.testng.Assert;
34178580Simpimport org.testng.annotations.Test;
35209233Sjchandraimport org.xml.sax.SAXParseException;
36178580Simp
37178580Simp/*
38209233Sjchandra * @bug 6483188
39209233Sjchandra * @summary Test Schema Validator can handle element with having large maxOccurs, but doesn't accept sequence with having large maxOccurs in FEATURE_SECURE_PROCESSING mode.
40209233Sjchandra */
41209233Sjchandrapublic class Bug6483188 {
42209233Sjchandra    static boolean _isSecureMode = false;
43209233Sjchandra    static {
44209233Sjchandra        if (System.getSecurityManager() != null) {
45178580Simp            _isSecureMode = true;
46178580Simp            System.out.println("Security Manager is present");
47209233Sjchandra        } else {
48209233Sjchandra            System.out.println("Security Manager is NOT present");
49209233Sjchandra        }
50209233Sjchandra    }
51209233Sjchandra
52178580Simp    SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
53209233Sjchandra
54178580Simp    @Test
55209233Sjchandra    public void testLargeElementNoSecurity() {
56209233Sjchandra        if (_isSecureMode)
57178580Simp            return; // jaxp secure feature can not be turned off when security
58178580Simp                    // manager is present
59178580Simp        try {
60209233Sjchandra            sf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, Boolean.FALSE);
61209233Sjchandra            URL url = getClass().getResource("test-element.xsd");
62209233Sjchandra            Schema s = sf.newSchema(url);
63209233Sjchandra            Validator v = s.newValidator();
64209233Sjchandra        } catch (Exception e) {
65209233Sjchandra            Assert.fail(e.getMessage());
66209233Sjchandra        }
67209233Sjchandra    }
68209233Sjchandra
69209233Sjchandra    @Test
70209233Sjchandra    public void testLargeElementWithSecurity() {
71178580Simp        try {
72209233Sjchandra            sf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, Boolean.TRUE);
73178580Simp            URL url = getClass().getResource("test-element.xsd");
74209233Sjchandra            Schema s = sf.newSchema(url);
75178580Simp            Validator v = s.newValidator();
76209233Sjchandra        } catch (Exception e) {
77209233Sjchandra            Assert.fail(e.getMessage());
78209233Sjchandra        }
79209233Sjchandra    }
80209233Sjchandra
81209233Sjchandra    @Test
82209233Sjchandra    public void testLargeSequenceWithSecurity() {
83178580Simp        try {
84209233Sjchandra            sf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, Boolean.TRUE);
85209233Sjchandra            URL url = getClass().getResource("test-sequence.xsd");
86209233Sjchandra            Schema s = sf.newSchema(url);
87209233Sjchandra            Validator v = s.newValidator();
88178580Simp            Assert.fail("Schema was accepted even with secure processing enabled.");
89178580Simp        } catch (SAXParseException e) {
90178580Simp            // falls through - exception expected
91209233Sjchandra        } catch (Exception e) {
92178580Simp            Assert.fail(e.getMessage());
93178580Simp        }
94209233Sjchandra    }
95209233Sjchandra
96178580Simp}
97209233Sjchandra