summaryrefslogtreecommitdiff
path: root/test/value_checker_test.ml
blob: da71cb2c9ed4dba8b144f1a366f3de60675d4497 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
open OUnit2

module VC = Vyos1x.Value_checker

let get_dir test_ctxt = in_testdata_dir test_ctxt ["validators"]

let buf = Buffer.create 4096

let raises_bad_validator f =
    try ignore @@ f (); false
    with VC.Bad_validator _ -> true

let test_check_regex_valid test_ctxt =
    let c = VC.Regex "[a-z]+" in
    let v = "fgsfds" in
    assert_equal (VC.validate_value (get_dir test_ctxt) buf c v) true

let test_check_regex_invalid test_ctxt =
    let c = VC.Regex "[a-z]+" in
    let v = "FGSFDS" in
    assert_equal (VC.validate_value (get_dir test_ctxt) buf c v) false

let test_check_external_valid test_ctxt =
    let c = VC.External ("anything", None) in
    let v = "fgsfds" in
    assert_equal (VC.validate_value (get_dir test_ctxt) buf c v) true

let test_check_external_invalid test_ctxt =
    let	c = VC.External ("nothing", None) in
    let	v = "fgsfds" in
    assert_equal (VC.validate_value (get_dir test_ctxt) buf c v) false

let test_check_external_bad_validator test_ctxt =
    let c = VC.External ("invalid", None) in
    let v = "fgsfds" in
    assert_bool "Invalid validator was executed successfully"
      (raises_bad_validator (fun () -> VC.validate_value (get_dir test_ctxt) buf c v))

let test_validate_any_valid test_ctxt =
    let cs = [VC.Regex "\\d+"; VC.Regex "[a-z]+"; VC.External ("anything", None)] in
    assert_equal (VC.validate_any (get_dir test_ctxt) cs "AAAA") None

let test_validate_any_invalid test_ctxt =
    let cs = [VC.Regex "\\d+"; VC.Regex "[a-z]+"] in
    assert_equal (VC.validate_any (get_dir test_ctxt) cs "AAAA") None

let test_validate_any_no_constraints test_ctxt =
    let cs = [] in
    assert_equal (VC.validate_any (get_dir test_ctxt) cs "foo") None

let suite =
    "VyConf value checker tests" >::: [
        "test_check_regex_valid" >:: test_check_regex_valid;
        "test_check_regex_invalid" >:: test_check_regex_invalid;
        "test_check_external_valid" >:: test_check_external_valid;
        "test_check_external_invalid" >:: test_check_external_invalid;
        "test_check_external_bad_validator" >:: test_check_external_bad_validator;
        "test_validate_any_valid" >:: test_validate_any_valid;
        "test_validate_any_invalid" >:: test_validate_any_invalid;
        "test_validate_any_no_constraints" >:: test_validate_any_no_constraints;
    ]

let () =
  run_test_tt_main suite