api-test.js 12 KB

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