validator_test.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. });
  169. describe('#isDomainName(value)', function () {
  170. it('"google.com" - valid Domain Name', function () {
  171. expect(validator.isDomainName('google.com')).to.equal(true);
  172. });
  173. it('"google" - invalid Domain Name', function () {
  174. expect(validator.isDomainName('google')).to.equal(false);
  175. });
  176. it('"123.123" - invalid Domain Name', function () {
  177. expect(validator.isDomainName('123.123')).to.equal(false);
  178. });
  179. it('"4goog.le" - valid Domain Name', function () {
  180. expect(validator.isDomainName('4goog.le')).to.equal(true);
  181. });
  182. it('"55454" - invalid Domain Name', function () {
  183. expect(validator.isDomainName('55454')).to.equal(false);
  184. })
  185. });
  186. describe('#hasSpaces()', function(){
  187. var testable = [
  188. { str: ' hello', detect: true },
  189. { str: 'hello world', detect: true },
  190. { str: 'hello ', detect: true },
  191. { str: 'hello', detect: false }
  192. ];
  193. testable.forEach(function(value){
  194. it('should ' + (value.detect ? '' : 'not') + ' detects spaces in `' + value.str + '`', function(){
  195. expect(validator.hasSpaces(value.str)).to.eql(value.detect);
  196. });
  197. });
  198. });
  199. describe('#isNotTrimmed', function(){
  200. var testable = [
  201. { str: ' hello world', detect: true },
  202. { str: ' hello world ', detect: true },
  203. { str: 'hello world ', detect: true },
  204. { str: 'hello world', detect: false },
  205. { str: 'hello world !', detect: false }
  206. ];
  207. testable.forEach(function(value){
  208. it('should ' + (value.detect ? '' : 'not') + 'trimmed string', function() {
  209. expect(validator.isNotTrimmed(value.str)).to.eql(value.detect);
  210. });
  211. });
  212. });
  213. describe('#empty()', function(){
  214. var testable = [
  215. { obj: "", detect: true },
  216. { obj: 0, detect: true },
  217. { obj: "0", detect: true },
  218. { obj: null, detect: true },
  219. { obj: undefined, detect: true },
  220. { obj: 'hello', detect: false },
  221. { obj: {}, detect: false },
  222. { obj: [], detect: false },
  223. { obj: ['a'], detect: false },
  224. { obj: 1, detect: false },
  225. { obj: true, detect: false }
  226. ];
  227. testable.forEach(function(value){
  228. it('should ' + (value.detect ? '' : 'not') + ' detect empty value in `' + new String(value.obj) + '`', function(){
  229. expect(validator.empty(value.obj)).to.eql(value.detect);
  230. });
  231. });
  232. });
  233. describe('#isValidUserName(value)', function() {
  234. var tests = [
  235. {m:'"" - invalid',i:'',e:false},
  236. {m:'"abc123" - valid',i:'abc123',e:true},
  237. {m:'"1abc123" - invalid',i:'1abc123',e:false},
  238. {m:'"abc123$" - invalid',i:'abc123$',e:false},
  239. {m:'"~1abc123" - invalid',i: '~1abc123',e:false},
  240. {m:'"abc12345679abc1234567890abc1234567890$" - invalid',i:'abc12345679abc1234567890abc1234567890$',e:false},
  241. {m:'"1abc123$$" - invalid',i:'1abc123$$',e:false},
  242. {m:'"a" - valid',i:'a',e:true},
  243. {m:'"!" - invalid',i:'!',e:false},
  244. {m:'"root$" - invalid',i:'root$',e:false},
  245. {m:'"rootU" - invalid',i:'rootU',e:false},
  246. {m:'"rUoot" - invalid',i:'rUoot',e:false}
  247. ];
  248. tests.forEach(function(test) {
  249. it(test.m + ' ', function () {
  250. expect(validator.isValidUserName(test.i)).to.equal(test.e);
  251. })
  252. });
  253. });
  254. describe('#isValidUNIXUser(value)', function() {
  255. var tests = [
  256. {m:'"" - invalid',i:'',e:false},
  257. {m:'"abc123" - valid',i:'abc123',e:true},
  258. {m:'"1abc123" - invalid',i:'1abc123',e:false},
  259. {m:'"abc123$" - invalid',i:'abc123$',e:false},
  260. {m:'"~1abc123" - invalid',i: '~1abc123',e:false},
  261. {m:'"abc12345679abc1234567890abc1234567890$" - invalid',i:'abc12345679abc1234567890abc1234567890$',e:false},
  262. {m:'"1abc123$$" - invalid',i:'1abc123$$',e:false},
  263. {m:'"a" - valid',i:'a',e:true},
  264. {m:'"!" - invalid',i:'!',e:false},
  265. {m:'"abc_" - valid',i:'abc_',e:true},
  266. {m:'"_abc" - valid',i:'_abc',e:true},
  267. {m:'"abc_abc" - valid',i:'_abc',e:true}
  268. ];
  269. tests.forEach(function(test) {
  270. it(test.m + ' ', function () {
  271. expect(validator.isValidUNIXUser(test.i)).to.equal(test.e);
  272. })
  273. });
  274. });
  275. describe('#isValidDir(value)', function() {
  276. var tests = [
  277. {m:'"dir" - invalid',i:'dir',e:false},
  278. {m:'"/dir" - valid',i:'/dir',e:true},
  279. {m:'"/dir1,dir2" - invalid',i:'/dir1,dir2',e:false},
  280. {m:'"/dir1,/dir2" - valid',i:'/dir1,/dir2',e:true},
  281. {m:'"/123" - valid',i:'/111',e:true},
  282. {m:'"/abc" - valid',i:'/abc',e:true},
  283. {m:'"/1a2b3c" - valid',i:'/1a2b3c',e:true}
  284. ];
  285. tests.forEach(function(test) {
  286. it(test.m + ' ', function () {
  287. expect(validator.isValidDir(test.i)).to.equal(test.e);
  288. })
  289. });
  290. });
  291. describe('#isAllowedDir(value)', function() {
  292. var tests = [
  293. {m:'"/home" - not allowed',i:'/home',e:false},
  294. {m:'"/homes" - not allowed',i:'/homes',e:false},
  295. {m:'"/home/" - not allowed',i:'/home/',e:false},
  296. {m:'"/homes/" - not allowed',i:'/homes/',e:false},
  297. {m:'"/dir" - allowed',i:'/dir',e:true},
  298. {m:'"/dir/home" - allowed',i:'/dir/home',e:true},
  299. {m:'"/dir/homes" - allowed',i:'/dir/homes',e:true},
  300. {m:'"/dir/home/" - allowed',i:'/dir/home/',e:true},
  301. {m:'"/dir/homes/" - allowed',i:'/dir/homes/',e:true}
  302. ];
  303. tests.forEach(function(test) {
  304. it(test.m + ' ', function () {
  305. expect(validator.isAllowedDir(test.i)).to.equal(test.e);
  306. })
  307. });
  308. });
  309. describe('#isValidConfigKey(value)', function() {
  310. var tests = [
  311. {m:'"123" - valid',i:'123',e:true},
  312. {m:'"abc" - valid',i:'abc',e:true},
  313. {m:'"abc123" - valid',i:'abc123',e:true},
  314. {m:'".abc." - valid',i:'.abc.',e:true},
  315. {m:'"_abc_" - valid',i:'_abc_',e:true},
  316. {m:'"-abc-" - valid',i:'-abc-',e:true},
  317. {m:'"abc 123" - invalid',i:'abc 123',e:false},
  318. {m:'"a"b" - invalid',i:'a"b',e:false},
  319. {m:'"a\'b" - invalid',i:'a\'b',e:false}
  320. ];
  321. tests.forEach(function(test) {
  322. it(test.m + ' ', function () {
  323. expect(validator.isValidConfigKey(test.i)).to.equal(test.e);
  324. })
  325. });
  326. })
  327. });