t_cookie.ml 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. (*
  2. * _ _ ____ _
  3. * _| || |_/ ___| ___ _ __ _ __ ___ | |
  4. * |_ .. _\___ \ / _ \ '_ \| '_ \ / _ \| |
  5. * |_ _|___) | __/ |_) | |_) | (_) |_|
  6. * |_||_| |____/ \___| .__/| .__/ \___/(_)
  7. * |_| |_|
  8. *
  9. * Personal Social Web.
  10. *
  11. * cookie_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_cookie () =
  30. let hk,hv = [("k0","v0");("k1","v1")] |> Cohttp.Cookie.Cookie_hdr.serialize in
  31. hk |> Assrt.equals_string __LOC__ "cookie";
  32. hv |> Assrt.equals_string __LOC__ "k0=v0; k1=v1";
  33. let _c = Cohttp.Cookie.Set_cookie_hdr.make
  34. ~domain:"example.com"
  35. ~expiration:`Session
  36. ~http_only:true
  37. ~path:"/foo/bar/"
  38. ~secure:true
  39. ("k","v=v; :") in
  40. assert _c.http_only;
  41. let (hk,hv) = _c |> Cohttp.Cookie.Set_cookie_hdr.serialize in
  42. hk |> Assrt.equals_string __LOC__ "Set-Cookie";
  43. hv |> Assrt.equals_string __LOC__ {|k=v=v; :; domain=example.com; path=/foo/bar/; secure; httponly|};
  44. (match hv
  45. |> Cohttp.Header.init_with "Cookie"
  46. |> Cohttp.Cookie.Cookie_hdr.extract with
  47. | [("k","v=v"); (":",""); ("domain","example.com"); ("path","/foo/bar/"); ("secure",""); ("httponly","")] ->
  48. assert true
  49. | l -> l |> List.length |> Assrt.equals_int __LOC__ (-1)
  50. );
  51. assert true
  52. let test_encrypt () =
  53. let sec = "My secret Secret 89 123456789 12" |> Cstruct.of_string
  54. and nonce = "123456789012" |> Cstruct.of_string in
  55. let ci = "Merhaba, world!"
  56. |> Cstruct.of_string
  57. |> Cookie.encrypt sec nonce
  58. in
  59. ci |> Assrt.equals_string __LOC__ "MTIzNDU2Nzg5MDEyAGoh40PQjxKT7k6hzTRa5pJ1Z6TauBDAsYaEMm2A4w";
  60. ci |> Cookie.decrypt sec
  61. |> Option.get
  62. |> Assrt.equals_string __LOC__ "Merhaba, world!";
  63. assert true
  64. let test_chacha20 () =
  65. let sec = "My secret Secret 89 123456789 12" |> Cstruct.of_string
  66. and nonce = "123456789012" |> Cstruct.of_string in
  67. assert (32 = (sec |> Cstruct.length));
  68. assert (12 = (nonce |> Cstruct.length));
  69. let key = sec |> Mirage_crypto.Chacha20.of_secret in
  70. "Merhaba, world!"
  71. |> Cstruct.of_string
  72. |> Mirage_crypto.Chacha20.authenticate_encrypt ~key ~nonce
  73. |> Mirage_crypto.Chacha20.authenticate_decrypt ~key ~nonce
  74. |> Option.get
  75. |> Cstruct.to_string
  76. |> Assrt.equals_string __LOC__ "Merhaba, world!";
  77. assert true
  78. let test_chacha20_b64 () =
  79. let sec = "My secret Secret 89 123456789 12" |> Cstruct.of_string
  80. and nonce = "123456789012" |> Cstruct.of_string in
  81. assert (32 = (sec |> Cstruct.length));
  82. assert (12 = (nonce |> Cstruct.length));
  83. let key = sec |> Mirage_crypto.Chacha20.of_secret in
  84. "Merhaba, world!"
  85. |> Cstruct.of_string
  86. |> Mirage_crypto.Chacha20.authenticate_encrypt ~key ~nonce
  87. |> Cstruct.to_string
  88. |> Base64.encode_string
  89. |> Base64.decode_exn
  90. |> Cstruct.of_string
  91. |> Mirage_crypto.Chacha20.authenticate_decrypt ~key ~nonce
  92. |> Option.get
  93. |> Cstruct.to_string
  94. |> Assrt.equals_string __LOC__ "Merhaba, world!";
  95. assert true
  96. let () =
  97. Mirage_crypto_rng_lwt.initialize (module Mirage_crypto_rng.Fortuna);
  98. Unix.chdir "../../../test/";
  99. test_cookie ();
  100. test_encrypt ();
  101. test_chacha20 ();
  102. test_chacha20_b64 ();
  103. assert true