f391b92010-07-11Martin Stjernholm constant log_msg = Tools.Testsuite.log_msg;
f115722010-07-11Martin Stjernholm constant log_status = Tools.Testsuite.log_status;
fb4b0d2010-07-11Martin Stjernholm void exit_test (int failure) { Tools.Testsuite.report_result (1 - !!failure, !!failure); exit (failure); }
0ed8e62001-04-22Mirar (Pontus Hagland) #ifndef TEST_NORMAL #define PRE "connecttest (closed): "
c3d2922001-04-19Mirar (Pontus Hagland) void fail() { // can't connect to socket - this is what we expect
fb4b0d2010-07-11Martin Stjernholm  exit_test(0); // ok
c3d2922001-04-19Mirar (Pontus Hagland) } void ok() { if (f->write("hej")==-1)
f391b92010-07-11Martin Stjernholm  log_msg(PRE "succeeded to connect to closed socket" " (port %d)\n",z);
c3d2922001-04-19Mirar (Pontus Hagland)  else
f391b92010-07-11Martin Stjernholm  log_msg(PRE "socket still open (??)" " (port %d)\n",z);
c3d2922001-04-19Mirar (Pontus Hagland) 
fb4b0d2010-07-11Martin Stjernholm  exit_test(1);
c3d2922001-04-19Mirar (Pontus Hagland) }
0ed8e62001-04-22Mirar (Pontus Hagland) #else #define PRE "connecttest (open): " void fail() {
f391b92010-07-11Martin Stjernholm  log_msg(PRE "can't connect to open port; failure reported\n");
fb4b0d2010-07-11Martin Stjernholm  exit_test(1); // fail
0ed8e62001-04-22Mirar (Pontus Hagland) } void ok() {
5c3d782001-06-13Henrik Grubbström (Grubba) // can connect to socket - this is what we expect
0ed8e62001-04-22Mirar (Pontus Hagland)  if (f->write("hej")==-1) {
f391b92010-07-11Martin Stjernholm  log_msg(PRE "connected ok, but socket closed" " (port %d)\n",z);
fb4b0d2010-07-11Martin Stjernholm  exit_test (1);
0ed8e62001-04-22Mirar (Pontus Hagland)  } else {
fb4b0d2010-07-11Martin Stjernholm  exit_test (0);
0ed8e62001-04-22Mirar (Pontus Hagland)  } } #endif
7c72472001-05-05Fredrik Hübinette (Hubbe) void rcb(){}
0ed8e62001-04-22Mirar (Pontus Hagland) void timeout() {
f391b92010-07-11Martin Stjernholm  log_msg(PRE "timeout - connection neither succeded " "nor failed\n");
fb4b0d2010-07-11Martin Stjernholm  exit_test(1);
0ed8e62001-04-22Mirar (Pontus Hagland) }
c3d2922001-04-19Mirar (Pontus Hagland) object f=Stdio.File();
4c118c2003-04-26Marcus Comstedt int z;
63a1d12022-06-06Martin Nilsson object|zero p=Stdio.Port();
c3d2922001-04-19Mirar (Pontus Hagland)  int main() {
4c118c2003-04-26Marcus Comstedt  if (!p->bind(0)) {
f391b92010-07-11Martin Stjernholm  log_msg(PRE "failed to bind a port: %s.\n", strerror(p->errno()));
fb4b0d2010-07-11Martin Stjernholm  exit_test(1);
44de462003-04-26Henrik Grubbström (Grubba)  }
4c118c2003-04-26Marcus Comstedt  z = (int)(p->query_address()/" ")[-1];
5520722007-06-18Martin Stjernholm // write("port: %d\n",z);
0ed8e62001-04-22Mirar (Pontus Hagland) #ifndef TEST_NORMAL
a4ad522004-10-22Henrik Grubbström (Grubba)  p->close();
c3d2922001-04-19Mirar (Pontus Hagland)  destruct(p); // this port can't be connected to now
a4ad522004-10-22Henrik Grubbström (Grubba)  p = 0;
0ed8e62001-04-22Mirar (Pontus Hagland) #endif
c260242001-04-19Mirar (Pontus Hagland) 
f115722010-07-11Martin Stjernholm  log_status(PRE "using port %d",z);
c260242001-04-19Mirar (Pontus Hagland) 
c3d2922001-04-19Mirar (Pontus Hagland)  sleep(0.1); f->open_socket();
5c3d782001-06-13Henrik Grubbström (Grubba)  // NOTE: Some OS's (NT) signal connection failure on the
2cabae2008-01-31Henrik Grubbström (Grubba)  // error callback (aka read_oob callback). f->set_nonblocking(rcb,ok,fail,fail,0);
0d492d2005-11-07Henrik Grubbström (Grubba)  int ok; if (catch { ok = f->connect("127.0.0.1",z); } && catch { ok = f->connect("localhost",z); })
42c59a2001-04-21Mirar (Pontus Hagland)  {
f115722010-07-11Martin Stjernholm #if 0
f391b92010-07-11Martin Stjernholm  log_msg(PRE "failed to connect " "to neither \"localhost\" nor \"127.0.0.1\"\n"); log_msg(PRE "reporting ok\n");
f115722010-07-11Martin Stjernholm #endif
fb4b0d2010-07-11Martin Stjernholm  exit_test (0);
0d492d2005-11-07Henrik Grubbström (Grubba)  } else if (!ok) {
c93f8d2007-06-19Martin Stjernholm #ifdef TEST_NORMAL
f391b92010-07-11Martin Stjernholm  log_msg(PRE "connect() failed with errno %d: %s\n", f->errno(), strerror(f->errno()));
fb4b0d2010-07-11Martin Stjernholm  exit_test (1);
0d492d2005-11-07Henrik Grubbström (Grubba) #else
fb4b0d2010-07-11Martin Stjernholm  exit_test (0);
0d492d2005-11-07Henrik Grubbström (Grubba) #endif
42c59a2001-04-21Mirar (Pontus Hagland)  }
62782b2001-06-13Henrik Grubbström (Grubba)  call_out(timeout, 10);
c3d2922001-04-19Mirar (Pontus Hagland)  return -1; }