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
  
96
  
97
  
98
  
99
  
100
  
101
  
102
  
103
  
104
  
105
  
106
  
107
  
108
  
109
  
110
  
111
  
112
  
113
  
114
  
115
  
116
  
117
  
118
  
constant log_msg = Tools.Testsuite.log_msg; 
constant log_status = Tools.Testsuite.log_status; 
 
void exit_test (int failure) 
{ 
  Tools.Testsuite.report_result (1 - !!failure, !!failure); 
  exit (failure); 
} 
 
#ifndef TEST_NORMAL 
 
#define PRE "connecttest (closed): " 
 
void fail() 
{  
// can't connect to socket - this is what we expect 
   exit_test(0); // ok 
} 
 
void ok() 
{ 
   if (f->write("hej")==-1) 
     log_msg(PRE "succeeded to connect to closed socket" 
             " (port %d)\n",z); 
   else 
     log_msg(PRE "socket still open (??)" 
             " (port %d)\n",z); 
 
   exit_test(1); 
} 
 
#else 
 
#define PRE "connecttest (open): " 
 
void fail() 
{  
   log_msg(PRE "can't connect to open port; failure reported\n"); 
   exit_test(1); // fail 
} 
 
void ok() 
{ 
// can connect to socket - this is what we expect 
   if (f->write("hej")==-1) 
   { 
      log_msg(PRE "connected ok, but socket closed" 
              " (port %d)\n",z); 
      exit_test (1); 
   } 
   else 
   { 
      exit_test (0); 
   } 
} 
 
#endif 
 
void rcb(){} 
 
void timeout() 
{ 
   log_msg(PRE "timeout - connection neither succeded " 
           "nor failed\n"); 
   exit_test(1); 
} 
 
object f=Stdio.File(); 
int z; 
object p=Stdio.Port(); 
 
int main() 
{ 
   if (!p->bind(0)) { 
     log_msg(PRE "failed to bind a port: %s.\n", strerror(p->errno())); 
     exit_test(1); 
   } 
   z = (int)(p->query_address()/" ")[-1]; 
//     write("port: %d\n",z); 
#ifndef TEST_NORMAL 
   p->close(); 
   destruct(p); // this port can't be connected to now 
   p = 0; 
#endif 
 
   log_status(PRE "using port %d",z); 
 
   sleep(0.1); 
    
   f->open_socket(); 
   // NOTE: Some OS's (NT) signal connection failure on the 
   //       error callback (aka read_oob callback). 
   f->set_nonblocking(rcb,ok,fail,fail,0); 
   int ok; 
   if (catch { ok = f->connect("127.0.0.1",z); } && 
       catch { ok = f->connect("localhost",z); }) 
   { 
#if 0 
      log_msg(PRE "failed to connect " 
              "to neither \"localhost\" nor \"127.0.0.1\"\n"); 
      log_msg(PRE "reporting ok\n"); 
#endif 
      exit_test (0); 
   } else if (!ok) { 
#ifdef TEST_NORMAL 
     log_msg(PRE "connect() failed with errno %d: %s\n", 
             f->errno(), strerror(f->errno())); 
     exit_test (1); 
#else 
     exit_test (0); 
#endif 
   } 
   call_out(timeout, 10); 
   return -1; 
}