1
  
2
  
3
  
4
  
5
  
6
  
7
  
8
  
9
  
10
  
11
  
12
  
13
  
14
  
15
  
16
  
17
  
18
  
19
  
20
  
21
  
22
  
23
  
24
  
25
  
26
  
27
  
28
  
29
  
30
  
31
  
32
  
33
  
34
  
35
  
36
  
37
  
38
  
39
  
40
  
41
  
42
  
43
  
44
  
45
  
46
  
47
  
48
  
49
  
50
  
51
  
52
  
53
  
54
  
55
  
56
  
57
  
58
  
59
  
60
  
61
  
62
  
63
  
64
  
65
  
66
  
67
  
68
  
69
  
70
  
71
  
72
  
73
  
74
  
75
  
76
  
77
  
78
  
79
  
80
  
81
  
82
  
83
  
84
  
85
  
86
  
87
  
88
  
89
  
90
  
91
  
92
  
93
  
94
  
95
  
// This test belongs in SSL.pmod, but it's here because there's 
// currently no way to filter out test pike files from pmod dirs. 
 
int main (int argc, array(string) argv) 
{ 
  string fail; 
 
  int clean_close = argv[1] == "1"; 
  int block = argv[2] == "1"; 
 
  Pike.Backend blocked_backend = Pike.Backend(); 
  Stdio.Port blocked_server = Stdio.Port(); 
  blocked_server->set_backend (blocked_backend); 
  blocked_server->bind (36565, lambda () { 
                                 fail = "Should never accept a connection.\n"; 
                               }); 
 
  Stdio.File con = Stdio.File(); 
  SSL.sslfile tlscon; 
  con->async_connect ( 
    "127.0.0.1", 36565, 
    lambda (int success) { 
      if (success) { 
        tlscon = SSL.sslfile (con, SSL.context(), 1, 0); 
        tlscon->set_write_callback (lambda () { 
                                      fail = "Handshake should not succeed.\n"; 
                                    }); 
      } 
      else 
        fail = "Failed to connect."; 
    }); 
 
  call_out ( 
    lambda () { 
      // Got the connection timeout we want. 
      if (!fail) { 
        if (mixed err = catch { 
            int init_is_open = tlscon->is_open(); 
            if (block) tlscon->set_blocking(); 
            int close_err; 
            if (mixed err = catch (tlscon->close (0, clean_close))) { 
              if (tlscon->errno() != System.ECONNRESET) 
                throw (err); 
              close_err = 1; 
            } 
            array(int) res = ({init_is_open, 
                               close_err, 
                               tlscon->is_open(), 
                               objectp (tlscon->shutdown()), 
                               tlscon->errno()}); 
            int successful; 
            if (clean_close) { 
              if (block) 
                // Clean close in blocking mode should block in 
                // tlscon->close until the other thread destructs the 
                // server and we get ECONNRESET. 
                successful = equal (res, ({1, 1, 0, 0, System.ECONNRESET})); 
              else 
                // Clean close in nonblocking mode shouldn't really 
                // close the fd, but the later shutdown should, and 
                // then set EPIPE. 
                successful = equal (res, ({1, 0, 2, 0, System.EPIPE})); 
            } 
            else { 
              // A nonclean close always succeeds without any ado. 
              successful = equal (res, ({1, 0, 0, 0, 0})); 
            } 
            if (!successful) 
              Tools.Testsuite.log_msg ( 
                "Async tls close on stalled connection failed: " 
                "clean_close=%d block=%d res=%{%O %}\n", 
                clean_close, block, res); 
            Tools.Testsuite.report_result (successful, !successful); 
            exit (!successful); 
          }) 
          fail = "Error in call out: " + describe_backtrace (err); 
      } 
      if (fail) werror (fail); 
      exit (2); 
    }, 0.5); 
 
  thread_create (lambda () { 
                   sleep (1); 
#if 0 
                 werror ("\n"); 
                   foreach (all_threads(), Thread.Thread t) 
                     werror ("\n" + describe_backtrace (t->backtrace())); 
#endif 
                 destruct (blocked_server); 
                 }); 
 
  return -1; 
}