httptest.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Copyright 2014-2023 The GmSSL Project. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the License); you may
  5. * not use this file except in compliance with the License.
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. */
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <stdlib.h>
  12. #include <gmssl/http.h>
  13. #include <gmssl/x509_crl.h>
  14. #include <gmssl/error.h>
  15. static int test_http_parse_uri(void)
  16. {
  17. char *tests[] = {
  18. "http://www.example.com:8080/ca/ca2023.crl",
  19. "http://www.example.com:80/ca/ca2023.crl",
  20. "http://www.example.com/ca/ca2023.crl",
  21. "http://www.example.com/ca2023.crl",
  22. "http://www.example.com:8080/",
  23. "http://www.example.com:8080",
  24. "http://www.example.com/",
  25. "http://www.example.com",
  26. };
  27. size_t i;
  28. char host[128];
  29. int port;
  30. char path[256];
  31. for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
  32. if (http_parse_uri(tests[i], host, &port, path) != 1) {
  33. fprintf(stderr, "error: tests[%zu]: %s\n", i, tests[i]);
  34. error_print();
  35. return -1;
  36. }
  37. printf("%s: host = %s, port = %d, path = %s\n", tests[i], host, port, path);
  38. }
  39. printf("%s() ok\n", __FUNCTION__);
  40. return 1;
  41. }
  42. static int test_http_parse_uri_bad(void)
  43. {
  44. char *tests[] = {
  45. "ldap://www.example.com:8080/ca/ca2023.crl",
  46. "http://www.example.com::8080/ca/ca2023.crl",
  47. "http://www.example.com:8080:/ca/ca2023.crl",
  48. "http://www.example.com:-100/ca/ca2023.crl",
  49. "http://www.example.com:/ca/ca2023.crl",
  50. "http:///ca2023.crl",
  51. "http:///",
  52. "http://",
  53. };
  54. size_t i;
  55. char host[128];
  56. int port;
  57. char path[256];
  58. for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
  59. if (http_parse_uri(tests[i], host, &port, path) != -1) {
  60. fprintf(stderr, "error: tests[%zu]: %s\n", i, tests[i]);
  61. printf("%s: host = %s, port = %d, path = %s\n", tests[i], host, port, path);
  62. error_print();
  63. return -1;
  64. }
  65. }
  66. printf("%s() ok\n", __FUNCTION__);
  67. return 1;
  68. }
  69. static int test_http_get_crl(void)
  70. {
  71. char *tests[] = {
  72. "http://crl.pki.goog/gsr1/gsr1.crl",
  73. };
  74. uint8_t buf[65536];
  75. size_t contentlen;
  76. size_t i;
  77. for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
  78. if (http_get(tests[i], buf, &contentlen, sizeof(buf)) != 1) {
  79. fprintf(stderr, "%s() tests[%zu] <%s> failure\n", __FUNCTION__, i, tests[i]);
  80. error_print();
  81. return -1;
  82. }
  83. x509_crl_print(stderr, 0, 0, "CRL", buf, contentlen);
  84. }
  85. printf("%s() ok\n", __FUNCTION__);
  86. return 1;
  87. }
  88. int main(void)
  89. {
  90. if (test_http_parse_uri() != 1) { error_print(); return -1; }
  91. if (test_http_parse_uri_bad() != 1) { error_print(); return -1; }
  92. if (test_http_get_crl() != 1) { error_print(); return -1; }
  93. printf("%s all tests passed\n", __FILE__);
  94. return 0;
  95. }