2022-11-23 16:56:39 +03:00
|
|
|
/***************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2023-01-02 15:51:48 +03:00
|
|
|
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
|
2022-11-23 16:56:39 +03:00
|
|
|
*
|
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
|
|
|
* are also available at https://curl.se/docs/copyright.html.
|
|
|
|
*
|
|
|
|
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
* copies of the Software, and permit persons to whom the Software is
|
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: curl
|
|
|
|
*
|
|
|
|
***************************************************************************/
|
|
|
|
#include "curlcheck.h"
|
|
|
|
#include "curl_get_line.h"
|
|
|
|
|
2023-08-17 15:43:37 +03:00
|
|
|
#if !defined(CURL_DISABLE_COOKIES) || !defined(CURL_DISABLE_ALTSVC) || \
|
|
|
|
!defined(CURL_DISABLE_HSTS) || !defined(CURL_DISABLE_NETRC)
|
|
|
|
|
2022-11-23 16:56:39 +03:00
|
|
|
/* The test XML does not supply a way to write files without newlines
|
|
|
|
* so we write our own
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define C64 "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
|
|
|
|
#define C256 C64 C64 C64 C64
|
|
|
|
#define C1024 C256 C256 C256 C256
|
|
|
|
#define C4096 C1024 C1024 C1024 C1024
|
|
|
|
|
|
|
|
static CURLcode unit_setup(void)
|
|
|
|
{
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static CURLcode unit_stop(void)
|
|
|
|
{
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef __GNUC__
|
2023-11-17 22:13:13 +03:00
|
|
|
#pragma GCC diagnostic push
|
2022-11-23 16:56:39 +03:00
|
|
|
#pragma GCC diagnostic ignored "-Woverlength-strings"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define NUMTESTS 6
|
|
|
|
static const char *filecontents[] = {
|
|
|
|
/* Both should be read */
|
|
|
|
"LINE1\n"
|
|
|
|
"LINE2 NEWLINE\n",
|
|
|
|
|
|
|
|
/* Both should be read */
|
|
|
|
"LINE1\n"
|
|
|
|
"LINE2 NONEWLINE",
|
|
|
|
|
|
|
|
/* Only first should be read */
|
|
|
|
"LINE1\n"
|
|
|
|
C4096,
|
|
|
|
|
|
|
|
/* First line should be read */
|
|
|
|
"LINE1\n"
|
|
|
|
C4096 "SOME EXTRA TEXT",
|
|
|
|
|
2024-02-06 12:15:52 +03:00
|
|
|
/* Only first should be read */
|
2022-11-23 16:56:39 +03:00
|
|
|
"LINE1\n"
|
|
|
|
C4096 "SOME EXTRA TEXT\n"
|
|
|
|
"LINE3\n",
|
|
|
|
|
|
|
|
"LINE1\x1aTEST"
|
|
|
|
};
|
|
|
|
|
|
|
|
#ifdef __GNUC__
|
|
|
|
#pragma GCC diagnostic warning "-Woverlength-strings"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
UNITTEST_START
|
|
|
|
size_t i;
|
2024-02-06 12:15:52 +03:00
|
|
|
int rc = 0;
|
2022-11-23 16:56:39 +03:00
|
|
|
for(i = 0; i < NUMTESTS; i++) {
|
|
|
|
FILE *fp;
|
2024-02-06 12:15:52 +03:00
|
|
|
struct dynbuf buf;
|
2024-05-11 22:36:05 +03:00
|
|
|
size_t len = 4096;
|
2022-11-23 16:56:39 +03:00
|
|
|
char *line;
|
2024-02-06 12:15:52 +03:00
|
|
|
Curl_dyn_init(&buf, len);
|
2022-11-23 16:56:39 +03:00
|
|
|
|
2023-03-29 08:30:50 +03:00
|
|
|
fp = fopen(arg, "wb");
|
2022-11-23 16:56:39 +03:00
|
|
|
abort_unless(fp != NULL, "Cannot open testfile");
|
|
|
|
fwrite(filecontents[i], 1, strlen(filecontents[i]), fp);
|
|
|
|
fclose(fp);
|
|
|
|
|
2023-03-29 08:30:50 +03:00
|
|
|
fp = fopen(arg, "rb");
|
2022-11-23 16:56:39 +03:00
|
|
|
abort_unless(fp != NULL, "Cannot open testfile");
|
|
|
|
|
2023-08-06 23:29:40 +03:00
|
|
|
fprintf(stderr, "Test %zd...", i);
|
2022-11-23 16:56:39 +03:00
|
|
|
switch(i) {
|
|
|
|
case 0:
|
2024-02-06 12:15:52 +03:00
|
|
|
rc = Curl_get_line(&buf, fp);
|
|
|
|
line = Curl_dyn_ptr(&buf);
|
2022-11-23 16:56:39 +03:00
|
|
|
fail_unless(line && !strcmp("LINE1\n", line),
|
2024-02-06 12:15:52 +03:00
|
|
|
"First line failed (1)");
|
|
|
|
rc = Curl_get_line(&buf, fp);
|
|
|
|
line = Curl_dyn_ptr(&buf);
|
2022-11-23 16:56:39 +03:00
|
|
|
fail_unless(line && !strcmp("LINE2 NEWLINE\n", line),
|
2024-02-06 12:15:52 +03:00
|
|
|
"Second line failed (1)");
|
|
|
|
rc = Curl_get_line(&buf, fp);
|
|
|
|
abort_unless(!Curl_dyn_len(&buf), "Missed EOF (1)");
|
2022-11-23 16:56:39 +03:00
|
|
|
break;
|
|
|
|
case 1:
|
2024-02-06 12:15:52 +03:00
|
|
|
rc = Curl_get_line(&buf, fp);
|
|
|
|
line = Curl_dyn_ptr(&buf);
|
2022-11-23 16:56:39 +03:00
|
|
|
fail_unless(line && !strcmp("LINE1\n", line),
|
2024-02-06 12:15:52 +03:00
|
|
|
"First line failed (2)");
|
|
|
|
rc = Curl_get_line(&buf, fp);
|
|
|
|
line = Curl_dyn_ptr(&buf);
|
2022-11-23 16:56:39 +03:00
|
|
|
fail_unless(line && !strcmp("LINE2 NONEWLINE\n", line),
|
2024-02-06 12:15:52 +03:00
|
|
|
"Second line failed (2)");
|
|
|
|
rc = Curl_get_line(&buf, fp);
|
|
|
|
abort_unless(!Curl_dyn_len(&buf), "Missed EOF (2)");
|
2022-11-23 16:56:39 +03:00
|
|
|
break;
|
|
|
|
case 2:
|
2024-02-06 12:15:52 +03:00
|
|
|
rc = Curl_get_line(&buf, fp);
|
|
|
|
line = Curl_dyn_ptr(&buf);
|
2022-11-23 16:56:39 +03:00
|
|
|
fail_unless(line && !strcmp("LINE1\n", line),
|
2024-02-06 12:15:52 +03:00
|
|
|
"First line failed (3)");
|
|
|
|
rc = Curl_get_line(&buf, fp);
|
|
|
|
fail_unless(!Curl_dyn_len(&buf),
|
|
|
|
"Did not detect max read on EOF (3)");
|
2022-11-23 16:56:39 +03:00
|
|
|
break;
|
|
|
|
case 3:
|
2024-02-06 12:15:52 +03:00
|
|
|
rc = Curl_get_line(&buf, fp);
|
|
|
|
line = Curl_dyn_ptr(&buf);
|
2022-11-23 16:56:39 +03:00
|
|
|
fail_unless(line && !strcmp("LINE1\n", line),
|
2024-02-06 12:15:52 +03:00
|
|
|
"First line failed (4)");
|
|
|
|
rc = Curl_get_line(&buf, fp);
|
|
|
|
fail_unless(!Curl_dyn_len(&buf),
|
|
|
|
"Did not ignore partial on EOF (4)");
|
2022-11-23 16:56:39 +03:00
|
|
|
break;
|
|
|
|
case 4:
|
2024-02-06 12:15:52 +03:00
|
|
|
rc = Curl_get_line(&buf, fp);
|
|
|
|
line = Curl_dyn_ptr(&buf);
|
2022-11-23 16:56:39 +03:00
|
|
|
fail_unless(line && !strcmp("LINE1\n", line),
|
2024-02-06 12:15:52 +03:00
|
|
|
"First line failed (5)");
|
|
|
|
rc = Curl_get_line(&buf, fp);
|
|
|
|
fail_unless(!Curl_dyn_len(&buf),
|
|
|
|
"Did not bail out on too long line");
|
2022-11-23 16:56:39 +03:00
|
|
|
break;
|
|
|
|
case 5:
|
2024-02-06 12:15:52 +03:00
|
|
|
rc = Curl_get_line(&buf, fp);
|
|
|
|
line = Curl_dyn_ptr(&buf);
|
2022-11-23 16:56:39 +03:00
|
|
|
fail_unless(line && !strcmp("LINE1\x1aTEST\n", line),
|
2024-02-06 12:15:52 +03:00
|
|
|
"Missed/Misinterpreted ^Z (6)");
|
|
|
|
rc = Curl_get_line(&buf, fp);
|
|
|
|
abort_unless(!Curl_dyn_len(&buf), "Missed EOF (6)");
|
2022-11-23 16:56:39 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
abort_unless(1, "Unknown case");
|
|
|
|
break;
|
|
|
|
}
|
2024-02-06 12:15:52 +03:00
|
|
|
Curl_dyn_free(&buf);
|
2022-11-23 16:56:39 +03:00
|
|
|
fclose(fp);
|
|
|
|
fprintf(stderr, "OK\n");
|
|
|
|
}
|
2024-05-11 22:36:05 +03:00
|
|
|
return (CURLcode)rc;
|
2022-11-23 16:56:39 +03:00
|
|
|
UNITTEST_STOP
|
2023-08-17 15:43:37 +03:00
|
|
|
|
2023-11-17 22:13:13 +03:00
|
|
|
#ifdef __GNUC__
|
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
#endif
|
|
|
|
|
2023-08-17 15:43:37 +03:00
|
|
|
#else
|
|
|
|
static CURLcode unit_setup(void)
|
|
|
|
{
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
static void unit_stop(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
UNITTEST_START
|
|
|
|
UNITTEST_STOP
|
|
|
|
|
|
|
|
#endif
|