Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/ip.js
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,9 @@ ip.address = function(name, family) {
};

ip.toLong = function(ip) {
if (!this.isV4Format(ip)) {
throw new Error('.toLong requires valid IPv4 as input: ' + ip);
}
var ipl = 0;
ip.split('.').forEach(function(octet) {
ipl <<= 8;
Expand Down
13 changes: 12 additions & 1 deletion test/api-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ describe('IP library for node.js', function() {
var addr = ip.address(nic, 'ipv6');
assert.ok(!addr || net.isIPv6(addr));
});
})
});
});
});
});
Expand All @@ -396,6 +396,17 @@ describe('IP library for node.js', function() {
assert.equal(ip.toLong('127.0.0.1'), 2130706433);
assert.equal(ip.toLong('255.255.255.255'), 4294967295);
});
it('should be able handle bad input', function() {
assert.throws(function () {
ip.toLong('fd12:3456:789a:1::1');
}, Error);
assert.throws(function () {
ip.toLong(null);
}, Error);
assert.throws(function () {
ip.toLong(undefined);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

false, empty string '', Function ? :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would end up testing here this .isV4Format more as in the end this will be used to validated input - so just added few possible values that can perhaps happens as bad input other than ipv6

}, Error);
});
});

describe('fromLong() method', function() {
Expand Down