b667cf1999-10-14Henrik Grubbström (Grubba) #!/usr/local/bin/pike
414b7d2008-07-13Marcus Comstedt final constant TEST_SIZE = 16384;
84ccbc2000-10-06Henrik Grubbström (Grubba) 
b866122004-02-03Martin Nilsson string testdata = random_string(TEST_SIZE);
84ccbc2000-10-06Henrik Grubbström (Grubba) 
f115722010-07-11Martin Stjernholm int verbose;
84ccbc2000-10-06Henrik Grubbström (Grubba) int testno;
02c4802000-10-07Henrik Grubbström (Grubba) object(Stdio.Port) loopback = Stdio.Port(); int loopbackport;
84ccbc2000-10-06Henrik Grubbström (Grubba) /* * Some helper functions. */
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 (max (testno - !!failure, 0), !!failure); exit (failure); }
84ccbc2000-10-06Henrik Grubbström (Grubba) object(Stdio.File) From(string f) { object(Stdio.File) from = Stdio.File(); if (!from->open(f, "r")) {
f391b92010-07-11Martin Stjernholm  log_msg("Failed to open %O for reading.\n", f);
fb4b0d2010-07-11Martin Stjernholm  exit_test(1);
84ccbc2000-10-06Henrik Grubbström (Grubba)  } return from; } object(Stdio.File) To(string f)
b667cf1999-10-14Henrik Grubbström (Grubba) {
84ccbc2000-10-06Henrik Grubbström (Grubba)  object(Stdio.File) to = Stdio.File(); if (!to->open(f, "cwt")) {
f391b92010-07-11Martin Stjernholm  log_msg("Failed to open %O for writing.\n", f);
fb4b0d2010-07-11Martin Stjernholm  exit_test(1);
84ccbc2000-10-06Henrik Grubbström (Grubba)  } return to;
b667cf1999-10-14Henrik Grubbström (Grubba) }
02c4802000-10-07Henrik Grubbström (Grubba) array(object(Stdio.File)) SocketPair() { object(Stdio.File) sock1, sock2; sock1 = Stdio.File(); sock1->connect("127.0.0.1", loopbackport); sock2 = loopback->accept(); if(!sock2) {
f391b92010-07-11Martin Stjernholm  log_msg("Accept returned 0\n");
fb4b0d2010-07-11Martin Stjernholm  exit_test(1);
02c4802000-10-07Henrik Grubbström (Grubba)  } return ({ sock1, sock2 }); }
dc4f082000-10-07Henrik Grubbström (Grubba) void Verify() { array(string) data = From("conftest.dst")->read()/TEST_SIZE; int i; for(i=0; i < sizeof(data); i++) { if (data[i] != testdata) {
f391b92010-07-11Martin Stjernholm  log_msg("Segment %d corrupted!\n", i);
dc4f082000-10-07Henrik Grubbström (Grubba)  int j; for (j=0; j < TEST_SIZE; j++) { if (data[i][j] != testdata[j]) {
f391b92010-07-11Martin Stjernholm  log_msg("First corrupt byte at segment offset %d: 0x%02x != 0x%02x\n", j, data[i][j], testdata[j]);
fb4b0d2010-07-11Martin Stjernholm  exit_test(1);
dc4f082000-10-07Henrik Grubbström (Grubba)  } }
f391b92010-07-11Martin Stjernholm  log_msg("Corrupt byte not found!\n");
fb4b0d2010-07-11Martin Stjernholm  exit_test(1);
dc4f082000-10-07Henrik Grubbström (Grubba)  } } }
84ccbc2000-10-06Henrik Grubbström (Grubba) /* * The driver function. */ void next() { testno++; function test;
f960bd2015-07-31Martin Nilsson  if (!(test = this["test"+testno])) exit_test(0);
84ccbc2000-10-06Henrik Grubbström (Grubba)  mixed err; if (err = catch {
f115722010-07-11Martin Stjernholm  log_status("Sendfile test: %d", testno);
84ccbc2000-10-06Henrik Grubbström (Grubba)  test(); }) { catch {
f391b92010-07-11Martin Stjernholm  log_msg("Test %d failed!\n" "%s\n", testno, describe_backtrace(err));
84ccbc2000-10-06Henrik Grubbström (Grubba)  };
fb4b0d2010-07-11Martin Stjernholm  exit_test(1);
84ccbc2000-10-06Henrik Grubbström (Grubba)  } } void done(int sent, int expected) { if (sent != expected) {
f391b92010-07-11Martin Stjernholm  log_msg("Test %d failed: %d != %d\n", testno, sent, expected);
fb4b0d2010-07-11Martin Stjernholm  exit_test(1);
84ccbc2000-10-06Henrik Grubbström (Grubba)  }
6f589f2000-10-07Henrik Grubbström (Grubba)  call_out(next, 0);
84ccbc2000-10-06Henrik Grubbström (Grubba) } /* * The actual tests. */ void test1() { /* First try sending out testdata to a plain file. */ if (!Stdio.sendfile(testdata/1024, 0, 0, -1, 0, To("conftest.src"), done, TEST_SIZE)) {
f391b92010-07-11Martin Stjernholm  log_msg("Stdio.sendfile() failed!\n");
fb4b0d2010-07-11Martin Stjernholm  exit_test(1);
84ccbc2000-10-06Henrik Grubbström (Grubba)  } } void test2() { /* Then try copying it to another file. */ if (!Stdio.sendfile(0, From("conftest.src"), 0, -1, 0, To("conftest.dst"), done, TEST_SIZE)) {
f391b92010-07-11Martin Stjernholm  log_msg("Stdio.sendfile() failed!\n");
fb4b0d2010-07-11Martin Stjernholm  exit_test(1);
84ccbc2000-10-06Henrik Grubbström (Grubba)  } } void test3()
b667cf1999-10-14Henrik Grubbström (Grubba) {
dc4f082000-10-07Henrik Grubbström (Grubba)  Verify();
b667cf1999-10-14Henrik Grubbström (Grubba) 
84ccbc2000-10-06Henrik Grubbström (Grubba)  /* Try with a headers + file + trailers combo. */ if (!Stdio.sendfile(testdata/4096, From("conftest.src"), 0, -1, testdata/512, To("conftest.dst"), done, TEST_SIZE*3)) {
f391b92010-07-11Martin Stjernholm  log_msg("Stdio.sendfile() failed!\n");
fb4b0d2010-07-11Martin Stjernholm  exit_test(1);
b667cf1999-10-14Henrik Grubbström (Grubba)  }
84ccbc2000-10-06Henrik Grubbström (Grubba) } void test4() {
dc4f082000-10-07Henrik Grubbström (Grubba)  Verify();
84ccbc2000-10-06Henrik Grubbström (Grubba) 
02c4802000-10-07Henrik Grubbström (Grubba)  /* Try a loopback test. */ array(object) pair = SocketPair(); if (!Stdio.sendfile(testdata/4096, From("conftest.src"), 0, -1, testdata/512, pair[0], done, TEST_SIZE*3)) {
f391b92010-07-11Martin Stjernholm  log_msg("Stdio.sendfile() failed!\n");
fb4b0d2010-07-11Martin Stjernholm  exit_test(1);
02c4802000-10-07Henrik Grubbström (Grubba)  } if (!Stdio.sendfile(testdata/4096, pair[1], 0, -1, testdata/512, To("conftest.dst"), done, TEST_SIZE*5)) {
f391b92010-07-11Martin Stjernholm  log_msg("Stdio.sendfile() failed!\n");
fb4b0d2010-07-11Martin Stjernholm  exit_test(1);
02c4802000-10-07Henrik Grubbström (Grubba)  }
84ccbc2000-10-06Henrik Grubbström (Grubba) } void test5() {
02c4802000-10-07Henrik Grubbström (Grubba)  /* Dummy (test 4 will call done twice). */ } void test6() {
dc4f082000-10-07Henrik Grubbström (Grubba)  Verify();
02c4802000-10-07Henrik Grubbström (Grubba)  next(); } void test7() {
84ccbc2000-10-06Henrik Grubbström (Grubba)  /* Clean up. */ rm("conftest.src"); rm("conftest.dst"); next(); } /* * Start the backend. */ int main(int argc, array(string) argv) {
f115722010-07-11Martin Stjernholm  verbose = (int) (getenv()->TEST_VERBOSITY || 2);
a393d72007-05-18Henrik Grubbström (Grubba) #if constant(alarm) alarm(5*60); // 5 minutes should be sufficient for this test. #endif
02c4802000-10-07Henrik Grubbström (Grubba)  loopback->bind(0); loopbackport = (int)((loopback->query_address()/" ")[1]);
84ccbc2000-10-06Henrik Grubbström (Grubba)  call_out(next, 0); return -1;
b667cf1999-10-14Henrik Grubbström (Grubba) }