test_qa_sock.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #include <assert.h>
  2. #include <errno.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <openssl/bio.h>
  6. #include <openssl/ssl.h>
  7. #include <openssl/x509.h>
  8. #include "qa/qa.h"
  9. #include "qa/qa_sock.h"
  10. FILE* ferr;
  11. void set_up(void)
  12. {
  13. ferr = fopen("/dev/null", "w");
  14. /* Initialize SSL Library by registering algorithms. */
  15. SSL_library_init();
  16. /* trash directly network informative messages */
  17. if (ferr) bio_err = BIO_new_fp(ferr, BIO_NOCLOSE);
  18. else errno = 0;
  19. }
  20. void tear_down(void)
  21. {
  22. fclose(ferr);
  23. }
  24. void test_host_port(void)
  25. {
  26. char uri[100];
  27. char *host, *port;
  28. /* uris of the form host:port shall be recognized */
  29. strcpy(uri, "host:port");
  30. host_port(uri, &host, &port);
  31. assert(!strcmp(host, "host") &&
  32. !strcmp(port, "port"));
  33. /* uris given as urls shall be recognized */
  34. strcpy(uri, "https://cheese");
  35. host_port(uri, &host, &port);
  36. assert(!strcmp(host, "cheese") &&
  37. !strcmp(port, "https"));
  38. /* uris containing just a hostname shall be recognized */
  39. strcpy(uri, "queer");
  40. host_port(uri, &host, &port);
  41. assert(!strcmp(host, "queer") &&
  42. !port);
  43. }
  44. void test_get_remote_cert(void)
  45. {
  46. X509 *crt;
  47. char url[100];
  48. /* NULL shall be returned if the host does not exists. */
  49. strcpy(url, "space_oddity");
  50. crt = get_remote_cert(url);
  51. assert(!crt);
  52. #ifdef NETWORK_TESTS
  53. /* Google™ shall support https, and accept tcp connections on https default port. */
  54. strcpy(url, "google.com:443");
  55. crt = get_remote_cert(url);
  56. assert(crt);
  57. #else
  58. printf("Skipping %s..\n", __func__);
  59. #endif /* NETWORK_TESTS */
  60. }
  61. int main(int argc, char **argv)
  62. {
  63. set_up();
  64. test_host_port();
  65. test_get_remote_cert();
  66. return 0;
  67. }