SSLTest.anubis 4.39 KB

read tools/basis.anubis
read TestItem.anubis
	
define String
   ssl_receive_message
     (
       SSL_Connection conn,
       List(Int8) so_far
     ) =
   if (Maybe(String))read(conn,1,60) is 
     {
       failure then implode(reverse(so_far)), 
       success(str) then if nth(0,str) is 
         {
           failure then implode(reverse(so_far)),
           success(c) then 
             if c = '?' then implode(reverse([c . so_far])) else
             if c = '!' then implode(reverse([c . so_far])) else
             ssl_receive_message(conn,[c . so_far])
         }
     }.

   

   
define One
   ssl_send_message
     (
       String me, 
       SSL_Connection conn,
       String message,
       Int32 i
     ) =
   if write(conn,message) is 
     {
       failure then print("\n"+me+": Cannot write into SSL connection."), 
       success(n) then
         if n = length(message)
         then unique
         else print("\n"+me+": Was not able to write the whole message into SSL connection.")
     }.
   
   
define One
   ssl_server_handler
     (
       SSL_Connection conn
     ) =
   with answer = "Very well, thank you !", 
   if local_SSL_address_and_port(conn) is (loc_a,loc_p) then 
   print("\nServer: Local address:port is "+ip_addr_to_string(loc_a)+":"+loc_p); 
   if remote_SSL_address_and_port(conn) is (rem_a,rem_p) then 
   print("\nServer: Remote address:port is "+ip_addr_to_string(rem_a)+":"+rem_p); 
   print("\nServer: Waiting for a question."); 
   with question = ssl_receive_message(conn,[]), 
   print("\nServer: question is: '"+question+"'");
   print("\nServer: Sending message: '"+answer+"'"); 
   ssl_send_message("Server",conn,answer,0). 

define One
   ssl_client_handler
     (
       SSL_Connection conn,
       Server server
     ) =
   with question = "How do you do ?", 
   if local_SSL_address_and_port(conn) is (loc_a,loc_p) then 
   print("\nClient: Local address:port is "+ip_addr_to_string(loc_a)+":"+loc_p); 
   if remote_SSL_address_and_port(conn) is (rem_a,rem_p) then 
   print("\nClient: Remote address:port is "+ip_addr_to_string(rem_a)+":"+rem_p); 
   print("\nClient: Sending message: '"+question+"'"); 
   ssl_send_message("Client",conn,question,0);
   print("\nClient: Waiting for the answer."); 
   with answer = ssl_receive_message(conn,[]), 
   print("\nClient: Answer is: '"+answer+"'").
   
   
   
   
define Server
   force_start_ssl_server
     (
       Int32 address, 
       Int32 port, 
       String server_name, 
       Server -> ((SSL_Connection) -> One) handler, 
       One -> One notify,
       Int32 retry
     ) =
   if start_ssl_server(address,port,server_name,handler,notify) is 
     {
       cannot_create_the_socket   then print("\nCannot create the listening socket."); alert,
       cannot_bind_to_port        then sleep(1000);
                                       print("\rRetry number "+(retry+1)+"                              ");
                               force_start_ssl_server(address,port,server_name,handler,notify,retry+1), 
       cannot_listen_on_port      then print("\nCannot listen on port "+port); alert,
       ok(server)                 then server
     }. 
   
   
define (Maybe(X509)) -> Bool
   policy
     (
       String server_name,
     ) =
   (Maybe(X509) mb_cert) |-> 
   if mb_cert is 
     {
       failure then print("\nClient: No SSL certificate received from server."); true,
       success(cert) then
         print("\nClient Invalid server certificate:\n");
         print(to_string(cert)); 
         print("\n");
         true
     }.
   
public define TestItem   
   ssl_server_test
     =
   with server_name = "Shadoko",
   ask_test_item("SSL client/server test",
             (One u) |->
     with port = (Int32)5432, 
    local_host = ip_address((127,0,0,1)), 
     print("\nStarting an SSL server on port number "+port+"\n"); 
    with server = force_start_ssl_server(0,port,server_name,
                      (Server s) |-> ssl_server_handler,(One u) |-> u,0),
           print("\nSSL server started.");
           print("\nClient: Opening an SSL connection to the SSL server.");
           if open_SSL_connection(server_name,local_host,port,policy(server_name)) is 
             {
               error(msg) then print("\nClient: Cannot connect to SSL server."), 
               ok(conn) then 
                 ssl_client_handler(conn,server);
                 print("\nShutting the server down.");
                 shutdown(server)
             }
       ).