ip.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. describe('ip', function() {
  2. beforeEach(function() {
  3. $([
  4. '<form class="form-horizontal" id="ipForm">',
  5. '<div class="form-group">',
  6. '<input type="text" name="ipv4" data-bv-ip data-bv-ip-ipv6="false" />',
  7. '</div>',
  8. '<div class="form-group">',
  9. '<input type="text" name="ipv6" data-bv-ip data-bv-ip-ipv4="false" />',
  10. '</div>',
  11. '<div class="form-group">',
  12. '<input type="text" name="both" data-bv-ip />',
  13. '</div>',
  14. '</form>'
  15. ].join('\n')).appendTo('body');
  16. $('#ipForm').bootstrapValidator();
  17. this.bv = $('#ipForm').data('bootstrapValidator');
  18. this.$ipv4 = this.bv.getFieldElements('ipv4');
  19. this.$ipv6 = this.bv.getFieldElements('ipv6');
  20. this.$both = this.bv.getFieldElements('both');
  21. });
  22. afterEach(function() {
  23. $('#ipForm').bootstrapValidator('destroy').remove();
  24. });
  25. it('Valid ipv4', function() {
  26. this.$ipv4.val('0.0.0.0');
  27. this.bv.validate();
  28. expect(this.bv.isValid()).toBeTruthy();
  29. this.bv.resetForm();
  30. this.$ipv4.val('192.168.1.1');
  31. this.bv.validate();
  32. expect(this.bv.isValid()).toBeTruthy();
  33. this.bv.resetForm();
  34. this.$ipv4.val('255.255.255.255');
  35. this.bv.validate();
  36. expect(this.bv.isValid()).toBeTruthy();
  37. });
  38. it('Invalid ipv4', function() {
  39. this.$ipv4.val('10.168.0001.100'); // extra 0 not allowed
  40. this.bv.validate();
  41. expect(this.bv.isValid()).toEqual(false);
  42. this.bv.resetForm();
  43. this.$ipv4.val('0.0.0.256'); // 256 not allowed, max is 255
  44. this.bv.validate();
  45. expect(this.bv.isValid()).toEqual(false);
  46. this.bv.resetForm();
  47. this.$ipv4.val('256.255.255.255'); // max is 255.255.255.255
  48. this.bv.validate();
  49. expect(this.bv.isValid()).toEqual(false);
  50. this.bv.resetForm();
  51. this.$ipv4.val('192.168. 224.0'); // internal space
  52. this.bv.validate();
  53. expect(this.bv.isValid()).toEqual(false);
  54. this.bv.resetForm();
  55. this.$ipv4.val('192.168.224.0 1'); // junk after valid address
  56. this.bv.validate();
  57. expect(this.bv.isValid()).toEqual(false);
  58. });
  59. it('Valid ipv6', function() {
  60. this.$ipv6.val('0000:0000:0000:0000:0000:0000:0000:0000');
  61. this.bv.validate();
  62. expect(this.bv.isValid()).toBeTruthy();
  63. this.bv.resetForm();
  64. this.$ipv6.val('fe00::1');
  65. this.bv.validate();
  66. expect(this.bv.isValid()).toBeTruthy();
  67. this.bv.resetForm();
  68. this.$ipv6.val('fe80::217:f2ff:fe07:ed62');
  69. this.bv.validate();
  70. expect(this.bv.isValid()).toBeTruthy();
  71. this.bv.resetForm();
  72. this.$ipv6.val('ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff');
  73. this.bv.validate();
  74. expect(this.bv.isValid()).toBeTruthy();
  75. });
  76. it('Invalid ipv6', function() {
  77. this.$ipv6.val('02001:0000:1234:0000:0000:C1C0:ABCD:0876'); // extra 0 not allowed
  78. this.bv.validate();
  79. expect(this.bv.isValid()).toEqual(false);
  80. this.bv.resetForm();
  81. this.$ipv6.val('2001:0000:1234:0000:00001:C1C0:ABCD:0876'); // extra 0 not allowed
  82. this.bv.validate();
  83. expect(this.bv.isValid()).toEqual(false);
  84. this.bv.resetForm();
  85. this.$ipv6.val('2001:0000:1234: 0000:0000:C1C0:ABCD:0876'); // internal space
  86. this.bv.validate();
  87. expect(this.bv.isValid()).toEqual(false);
  88. this.bv.resetForm();
  89. this.$ipv6.val('2001:0000:1234:0000:0000:C1C0:ABCD:0876 0'); // junk after valid address
  90. this.bv.validate();
  91. expect(this.bv.isValid()).toEqual(false);
  92. this.bv.resetForm();
  93. this.$ipv6.val('3ffe:0b00:0000:0001:0000:0000:000a'); // seven segment
  94. this.bv.validate();
  95. expect(this.bv.isValid()).toEqual(false);
  96. this.bv.resetForm();
  97. this.$ipv6.val('FF02:0000:0000:0000:0000:0000:0000:0000:0001'); // nine segment
  98. this.bv.validate();
  99. expect(this.bv.isValid()).toEqual(false);
  100. this.bv.resetForm();
  101. this.$ipv6.val('::1111:2222:3333:4444:5555:6666::'); // double "::"
  102. this.bv.validate();
  103. expect(this.bv.isValid()).toEqual(false);
  104. this.bv.resetForm();
  105. this.$ipv6.val('3ffe:b00::1::a'); // double "::"
  106. this.bv.validate();
  107. expect(this.bv.isValid()).toEqual(false);
  108. });
  109. it('Both', function() {
  110. this.$both.val('255.255.255.255'); // valid
  111. this.bv.validate();
  112. expect(this.bv.isValid()).toBeTruthy();
  113. this.bv.resetForm();
  114. this.$both.val('256.0.0.0'); // 256 not allowed, max is 255
  115. this.bv.validate();
  116. expect(this.bv.isValid()).toEqual(false);
  117. this.bv.resetForm();
  118. this.$both.val('2001:0db8:0000:85a3:0000:0000:ac1f:8001'); // valid
  119. this.bv.validate();
  120. expect(this.bv.isValid()).toBeTruthy();
  121. this.bv.resetForm();
  122. this.$both.val('2001:0000:1234:0000:0000:C1C0:ABCD:0876 0'); // junk after valid address
  123. this.bv.validate();
  124. expect(this.bv.isValid()).toEqual(false);
  125. });
  126. });