Committed by
Gerrit Code Review
Invalid pattern restriction check
Change-Id: I5398cb6e85f06efd7937562ba0d9a51eff23572d (cherry picked from commit 8d0ad2fa)
Showing
3 changed files
with
51 additions
and
7 deletions
... | @@ -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; |
... | @@ -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(); |
... | @@ -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 | } | ... | ... |
-
Please register or login to post a comment