@@ -251,6 +251,57 @@ describe('IP library for node.js', () => {
251251 } ) ;
252252 } ) ;
253253
254+ describe ( 'normalizeIpv4() method' , ( ) => {
255+ // Testing valid inputs with different notations
256+ it ( 'should correctly normalize "127.0.0.1"' , ( ) => {
257+ assert . equal ( ip . normalizeToLong ( '127.0.0.1' ) , 2130706433 ) ;
258+ } ) ;
259+
260+ it ( 'should correctly handle "127.1" as two parts' , ( ) => {
261+ assert . equal ( ip . normalizeToLong ( '127.1' ) , 2130706433 ) ;
262+ } ) ;
263+
264+ it ( 'should correctly handle "127.0.1" as three parts' , ( ) => {
265+ assert . equal ( ip . normalizeToLong ( '127.0.1' ) , 2130706433 ) ;
266+ } ) ;
267+
268+
269+ it ( 'should correctly handle hexadecimal notation "0x7f.0x0.0x0.0x1"' , ( ) => {
270+ assert . equal ( ip . normalizeToLong ( '0x7f.0x0.0x0.0x1' ) , 2130706433 ) ;
271+ } ) ;
272+
273+ // Testing with fewer than 4 parts
274+ it ( 'should correctly handle "0x7f000001" as a single part' , ( ) => {
275+ assert . equal ( ip . normalizeToLong ( '0x7f000001' ) , 2130706433 ) ;
276+ } ) ;
277+
278+ it ( 'should correctly handle octal notation "010.0.0.01"' , ( ) => {
279+ assert . equal ( ip . normalizeToLong ( '010.0.0.01' ) , 134217729 ) ;
280+ } ) ;
281+
282+ // Testing invalid inputs
283+ it ( 'should return -1 for an invalid address "256.100.50.25"' , ( ) => {
284+ assert . equal ( ip . normalizeToLong ( '256.100.50.25' ) , - 1 ) ;
285+ } ) ;
286+
287+ it ( 'should return -1 for an address with invalid octal "019.0.0.1"' , ( ) => {
288+ assert . equal ( ip . normalizeToLong ( '019.0.0.1' ) , - 1 ) ;
289+ } ) ;
290+
291+ it ( 'should return -1 for an address with invalid hex "0xGG.0.0.1"' , ( ) => {
292+ assert . equal ( ip . normalizeToLong ( '0xGG.0.0.1' ) , - 1 ) ;
293+ } ) ;
294+
295+ // Testing edge cases
296+ it ( 'should return -1 for an empty string' , ( ) => {
297+ assert . equal ( ip . normalizeToLong ( '' ) , - 1 ) ;
298+ } ) ;
299+
300+ it ( 'should return -1 for a string with too many parts "192.168.0.1.100"' , ( ) => {
301+ assert . equal ( ip . normalizeToLong ( '192.168.0.1.100' ) , - 1 ) ;
302+ } ) ;
303+ } ) ;
304+
254305 describe ( 'isPrivate() method' , ( ) => {
255306 it ( 'should check if an address is localhost' , ( ) => {
256307 assert . equal ( ip . isPrivate ( '127.0.0.1' ) , true ) ;
@@ -300,6 +351,10 @@ describe('IP library for node.js', () => {
300351 assert . equal ( ip . isPrivate ( '::1' ) , true ) ;
301352 assert . equal ( ip . isPrivate ( 'fe80::1' ) , true ) ;
302353 } ) ;
354+
355+ it ( 'should correctly identify hexadecimal IP addresses like \'0x7f.1\' as private' , ( ) => {
356+ assert . equal ( ip . isPrivate ( '0x7f.1' ) , true ) ;
357+ } ) ;
303358 } ) ;
304359
305360 describe ( 'loopback() method' , ( ) => {
@@ -413,4 +468,42 @@ describe('IP library for node.js', () => {
413468 assert . equal ( ip . fromLong ( 4294967295 ) , '255.255.255.255' ) ;
414469 } ) ;
415470 } ) ;
471+
472+ // IPv4 loopback in octal notation
473+ it ( 'should return true for octal representation "0177.0.0.1"' , ( ) => {
474+ assert . equal ( ip . isLoopback ( '0177.0.0.1' ) , true ) ;
475+ } ) ;
476+
477+ it ( 'should return true for octal representation "0177.0.1"' , ( ) => {
478+ assert . equal ( ip . isLoopback ( '0177.0.1' ) , true ) ;
479+ } ) ;
480+
481+ it ( 'should return true for octal representation "0177.1"' , ( ) => {
482+ assert . equal ( ip . isLoopback ( '0177.1' ) , true ) ;
483+ } ) ;
484+
485+ // IPv4 loopback in hexadecimal notation
486+ it ( 'should return true for hexadecimal representation "0x7f.0.0.1"' , ( ) => {
487+ assert . equal ( ip . isLoopback ( '0x7f.0.0.1' ) , true ) ;
488+ } ) ;
489+
490+ // IPv4 loopback in hexadecimal notation
491+ it ( 'should return true for hexadecimal representation "0x7f.0.1"' , ( ) => {
492+ assert . equal ( ip . isLoopback ( '0x7f.0.1' ) , true ) ;
493+ } ) ;
494+
495+ // IPv4 loopback in hexadecimal notation
496+ it ( 'should return true for hexadecimal representation "0x7f.1"' , ( ) => {
497+ assert . equal ( ip . isLoopback ( '0x7f.1' ) , true ) ;
498+ } ) ;
499+
500+ // IPv4 loopback as a single long integer
501+ it ( 'should return true for single long integer representation "2130706433"' , ( ) => {
502+ assert . equal ( ip . isLoopback ( '2130706433' ) , true ) ;
503+ } ) ;
504+
505+ // IPv4 non-loopback address
506+ it ( 'should return false for "192.168.1.1"' , ( ) => {
507+ assert . equal ( ip . isLoopback ( '192.168.1.1' ) , false ) ;
508+ } ) ;
416509} ) ;
0 commit comments