api-test.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. 'use strict';
  2. var ip = require('..');
  3. var assert = require('assert');
  4. var net = require('net');
  5. var os = require('os');
  6. describe('IP library for node.js', function() {
  7. describe('toBuffer()/toString() methods', function() {
  8. it('should convert to buffer IPv4 address', function() {
  9. var buf = ip.toBuffer('127.0.0.1');
  10. assert.equal(buf.toString('hex'), '7f000001');
  11. assert.equal(ip.toString(buf), '127.0.0.1');
  12. });
  13. it('should convert to buffer IPv4 address in-place', function() {
  14. var buf = new Buffer(128);
  15. var offset = 64;
  16. ip.toBuffer('127.0.0.1', buf, offset);
  17. assert.equal(buf.toString('hex', offset, offset + 4), '7f000001');
  18. assert.equal(ip.toString(buf, offset, 4), '127.0.0.1');
  19. });
  20. it('should convert to buffer IPv6 address', function() {
  21. var buf = ip.toBuffer('::1');
  22. assert(/(00){15,15}01/.test(buf.toString('hex')));
  23. assert.equal(ip.toString(buf), '::1');
  24. assert.equal(ip.toString(ip.toBuffer('1::')), '1::');
  25. assert.equal(ip.toString(ip.toBuffer('abcd::dcba')), 'abcd::dcba');
  26. });
  27. it('should convert to buffer IPv6 address in-place', function() {
  28. var buf = new Buffer(128);
  29. var offset = 64;
  30. ip.toBuffer('::1', buf, offset);
  31. assert(/(00){15,15}01/.test(buf.toString('hex', offset, offset + 16)));
  32. assert.equal(ip.toString(buf, offset, 16), '::1');
  33. assert.equal(ip.toString(ip.toBuffer('1::', buf, offset),
  34. offset, 16), '1::');
  35. assert.equal(ip.toString(ip.toBuffer('abcd::dcba', buf, offset),
  36. offset, 16), 'abcd::dcba');
  37. });
  38. it('should convert to buffer IPv6 mapped IPv4 address', function() {
  39. var buf = ip.toBuffer('::ffff:127.0.0.1');
  40. assert.equal(buf.toString('hex'), '00000000000000000000ffff7f000001');
  41. assert.equal(ip.toString(buf), '::ffff:7f00:1');
  42. buf = ip.toBuffer('ffff::127.0.0.1');
  43. assert.equal(buf.toString('hex'), 'ffff000000000000000000007f000001');
  44. assert.equal(ip.toString(buf), 'ffff::7f00:1');
  45. buf = ip.toBuffer('0:0:0:0:0:ffff:127.0.0.1');
  46. assert.equal(buf.toString('hex'), '00000000000000000000ffff7f000001');
  47. assert.equal(ip.toString(buf), '::ffff:7f00:1');
  48. });
  49. });
  50. describe('fromPrefixLen() method', function() {
  51. it('should create IPv4 mask', function() {
  52. assert.equal(ip.fromPrefixLen(24), '255.255.255.0');
  53. });
  54. it('should create IPv6 mask', function() {
  55. assert.equal(ip.fromPrefixLen(64), 'ffff:ffff:ffff:ffff::');
  56. });
  57. it('should create IPv6 mask explicitly', function() {
  58. assert.equal(ip.fromPrefixLen(24, 'IPV6'), 'ffff:ff00::');
  59. });
  60. });
  61. describe('not() method', function() {
  62. it('should reverse bits in address', function() {
  63. assert.equal(ip.not('255.255.255.0'), '0.0.0.255');
  64. });
  65. });
  66. describe('or() method', function() {
  67. it('should or bits in ipv4 addresses', function() {
  68. assert.equal(ip.or('0.0.0.255', '192.168.1.10'), '192.168.1.255');
  69. });
  70. it('should or bits in ipv6 addresses', function() {
  71. assert.equal(ip.or('::ff', '::abcd:dcba:abcd:dcba'),
  72. '::abcd:dcba:abcd:dcff');
  73. });
  74. it('should or bits in mixed addresses', function() {
  75. assert.equal(ip.or('0.0.0.255', '::abcd:dcba:abcd:dcba'),
  76. '::abcd:dcba:abcd:dcff');
  77. });
  78. });
  79. describe('mask() method', function() {
  80. it('should mask bits in address', function() {
  81. assert.equal(ip.mask('192.168.1.134', '255.255.255.0'), '192.168.1.0');
  82. assert.equal(ip.mask('192.168.1.134', '::ffff:ff00'), '::ffff:c0a8:100');
  83. });
  84. it('should not leak data', function() {
  85. for (var i = 0; i < 10; i++)
  86. assert.equal(ip.mask('::1', '0.0.0.0'), '::');
  87. });
  88. });
  89. describe('subnet() method', function() {
  90. // Test cases calculated with http://www.subnet-calculator.com/
  91. var ipv4Subnet = ip.subnet('192.168.1.134', '255.255.255.192');
  92. it('should compute ipv4 network address', function() {
  93. assert.equal(ipv4Subnet.networkAddress, '192.168.1.128');
  94. });
  95. it('should compute ipv4 network\'s first address', function() {
  96. assert.equal(ipv4Subnet.firstAddress, '192.168.1.129');
  97. });
  98. it('should compute ipv4 network\'s last address', function() {
  99. assert.equal(ipv4Subnet.lastAddress, '192.168.1.190');
  100. });
  101. it('should compute ipv4 broadcast address', function() {
  102. assert.equal(ipv4Subnet.broadcastAddress, '192.168.1.191');
  103. });
  104. it('should compute ipv4 subnet number of addresses', function() {
  105. assert.equal(ipv4Subnet.length, 64);
  106. });
  107. it('should compute ipv4 subnet number of addressable hosts', function() {
  108. assert.equal(ipv4Subnet.numHosts, 62);
  109. });
  110. it('should compute ipv4 subnet mask', function() {
  111. assert.equal(ipv4Subnet.subnetMask, '255.255.255.192');
  112. });
  113. it('should compute ipv4 subnet mask\'s length', function() {
  114. assert.equal(ipv4Subnet.subnetMaskLength, 26);
  115. });
  116. it('should know whether a subnet contains an address', function() {
  117. assert.equal(ipv4Subnet.contains('192.168.1.180'), true);
  118. });
  119. it('should know whether a subnet does not contain an address', function() {
  120. assert.equal(ipv4Subnet.contains('192.168.1.195'), false);
  121. });
  122. });
  123. describe('subnet() method with mask length 32', function() {
  124. // Test cases calculated with http://www.subnet-calculator.com/
  125. var ipv4Subnet = ip.subnet('192.168.1.134', '255.255.255.255');
  126. it('should compute ipv4 network\'s first address', function() {
  127. assert.equal(ipv4Subnet.firstAddress, '192.168.1.134');
  128. });
  129. it('should compute ipv4 network\'s last address', function() {
  130. assert.equal(ipv4Subnet.lastAddress, '192.168.1.134');
  131. });
  132. it('should compute ipv4 subnet number of addressable hosts', function() {
  133. assert.equal(ipv4Subnet.numHosts, 1);
  134. });
  135. });
  136. describe('subnet() method with mask length 31', function() {
  137. // Test cases calculated with http://www.subnet-calculator.com/
  138. var ipv4Subnet = ip.subnet('192.168.1.134', '255.255.255.254');
  139. it('should compute ipv4 network\'s first address', function() {
  140. assert.equal(ipv4Subnet.firstAddress, '192.168.1.134');
  141. });
  142. it('should compute ipv4 network\'s last address', function() {
  143. assert.equal(ipv4Subnet.lastAddress, '192.168.1.135');
  144. });
  145. it('should compute ipv4 subnet number of addressable hosts', function() {
  146. assert.equal(ipv4Subnet.numHosts, 2);
  147. });
  148. });
  149. describe('cidrSubnet() method', function() {
  150. // Test cases calculated with http://www.subnet-calculator.com/
  151. var ipv4Subnet = ip.cidrSubnet('192.168.1.134/26');
  152. it('should compute an ipv4 network address', function() {
  153. assert.equal(ipv4Subnet.networkAddress, '192.168.1.128');
  154. });
  155. it('should compute an ipv4 network\'s first address', function() {
  156. assert.equal(ipv4Subnet.firstAddress, '192.168.1.129');
  157. });
  158. it('should compute an ipv4 network\'s last address', function() {
  159. assert.equal(ipv4Subnet.lastAddress, '192.168.1.190');
  160. });
  161. it('should compute an ipv4 broadcast address', function() {
  162. assert.equal(ipv4Subnet.broadcastAddress, '192.168.1.191');
  163. });
  164. it('should compute an ipv4 subnet number of addresses', function() {
  165. assert.equal(ipv4Subnet.length, 64);
  166. });
  167. it('should compute an ipv4 subnet number of addressable hosts', function() {
  168. assert.equal(ipv4Subnet.numHosts, 62);
  169. });
  170. it('should compute an ipv4 subnet mask', function() {
  171. assert.equal(ipv4Subnet.subnetMask, '255.255.255.192');
  172. });
  173. it('should compute an ipv4 subnet mask\'s length', function() {
  174. assert.equal(ipv4Subnet.subnetMaskLength, 26);
  175. });
  176. it('should know whether a subnet contains an address', function() {
  177. assert.equal(ipv4Subnet.contains('192.168.1.180'), true);
  178. });
  179. it('should know whether a subnet contains an address', function() {
  180. assert.equal(ipv4Subnet.contains('192.168.1.195'), false);
  181. });
  182. });
  183. describe('cidr() method', function() {
  184. it('should mask address in CIDR notation', function() {
  185. assert.equal(ip.cidr('192.168.1.134/26'), '192.168.1.128');
  186. assert.equal(ip.cidr('2607:f0d0:1002:51::4/56'), '2607:f0d0:1002::');
  187. });
  188. });
  189. describe('isEqual() method', function() {
  190. it('should check if addresses are equal', function() {
  191. assert(ip.isEqual('127.0.0.1', '::7f00:1'));
  192. assert(!ip.isEqual('127.0.0.1', '::7f00:2'));
  193. assert(ip.isEqual('127.0.0.1', '::ffff:7f00:1'));
  194. assert(!ip.isEqual('127.0.0.1', '::ffaf:7f00:1'));
  195. assert(ip.isEqual('::ffff:127.0.0.1', '::ffff:127.0.0.1'));
  196. assert(ip.isEqual('::ffff:127.0.0.1', '127.0.0.1'));
  197. });
  198. });
  199. describe('isPrivate() method', function() {
  200. it('should check if an address is localhost', function() {
  201. assert.equal(ip.isPrivate('127.0.0.1'), true);
  202. });
  203. it('should check if an address is from a 192.168.x.x network', function() {
  204. assert.equal(ip.isPrivate('192.168.0.123'), true);
  205. assert.equal(ip.isPrivate('192.168.122.123'), true);
  206. assert.equal(ip.isPrivate('192.162.1.2'), false);
  207. });
  208. it('should check if an address is from a 172.16.x.x network', function() {
  209. assert.equal(ip.isPrivate('172.16.0.5'), true);
  210. assert.equal(ip.isPrivate('172.16.123.254'), true);
  211. assert.equal(ip.isPrivate('171.16.0.5'), false);
  212. assert.equal(ip.isPrivate('172.25.232.15'), true);
  213. assert.equal(ip.isPrivate('172.15.0.5'), false);
  214. assert.equal(ip.isPrivate('172.32.0.5'), false);
  215. });
  216. it('should check if an address is from a 169.254.x.x network', function() {
  217. assert.equal(ip.isPrivate('169.254.2.3'), true);
  218. assert.equal(ip.isPrivate('169.254.221.9'), true);
  219. assert.equal(ip.isPrivate('168.254.2.3'), false);
  220. });
  221. it('should check if an address is from a 10.x.x.x network', function() {
  222. assert.equal(ip.isPrivate('10.0.2.3'), true);
  223. assert.equal(ip.isPrivate('10.1.23.45'), true);
  224. assert.equal(ip.isPrivate('12.1.2.3'), false);
  225. });
  226. it('should check if an address is from a private IPv6 network', function() {
  227. assert.equal(ip.isPrivate('fd12:3456:789a:1::1'), true);
  228. assert.equal(ip.isPrivate('fe80::f2de:f1ff:fe3f:307e'), true);
  229. assert.equal(ip.isPrivate('::ffff:10.100.1.42'), true);
  230. assert.equal(ip.isPrivate('::FFFF:172.16.200.1'), true);
  231. assert.equal(ip.isPrivate('::ffff:192.168.0.1'), true);
  232. });
  233. it('should check if an address is from the internet', function() {
  234. assert.equal(ip.isPrivate('165.225.132.33'), false); // joyent.com
  235. });
  236. it('should check if an address is a loopback IPv6 address', function() {
  237. assert.equal(ip.isPrivate('::'), true);
  238. assert.equal(ip.isPrivate('::1'), true);
  239. assert.equal(ip.isPrivate('fe80::1'), true);
  240. });
  241. });
  242. describe('loopback() method', function() {
  243. describe('undefined', function() {
  244. it('should respond with 127.0.0.1', function() {
  245. assert.equal(ip.loopback(), '127.0.0.1')
  246. });
  247. });
  248. describe('ipv4', function() {
  249. it('should respond with 127.0.0.1', function() {
  250. assert.equal(ip.loopback('ipv4'), '127.0.0.1')
  251. });
  252. });
  253. describe('ipv6', function() {
  254. it('should respond with fe80::1', function() {
  255. assert.equal(ip.loopback('ipv6'), 'fe80::1')
  256. });
  257. });
  258. });
  259. describe('isLoopback() method', function() {
  260. describe('127.0.0.1', function() {
  261. it('should respond with true', function() {
  262. assert.ok(ip.isLoopback('127.0.0.1'))
  263. });
  264. });
  265. describe('127.8.8.8', function () {
  266. it('should respond with true', function () {
  267. assert.ok(ip.isLoopback('127.8.8.8'))
  268. });
  269. });
  270. describe('8.8.8.8', function () {
  271. it('should respond with false', function () {
  272. assert.equal(ip.isLoopback('8.8.8.8'), false);
  273. });
  274. });
  275. describe('fe80::1', function() {
  276. it('should respond with true', function() {
  277. assert.ok(ip.isLoopback('fe80::1'))
  278. });
  279. });
  280. describe('::1', function() {
  281. it('should respond with true', function() {
  282. assert.ok(ip.isLoopback('::1'))
  283. });
  284. });
  285. describe('::', function() {
  286. it('should respond with true', function() {
  287. assert.ok(ip.isLoopback('::'))
  288. });
  289. });
  290. });
  291. describe('address() method', function() {
  292. describe('undefined', function() {
  293. it('should respond with a private ip', function() {
  294. assert.ok(ip.isPrivate(ip.address()));
  295. });
  296. });
  297. describe('private', function() {
  298. [ undefined, 'ipv4', 'ipv6' ].forEach(function(family) {
  299. describe(family, function() {
  300. it('should respond with a private ip', function() {
  301. assert.ok(ip.isPrivate(ip.address('private', family)));
  302. });
  303. });
  304. });
  305. });
  306. var interfaces = os.networkInterfaces();
  307. Object.keys(interfaces).forEach(function(nic) {
  308. describe(nic, function() {
  309. [ undefined, 'ipv4' ].forEach(function(family) {
  310. describe(family, function() {
  311. it('should respond with an ipv4 address', function() {
  312. var addr = ip.address(nic, family);
  313. assert.ok(!addr || net.isIPv4(addr));
  314. });
  315. });
  316. });
  317. describe('ipv6', function() {
  318. it('should respond with an ipv6 address', function() {
  319. var addr = ip.address(nic, 'ipv6');
  320. assert.ok(!addr || net.isIPv6(addr));
  321. });
  322. })
  323. });
  324. });
  325. });
  326. describe('toLong() method', function() {
  327. it('should respond with a int', function() {
  328. assert.equal(ip.toLong('127.0.0.1'), 2130706433);
  329. assert.equal(ip.toLong('255.255.255.255'), 4294967295);
  330. });
  331. });
  332. describe('fromLong() method', function() {
  333. it('should repond with ipv4 address', function() {
  334. assert.equal(ip.fromLong(2130706433), '127.0.0.1');
  335. assert.equal(ip.fromLong(4294967295), '255.255.255.255');
  336. });
  337. })
  338. });