pike.git / lib / 7.8 / modules / SSL.pmod / testsuite.in

version» Context lines:

pike.git/lib/7.8/modules/SSL.pmod/testsuite.in:1: + START_MARKER(7.8)    -  + dnl Note: This module has some tests with an external test script in + dnl src/modules/_Stdio +  + cond_resolv(Crypto.Hash, [[ +  + test_do([[ + #define ASSERT(X) if(!(X)) error("Assertion failed.\n") +  +  object rsa = Crypto.RSA(); +  rsa->generate_key( 1024, random_string ); +  string _key = Tools.PEM.simple_build_pem("RSA PRIVATE KEY", +  Standards.PKCS.RSA.private_key(rsa)); +  +  object msg = Tools.PEM.pem_msg()->init(_key); +  object part = msg->parts["RSA PRIVATE KEY"]; +  object rsa_again=Standards.PKCS.RSA.parse_private_key(part->decoded_body()); +  +  ASSERT(rsa->public_key_equal(rsa_again)); +  +  array attrs = ({ +  (["organizationName":Standards.ASN1.Types.PrintableString("Test")]), +  (["commonName":Standards.ASN1.Types.PrintableString("*")]), +  }); +  +  string _cert = Tools.PEM.simple_build_pem("CERTIFICATE", +  Tools.X509.make_selfsigned_rsa_certificate(rsa_again, 3600*24, attrs)); +  +  add_constant("pem_key", _key); +  add_constant("pem_cert", _cert); + ]]) +  + test_program([[ +  + #define ASSERT(X) if(!(X)) error("Assertion failed.\n") +  + class Server { +  SSL.context ctx; +  +  void create() { +  ctx = SSL.context(); +  ctx->random = random_string; +  +  object msg = Tools.PEM.pem_msg()->init( pem_cert ); +  object part = msg->parts["CERTIFICATE"]; +  string cert = part->decoded_body(); +  ASSERT(cert); +  +  msg = Tools.PEM.pem_msg()->init( pem_key ); +  part = msg->parts["RSA PRIVATE KEY"]; +  string key = part->decoded_body(); +  ASSERT(key); +  +  object rsa = Standards.PKCS.RSA.parse_private_key(key); +  ctx->rsa = rsa; +  ASSERT(rsa->rsa_size()>512); +  ctx->short_rsa = Crypto.RSA()->generate_key(512, ctx->random); +  ctx->rsa_mode(); +  +  object tbs = Tools.X509.decode_certificate(cert); +  ASSERT(tbs->public_key->rsa->public_key_equal(rsa)); +  ctx->certificates = ({ cert }); +  +  add_constant("server_ctx", ctx); +  } + } +  + int a() { +  object s = Server(); +  return 1; + } + ]]) +  + test_do([[ +  add_constant("client_msg", Crypto.Random.random_string(65536)); + ]]) +  + ]]) + dnl Avoid too large strings to cond... + cond_resolv(Crypto.Hash, [[ + dnl client_min, client_max, server_min, server_max, expected + define(test_ssl, [[ +  test_any([[ +  log_status("Testing SSL 3.$1..3.$2 client with SSL 3.$3..3.$4 server..."); +  +  Stdio.File client_con = Stdio.File(); +  Stdio.File server_con = +  client_con->pipe(Stdio.PROP_NONBLOCK | Stdio.PROP_BIDIRECTIONAL); +  +  SSL.sslfile server = SSL.sslfile(server_con, server_ctx, UNDEFINED, +  0, $3, $4); +  +  SSL.context client_ctx = SSL.context(); +  client_ctx->random = Crypto.Random.random_string; +  SSL.sslfile client = SSL.sslfile(client_con, client_ctx, 1, 0, $1, $2); +  +  int state; +  +  int trigged = 10; +  +  string server_buf = ""; +  void server_send_data() +  { +  trigged = 2; +  int bytes = server->write(server_buf); +  server_buf = server_buf[bytes..]; +  if (!sizeof(server_buf)) { +  server->set_write_callback(UNDEFINED); +  if (state) { +  server->close(); +  state = 2; +  } +  } +  }; +  void server_got_data(mixed ignored, string data) +  { +  trigged = 2; +  if (!sizeof(server_buf)) server->set_write_callback(server_send_data); +  server_buf += data; +  }; +  void server_got_close() +  { +  trigged = 2; +  }; +  void server_alert() +  { +  trigged = 0; +  // server->close(); +  }; +  +  string client_recv_buf = ""; +  string client_send_buf = client_msg; +  void client_got_data(mixed ignored, string data) +  { +  trigged = 2; +  client_recv_buf += data; +  }; +  void client_send_data() +  { +  trigged = 2; +  int bytes = client->write(client_send_buf[..4095]); +  client_send_buf = client_send_buf[bytes..]; +  if (!sizeof(client_send_buf)) { +  client->set_write_callback(UNDEFINED); +  state = 1; +  } +  }; +  void client_got_close() +  { +  trigged = 2; +  if (state == 2) state = 3; +  client->close(); +  }; +  void client_alert() +  { +  trigged = 0; +  // client->close(); +  }; +  +  server->set_nonblocking(server_got_data, 0, server_got_close); +  server->set_alert_callback(server_alert); +  client->set_nonblocking(client_got_data, client_send_data, +  client_got_close); +  client->set_alert_callback(client_alert); +  +  // We loop the backend while something happens... +  while (trigged--) { +  Pike.DefaultBackend(0.1); +  } +  +  return (state == 3) ? client_recv_buf : state; +  ]], $5) + ]]) +  +  test_ssl(0,0,0,0,client_msg) +  test_ssl(0,1,0,0,client_msg) +  test_ssl(1,1,0,0,0) +  test_ssl(0,2,0,0,client_msg) +  test_ssl(1,2,0,0,0) +  test_ssl(2,2,0,0,0) +  +  test_ssl(0,0,0,1,client_msg) +  test_ssl(0,0,1,1,0) +  test_ssl(0,1,0,1,client_msg) +  test_ssl(0,1,1,1,client_msg) +  test_ssl(1,1,0,1,client_msg) +  test_ssl(1,1,1,1,client_msg) +  test_ssl(0,2,0,1,client_msg) +  test_ssl(0,2,1,1,client_msg) +  test_ssl(1,2,0,1,client_msg) +  test_ssl(1,2,1,1,client_msg) +  test_ssl(2,2,0,1,0) +  test_ssl(2,2,1,1,0) +  +  test_ssl(0,0,0,2,client_msg) +  test_ssl(0,0,1,2,0) +  test_ssl(0,0,2,2,0) +  test_ssl(0,1,0,2,client_msg) +  test_ssl(0,1,1,2,client_msg) +  test_ssl(0,1,2,2,0) +  test_ssl(1,1,0,2,client_msg) +  test_ssl(1,1,1,2,client_msg) +  test_ssl(1,1,2,2,0) +  test_ssl(0,2,0,2,client_msg) +  test_ssl(0,2,1,2,client_msg) +  test_ssl(0,2,2,2,client_msg) +  test_ssl(1,2,0,2,client_msg) +  test_ssl(1,2,1,2,client_msg) +  test_ssl(1,2,2,2,client_msg) +  test_ssl(2,2,0,2,client_msg) +  test_ssl(2,2,1,2,client_msg) +  test_ssl(2,2,2,2,client_msg) +  +  test_do([[ +  add_constant("client_msg"); +  add_constant("server_ctx"); +  add_constant("pem_cert"); +  add_constant("pem_key"); +  ]]) +  + ]]) +  + dnl test_do([[ + dnl object p = Process.Process(RUNPIKE + " \"]]SRCDIR[[/https.pike\""); + dnl sleep(5); /* Wait a little for the server to startup */ + dnl Protocols.HTTP.Query q = Protocols.HTTP.get_url("https://localhost:25678"); + dnl if (q->status != 200 || search("html", q->data()) < 0) + dnl error("Failed\n"); + dnl /* How to kill the process if running on windows? */ + dnl p->kill(15); + dnl ]]) + END_MARKER   Newline at end of file added.