propagator/test_parse_content_length.c
Alexey Sheplyakov b212b3d031 ftp_get_filesize: handle directories with "many" files
So propagator can download ISO from a directory with "many" [1]
files and display a nice progress bar.

[1] In fact 12 of them or so is enough.

The `LIST` ftp command produces quite a lot of noise^W data.
For instance, this single entry:

-rw-r--r--    1 ftp      ftp      1862483968 Jul 28 00:23
alt-p10-gnome3-20210728-aarch64.iso

takes 95 bytes, hence it takes only 21 files to overflow the fixed
size 2000 bytes buffer, so `ftp_get_filesize` is unable to figure
out the size of 22nd and subsequent files. Process the output of
the `LIST` command line by line to avoid the problem.

While at it avoid buffer underruns (i.e. when the server reply
contains no whitespace at all).
2021-10-07 13:21:37 +04:00

64 lines
1.8 KiB
C

#include <stdio.h>
#include <stdarg.h>
#include "test_common.c" /* yes, include the source file */
#include "url.c" /* yes, include the source file */
static int test_parse_content_length(const char *headers, unsigned long expected, int shouldfail) {
int err;
unsigned long val;
err = parse_content_length(headers, &val);
if (err < 0 && !shouldfail) {
log_message("%s: unexpectedly failed on input '%s'", __func__, headers);
return 2;
}
if (err == 0 && shouldfail) {
log_message("%s: unexpectedly passed on invalid input '%s'", __func__, headers);
return 3;
}
if (!shouldfail && val != expected) {
log_message("%s: expected %lu, got %lu on input '%s'",
__func__, expected, val, headers);
return 5;
}
if (!shouldfail && val == expected) {
log_message("%s: OK: '%s'", __func__, headers);
}
return 0;
}
#define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
int main(int argc, char **argv) {
int i, err = 0;
const char *invalid_inputs[] = {
"", /* empty */
"Abyrvalg: foobar", /* no Content-Length: */
"Content-Length: xyz", /* no digits */
"Content-Length: 12xyz", /* invalid characters */
"Content-Length: 12 34", /* stray symbols */
"Content-Length: 123456789012345678901234567890", /* too big */
};
const char *valid_inputs[] = {
"Content-Length: 1234",
"Content-Length: 1234",
"Content-Length: 1234 ",
"Content-Length: 1234 ",
"Content-Length: 1234 \r\n\r\n",
};
for (i = 0; i < ARRAY_SIZE(invalid_inputs); i++) {
err += test_parse_content_length(invalid_inputs[i], 0,
/* shouldfail = */ 1);
}
for (i = 0; i < ARRAY_SIZE(valid_inputs); i++) {
err += test_parse_content_length(valid_inputs[i], 1234,
/* shouldfail = */ 0);
}
return err;
}
/* compile command:
gcc -O2 -g -flto -o test_parse_content_length.c test_parse_content_length.c
*/