ipaddress_test.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. // Copyright 2011 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. goog.provide('goog.net.IpAddressTest');
  15. goog.setTestOnly('goog.net.IpAddressTest');
  16. goog.require('goog.array');
  17. goog.require('goog.math.Integer');
  18. goog.require('goog.net.IpAddress');
  19. goog.require('goog.net.Ipv4Address');
  20. goog.require('goog.net.Ipv6Address');
  21. goog.require('goog.testing.jsunit');
  22. function testInvalidStrings() {
  23. assertEquals(null, goog.net.IpAddress.fromString(''));
  24. assertEquals(null, goog.net.IpAddress.fromString('016.016.016.016'));
  25. assertEquals(null, goog.net.IpAddress.fromString('016.016.016'));
  26. assertEquals(null, goog.net.IpAddress.fromString('016.016'));
  27. assertEquals(null, goog.net.IpAddress.fromString('016'));
  28. assertEquals(null, goog.net.IpAddress.fromString('000.000.000.000'));
  29. assertEquals(null, goog.net.IpAddress.fromString('000'));
  30. assertEquals(null, goog.net.IpAddress.fromString('0x0a.0x0a.0x0a.0x0a'));
  31. assertEquals(null, goog.net.IpAddress.fromString('0x0a.0x0a.0x0a'));
  32. assertEquals(null, goog.net.IpAddress.fromString('0x0a.0x0a'));
  33. assertEquals(null, goog.net.IpAddress.fromString('0x0a'));
  34. assertEquals(null, goog.net.IpAddress.fromString('42.42.42.42.42'));
  35. assertEquals(null, goog.net.IpAddress.fromString('42.42.42'));
  36. assertEquals(null, goog.net.IpAddress.fromString('42.42'));
  37. assertEquals(null, goog.net.IpAddress.fromString('42'));
  38. assertEquals(null, goog.net.IpAddress.fromString('42..42.42'));
  39. assertEquals(null, goog.net.IpAddress.fromString('42..42.42.42'));
  40. assertEquals(null, goog.net.IpAddress.fromString('42.42.42.42.'));
  41. assertEquals(null, goog.net.IpAddress.fromString('42.42.42.42...'));
  42. assertEquals(null, goog.net.IpAddress.fromString('.42.42.42.42'));
  43. assertEquals(null, goog.net.IpAddress.fromString('...42.42.42.42'));
  44. assertEquals(null, goog.net.IpAddress.fromString('42.42.42.-0'));
  45. assertEquals(null, goog.net.IpAddress.fromString('42.42.42.+0'));
  46. assertEquals(null, goog.net.IpAddress.fromString('.'));
  47. assertEquals(null, goog.net.IpAddress.fromString('...'));
  48. assertEquals(null, goog.net.IpAddress.fromString('bogus'));
  49. assertEquals(null, goog.net.IpAddress.fromString('bogus.com'));
  50. assertEquals(null, goog.net.IpAddress.fromString('192.168.0.1.com'));
  51. assertEquals(
  52. null, goog.net.IpAddress.fromString('12345.67899.-54321.-98765'));
  53. assertEquals(null, goog.net.IpAddress.fromString('257.0.0.0'));
  54. assertEquals(null, goog.net.IpAddress.fromString('42.42.42.-42'));
  55. assertEquals(null, goog.net.IpAddress.fromString('3ff3:::1'));
  56. assertEquals(null, goog.net.IpAddress.fromString('3ffe::1.net'));
  57. assertEquals(null, goog.net.IpAddress.fromString('3ffe::1::1'));
  58. assertEquals(null, goog.net.IpAddress.fromString('1::2::3::4:5'));
  59. assertEquals(null, goog.net.IpAddress.fromString('::7:6:5:4:3:2:'));
  60. assertEquals(null, goog.net.IpAddress.fromString(':6:5:4:3:2:1::'));
  61. assertEquals(null, goog.net.IpAddress.fromString('2001::db:::1'));
  62. assertEquals(null, goog.net.IpAddress.fromString('FEDC:9878'));
  63. assertEquals(null, goog.net.IpAddress.fromString('+1.+2.+3.4'));
  64. assertEquals(null, goog.net.IpAddress.fromString('1.2.3.4e0'));
  65. assertEquals(null, goog.net.IpAddress.fromString('::7:6:5:4:3:2:1:0'));
  66. assertEquals(null, goog.net.IpAddress.fromString('7:6:5:4:3:2:1:0::'));
  67. assertEquals(null, goog.net.IpAddress.fromString('9:8:7:6:5:4:3::2:1'));
  68. assertEquals(null, goog.net.IpAddress.fromString('0:1:2:3::4:5:6:7'));
  69. assertEquals(null, goog.net.IpAddress.fromString('3ffe:0:0:0:0:0:0:0:1'));
  70. assertEquals(null, goog.net.IpAddress.fromString('3ffe::10000'));
  71. assertEquals(null, goog.net.IpAddress.fromString('3ffe::goog'));
  72. assertEquals(null, goog.net.IpAddress.fromString('3ffe::-0'));
  73. assertEquals(null, goog.net.IpAddress.fromString('3ffe::+0'));
  74. assertEquals(null, goog.net.IpAddress.fromString('3ffe::-1'));
  75. assertEquals(null, goog.net.IpAddress.fromString(':'));
  76. assertEquals(null, goog.net.IpAddress.fromString(':::'));
  77. assertEquals(null, goog.net.IpAddress.fromString('a:'));
  78. assertEquals(null, goog.net.IpAddress.fromString('::a:'));
  79. assertEquals(null, goog.net.IpAddress.fromString('0xa::'));
  80. assertEquals(null, goog.net.IpAddress.fromString('::1.2.3'));
  81. assertEquals(null, goog.net.IpAddress.fromString('::1.2.3.4.5'));
  82. assertEquals(null, goog.net.IpAddress.fromString('::1.2.3.4:'));
  83. assertEquals(null, goog.net.IpAddress.fromString('1.2.3.4::'));
  84. assertEquals(null, goog.net.IpAddress.fromString('2001:db8::1:'));
  85. assertEquals(null, goog.net.IpAddress.fromString(':2001:db8::1'));
  86. }
  87. function testVersion() {
  88. var ip4 = goog.net.IpAddress.fromString('1.2.3.4');
  89. assertEquals(ip4.getVersion(), 4);
  90. var ip6 = goog.net.IpAddress.fromString('2001:dead::beef:1');
  91. assertEquals(ip6.getVersion(), 6);
  92. ip6 = goog.net.IpAddress.fromString('::192.168.1.1');
  93. assertEquals(ip6.getVersion(), 6);
  94. }
  95. function testStringIpv4Address() {
  96. assertEquals(
  97. '192.168.1.1', new goog.net.Ipv4Address('192.168.1.1').toString());
  98. assertEquals('1.1.1.1', new goog.net.Ipv4Address('1.1.1.1').toString());
  99. assertEquals(
  100. '224.56.33.2', new goog.net.Ipv4Address('224.56.33.2').toString());
  101. assertEquals(
  102. '255.255.255.255',
  103. new goog.net.Ipv4Address('255.255.255.255').toString());
  104. assertEquals('0.0.0.0', new goog.net.Ipv4Address('0.0.0.0').toString());
  105. }
  106. function testIntIpv4Address() {
  107. var ip4Str = new goog.net.Ipv4Address('1.1.1.1');
  108. var ip4Int = new goog.net.Ipv4Address(new goog.math.Integer([16843009], 0));
  109. assertTrue(ip4Str.equals(ip4Int));
  110. assertEquals(ip4Str.toString(), ip4Int.toString());
  111. assertThrows(
  112. 'Ipv4(-1)',
  113. goog.partial(goog.net.Ipv4Address, goog.math.Integer.fromInt(-1)));
  114. assertThrows(
  115. 'Ipv4(2**32)',
  116. goog.partial(goog.net.Ipv4Address, goog.math.Integer.ONE.shiftLeft(32)));
  117. }
  118. function testStringIpv6Address() {
  119. assertEquals(
  120. '1:2:3:4:5:6:7:8',
  121. new goog.net.Ipv6Address('1:2:3:4:5:6:7:8').toString());
  122. assertEquals(
  123. '::1:2:3:4:5:6:7',
  124. new goog.net.Ipv6Address('::1:2:3:4:5:6:7').toString());
  125. assertEquals(
  126. '1:2:3:4:5:6:7::',
  127. new goog.net.Ipv6Address('1:2:3:4:5:6:7:0').toString());
  128. assertEquals(
  129. '2001:0:0:4::8',
  130. new goog.net.Ipv6Address('2001:0:0:4:0:0:0:8').toString());
  131. assertEquals(
  132. '2001::4:5:6:7:8',
  133. new goog.net.Ipv6Address('2001:0:0:4:5:6:7:8').toString());
  134. assertEquals(
  135. '2001::3:4:5:6:7:8',
  136. new goog.net.Ipv6Address('2001:0:3:4:5:6:7:8').toString());
  137. assertEquals(
  138. '0:0:3::ffff', new goog.net.Ipv6Address('0:0:3:0:0:0:0:ffff').toString());
  139. assertEquals(
  140. '::4:0:0:0:ffff',
  141. new goog.net.Ipv6Address('0:0:0:4:0:0:0:ffff').toString());
  142. assertEquals(
  143. '::5:0:0:ffff',
  144. new goog.net.Ipv6Address('0:0:0:0:5:0:0:ffff').toString());
  145. assertEquals(
  146. '1::4:0:0:7:8', new goog.net.Ipv6Address('1:0:0:4:0:0:7:8').toString());
  147. assertEquals('::', new goog.net.Ipv6Address('0:0:0:0:0:0:0:0').toString());
  148. assertEquals('::1', new goog.net.Ipv6Address('0:0:0:0:0:0:0:1').toString());
  149. assertEquals(
  150. '2001:658:22a:cafe::',
  151. new goog.net.Ipv6Address('2001:0658:022a:cafe:0000:0000:0000:0000')
  152. .toString());
  153. assertEquals('::102:304', new goog.net.Ipv6Address('::1.2.3.4').toString());
  154. assertEquals(
  155. '::ffff:303:303', new goog.net.Ipv6Address('::ffff:3.3.3.3').toString());
  156. assertEquals(
  157. '::ffff:ffff', new goog.net.Ipv6Address('::255.255.255.255').toString());
  158. }
  159. function testIntIpv6Address() {
  160. var ip6Str = new goog.net.Ipv6Address('2001::dead:beef:1');
  161. var ip6Int = new goog.net.Ipv6Address(
  162. new goog.math.Integer([3203334145, 57005, 0, 536936448], 0));
  163. assertTrue(ip6Str.equals(ip6Int));
  164. assertEquals(ip6Str.toString(), ip6Int.toString());
  165. assertThrows(
  166. 'Ipv6(-1)',
  167. goog.partial(goog.net.Ipv6Address, goog.math.Integer.fromInt(-1)));
  168. assertThrows(
  169. 'Ipv6(2**128)',
  170. goog.partial(goog.net.Ipv6Address, goog.math.Integer.ONE.shiftLeft(128)));
  171. }
  172. function testDottedQuadIpv6() {
  173. new goog.net.Ipv6Address('7::0.128.0.127');
  174. new goog.net.Ipv6Address('7::0.128.0.128');
  175. new goog.net.Ipv6Address('7::128.128.0.127');
  176. new goog.net.Ipv6Address('7::0.128.128.127');
  177. }
  178. function testMappedIpv4Address() {
  179. var testAddresses = ['::ffff:1.2.3.4', '::FFFF:102:304'];
  180. var ipv4Str = '1.2.3.4';
  181. var ip1 = new goog.net.Ipv6Address(testAddresses[0]);
  182. var ip2 = new goog.net.Ipv6Address(testAddresses[1]);
  183. var ipv4 = new goog.net.Ipv4Address(ipv4Str);
  184. assertTrue(ip1.isMappedIpv4Address());
  185. assertTrue(ip2.isMappedIpv4Address());
  186. assertTrue(ip1.equals(ip2));
  187. assertTrue(ipv4.equals(ip1.getMappedIpv4Address()));
  188. assertTrue(ipv4.equals(ip2.getMappedIpv4Address()));
  189. }
  190. function testUriString() {
  191. var ip4Str = '192.168.1.1';
  192. var ip4Uri = goog.net.IpAddress.fromUriString(ip4Str);
  193. var ip4 = goog.net.IpAddress.fromString(ip4Str);
  194. assertTrue(ip4Uri.equals(ip4));
  195. var ip6Str = '2001:dead::beef:1';
  196. assertEquals(null, goog.net.IpAddress.fromUriString(ip6Str));
  197. var ip6Uri = goog.net.IpAddress.fromUriString('[' + ip6Str + ']');
  198. var ip6 = goog.net.IpAddress.fromString(ip6Str);
  199. assertTrue(ip6Uri.equals(ip6));
  200. assertEquals(ip6Uri.toString(), ip6Str);
  201. assertEquals(ip6Uri.toUriString(), '[' + ip6Str + ']');
  202. }
  203. function testIsSiteLocal() {
  204. var siteLocalAddresses = [
  205. '10.0.0.0', '10.255.255.255', '172.16.0.0', '172.31.255.255', '192.168.0.0',
  206. '192.168.255.255', 'fd00::', 'fdff:ffff:ffff:ffff:ffff:ffff:ffff:ffff'
  207. ];
  208. goog.array.forEach(siteLocalAddresses, function(siteLocalAddress) {
  209. assertTrue(goog.net.IpAddress.fromString(siteLocalAddress).isSiteLocal());
  210. });
  211. var nonSiteLocalAddresses = [
  212. '9.255.255.255', '11.0.0.0', '172.15.255.255', '172.32.0.0',
  213. '192.167.255.255', '192.169.0.0', 'fcff:ffff:ffff:ffff:ffff:ffff:ffff:ffff',
  214. 'fe00::'
  215. ];
  216. goog.array.forEach(nonSiteLocalAddresses, function(nonSiteLocalAddress) {
  217. assertFalse(
  218. goog.net.IpAddress.fromString(nonSiteLocalAddress).isSiteLocal());
  219. });
  220. }
  221. function testIsLinkLocal() {
  222. var linkLocalAddresses = [
  223. '169.254.0.0', '169.254.255.255', 'fe80::',
  224. 'febf:ffff:ffff:ffff:ffff:ffff:ffff:ffff'
  225. ];
  226. goog.array.forEach(linkLocalAddresses, function(linkLocalAddress) {
  227. assertTrue(goog.net.IpAddress.fromString(linkLocalAddress).isLinkLocal());
  228. });
  229. var nonLinkLocalAddresses = [
  230. '169.253.255.255', '169.255.0.0', 'fe7f:ffff:ffff:ffff:ffff:ffff:ffff:ffff',
  231. 'fec0::'
  232. ];
  233. goog.array.forEach(nonLinkLocalAddresses, function(nonLinkLocalAddress) {
  234. assertFalse(
  235. goog.net.IpAddress.fromString(nonLinkLocalAddress).isLinkLocal());
  236. });
  237. }