Skip to content

Commit d30f7f2

Browse files
committed
Update test example
Signed-off-by: Changlei Li <changlei.li@cloud.com> Fix test Signed-off-by: Changlei Li <changlei.li@cloud.com>
1 parent e756f26 commit d30f7f2

2 files changed

Lines changed: 38 additions & 50 deletions

File tree

ocaml/libs/stunnel/stunnel.ml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
*)
1414
(* Copyright (C) 2007 XenSource Inc *)
1515

16-
module D = Debug.Make (struct let name = "stunnel" end)
16+
module D = struct
17+
let debug fmt = Printf.ksprintf (fun s -> Printf.printf "%s\n%!" s) fmt
18+
end
1719

1820
open Printf
1921
open Xapi_stdext_unix

ocaml/libs/stunnel/test/test_stunnel.ml

Lines changed: 35 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -63,23 +63,7 @@ let test_unix_socket_proxy remote_host remote_port purpose timeout =
6363
Printf.printf "✗ Socket communication error: %s\n"
6464
(Printexc.to_string e)
6565
) ;
66-
67-
(* Check certificate using separate verification process *)
68-
Printf.printf "→ Checking certificate (separate process)...\n" ;
69-
match Stunnel.check_cert ~verify_cert ~remote_host ~remote_port with
70-
| Ok () ->
71-
Printf.printf "✓ Certificate verification passed\n" ;
72-
Ok ()
73-
| Error (Stunnel.Certificate_verify reasons) ->
74-
Printf.printf "✗ Certificate_verify [%s]\n"
75-
(String.concat "; " reasons) ;
76-
Error (Stunnel.Certificate_verify reasons)
77-
| Error (Stunnel.Stunnel reason) ->
78-
Printf.printf "✗ Stunnel error: %s\n" reason ;
79-
Error (Stunnel.Stunnel reason)
80-
| Error (Stunnel.Unknown reason) ->
81-
Printf.printf "✗ Unknown error: %s\n" reason ;
82-
Error (Stunnel.Unknown reason)
66+
Ok ()
8367
)
8468
in
8569
( match result with
@@ -94,8 +78,23 @@ let test_unix_socket_proxy remote_host remote_port purpose timeout =
9478
(** Test explicit start/stop lifecycle *)
9579
let test_explicit_lifecycle remote_host remote_port purpose timeout =
9680
Printf.printf "\n→ Testing explicit start/stop lifecycle\n" ;
97-
Printf.printf "→ Step 1: Starting proxy to %s:%d\n" remote_host remote_port ;
9881

82+
(* Step 1: Fetch certificate and print *)
83+
Printf.printf "→ Step 1: Fetching server certificate from %s:%d\n" remote_host
84+
remote_port ;
85+
( match Stunnel.fetch_server_cert ~remote_host ~remote_port with
86+
| None ->
87+
Printf.printf "✗ Failed to fetch certificate\n"
88+
| Some cert ->
89+
Printf.printf "✓ Certificate fetched successfully (%d bytes)\n"
90+
(String.length cert) ;
91+
let preview_len = min 200 (String.length cert) in
92+
Printf.printf "→ Certificate preview:\n%s\n"
93+
(String.sub cert 0 preview_len)
94+
) ;
95+
96+
(* Step 2: Starting proxy *)
97+
Printf.printf "\n→ Step 2: Starting proxy to %s:%d\n" remote_host remote_port ;
9998
let verify_cert = Stunnel_client.construct_cert_verification ~purpose in
10099
match
101100
Stunnel.UnixSocketProxy.start ~verify_cert ~remote_host ~remote_port ()
@@ -114,35 +113,8 @@ let test_explicit_lifecycle remote_host remote_port purpose timeout =
114113
Printf.printf "✓ Proxy started at: %s\n"
115114
(Stunnel.UnixSocketProxy.socket_path proxy) ;
116115

117-
(* Step 2: Fetch certificate and print *)
118-
Printf.printf "\n→ Step 2: Fetching server certificate\n" ;
119-
( match Stunnel.fetch_server_cert ~remote_host ~remote_port with
120-
| None ->
121-
Printf.printf "✗ Failed to fetch certificate\n"
122-
| Some cert ->
123-
Printf.printf "✓ Certificate fetched successfully (%d bytes)\n"
124-
(String.length cert) ;
125-
let preview_len = min 200 (String.length cert) in
126-
Printf.printf "→ Certificate preview:\n%s\n"
127-
(String.sub cert 0 preview_len)
128-
) ;
129-
130-
(* Step 3: Check certificate using separate verification process *)
131-
Printf.printf "\n→ Step 3: Checking certificate (separate process)\n" ;
132-
( match Stunnel.check_cert ~verify_cert ~remote_host ~remote_port with
133-
| Ok () ->
134-
Printf.printf "✓ Certificate verification passed\n"
135-
| Error (Stunnel.Certificate_verify reasons) ->
136-
Printf.printf "✗ Stunnel.Certificate_verify [%s]\n"
137-
(String.concat "; " reasons)
138-
| Error (Stunnel.Stunnel reason) ->
139-
Printf.printf "✗ Stunnel.Stunnel %s\n" reason
140-
| Error (Stunnel.Unknown reason) ->
141-
Printf.printf "✗ Stunnel.Unknown %s\n" reason
142-
) ;
143-
144-
(* Step 4: Send hello through socket *)
145-
Printf.printf "\n→ Step 4: Sending 'hello' through socket\n" ;
116+
(* Step 3: Send hello through socket *)
117+
Printf.printf "\n→ Step 3: Sending 'hello' through socket\n" ;
146118
let socket_path = Stunnel.UnixSocketProxy.socket_path proxy in
147119
( try
148120
let sock = Unix.socket Unix.PF_UNIX Unix.SOCK_STREAM 0 in
@@ -151,8 +123,8 @@ let test_explicit_lifecycle remote_host remote_port purpose timeout =
151123
let sent = Unix.send_substring sock msg 0 (String.length msg) [] in
152124
Printf.printf "✓ Sent %d bytes through socket\n" sent ;
153125

154-
(* Step 5: Receive response *)
155-
Printf.printf "\n→ Step 5: Receiving response\n" ;
126+
(* Step 4: Receive response *)
127+
Printf.printf "\n→ Step 4: Receiving response\n" ;
156128
let buf = Bytes.create 1024 in
157129
Unix.setsockopt_float sock Unix.SO_RCVTIMEO timeout ;
158130
( try
@@ -177,6 +149,20 @@ let test_explicit_lifecycle remote_host remote_port purpose timeout =
177149
(Printexc.to_string e)
178150
) ;
179151

152+
(* Step 5: Diagnose proxy status *)
153+
Printf.printf "\n→ Step 5: Diagnosing proxy status\n" ;
154+
( match Stunnel.UnixSocketProxy.diagnose proxy with
155+
| Ok () ->
156+
Printf.printf "✓ Proxy status OK (no errors in log)\n"
157+
| Error (Stunnel.Certificate_verify reasons) ->
158+
Printf.printf "✗ Certificate_verify [%s]\n"
159+
(String.concat "; " reasons)
160+
| Error (Stunnel.Stunnel reason) ->
161+
Printf.printf "✗ Stunnel error: %s\n" reason
162+
| Error (Stunnel.Unknown reason) ->
163+
Printf.printf "✗ Unknown error: %s\n" reason
164+
) ;
165+
180166
(* Step 6: Stop proxy *)
181167
Printf.printf "\n→ Step 6: Stopping proxy\n" ;
182168
Stunnel.UnixSocketProxy.stop proxy ;

0 commit comments

Comments
 (0)