unit3200.c (5340B)
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 "unitcheck.h" 25 #include "curl_get_line.h" 26 #include "memdebug.h" 27 28 static CURLcode test_unit3200(char *arg) 29 { 30 UNITTEST_BEGIN_SIMPLE 31 32 #if !defined(CURL_DISABLE_COOKIES) || !defined(CURL_DISABLE_ALTSVC) || \ 33 !defined(CURL_DISABLE_HSTS) || !defined(CURL_DISABLE_NETRC) 34 35 #if defined(CURL_GNUC_DIAG) || defined(__clang__) 36 #pragma GCC diagnostic push 37 #pragma GCC diagnostic ignored "-Woverlength-strings" 38 #endif 39 40 /* The test XML does not supply a way to write files without newlines 41 * so we write our own 42 */ 43 44 #define C64 "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" 45 #define C256 C64 C64 C64 C64 46 #define C1024 C256 C256 C256 C256 47 #define C4096 C1024 C1024 C1024 C1024 48 49 static const char *filecontents[] = { 50 /* Both should be read */ 51 "LINE1\n" 52 "LINE2 NEWLINE\n", 53 54 /* Both should be read */ 55 "LINE1\n" 56 "LINE2 NONEWLINE", 57 58 /* Only first should be read */ 59 "LINE1\n" 60 C4096, 61 62 /* First line should be read */ 63 "LINE1\n" 64 C4096 "SOME EXTRA TEXT", 65 66 /* Only first should be read */ 67 "LINE1\n" 68 C4096 "SOME EXTRA TEXT\n" 69 "LINE3\n", 70 71 "LINE1\x1aTEST" 72 }; 73 74 #if defined(CURL_GNUC_DIAG) || defined(__clang__) 75 #pragma GCC diagnostic pop 76 #endif 77 78 size_t i; 79 int rc = 0; 80 for(i = 0; i < CURL_ARRAYSIZE(filecontents); i++) { 81 FILE *fp; 82 struct dynbuf buf; 83 size_t len = 4096; 84 char *line; 85 curlx_dyn_init(&buf, len); 86 87 fp = fopen(arg, "wb"); 88 abort_unless(fp != NULL, "Cannot open testfile"); 89 fwrite(filecontents[i], 1, strlen(filecontents[i]), fp); 90 fclose(fp); 91 92 fp = fopen(arg, "rb"); 93 abort_unless(fp != NULL, "Cannot open testfile"); 94 95 curl_mfprintf(stderr, "Test %zd...", i); 96 switch(i) { 97 case 0: 98 rc = Curl_get_line(&buf, fp); 99 line = curlx_dyn_ptr(&buf); 100 fail_unless(rc && line && !strcmp("LINE1\n", line), 101 "First line failed (1)"); 102 rc = Curl_get_line(&buf, fp); 103 line = curlx_dyn_ptr(&buf); 104 fail_unless(rc && line && !strcmp("LINE2 NEWLINE\n", line), 105 "Second line failed (1)"); 106 rc = Curl_get_line(&buf, fp); 107 abort_unless(!curlx_dyn_len(&buf), "Missed EOF (1)"); 108 break; 109 case 1: 110 rc = Curl_get_line(&buf, fp); 111 line = curlx_dyn_ptr(&buf); 112 fail_unless(rc && line && !strcmp("LINE1\n", line), 113 "First line failed (2)"); 114 rc = Curl_get_line(&buf, fp); 115 line = curlx_dyn_ptr(&buf); 116 fail_unless(rc && line && !strcmp("LINE2 NONEWLINE\n", line), 117 "Second line failed (2)"); 118 rc = Curl_get_line(&buf, fp); 119 abort_unless(!curlx_dyn_len(&buf), "Missed EOF (2)"); 120 break; 121 case 2: 122 rc = Curl_get_line(&buf, fp); 123 line = curlx_dyn_ptr(&buf); 124 fail_unless(rc && line && !strcmp("LINE1\n", line), 125 "First line failed (3)"); 126 rc = Curl_get_line(&buf, fp); 127 fail_unless(!curlx_dyn_len(&buf), 128 "Did not detect max read on EOF (3)"); 129 break; 130 case 3: 131 rc = Curl_get_line(&buf, fp); 132 line = curlx_dyn_ptr(&buf); 133 fail_unless(rc && line && !strcmp("LINE1\n", line), 134 "First line failed (4)"); 135 rc = Curl_get_line(&buf, fp); 136 fail_unless(!curlx_dyn_len(&buf), 137 "Did not ignore partial on EOF (4)"); 138 break; 139 case 4: 140 rc = Curl_get_line(&buf, fp); 141 line = curlx_dyn_ptr(&buf); 142 fail_unless(rc && line && !strcmp("LINE1\n", line), 143 "First line failed (5)"); 144 rc = Curl_get_line(&buf, fp); 145 fail_unless(!curlx_dyn_len(&buf), 146 "Did not bail out on too long line"); 147 break; 148 case 5: 149 rc = Curl_get_line(&buf, fp); 150 line = curlx_dyn_ptr(&buf); 151 fail_unless(rc && line && !strcmp("LINE1\x1aTEST\n", line), 152 "Missed/Misinterpreted ^Z (6)"); 153 rc = Curl_get_line(&buf, fp); 154 abort_unless(!curlx_dyn_len(&buf), "Missed EOF (6)"); 155 break; 156 default: 157 abort_unless(1, "Unknown case"); 158 break; 159 } 160 curlx_dyn_free(&buf); 161 fclose(fp); 162 curl_mfprintf(stderr, "OK\n"); 163 } 164 return (CURLcode)rc; 165 166 #endif 167 168 UNITTEST_END_SIMPLE 169 }