unit1620.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curlcheck.h"
  25. #include "urldata.h"
  26. #include "url.h"
  27. #include "memdebug.h" /* LAST include file */
  28. static CURLcode unit_setup(void)
  29. {
  30. CURLcode res = CURLE_OK;
  31. global_init(CURL_GLOBAL_ALL);
  32. return res;
  33. }
  34. static void unit_stop(void)
  35. {
  36. curl_global_cleanup();
  37. }
  38. static void test_parse(
  39. const char *input,
  40. const char *exp_username,
  41. const char *exp_password,
  42. const char *exp_options)
  43. {
  44. char *userstr = NULL;
  45. char *passwdstr = NULL;
  46. char *options = NULL;
  47. CURLcode rc = Curl_parse_login_details(input, strlen(input),
  48. &userstr, &passwdstr, &options);
  49. fail_unless(rc == CURLE_OK, "Curl_parse_login_details() failed");
  50. fail_unless(!!exp_username == !!userstr, "username expectation failed");
  51. fail_unless(!!exp_password == !!passwdstr, "password expectation failed");
  52. fail_unless(!!exp_options == !!options, "options expectation failed");
  53. if(!unitfail) {
  54. fail_unless(!exp_username || strcmp(userstr, exp_username) == 0,
  55. "userstr should be equal to exp_username");
  56. fail_unless(!exp_password || strcmp(passwdstr, exp_password) == 0,
  57. "passwdstr should be equal to exp_password");
  58. fail_unless(!exp_options || strcmp(options, exp_options) == 0,
  59. "options should be equal to exp_options");
  60. }
  61. free(userstr);
  62. free(passwdstr);
  63. free(options);
  64. }
  65. UNITTEST_START
  66. {
  67. CURLcode rc;
  68. struct Curl_easy *empty;
  69. enum dupstring i;
  70. bool async = FALSE;
  71. bool protocol_connect = FALSE;
  72. rc = Curl_open(&empty);
  73. if(rc)
  74. goto unit_test_abort;
  75. fail_unless(rc == CURLE_OK, "Curl_open() failed");
  76. rc = Curl_connect(empty, &async, &protocol_connect);
  77. fail_unless(rc == CURLE_URL_MALFORMAT,
  78. "Curl_connect() failed to return CURLE_URL_MALFORMAT");
  79. fail_unless(empty->magic == CURLEASY_MAGIC_NUMBER,
  80. "empty->magic should be equal to CURLEASY_MAGIC_NUMBER");
  81. /* double invoke to ensure no dependency on internal state */
  82. rc = Curl_connect(empty, &async, &protocol_connect);
  83. fail_unless(rc == CURLE_URL_MALFORMAT,
  84. "Curl_connect() failed to return CURLE_URL_MALFORMAT");
  85. rc = Curl_init_userdefined(empty);
  86. fail_unless(rc == CURLE_OK, "Curl_userdefined() failed");
  87. rc = Curl_init_do(empty, empty->conn);
  88. fail_unless(rc == CURLE_OK, "Curl_init_do() failed");
  89. test_parse("hostname", "hostname", NULL, NULL);
  90. test_parse("user:password", "user", "password", NULL);
  91. test_parse("user:password;options", "user", "password", "options");
  92. test_parse("user:password;options;more", "user", "password", "options;more");
  93. test_parse("", "", NULL, NULL);
  94. test_parse(":", "", "", NULL);
  95. test_parse(":;", "", "", NULL);
  96. test_parse(":password", "", "password", NULL);
  97. test_parse(":password;", "", "password", NULL);
  98. test_parse(";options", "", NULL, "options");
  99. test_parse("user;options", "user", NULL, "options");
  100. test_parse("user:;options", "user", "", "options");
  101. test_parse("user;options:password", "user", "password", "options");
  102. test_parse("user;options:", "user", "", "options");
  103. Curl_freeset(empty);
  104. for(i = (enum dupstring)0; i < STRING_LAST; i++) {
  105. fail_unless(empty->set.str[i] == NULL,
  106. "Curl_free() did not set to NULL");
  107. }
  108. rc = Curl_close(&empty);
  109. fail_unless(rc == CURLE_OK, "Curl_close() failed");
  110. }
  111. UNITTEST_STOP