[LTP] [PATCH v2 2/6] fs: rewrite stream01 test using new API
Cyril Hrubis
chrubis@suse.cz
Tue Apr 7 16:57:03 CEST 2026
Hi!
> +static void read_file(const char *file, const char *str, size_t n)
> +{
> + char buf[n];
> + FILE *stream;
> + size_t len;
>
> -#define PASSED 1
> -#define FAILED 0
> + memset(buf, 0, sizeof(buf));
>
> -/* XXX: add setup and cleanup. */
> + stream = SAFE_FOPEN(file, "r");
> + len = SAFE_FREAD(buf, n, n, stream);
Huh, we pass buffer of size n but ask it to read n * n bytes? That does
not seem to be right.
Also the buffer must always be bigger than n to allow the fread() to
read more bytes that we wrote, in a case that the file was written into
intcorrectly. So we need something like:
char buf[128];
...
len = SAFE_FREAD(buf, 1, sizeof(buf), stream);
> + SAFE_FCLOSE(stream);
>
> -char progname[] = "stream01()";
> -char tempfile1[40] = "";
> -char tempfile2[40] = "";
> + TST_EXP_EXPR(len == n, "Read the entire %s file buffer", file);
> + TST_EXP_EQ_STRN(buf, str, len);
> +}
>
> -/*--------------------------------------------------------------------*/
> -int main(int ac, char *av[])
> +static void run(void)
> {
> FILE *stream;
> - char buf[10];
> - int i;
> - int lc;
> -
> - /*
> - * parse standard options
> - */
> - tst_parse_opts(ac, av, NULL, NULL);
>
> - local_flag = PASSED;
> - tst_tmpdir();
> - for (lc = 0; TEST_LOOPING(lc); lc++) {
> + tst_res(TINFO, "Write %s file", FILENAME1);
> + stream = SAFE_FOPEN(FILENAME1, "a+");
> + SAFE_FWRITE(buff_file1, strlen(buff_file1), 3, stream);
>
> - sprintf(tempfile1, "stream011.%d", getpid());
> - sprintf(tempfile2, "stream012.%d", getpid());
> - /*--------------------------------------------------------------------*/
> - //block0:
> - if ((stream = fopen(tempfile1, "a+")) == NULL) {
> - tst_brkm(TFAIL, NULL, "fopen(%s) a+ failed: %s",
> - tempfile1,
> - strerror(errno));
> - }
> - fwrite("a", 1, 1, stream);
> - if ((stream = freopen(tempfile2, "a+", stream)) == NULL) {
> - tst_brkm(TFAIL | TERRNO, NULL, "freopen(%s) a+ failed",
> - tempfile2);
> - }
> - fwrite("a", 1, 1, stream);
> - fclose(stream);
> + tst_res(TINFO, "Write %s file streaming into %s file", FILENAME2, FILENAME1);
> + stream = SAFE_FREOPEN(FILENAME2, "a+", stream);
> + SAFE_FWRITE(buff_file2, strlen(buff_file2), 3, stream);
And here as well we ask to write strlen(buff_fiel2) * 3
bytes but the buff_file2 is only 3 characters.
> + SAFE_FCLOSE(stream);
>
> - local_flag = PASSED;
> + read_file(FILENAME1, buff_file1, 3);
> + read_file(FILENAME2, buff_file2, 3);
>
> - /*--------------------------------------------------------------------*/
> - unlink(tempfile1);
> - unlink(tempfile2);
> -
> - } /* end for */
> - tst_rmdir();
> - tst_exit();
> + SAFE_UNLINK(FILENAME1);
> + SAFE_UNLINK(FILENAME2);
> }
> +
> +static struct tst_test test = {
> + .test_all = run,
> + .needs_tmpdir = 1,
> +};
>
> --
> 2.51.0
>
>
> --
> Mailing list info: https://lists.linux.it/listinfo/ltp
--
Cyril Hrubis
chrubis@suse.cz
More information about the ltp
mailing list