Seppo.Social -> Seppo.mro.name
[Seppo.git] / test / t_ban.ml
blob6c516c30527bc642eb86c8ded99d95f153c763c7
1 (*
2 * _ _ ____ _
3 * _| || |_/ ___| ___ _ __ _ __ ___ | |
4 * |_ .. _\___ \ / _ \ '_ \| '_ \ / _ \| |
5 * |_ _|___) | __/ |_) | |_) | (_) |_|
6 * |_||_| |____/ \___| .__/| .__/ \___/(_)
7 * |_| |_|
9 * Personal Social Web.
11 * Copyright (C) The #Seppo contributors. All rights reserved.
13 * This program is free software: you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation, either version 3 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27 open Seppo_lib
29 let test_scanf () =
30 Logr.info (fun m -> m "test_scanf");
31 (let tup2 a b = (a, b) in
32 (let c, t = Scanf.sscanf "4 2022-04-06T14:55:17Z" "%i %s" tup2 in
33 Assrt.equals_string __LOC__ "2022-04-06T14:55:17Z" t;
34 Assrt.equals_int __LOC__ 4 c);
35 try
36 let _ = Scanf.sscanf "fail 2022-04-06T14:55:17Z" "%i %s" tup2 in
37 assert false
38 with Scanf.Scan_failure _msg ->
39 Assrt.equals_string __LOC__
40 "scanf: bad input at char number 0: character 'f' is not a decimal digit"
41 _msg);
42 assert true
44 let test_hash () =
45 Logr.info (fun m -> m "test_hash");
46 let lut = Hashtbl.create 20 in
47 Hashtbl.add lut "127.0.0.1" (1, Ptime.epoch);
48 try
49 match Hashtbl.find lut "127.0.0.1" with
50 | bad, tim ->
51 assert (bad = 1);
52 assert (tim = Ptime.epoch)
53 with Not_found -> assert false
55 let t_of_sexp sx =
56 match sx with
57 | Ok
58 (Sexplib0.Sexp.List
60 Sexplib0.Sexp.Atom adr_;
61 Sexplib0.Sexp.Atom sev_;
62 Sexplib0.Sexp.Atom tim_;
63 ]) ->
65 ( adr_,
66 int_of_string sev_,
67 Option.value ~default:Ptime.epoch
68 (Ptime.of_float_s (float_of_string tim_)) )
69 | _ -> Error "Cannot parse Csexp"
71 let sexp_of_t (adr, sev, tim) =
72 Sexplib0.Sexp.List
74 Sexplib0.Sexp.Atom adr;
75 Sexplib0.Sexp.Atom (string_of_int sev);
76 Sexplib0.Sexp.Atom (string_of_float (Ptime.to_float_s tim));
79 module Csexp = Csexp.Make (Sexplib0.Sexp)
81 let test_csexp () =
82 Logr.info (fun m -> m "test_csexp");
83 let ban =
84 ( "127.0.0.1",
86 Option.value ~default:Ptime.epoch (Ptime.of_float_s 1605912887.3501091) )
89 let s = sexp_of_t ban |> Csexp.to_string in
90 Assrt.equals_string __LOC__ "(9:127.0.0.11:413:1605912887.35)" s;
92 match s |> Csexp.parse_string |> t_of_sexp with
93 | Ok (adr', sev', tim') ->
94 let adr, sev, tim = ban in
95 Assrt.equals_string __LOC__ adr adr';
96 Assrt.equals_int __LOC__ sev sev';
97 Assrt.equals_float __LOC__ (Ptime.to_float_s tim) (Ptime.to_float_s tim')
98 1e-3
99 | _ -> assert false
102 let _test_seq () =
103 Logr.info (fun m -> m "test_seq");
104 Printf.sprintf "%.0f" 1.0
105 |> Assrt.equals_string __LOC__ "1";
106 let fn = "tmp/seq.cdb" in
107 (try
108 Unix.mkdir "tmp" File.pDir;
109 with Unix.Unix_error (Unix.EEXIST, _, _) -> ());
110 (try
111 Unix.unlink fn;
112 Unix.unlink (fn ^ "~")
113 with Unix.Unix_error (Unix.ENOENT, _, _) -> ());
114 File.touch fn;
115 let cdb = Mapcdb.Cdb fn
116 and k = "127.0.0.1"
117 and t0 = Ptime.epoch in
118 let f x = x *. Ban.chunk_s |> Ptime.Span.of_float_s |> Option.get |> Ptime.add_span t0 |> Option.get in
119 let t1 = f 0.5 in
120 let _t2 = f 1.5 in
121 let _t3 = f 2.5 in
122 let t4 = f 3.5 in
123 let t5 = f 4.5 in
124 Logr.info (fun m -> m "test_seq 1");
125 Assrt.equals_none __LOC__ (Ban.check cdb t0 k);
126 Logr.info (fun m -> m "test_seq 10");
127 Ban.escalate cdb t1 k;
128 Mapcdb.find_string_opt k cdb
129 |> Option.value ~default:"fail"
130 |> Assrt.equals_string __LOC__ "1970-01-01T00:15:00-00:00";
131 Assrt.equals_none __LOC__ (Ban.check cdb t1 k);
132 Ban.escalate cdb t1 k;
133 Ban.escalate cdb t1 k;
134 Ban.escalate cdb t1 k;
135 Ban.check cdb t1 k |> Option.get |> Ptime.to_rfc3339
136 |> Assrt.equals_string __LOC__ "1970-01-01T00:25:00-00:00";
137 Ban.escalate cdb t4 k;
138 Assrt.equals_none __LOC__ (Ban.check cdb t5 k);
139 assert true
141 let () =
142 Unix.chdir "../../../test/";
143 test_scanf ();
144 test_hash ();
145 test_csexp ();
146 (* test_seq (); *)
147 assert true