validator_test.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. var validator = require('utils/validator');
  19. describe('validator', function () {
  20. describe('#isValidEmail(value)', function () {
  21. it('should return false if value is null', function () {
  22. expect(validator.isValidEmail(null)).to.equal(false);
  23. })
  24. it('should return false if value is ""', function () {
  25. expect(validator.isValidEmail('')).to.equal(false);
  26. })
  27. it('should return false if value is "a.com"', function () {
  28. expect(validator.isValidEmail('a.com')).to.equal(false);
  29. })
  30. it('should return false if value is "@a.com"', function () {
  31. expect(validator.isValidEmail('@a.com')).to.equal(false);
  32. })
  33. it('should return false if value is "a@.com"', function () {
  34. expect(validator.isValidEmail('a@.com')).to.equal(false);
  35. })
  36. it('should return true if value is "a@a.com"', function () {
  37. expect(validator.isValidEmail('a@a.com')).to.equal(true);
  38. })
  39. it('should return true if value is "user@a.b.com"', function () {
  40. expect(validator.isValidEmail('user@a.b.com')).to.equal(true);
  41. })
  42. })
  43. describe('#isValidInt(value)', function () {
  44. it('should return false if value is null', function () {
  45. expect(validator.isValidInt(null)).to.equal(false);
  46. })
  47. it('should return false if value is ""', function () {
  48. expect(validator.isValidInt('')).to.equal(false);
  49. })
  50. it('should return false if value is "abc"', function () {
  51. expect(validator.isValidInt('abc')).to.equal(false);
  52. })
  53. it('should return false if value is "0xff"', function () {
  54. expect(validator.isValidInt('0xff')).to.equal(false);
  55. })
  56. it('should return false if value is " 1""', function () {
  57. expect(validator.isValidInt(' 1')).to.equal(false);
  58. })
  59. it('should return false if value is "1 "', function () {
  60. expect(validator.isValidInt('1 ')).to.equal(false);
  61. })
  62. it('should return true if value is "10"', function () {
  63. expect(validator.isValidInt('10')).to.equal(true);
  64. })
  65. it('should return true if value is "-123"', function () {
  66. expect(validator.isValidInt('-123')).to.equal(true);
  67. })
  68. it('should return true if value is "0"', function () {
  69. expect(validator.isValidInt('0')).to.equal(true);
  70. })
  71. it('should return true if value is 10', function () {
  72. expect(validator.isValidInt(10)).to.equal(true);
  73. })
  74. it('should return true if value is -123', function () {
  75. expect(validator.isValidInt(10)).to.equal(true);
  76. })
  77. it('should return true if value is 0', function () {
  78. expect(validator.isValidInt(10)).to.equal(true);
  79. })
  80. })
  81. describe('#isValidFloat(value)', function () {
  82. it('should return false if value is null', function () {
  83. expect(validator.isValidFloat(null)).to.equal(false);
  84. })
  85. it('should return false if value is ""', function () {
  86. expect(validator.isValidFloat('')).to.equal(false);
  87. })
  88. it('should return false if value is "abc"', function () {
  89. expect(validator.isValidFloat('abc')).to.equal(false);
  90. })
  91. it('should return false if value is "0xff"', function () {
  92. expect(validator.isValidFloat('0xff')).to.equal(false);
  93. })
  94. it('should return false if value is " 1""', function () {
  95. expect(validator.isValidFloat(' 1')).to.equal(false);
  96. })
  97. it('should return false if value is "1 "', function () {
  98. expect(validator.isValidFloat('1 ')).to.equal(false);
  99. })
  100. it('should return true if value is "10"', function () {
  101. expect(validator.isValidFloat('10')).to.equal(true);
  102. })
  103. it('should return true if value is "-123"', function () {
  104. expect(validator.isValidFloat('-123')).to.equal(true);
  105. })
  106. it('should return true if value is "0"', function () {
  107. expect(validator.isValidFloat('0')).to.equal(true);
  108. })
  109. it('should return true if value is 10', function () {
  110. expect(validator.isValidFloat(10)).to.equal(true);
  111. })
  112. it('should return true if value is -123', function () {
  113. expect(validator.isValidFloat(10)).to.equal(true);
  114. })
  115. it('should return true if value is 0', function () {
  116. expect(validator.isValidFloat(10)).to.equal(true);
  117. })
  118. it('should return true if value is "0.0"', function () {
  119. expect(validator.isValidFloat("0.0")).to.equal(true);
  120. })
  121. it('should return true if value is "10.123"', function () {
  122. expect(validator.isValidFloat("10.123")).to.equal(true);
  123. })
  124. it('should return true if value is "-10.123"', function () {
  125. expect(validator.isValidFloat("-10.123")).to.equal(true);
  126. })
  127. it('should return true if value is 10.123', function () {
  128. expect(validator.isValidFloat(10.123)).to.equal(true);
  129. })
  130. it('should return true if value is -10.123', function () {
  131. expect(validator.isValidFloat(-10.123)).to.equal(true);
  132. })
  133. })
  134. /*describe('#isIpAddress(value)', function () {
  135. it('"127.0.0.1" - valid IP', function () {
  136. expect(validator.isIpAddress('127.0.0.1')).to.equal(true);
  137. })
  138. it('"227.3.67.196" - valid IP', function () {
  139. expect(validator.isIpAddress('227.3.67.196')).to.equal(true);
  140. })
  141. it('"327.0.0.0" - invalid IP', function () {
  142. expect(validator.isIpAddress('327.0.0.0')).to.equal(false);
  143. })
  144. it('"127.0.0." - invalid IP', function () {
  145. expect(validator.isIpAddress('127.0.0.')).to.equal(false);
  146. })
  147. it('"127.0." - invalid IP', function () {
  148. expect(validator.isIpAddress('127.0.')).to.equal(false);
  149. })
  150. it('"127" - invalid IP', function () {
  151. expect(validator.isIpAddress('127')).to.equal(false);
  152. })
  153. it('"127.333.0.1" - invalid IP', function () {
  154. expect(validator.isIpAddress('127.333.0.1')).to.equal(false);
  155. })
  156. it('"127.0.333.1" - invalid IP', function () {
  157. expect(validator.isIpAddress('127.0.333.1')).to.equal(false);
  158. })
  159. it('"127.0.1.333" - invalid IP', function () {
  160. expect(validator.isIpAddress('127.0.1.333')).to.equal(false);
  161. })
  162. it('"127.0.0.0:45555" - valid IP', function () {
  163. expect(validator.isIpAddress('127.0.0.0:45555')).to.equal(true);
  164. })
  165. it('"327.0.0.0:45555" - invalid IP', function () {
  166. expect(validator.isIpAddress('327.0.0.0:45555')).to.equal(false);
  167. })
  168. it('"0.0.0.0" - invalid IP', function () {
  169. expect(validator.isIpAddress('0.0.0.0')).to.equal(false);
  170. })
  171. it('"0.0.0.0:12" - invalid IP', function () {
  172. expect(validator.isIpAddress('0.0.0.0:12')).to.equal(false);
  173. })
  174. it('"1.0.0.0:0" - invalid IP', function () {
  175. expect(validator.isIpAddress('1.0.0.0:0')).to.equal(false);
  176. })
  177. })*/
  178. describe('#isDomainName(value)', function () {
  179. it('"google.com" - valid Domain Name', function () {
  180. expect(validator.isDomainName('google.com')).to.equal(true);
  181. })
  182. it('"google" - invalid Domain Name', function () {
  183. expect(validator.isDomainName('google')).to.equal(false);
  184. })
  185. it('"123.123" - invalid Domain Name', function () {
  186. expect(validator.isDomainName('123.123')).to.equal(false);
  187. })
  188. it('"4goog.le" - valid Domain Name', function () {
  189. expect(validator.isDomainName('4goog.le')).to.equal(true);
  190. })
  191. it('"55454" - invalid Domain Name', function () {
  192. expect(validator.isDomainName('55454')).to.equal(false);
  193. })
  194. })
  195. describe('#isValidUserName(value)', function() {
  196. var tests = [
  197. {m:'"" - invalid',i:'',e:false},
  198. {m:'"abc123" - valid',i:'abc123',e:true},
  199. {m:'"1abc123" - invalid',i:'1abc123',e:false},
  200. {m:'"abc123$" - invalid',i:'abc123$',e:false},
  201. {m:'"~1abc123" - invalid',i: '~1abc123',e:false},
  202. {m:'"abc12345679abc1234567890abc1234567890$" - invalid',i:'abc12345679abc1234567890abc1234567890$',e:false},
  203. {m:'"1abc123$$" - invalid',i:'1abc123$$',e:false},
  204. {m:'"a" - valid',i:'a',e:true},
  205. {m:'"!" - invalid',i:'!',e:false},
  206. {m:'"root$" - invalid',i:'root$',e:false},
  207. {m:'"rootU" - invalid',i:'rootU',e:false},
  208. {m:'"rUoot" - invalid',i:'rUoot',e:false}
  209. ];
  210. tests.forEach(function(test) {
  211. it(test.m + ' ', function () {
  212. expect(validator.isValidUserName(test.i)).to.equal(test.e);
  213. })
  214. });
  215. })
  216. describe('#isValidUNIXUser(value)', function() {
  217. var tests = [
  218. {m:'"" - invalid',i:'',e:false},
  219. {m:'"abc123" - valid',i:'abc123',e:true},
  220. {m:'"1abc123" - invalid',i:'1abc123',e:false},
  221. {m:'"abc123$" - invalid',i:'abc123$',e:false},
  222. {m:'"~1abc123" - invalid',i: '~1abc123',e:false},
  223. {m:'"abc12345679abc1234567890abc1234567890$" - invalid',i:'abc12345679abc1234567890abc1234567890$',e:false},
  224. {m:'"1abc123$$" - invalid',i:'1abc123$$',e:false},
  225. {m:'"a" - valid',i:'a',e:true},
  226. {m:'"!" - invalid',i:'!',e:false},
  227. {m:'"abc_" - valid',i:'abc_',e:true},
  228. {m:'"_abc" - valid',i:'_abc',e:true},
  229. {m:'"abc_abc" - valid',i:'_abc',e:true}
  230. ];
  231. tests.forEach(function(test) {
  232. it(test.m + ' ', function () {
  233. expect(validator.isValidUNIXUser(test.i)).to.equal(test.e);
  234. })
  235. });
  236. })
  237. describe('#isValidDir(value)', function() {
  238. var tests = [
  239. {m:'"dir" - invalid',i:'dir',e:false},
  240. {m:'"/dir" - valid',i:'/dir',e:true},
  241. {m:'"/dir1,dir2" - invalid',i:'/dir1,dir2',e:false},
  242. {m:'"/dir1,/dir2" - valid',i:'/dir1,/dir2',e:true},
  243. {m:'"/123" - valid',i:'/111',e:true},
  244. {m:'"/abc" - valid',i:'/abc',e:true},
  245. {m:'"/1a2b3c" - valid',i:'/1a2b3c',e:true}
  246. ];
  247. tests.forEach(function(test) {
  248. it(test.m + ' ', function () {
  249. expect(validator.isValidDir(test.i)).to.equal(test.e);
  250. })
  251. });
  252. })
  253. describe('#isValidConfigKey(value)', function() {
  254. var tests = [
  255. {m:'"123" - valid',i:'123',e:true},
  256. {m:'"abc" - valid',i:'abc',e:true},
  257. {m:'"abc123" - valid',i:'abc123',e:true},
  258. {m:'".abc." - valid',i:'.abc.',e:true},
  259. {m:'"_abc_" - valid',i:'_abc_',e:true},
  260. {m:'"-abc-" - valid',i:'-abc-',e:true},
  261. {m:'"abc 123" - invalid',i:'abc 123',e:false},
  262. {m:'"a"b" - invalid',i:'a"b',e:false},
  263. {m:'"a\'b" - invalid',i:'a\'b',e:false}
  264. ];
  265. tests.forEach(function(test) {
  266. it(test.m + ' ', function () {
  267. expect(validator.isValidConfigKey(test.i)).to.equal(test.e);
  268. })
  269. });
  270. })
  271. })