2727import java .net .UnknownHostException ;
2828import java .nio .charset .StandardCharsets ;
2929import java .util .Enumeration ;
30+ import java .util .List ;
3031import java .util .Locale ;
3132
3233import static org .hamcrest .Matchers .equalTo ;
@@ -180,10 +181,12 @@ public void testConvertDottedQuadToHex() throws UnknownHostException {
180181 // Shouldn't hit DNS, because it's an IP string literal.
181182 InetAddress ipv6Addr = InetAddress .getByName (ipString );
182183 assertEquals (ipv6Addr , InetAddresses .forString (ipString ));
184+ int extraLength = randomInt (8 );
185+ int offset = randomInt (extraLength );
183186 byte [] asBytes = ipString .getBytes (StandardCharsets .UTF_8 );
184- byte [] bytes = new byte [32 ];
185- System .arraycopy (asBytes , 0 , bytes , 8 , asBytes .length );
186- assertEquals (ipv6Addr , InetAddresses .forString (bytes , 8 , asBytes .length ));
187+ byte [] bytes = new byte [asBytes . length + extraLength ];
188+ System .arraycopy (asBytes , 0 , bytes , offset , asBytes .length );
189+ assertEquals (ipv6Addr , InetAddresses .forString (bytes , offset , asBytes .length ));
187190 assertTrue (InetAddresses .isInetAddress (ipString ));
188191 }
189192 }
@@ -206,6 +209,35 @@ public void testToAddrStringIPv6() {
206209 assertEquals ("::1" , InetAddresses .toAddrString (InetAddresses .forString ("0:0:0:0:0:0:0:1" )));
207210 assertEquals ("2001:658:22a:cafe::" , InetAddresses .toAddrString (InetAddresses .forString ("2001:0658:022a:cafe::" )));
208211 assertEquals ("::102:304" , InetAddresses .toAddrString (InetAddresses .forString ("::1.2.3.4" )));
212+ assertEquals ("2001:db8::1:0:0:1" , InetAddresses .toAddrString (InetAddresses .forString ("2001:db8::1:0:0:1" )));
213+ }
214+
215+ public void testWithOffsets () {
216+ List <String > ipStrings = List .of (
217+ "1:2:3:4:5:6:7:8" ,
218+ "2001:0:0:4::8" ,
219+ "2001::4:5:6:7:8" ,
220+ "2001:0:3:4:5:6:7:8" ,
221+ "0:0:3::ffff" ,
222+ "::4:0:0:0:ffff" ,
223+ "::5:0:0:ffff" ,
224+ "1::4:0:0:7:8" ,
225+ "::" ,
226+ "::1" ,
227+ "2001:658:22a:cafe::" ,
228+ "::102:304" ,
229+ "2001:db8::1:0:0:1" ,
230+ "2001:db8::1:0:1:0"
231+ );
232+ for (String ipString : ipStrings ) {
233+ byte [] bytes = ipString .getBytes (StandardCharsets .UTF_8 );
234+ int extraLength = randomInt (8 );
235+ int offset = randomInt (extraLength );
236+ byte [] bytesWithPadding = new byte [bytes .length + extraLength ];
237+ System .arraycopy (bytes , 0 , bytesWithPadding , offset , bytes .length );
238+ InetAddress addr = InetAddresses .forString (bytesWithPadding , offset , bytes .length );
239+ assertEquals (ipString , InetAddresses .toAddrString (addr ));
240+ }
209241 }
210242
211243 public void testToUriStringIPv4 () {
0 commit comments