ban_test.ml 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. (*
  2. * _ _ ____ _
  3. * _| || |_/ ___| ___ _ __ _ __ ___ | |
  4. * |_ .. _\___ \ / _ \ '_ \| '_ \ / _ \| |
  5. * |_ _|___) | __/ |_) | |_) | (_) |_|
  6. * |_||_| |____/ \___| .__/| .__/ \___/(_)
  7. * |_| |_|
  8. *
  9. * Personal Social Web.
  10. *
  11. * Copyright (C) The #Seppo contributors. All rights reserved.
  12. *
  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.
  17. *
  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.
  22. *
  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/>.
  25. *)
  26. open Seppo_lib
  27. let test_scanf () =
  28. Logr.info (fun m -> m "test_scanf");
  29. (let tup2 a b = (a, b) in
  30. (let c, t = Scanf.sscanf "4 2022-04-06T14:55:17Z" "%i %s" tup2 in
  31. Assrt.equals_string __LOC__ "2022-04-06T14:55:17Z" t;
  32. Assrt.equals_int __LOC__ 4 c);
  33. try
  34. let _ = Scanf.sscanf "fail 2022-04-06T14:55:17Z" "%i %s" tup2 in
  35. assert false
  36. with Scanf.Scan_failure _msg ->
  37. Assrt.equals_string __LOC__
  38. "scanf: bad input at char number 0: character 'f' is not a decimal digit"
  39. _msg);
  40. assert true
  41. let test_hash () =
  42. Logr.info (fun m -> m "test_hash");
  43. let lut = Hashtbl.create 20 in
  44. Hashtbl.add lut "127.0.0.1" (1, Ptime.epoch);
  45. try
  46. match Hashtbl.find lut "127.0.0.1" with
  47. | bad, tim ->
  48. assert (bad = 1);
  49. assert (tim = Ptime.epoch)
  50. with Not_found -> assert false
  51. let t_of_sexp sx =
  52. match sx with
  53. | Ok
  54. (Sexplib0.Sexp.List
  55. [
  56. Sexplib0.Sexp.Atom adr_;
  57. Sexplib0.Sexp.Atom sev_;
  58. Sexplib0.Sexp.Atom tim_;
  59. ]) ->
  60. Ok
  61. ( adr_,
  62. int_of_string sev_,
  63. Option.value ~default:Ptime.epoch
  64. (Ptime.of_float_s (float_of_string tim_)) )
  65. | _ -> Error "Cannot parse Csexp"
  66. let sexp_of_t (adr, sev, tim) =
  67. Sexplib0.Sexp.List
  68. [
  69. Sexplib0.Sexp.Atom adr;
  70. Sexplib0.Sexp.Atom (string_of_int sev);
  71. Sexplib0.Sexp.Atom (string_of_float (Ptime.to_float_s tim));
  72. ]
  73. module Csexp = Csexp.Make (Sexplib0.Sexp)
  74. let test_csexp () =
  75. Logr.info (fun m -> m "test_csexp");
  76. let ban =
  77. ( "127.0.0.1",
  78. 4,
  79. Option.value ~default:Ptime.epoch (Ptime.of_float_s 1605912887.3501091) )
  80. in
  81. let s = sexp_of_t ban |> Csexp.to_string in
  82. Assrt.equals_string __LOC__ "(9:127.0.0.11:413:1605912887.35)" s;
  83. match s |> Csexp.parse_string |> t_of_sexp with
  84. | Ok (adr', sev', tim') ->
  85. let adr, sev, tim = ban in
  86. Assrt.equals_string __LOC__ adr adr';
  87. Assrt.equals_int __LOC__ sev sev';
  88. Assrt.equals_float __LOC__ (Ptime.to_float_s tim) (Ptime.to_float_s tim')
  89. 1e-3
  90. | _ -> assert false
  91. let _test_seq () =
  92. Logr.info (fun m -> m "test_seq");
  93. Printf.sprintf "%.0f" 1.0
  94. |> Assrt.equals_string __LOC__ "1";
  95. let fn = "tmp/seq.cdb" in
  96. (try
  97. Unix.mkdir "tmp" File.pDir;
  98. with Unix.Unix_error (Unix.EEXIST, _, _) -> ());
  99. (try
  100. Unix.unlink fn;
  101. Unix.unlink (fn ^ "~")
  102. with Unix.Unix_error (Unix.ENOENT, _, _) -> ());
  103. File.touch fn;
  104. let cdb = Mapcdb.Cdb fn
  105. and k = "127.0.0.1"
  106. and t0 = Ptime.epoch in
  107. let f x = x *. Ban.chunk_s |> Ptime.Span.of_float_s |> Option.get |> Ptime.add_span t0 |> Option.get in
  108. let t1 = f 0.5 in
  109. let _t2 = f 1.5 in
  110. let _t3 = f 2.5 in
  111. let t4 = f 3.5 in
  112. let t5 = f 4.5 in
  113. Logr.info (fun m -> m "test_seq 1");
  114. Assrt.equals_none __LOC__ (Ban.check cdb t0 k);
  115. Logr.info (fun m -> m "test_seq 10");
  116. Ban.escalate cdb t1 k;
  117. Mapcdb.find_string_opt k cdb
  118. |> Option.value ~default:"fail"
  119. |> Assrt.equals_string __LOC__ "1970-01-01T00:15:00-00:00";
  120. Assrt.equals_none __LOC__ (Ban.check cdb t1 k);
  121. Ban.escalate cdb t1 k;
  122. Ban.escalate cdb t1 k;
  123. Ban.escalate cdb t1 k;
  124. Ban.check cdb t1 k |> Option.get |> Ptime.to_rfc3339
  125. |> Assrt.equals_string __LOC__ "1970-01-01T00:25:00-00:00";
  126. Ban.escalate cdb t4 k;
  127. Assrt.equals_none __LOC__ (Ban.check cdb t5 k);
  128. assert true
  129. let () =
  130. Unix.chdir "../../../test/";
  131. test_scanf ();
  132. test_hash ();
  133. test_csexp ();
  134. (* test_seq (); *)
  135. assert true