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