Gaurav Agrawal
Committed by Gerrit Code Review

Invalid pattern restriction check

Change-Id: I5398cb6e85f06efd7937562ba0d9a51eff23572d
(cherry picked from commit 8d0ad2fa)
...@@ -16,6 +16,8 @@ ...@@ -16,6 +16,8 @@
16 16
17 package org.onosproject.yangutils.parser.impl.listeners; 17 package org.onosproject.yangutils.parser.impl.listeners;
18 18
19 +import java.util.regex.Pattern;
20 +import java.util.regex.PatternSyntaxException;
19 import org.onosproject.yangutils.datamodel.YangDataTypes; 21 import org.onosproject.yangutils.datamodel.YangDataTypes;
20 import org.onosproject.yangutils.datamodel.YangDerivedInfo; 22 import org.onosproject.yangutils.datamodel.YangDerivedInfo;
21 import org.onosproject.yangutils.datamodel.YangPatternRestriction; 23 import org.onosproject.yangutils.datamodel.YangPatternRestriction;
...@@ -98,8 +100,8 @@ public final class PatternRestrictionListener { ...@@ -98,8 +100,8 @@ public final class PatternRestrictionListener {
98 * Sets the pattern restriction to type. 100 * Sets the pattern restriction to type.
99 * 101 *
100 * @param listener listener's object 102 * @param listener listener's object
101 - * @param type Yang type for which pattern restriction to be set 103 + * @param type Yang type for which pattern restriction to be set
102 - * @param ctx context object of the grammar rule 104 + * @param ctx context object of the grammar rule
103 */ 105 */
104 private static void setPatternRestriction(TreeWalkListener listener, YangType type, 106 private static void setPatternRestriction(TreeWalkListener listener, YangType type,
105 GeneratedYangParser.PatternStatementContext ctx) { 107 GeneratedYangParser.PatternStatementContext ctx) {
...@@ -114,7 +116,8 @@ public final class PatternRestrictionListener { ...@@ -114,7 +116,8 @@ public final class PatternRestrictionListener {
114 throw parserException; 116 throw parserException;
115 } 117 }
116 118
117 - String patternArgument = ctx.string().getText().replace("\"", EMPTY_STRING); 119 + // Validate and get valid pattern restriction string.
120 + String patternArgument = getValidPattern(ctx);
118 121
119 if (type.getDataType() == YangDataTypes.STRING) { 122 if (type.getDataType() == YangDataTypes.STRING) {
120 YangStringRestriction stringRestriction = (YangStringRestriction) type.getDataTypeExtendedInfo(); 123 YangStringRestriction stringRestriction = (YangStringRestriction) type.getDataTypeExtendedInfo();
...@@ -145,10 +148,10 @@ public final class PatternRestrictionListener { ...@@ -145,10 +148,10 @@ public final class PatternRestrictionListener {
145 * It is called when parser exits from grammar rule (pattern). 148 * It is called when parser exits from grammar rule (pattern).
146 * 149 *
147 * @param listener listener's object 150 * @param listener listener's object
148 - * @param ctx context object of the grammar rule 151 + * @param ctx context object of the grammar rule
149 */ 152 */
150 public static void processPatternRestrictionExit(TreeWalkListener listener, 153 public static void processPatternRestrictionExit(TreeWalkListener listener,
151 - GeneratedYangParser.PatternStatementContext ctx) { 154 + GeneratedYangParser.PatternStatementContext ctx) {
152 155
153 // Check for stack to be non empty. 156 // Check for stack to be non empty.
154 checkStackIsNotEmpty(listener, MISSING_HOLDER, PATTERN_DATA, ctx.string().getText(), EXIT); 157 checkStackIsNotEmpty(listener, MISSING_HOLDER, PATTERN_DATA, ctx.string().getText(), EXIT);
...@@ -164,4 +167,25 @@ public final class PatternRestrictionListener { ...@@ -164,4 +167,25 @@ public final class PatternRestrictionListener {
164 ctx.string().getText(), EXIT)); 167 ctx.string().getText(), EXIT));
165 } 168 }
166 } 169 }
170 +
171 + /**
172 + * Validates and return the valid pattern.
173 + *
174 + * @param ctx context object of the grammar rule
175 + * @return validated string
176 + */
177 + private static String getValidPattern(GeneratedYangParser.PatternStatementContext ctx) {
178 + String userInputPattern = ctx.string().getText().replace("\"", EMPTY_STRING);
179 + try {
180 + Pattern.compile(userInputPattern);
181 + } catch (PatternSyntaxException exception) {
182 + ParserException parserException = new ParserException("YANG file error : " +
183 + YangConstructType.getYangConstructType(PATTERN_DATA) + " name " + ctx.string().getText() +
184 + " is not a valid regular expression");
185 + parserException.setLine(ctx.getStart().getLine());
186 + parserException.setCharPosition(ctx.getStart().getCharPositionInLine());
187 + throw parserException;
188 + }
189 + return userInputPattern;
190 + }
167 } 191 }
......
...@@ -18,17 +18,16 @@ package org.onosproject.yangutils.parser.impl.listeners; ...@@ -18,17 +18,16 @@ package org.onosproject.yangutils.parser.impl.listeners;
18 18
19 import java.io.IOException; 19 import java.io.IOException;
20 import java.util.ListIterator; 20 import java.util.ListIterator;
21 -
22 import org.junit.Test; 21 import org.junit.Test;
23 -import org.onosproject.yangutils.datamodel.YangNode; 22 +import org.onosproject.yangutils.datamodel.YangDataTypes;
24 -import org.onosproject.yangutils.datamodel.YangNodeType;
25 -import org.onosproject.yangutils.datamodel.YangModule;
26 import org.onosproject.yangutils.datamodel.YangLeaf; 23 import org.onosproject.yangutils.datamodel.YangLeaf;
27 import org.onosproject.yangutils.datamodel.YangLeafList; 24 import org.onosproject.yangutils.datamodel.YangLeafList;
28 -import org.onosproject.yangutils.datamodel.YangDataTypes; 25 +import org.onosproject.yangutils.datamodel.YangModule;
29 -import org.onosproject.yangutils.datamodel.YangTypeDef; 26 +import org.onosproject.yangutils.datamodel.YangNode;
27 +import org.onosproject.yangutils.datamodel.YangNodeType;
30 import org.onosproject.yangutils.datamodel.YangPatternRestriction; 28 import org.onosproject.yangutils.datamodel.YangPatternRestriction;
31 import org.onosproject.yangutils.datamodel.YangStringRestriction; 29 import org.onosproject.yangutils.datamodel.YangStringRestriction;
30 +import org.onosproject.yangutils.datamodel.YangTypeDef;
32 import org.onosproject.yangutils.parser.exceptions.ParserException; 31 import org.onosproject.yangutils.parser.exceptions.ParserException;
33 import org.onosproject.yangutils.parser.impl.YangUtilsParserManager; 32 import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
34 33
...@@ -194,4 +193,12 @@ public class PatternRestrictionListenerTest { ...@@ -194,4 +193,12 @@ public class PatternRestrictionListenerTest {
194 .getPatternList().listIterator(); 193 .getPatternList().listIterator();
195 assertThat(patternListIterator.next(), is("[a-zA-Z]")); 194 assertThat(patternListIterator.next(), is("[a-zA-Z]"));
196 } 195 }
196 +
197 + /**
198 + * Checks invalid pattern sub-statement.
199 + */
200 + @Test(expected = ParserException.class)
201 + public void processInvalidPatternSubStatements() throws IOException, ParserException {
202 + YangNode node = manager.getDataModel("src/test/resources/InvalidPatternSubStatements.yang");
203 + }
197 } 204 }
......
1 +module Test {
2 + yang-version 1;
3 + namespace http://huawei.com;
4 + prefix Ant;
5 + leaf invalid-interval {
6 + type string {
7 + pattern "[a-zA-Z]\" {
8 + description "pattern description";
9 + reference "pattern reference";
10 + }
11 + }
12 + }
13 +}