Bug 5410 - XSD regular expressions are interpreted as Java regexes (1/2)
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / Bug5410Test.java
1 /*
2  * Copyright (c) 2017 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14
15 import com.google.common.collect.ImmutableList;
16 import java.io.ByteArrayOutputStream;
17 import java.io.PrintStream;
18 import java.io.UnsupportedEncodingException;
19 import java.util.List;
20 import org.junit.Test;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
25 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
26 import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
27 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
28 import org.opendaylight.yangtools.yang.stmt.StmtTestUtils;
29
30 public class Bug5410Test {
31     private static final String FOO_NS = "foo";
32     private static final String FOO_REV = "1970-01-01";
33
34     @Test
35     public void testJavaRegexFromXSD() {
36         testPattern("^[^:]+$", "^\\^[^:]+\\$$", ImmutableList.of("^a$", "^abc$"),
37                 ImmutableList.of("abc$", "^abc", "^a:bc$"));
38         testPattern("^[$^]$", "^\\^[$^]\\$$", ImmutableList.of("^^$", "^$$"), ImmutableList.of("^^", "^$", "$^", "$$"));
39         testPattern("[$-%]+", "^[$-%]+$", ImmutableList.of("$", "%", "%$"), ImmutableList.of("$-", "$-%", "-", "^"));
40         testPattern("[$-&]+", "^[$-&]+$", ImmutableList.of("$", "%&", "%$", "$%&"), ImmutableList.of("#", "$-&", "'"));
41
42         testPattern("[a-z&&[^m-p]]+", "^[a-z&&[^m-p]]+$", ImmutableList.of("a", "z", "az"),
43                 ImmutableList.of("m", "anz", "o"));
44         testPattern("^[\\[-b&&[^^-a]]+$", "^\\^[\\[-b&&[^^-a]]+\\$$", ImmutableList.of("^[$", "^\\$", "^]$", "^b$"),
45                 ImmutableList.of("^a$", "^^$", "^_$"));
46
47         testPattern("[^^-~&&[^$-^]]", "^[^^-~&&[^$-^]]$", ImmutableList.of("!", "\"", "#"),
48                 ImmutableList.of("a", "A", "z", "Z", "$", "%", "^", "}"));
49         testPattern("\\\\\\[^[^^-~&&[^$-^]]", "^\\\\\\[\\^[^^-~&&[^$-^]]$",
50                 ImmutableList.of("\\[^ ", "\\[^!", "\\[^\"", "\\[^#"),
51                 ImmutableList.of("\\[^a", "\\[^A", "\\[^z", "\\[^Z", "\\[^$", "\\[^%", "\\[^^", "\\[^}"));
52         testPattern("^\\[^\\\\[^^-b&&[^\\[-\\]]]\\]^", "^\\^\\[\\^\\\\[^^-b&&[^\\[-\\]]]\\]\\^$",
53                 ImmutableList.of("^[^\\c]^", "^[^\\Z]^"),
54                 ImmutableList.of("^[^\\[]^", "^[^\\\\]^", "^[^\\]]^", "^[^\\^]^", "^[^\\_]^", "^[^\\b]^"));
55         testPattern("[\\^]$", "^[\\^]\\$$", ImmutableList.of("^$"),
56                 ImmutableList.of("^", "$", "$^", "\\", "\\^", "\\^\\", "\\^\\$"));
57     }
58
59     @Test
60     public void testInvalidXSDRegexes() throws UnsupportedEncodingException {
61         testInvalidPattern("$^a^[$^\\]", "Unclosed character class");
62         testInvalidPattern("$(\\)", "Unclosed group");
63     }
64
65     @Test
66     public void testJavaPattern() {
67         testPattern("^[$^]+$", ImmutableList.of("$^", "^", "$"), ImmutableList.of("\\", "a"));
68         testPattern("^[^$-^]$", ImmutableList.of("a", "_", "#"), ImmutableList.of("%", "^", "$", "]", "\\"));
69     }
70
71     @Test
72     public void testYangPattern() throws Exception {
73         final SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug5410");
74         assertNotNull(context);
75
76         final PatternConstraint pattern = getPatternConstraintOf(context, "leaf-with-pattern");
77
78         final String rawRegex = pattern.getRawRegularExpression();
79         final String expectedYangRegex = "$0$.*|$1$[a-zA-Z0-9./]{1,8}$[a-zA-Z0-9./]{22}|$5$(rounds=\\d+$)?[a-zA-Z0-9./]{1,16}$[a-zA-Z0-9./]{43}|$6$(rounds=\\d+$)?[a-zA-Z0-9./]{1,16}$[a-zA-Z0-9./]{86}";
80         assertEquals(expectedYangRegex, rawRegex);
81
82         final String javaRegexFromYang = pattern.getRegularExpression();
83         final String expectedJavaRegex = "^\\$0\\$.*|\\$1\\$[a-zA-Z0-9./]{1,8}\\$[a-zA-Z0-9./]{22}|\\$5\\$(rounds=\\d+\\$)?[a-zA-Z0-9./]{1,16}\\$[a-zA-Z0-9./]{43}|\\$6\\$(rounds=\\d+\\$)?[a-zA-Z0-9./]{1,16}\\$[a-zA-Z0-9./]{86}$";
84         assertEquals(expectedJavaRegex, javaRegexFromYang);
85
86         final String value = "$6$AnrKGc0V$B/0/A.pWg4HrrA6YiEJOtFGibQ9Fmm5.4rI/00gEz3QeB7joSxBU3YtbHDm6NSkS1dKTQy3BWhwKKDS8nB5S//";
87         testPattern(javaRegexFromYang, ImmutableList.of(value), ImmutableList.of());
88     }
89
90     @Test
91     public void testCaret() {
92         testPattern("^", "\\^");
93     }
94
95     @Test
96     public void testTextCaret() {
97         testPattern("abc^", "abc\\^");
98     }
99
100     @Test
101     public void testTextDollar() {
102         testPattern("abc$", "abc\\$");
103     }
104
105     @Test
106     public void testCaretCaret() {
107         testPattern("^^", "\\^\\^");
108     }
109
110     @Test
111     public void testCaretDollar() {
112         testPattern("^$", "\\^\\$");
113     }
114
115     @Test
116     public void testDot() {
117         testPattern(".", ".");
118     }
119
120     @Test
121     public void testNotColon() {
122         testPattern("[^:]+", "[^:]+");
123     }
124
125     @Test
126     public void testDollar() {
127         testPattern("$", "\\$");
128     }
129
130     @Test
131     public void testDollarOneDollar() {
132         testPattern("$1$", "\\$1\\$");
133     }
134
135     @Test
136     public void testDollarPercentRange() {
137         testPattern("[$-%]+", "[$-%]+");
138     }
139
140     @Test
141     public void testDollarRange() {
142         testPattern("[$$]+", "[$$]+");
143     }
144
145     @Test
146     public void testDollarCaretRange() {
147         testPattern("[$^]+", "[$^]+");
148     }
149
150     @Test
151     public void testSimple() {
152         testPattern("abc", "abc");
153     }
154
155     @Test
156     public void testDotPlus() {
157         testPattern(".+", ".+");
158     }
159
160     @Test
161     public void testDotStar() {
162         testPattern(".*", ".*");
163     }
164
165     @Test
166     public void testSimpleOptional() {
167         testPattern("a?", "a?");
168     }
169
170     @Test
171     public void testRangeOptional() {
172         testPattern("[a-z]?", "[a-z]?");
173     }
174
175     private static void testPattern(final String xsdRegex, final String expectedJavaRegex,
176             final List<String> positiveMatches, final List<String> negativeMatches) {
177         final String javaRegexFromXSD = javaRegexFromXSD(xsdRegex);
178         assertEquals(expectedJavaRegex, javaRegexFromXSD);
179
180         for (final String value : positiveMatches) {
181             assertTrue("Value '" + value + "' does not match java regex '" + javaRegexFromXSD + "'",
182                     testMatch(javaRegexFromXSD, value));
183         }
184         for (final String value : negativeMatches) {
185             assertFalse("Value '" + value + "' matches java regex '" + javaRegexFromXSD + "'",
186                     testMatch(javaRegexFromXSD, value));
187         }
188     }
189
190     private static void testPattern(final String javaRegex, final List<String> positiveMatches,
191             final List<String> negativeMatches) {
192         for (final String value : positiveMatches) {
193             assertTrue("Value '" + value + "' does not match java regex '" + javaRegex + "'",
194                     testMatch(javaRegex, value));
195         }
196         for (final String value : negativeMatches) {
197             assertFalse("Value '" + value + "' matches java regex '" + javaRegex + "'", testMatch(javaRegex, value));
198         }
199     }
200
201     private static String javaRegexFromXSD(final String xsdRegex) {
202         return PatternStatementImpl.Definition.getJavaRegexFromXSD(xsdRegex);
203     }
204
205     private static boolean testMatch(final String javaRegex, final String value) {
206         return value.matches(javaRegex);
207     }
208
209     private static void testPattern(final String xsdRegex, final String unanchoredJavaRegex) {
210         testPattern(xsdRegex, '^' + unanchoredJavaRegex + '$', ImmutableList.of(), ImmutableList.of());
211     }
212
213     private static PatternConstraint getPatternConstraintOf(final SchemaContext context, final String leafName) {
214         final DataSchemaNode dataChildByName = context.getDataChildByName(foo(leafName));
215         assertTrue(dataChildByName instanceof LeafSchemaNode);
216         final LeafSchemaNode leaf = (LeafSchemaNode) dataChildByName;
217         final TypeDefinition<? extends TypeDefinition<?>> type = leaf.getType();
218         assertTrue(type instanceof StringTypeDefinition);
219         final StringTypeDefinition strType = (StringTypeDefinition) type;
220         return strType.getPatternConstraints().iterator().next();
221     }
222
223     private static QName foo(final String localName) {
224         return QName.create(FOO_NS, FOO_REV, localName);
225     }
226
227     private static void testInvalidPattern(final String xsdRegex, final String expectedMessage) throws UnsupportedEncodingException {
228         final PrintStream stdout = System.out;
229         final ByteArrayOutputStream output = new ByteArrayOutputStream();
230         System.setOut(new PrintStream(output, true, "UTF-8"));
231
232         javaRegexFromXSD(xsdRegex);
233
234         final String testLog = output.toString();
235         assertTrue(testLog.contains(expectedMessage));
236         System.setOut(stdout);
237     }
238 }