Skip to content

Commit 20a179f

Browse files
authored
Added test cases for not allowed validator, Handled invalid keyword test case for recurisve reference and empty array case for prefixItems validator (#890)
* Added not allowed validator test cases * Updated JAVA doc for NotAllowedValidatorTest * Added PrefixItemsValidatorTest for exception handling test case * Added RecursiveReferenceValidatorExceptionTest for handling invalid recursive ref keyword * Extracted method from isValidInet6Address to get valid and important from inet6Address for further processing. * Renamed strict variable in DurationFormat.java to make it more understandable. * Added explaining variable for return statement in TimeFormat.java. * Extracted a class from BaseJsonValidator with methods concerning to validation messages. * Removed unnecessary override in BaseJsonValidator to improve code readability. * Moved version detection related methods from AbstractJsonSchemaTestSuite to SpecVersionDetector for better cohesion across methods
1 parent e86c817 commit 20a179f

File tree

14 files changed

+582
-388
lines changed

14 files changed

+582
-388
lines changed

src/main/java/com/networknt/org/apache/commons/validator/routines/InetAddressValidator.java

Lines changed: 46 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -59,19 +59,59 @@ public class InetAddressValidator implements Serializable {
5959
*/
6060
private static final InetAddressValidator VALIDATOR = new InetAddressValidator();
6161

62-
/** IPv4 RegexValidator */
62+
/**
63+
* IPv4 RegexValidator
64+
*/
6365
private final RegexValidator ipv4Validator = new RegexValidator(IPV4_REGEX);
6466

6567
/**
6668
* Returns the singleton instance of this validator.
69+
*
6770
* @return the singleton instance of this validator
6871
*/
6972
public static InetAddressValidator getInstance() {
7073
return VALIDATOR;
7174
}
7275

76+
/**
77+
* This method parse and validate the given inet6Address and return the valid inet6Address Part
78+
*
79+
* @param inet6Address inet6Address to be processed
80+
* @return valid part of inet6Address after processing
81+
*/
82+
private static String getValidInet6AddressPart(String inet6Address) {
83+
String[] parts;
84+
// remove prefix size. This will appear after the zone id (if any)
85+
parts = inet6Address.split("/", -1);
86+
if (parts.length > 2) {
87+
return null;
88+
}
89+
if (parts.length == 2) {
90+
if (!parts[1].matches("\\d{1,3}")) {
91+
return null;
92+
}
93+
final int bits = Integer.parseInt(parts[1]); // cannot fail because of RE check
94+
if (bits < 0 || bits > MAX_BYTE) {
95+
return null;
96+
}
97+
}
98+
// remove zone-id
99+
parts = parts[0].split("%", -1);
100+
if (parts.length > 2) {
101+
return null;
102+
}
103+
// The id syntax is implementation independent, but it presumably cannot allow:
104+
// whitespace, '/' or '%'
105+
if ((parts.length == 2) && !parts[1].matches("[^\\s/%]+")) {
106+
return null;
107+
}
108+
inet6Address = parts[0];
109+
return inet6Address;
110+
}
111+
73112
/**
74113
* Checks if the specified string is a valid IPv4 or IPv6 address.
114+
*
75115
* @param inetAddress the string to validate
76116
* @return true if the string validates as an IP address
77117
*/
@@ -81,6 +121,7 @@ public boolean isValid(final String inetAddress) {
81121

82122
/**
83123
* Validates an IPv4 address. Returns true if valid.
124+
*
84125
* @param inet4Address the IPv4 address to validate
85126
* @return true if the argument contains a valid IPv4 address
86127
*/
@@ -121,38 +162,14 @@ public boolean isValidInet4Address(final String inet4Address) {
121162

122163
/**
123164
* Validates an IPv6 address. Returns true if valid.
165+
*
124166
* @param inet6Address the IPv6 address to validate
125167
* @return true if the argument contains a valid IPv6 address
126-
*
127168
* @since 1.4.1
128169
*/
129170
public boolean isValidInet6Address(String inet6Address) {
130-
String[] parts;
131-
// remove prefix size. This will appear after the zone id (if any)
132-
parts = inet6Address.split("/", -1);
133-
if (parts.length > 2) {
134-
return false; // can only have one prefix specifier
135-
}
136-
if (parts.length == 2) {
137-
if (!parts[1].matches("\\d{1,3}")) {
138-
return false; // not a valid number
139-
}
140-
final int bits = Integer.parseInt(parts[1]); // cannot fail because of RE check
141-
if (bits < 0 || bits > MAX_BYTE) {
142-
return false; // out of range
143-
}
144-
}
145-
// remove zone-id
146-
parts = parts[0].split("%", -1);
147-
if (parts.length > 2) {
148-
return false;
149-
}
150-
// The id syntax is implementation independent, but it presumably cannot allow:
151-
// whitespace, '/' or '%'
152-
if ((parts.length == 2) && !parts[1].matches("[^\\s/%]+")) {
153-
return false; // invalid id
154-
}
155-
inet6Address = parts[0];
171+
inet6Address = getValidInet6AddressPart(inet6Address);
172+
if (inet6Address == null) return false; // invalid id
156173
final boolean containsCompressedZeroes = inet6Address.contains("::");
157174
if (containsCompressedZeroes && (inet6Address.indexOf("::") != inet6Address.lastIndexOf("::"))) {
158175
return false;
@@ -209,9 +226,6 @@ public boolean isValidInet6Address(String inet6Address) {
209226
}
210227
validOctets++;
211228
}
212-
if (validOctets > IPV6_MAX_HEX_GROUPS || (validOctets < IPV6_MAX_HEX_GROUPS && !containsCompressedZeroes)) {
213-
return false;
214-
}
215-
return true;
229+
return validOctets <= IPV6_MAX_HEX_GROUPS && (validOctets >= IPV6_MAX_HEX_GROUPS || containsCompressedZeroes);
216230
}
217231
}

0 commit comments

Comments
 (0)