t_cfg.ml 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. (*
  2. * _ _ ____ _
  3. * _| || |_/ ___| ___ _ __ _ __ ___ | |
  4. * |_ .. _\___ \ / _ \ '_ \| '_ \ / _ \| |
  5. * |_ _|___) | __/ |_) | |_) | (_) |_|
  6. * |_||_| |____/ \___| .__/| .__/ \___/(_)
  7. * |_| |_|
  8. *
  9. * Personal Social Web.
  10. *
  11. * cfg_test.ml
  12. *
  13. * Copyright (C) The #Seppo contributors. All rights reserved.
  14. *
  15. * This program is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation, either version 3 of the License, or
  18. * (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  27. *)
  28. open Seppo_lib
  29. let test_rnd_pwd () =
  30. let p = Cfg.random_pwd () in
  31. Logr.info (fun m -> m "cfg_test.test_rnd_pwd 1 %s" p);
  32. p |> String.length |> Assrt.equals_int __LOC__ 16;
  33. assert true
  34. let test_csexp () =
  35. Logr.info (fun m -> m "cfg_test.test_csexp");
  36. Csexp.Atom "uhu"
  37. |> Csexp.to_string
  38. |> Assrt.equals_string __LOC__ "3:uhu";
  39. Csexp.List [ Csexp.Atom "a"; Csexp.Atom "b"]
  40. |> Csexp.to_string
  41. |> Assrt.equals_string __LOC__ "(1:a1:b)";
  42. Csexp.List [ Csexp.Atom "/^.*$/"; Csexp.Atom "$1"]
  43. |> Csexp.to_string
  44. |> Assrt.equals_string __LOC__ "(6:/^.*$/2:$1)";
  45. Csexp.List [ Csexp.Atom "/^.* $/"; Csexp.Atom "$1 "]
  46. |> Csexp.to_string
  47. |> Assrt.equals_string __LOC__ "(7:/^.* $/3:$1 )";
  48. Csexp.List [
  49. Csexp.List [ Csexp.Atom "/^.*$/"; Csexp.Atom "$1" ];
  50. Csexp.List [ Csexp.Atom "/.*/"; Csexp.Atom "$1" ]
  51. ]
  52. |> Csexp.to_string
  53. |> Assrt.equals_string __LOC__ "((6:/^.*$/2:$1)(4:/.*/2:$1))";
  54. (match Csexp.parse_string "((6:/^.*$/2:$1)(4:/.*/2:$1))" with
  55. | Ok Csexp.List [
  56. Csexp.List [ Csexp.Atom a; Csexp.Atom b ];
  57. Csexp.List [ Csexp.Atom c; Csexp.Atom d ]
  58. ] ->
  59. a |> Assrt.equals_string __LOC__ "/^.*$/";
  60. b |> Assrt.equals_string __LOC__ "$1";
  61. c |> Assrt.equals_string __LOC__ "/.*/";
  62. d |> Assrt.equals_string __LOC__ "$1"
  63. | Error (idx, txt) ->
  64. Logr.err (fun m -> m "%s char %d of '%s'" E.e9001 idx txt);
  65. assert false;
  66. | _ -> assert false);
  67. (match Csexp.parse_string "1:a" with
  68. | Ok Csexp.Atom "a" -> ()
  69. | _ -> assert false);
  70. (* https://github.com/ocaml-dune/csexp/issues/21 *)
  71. (match Csexp.parse_string_many "1:a" with
  72. | Ok [Csexp.Atom "a"] -> ()
  73. | _ -> assert false);
  74. (match Csexp.parse_string_many "1:a2:bb" with
  75. | Ok [Csexp.Atom "a"; Csexp.Atom "bb"] -> ()
  76. | _ -> assert false);
  77. (match Csexp.parse_string_many "(1:a2:bb)" with
  78. | Ok [Csexp.List [Csexp.Atom "a"; Csexp.Atom "bb"]] -> ()
  79. | _ -> assert false);
  80. (match Csexp.parse_string_many "(1:a)(2:bb)" with
  81. | Ok [Csexp.List [Csexp.Atom "a"]; Csexp.List [Csexp.Atom "bb"]] -> ()
  82. | _ -> assert false);
  83. let fn = "tmp/~cfg~" in
  84. (try Sys.remove fn with | _ -> ());
  85. let oc = open_out_gen [ Open_wronly; Open_creat; Open_excl; Open_binary ] 0o444 fn in
  86. Csexp.Atom "a\nb" |> Csexp.to_channel oc;
  87. oc |> close_out;
  88. let ic = open_in_gen [ Open_rdonly; Open_binary ] 0 fn in
  89. (match Csexp.input_many ic with
  90. | Ok [Csexp.Atom v] -> Assrt.equals_string __LOC__ "a\nb" v
  91. | _ -> assert false);
  92. close_in ic;
  93. Unix.unlink fn;
  94. assert true
  95. let test_sexp0 () =
  96. Logr.info (fun m -> m "cfg_test.test_sexp0");
  97. let open Sexplib in
  98. Sexp.Atom "uhu"
  99. |> Sexp.to_string_hum
  100. |> Assrt.equals_string __LOC__ "uhu"
  101. ;
  102. Sexp.List [ Sexp.Atom "a"; Sexp.Atom "b"]
  103. |> Sexp.to_string_hum
  104. |> Assrt.equals_string __LOC__ "(a b)"
  105. ;
  106. Sexp.List [ Sexp.Atom "/^.*$/"; Sexp.Atom "$1"]
  107. |> Sexp.to_string_hum
  108. |> Assrt.equals_string __LOC__ "(/^.*$/ $1)"
  109. ;
  110. Sexp.List [ Sexp.Atom "/^.* $/"; Sexp.Atom "$1 "]
  111. |> Sexp.to_string_hum
  112. |> Assrt.equals_string __LOC__ "(\"/^.* $/\" \"$1 \")"
  113. ;
  114. (match Sexp.of_string " ( ( /^.*$/ $1 ) ( \"/.*/\" \"$1\" ) )" with
  115. | Sexp.List [
  116. Sexp.List [ Sexp.Atom a; Sexp.Atom b ];
  117. Sexp.List [ Sexp.Atom c; Sexp.Atom d ]
  118. ] ->
  119. a |> Assrt.equals_string __LOC__ "/^.*$/";
  120. b |> Assrt.equals_string __LOC__ "$1";
  121. c |> Assrt.equals_string __LOC__ "/.*/";
  122. d |> Assrt.equals_string __LOC__ "$1"
  123. | _ -> assert false);
  124. assert true
  125. let test_bcrypt () =
  126. Logr.info (fun m -> m "cfg_test.test_bcrypt");
  127. assert (
  128. "$2y$06$4xLSOcTZedSV78qdnjktl.0V4VsUwLycCIkIaRrdxoSP6jtCWJJxu"
  129. |> Bcrypt.hash_of_string
  130. |> Bcrypt.verify "correct battery horse staple");
  131. (* compatibility with golang.org/x/crypto/bcrypt bcrypt.GenerateFromPassword *)
  132. assert (
  133. "$2a$10$qp5lpHEpXCym7CGYxnPJ7.OhIRAyvtj6iPWJslXOFzl4Hni23h4tu"
  134. |> Bcrypt.hash_of_string
  135. |> Bcrypt.verify "demodemodemo")
  136. (*
  137. let test_sexp1 () =
  138. let c0 = Config.load "config_test.0.sx" in
  139. assert ("string" = c0.title);
  140. Assrt.equals_int __LOC__ 5 (List.length c0.url_cleaner);
  141. Assrt.equals_int __LOC__ 2 (List.length c0.posse)
  142. *)
  143. let test_profile () =
  144. Logr.info (fun m -> m "cfg_test.test_profile");
  145. let p = Cfg.Profile.from_file "data/profile.s" |> Result.get_ok in
  146. let Rfc4287.Rfc4646 language = p.language in
  147. p.title |> Assrt.equals_string __LOC__ "Oh my blog";
  148. p.bio |> Assrt.equals_string __LOC__ "some longer text
  149. that may span mul-
  150. tiple lines";
  151. language |> Assrt.equals_string __LOC__ "en";
  152. p.timezone |> Timedesc.Time_zone.name |> Assrt.equals_string __LOC__ "Europe/Zurich";
  153. p.posts_per_page |> Assrt.equals_int __LOC__ 50;
  154. assert true
  155. let () =
  156. Mirage_crypto_rng_lwt.initialize (module Mirage_crypto_rng.Fortuna);
  157. Unix.chdir "../../../test/";
  158. test_rnd_pwd ();
  159. test_csexp ();
  160. test_sexp0 ();
  161. test_bcrypt ();
  162. (* test_sexp1 (); *)
  163. test_profile ();
  164. assert true