File: test_stringprep.ml

package info (click to toggle)
camomile 2.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 39,888 kB
  • sloc: ml: 14,026; xml: 230; makefile: 22
file content (126 lines) | stat: -rw-r--r-- 3,432 bytes parent folder | download
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
(* Copyright 2010 Pierre Chambart *)

open Blender
open Camomile
module M = StringPrep.Make (UTF8)
open M

let char_of_string s = UChar.chr_of_uint (int_of_string ("0x" ^ s))
let type_pat = Str.regexp "[ \\t]*\\([EDBP]\\);"

let profile_pat =
  Str.regexp "[ \\t]*\\(node\\|name\\|res\\|sasl\\|trace\\|iscsi\\|mib\\);"

let val_pat = Str.regexp "[ \\t]*\\([0-9a-fA-F]+\\)"
let sep_pat = Str.regexp "[ \\t]*;"

type test =
  | E of (UTF8.t * UTF8.t)
  | D of (UTF8.t * UTF8.t)
  | B of UTF8.t
  | P of UTF8.t

let text_of_uchars l =
  let b = UTF8.Buf.create 0 in
  List.iter (fun x -> UTF8.Buf.add_char b x) l;
  UTF8.Buf.contents b

let parse_single s p =
  let rec f p =
    if Str.string_match sep_pat s p then []
    else if Str.string_match val_pat s p then (
      let c = char_of_string (Str.matched_group 1 s) in
      c :: f (Str.match_end ()))
    else failwith "parse error"
  in
  text_of_uchars (f p)

let parse_couple s p =
  let s1 = parse_single s p in
  let p = Str.match_end () in
  let s2 = parse_single s p in
  (s1, s2)

let parse_type s p =
  if Str.string_match type_pat s p then (
    match Str.matched_group 1 s with
      | "E" -> E (parse_couple s (Str.match_end ()))
      | "D" -> D (parse_couple s (Str.match_end ()))
      | "B" -> B (parse_single s (Str.match_end ()))
      | "P" -> P (parse_single s (Str.match_end ()))
      | _ -> failwith "wrong test type")
  else failwith "parse test error"

let profile_of_string = function
  | "node" -> `Nodeprep
  | "name" -> `Nameprep
  | "res" -> `Resourceprep
  | "sasl" -> `Saslprep
  | "trace" -> `Trace
  | "iscsi" -> `Iscsi
  | "mib" -> `Mib
  | _ -> failwith "profile parse error"

let parse_line s =
  if Str.string_match profile_pat s 0 then
    Some
      (let profile = profile_of_string (Str.matched_group 1 s) in
       let p = Str.match_end () in
       let t = parse_type s p in
       (profile, t))
  else None

let check_line n s =
  match parse_line s with
    | None -> 0
    | Some (profile, t) ->
        (match t with
          | E (s1, s2) ->
              expect_equal
                ~msg:(lazy ("line: " ^ string_of_int n))
                (stringprep profile s1) (stringprep profile s2)
          | D (s1, s2) ->
              expect_true
                ~msg:(lazy ("line: " ^ string_of_int n))
                (stringprep profile s1 <> stringprep profile s2)
          | B s ->
              expect_true
                ~msg:(lazy ("line: " ^ string_of_int n))
                (try
                   ignore (stringprep profile s);
                   false
                 with
                  | Bad_bidi -> true
                  | _ -> false)
          | P s ->
              expect_true
                ~msg:(lazy ("line: " ^ string_of_int n))
                (try
                   ignore (stringprep profile s);
                   false
                 with
                  | Prohibited _ -> true
                  | _ -> false));
        1

exception Ok of string

let check_file f =
  let c = open_in_bin f in
  let rec check line n =
    try raise (Ok (input_line c)) with
      | End_of_file -> n
      | Ok s ->
          let n = n + check_line line s in
          check (line + 1) n
  in
  let _ = check 1 0 in
  close_in c

let testfile = input_filename "data/stringprep"

let _ =
  test ~desc:"stringprep" ~body:(fun () ->
      expect_pass ~body:(fun () -> check_file testfile))

let () = Blender.main ()