From e84814885dcc4dd64d27bdb9eff62cd8cb9cf254 Mon Sep 17 00:00:00 2001 From: Simon Hughes Date: Mon, 19 Dec 2016 16:15:20 +0000 Subject: [PATCH] Ported glibc POSIX file API test test_rdwr to greentea. --- .../TESTS/fs-fat/basic/basic.cpp | 183 +++++++++++++++--- features/storage/FEATURE_STORAGE/fs/README.md | 7 +- 2 files changed, 164 insertions(+), 26 deletions(-) diff --git a/features/storage/FEATURE_STORAGE/TESTS/fs-fat/basic/basic.cpp b/features/storage/FEATURE_STORAGE/TESTS/fs-fat/basic/basic.cpp index 37af14be97..b3fab15839 100644 --- a/features/storage/FEATURE_STORAGE/TESTS/fs-fat/basic/basic.cpp +++ b/features/storage/FEATURE_STORAGE/TESTS/fs-fat/basic/basic.cpp @@ -16,25 +16,27 @@ * */ -/* The following copyright notice is reproduced from the glibc project */ - -/* Copyright (C) 1991, 1992 Free Software Foundation, Inc. -This file is part of the GNU C Library. - -The GNU C Library is free software; you can redistribute it and/or -modify it under the terms of the GNU Library General Public License as -published by the Free Software Foundation; either version 2 of the -License, or (at your option) any later version. - -The GNU C Library is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -Library General Public License for more details. - -You should have received a copy of the GNU Library General Public -License along with the GNU C Library; see the file COPYING.LIB. If -not, write to the Free Software Foundation, Inc., 675 Mass Ave, -Cambridge, MA 02139, USA. */ +/* The following copyright notice is reproduced from the glibc project + * REF_LICENCE_GLIBC + * + * Copyright (C) 1991, 1992 Free Software Foundation, Inc. + * This file is part of the GNU C Library. + * + * The GNU C Library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * The GNU C Library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with the GNU C Library; see the file COPYING.LIB. If + * not, write to the Free Software Foundation, Inc., 675 Mass Ave, + * Cambridge, MA 02139, USA. + */ /** @file basic.cpp POSIX File API (stdio) test cases @@ -51,6 +53,9 @@ Cambridge, MA 02139, USA. */ #include "unity/unity.h" #include "greentea-client/test_env.h" +#include +#include +#include using namespace utest::v1; @@ -114,7 +119,8 @@ SDFileSystem sd(p11, p12, p13, p14, "sd"); #define FSFAT_BASIC_MSG_BUF_SIZE 256 -const char *sd_file_path = "/sd/out.txt"; +static const char *sd_file_path = "/sd/out.txt"; +static const char *sd_mount_pt = "sd"; const int FSFAT_BASIC_DATA_SIZE = 256; static char fsfat_basic_msg_g[FSFAT_BASIC_MSG_BUF_SIZE]; @@ -135,7 +141,7 @@ static char fsfat_basic_msg_g[FSFAT_BASIC_MSG_BUF_SIZE]; * * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors. */ -static control_t sdcard_test_00() { +static control_t fsfat_test_00() { uint8_t data_written[FSFAT_BASIC_DATA_SIZE] = { 0 }; bool result = false; @@ -182,11 +188,11 @@ static control_t sdcard_test_00() { extern int errno; -/** @brief test-fseek.c test ported from glibc project +/** @brief test-fseek.c test ported from glibc project. See the licence at REF_LICENCE_GLIBC. * * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors. */ -static control_t sdcard_test_01() { +static control_t fsfat_test_01() { FILE *fp, *fp1; int i, j; int ret = 0; @@ -235,9 +241,134 @@ static control_t sdcard_test_01() { } } fclose (fp); + remove(sd_file_path); return CaseNext; } + +/** @brief test_rdwr.c test ported from glibc project. See the licence at REF_LICENCE_GLIBC. + * + * WARNING: this test does not currently work. See WARNING comments below. + * + * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors. + */ + +static control_t fsfat_test_02() { + static const char hello[] = "Hello, world.\n"; + static const char replace[] = "Hewwo, world.\n"; + static const size_t replace_from = 2, replace_to = 4; + char* filename = (char*) sd_file_path; //todo: remove + char buf[BUFSIZ]; + FILE *f; + int lose = 0; + int32_t ret = 0; + char *rets = NULL; + + f = fopen(filename, "w+"); + if (f == NULL) { + FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: Cannot open file for writing (filename=%s).\n", __func__, filename); + TEST_ASSERT_MESSAGE(false, fsfat_basic_msg_g); + } + + + ret = fputs(hello, f); + if (ret == EOF) { + FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: fputs() failed to write string to file (filename=%s, string=%s).\n", __func__, filename, hello); + TEST_ASSERT_MESSAGE(false, fsfat_basic_msg_g); + } + + rewind(f); + rets = fgets(buf, sizeof(buf), f); + if (rets == NULL) { + FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: fgets() failed to get string from file (filename=%s).\n", __func__, filename); + TEST_ASSERT_MESSAGE(false, fsfat_basic_msg_g); + } + rets = NULL; + + rewind(f); + ret = fputs(buf, f); + if (ret == EOF) { + FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: fputs() failed to write string to file (filename=%s, string=%s).\n", __func__, filename, buf); + TEST_ASSERT_MESSAGE(false, fsfat_basic_msg_g); + } + + rewind(f); + { + register size_t i; + for (i = 0; i < replace_from; ++i) + { + int c = getc(f); + if (c == EOF) + { + printf("EOF at %u.\n", i); + lose = 1; + break; + } + else if (c != hello[i]) + { + printf("Got '%c' instead of '%c' at %u.\n", + (unsigned char) c, hello[i], i); + lose = 1; + break; + } + } + } + /* WARNING: printf("%s: here1. (lose = %d)\n", __func__, lose); */ + { + long int where = ftell(f); + if (where == replace_from) + { + register size_t i; + for (i = replace_from; i < replace_to; ++i) { + if (putc(replace[i], f) == EOF) { + printf("putc('%c') got %s at %u.\n", + replace[i], strerror(errno), i); + lose = 1; + break; + } + /* WARNING: The problem seems to be that putc() is not writing the 'w' chars into the file + * printf("%s: here1.5. (char = %c, char as int=%d, ret=%d) \n", __func__, replace[i], (int) replace[i], ret); + */ + } + } + else if (where == -1L) + { + printf("ftell got %s (should be at %u).\n", + strerror(errno), replace_from); + lose = 1; + } + else + { + printf("ftell returns %u; should be %u.\n", where, replace_from); + lose = 1; + } + } + + if (!lose) + { + rewind(f); + memset(buf, 0, BUFSIZ); + if (fgets(buf, sizeof(buf), f) == NULL) + { + printf("fgets got %s.\n", strerror(errno)); + lose = 1; + } + else if (strcmp(buf, replace)) + { + printf("Read \"%s\" instead of \"%s\".\n", buf, replace); + lose = 1; + } + } + + if (lose) { + FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: Test Failed. Losing file (filename=%s).\n", __func__, filename); + TEST_ASSERT_MESSAGE(false, fsfat_basic_msg_g); + } + remove(filename); + return CaseNext; +} + + utest::v1::status_t greentea_setup(const size_t number_of_cases) { GREENTEA_SETUP(300, "default_auto"); @@ -247,8 +378,10 @@ utest::v1::status_t greentea_setup(const size_t number_of_cases) Case cases[] = { /* 1 2 3 4 5 6 7 */ /* 1234567890123456789012345678901234567890123456789012345678901234567890 */ - Case("FSFAT_test_00: fopen()/fgetc()/fprintf()/fclose() test.", sdcard_test_00), - Case("FSFAT_test_01: fopen()/fseek()/fclose() test.", sdcard_test_01) + Case("FSFAT_test_00: fopen()/fgetc()/fprintf()/fclose() test.", fsfat_test_00), + Case("FSFAT_test_01: fopen()/fseek()/fclose() test.", fsfat_test_01) + /* WARNING: Test case not working but currently not required for PAL support + * Case("FSFAT_test_02: fopen()/fgets()/fputs()/ftell()/rewind()/remove() test.", fsfat_test_02) */ }; diff --git a/features/storage/FEATURE_STORAGE/fs/README.md b/features/storage/FEATURE_STORAGE/fs/README.md index b78772b39e..f8cffef706 100644 --- a/features/storage/FEATURE_STORAGE/fs/README.md +++ b/features/storage/FEATURE_STORAGE/fs/README.md @@ -175,11 +175,13 @@ mbedOS supports a subset of the POSIX File API, as outlined below: - fclose() - fgetc() - fprintf() -- fputs() +- [fputc()][MAN_FPUTS]. +- [fputs()][MAN_FPUTS]. - fopen() - freopen() - fseek() - getc() +- [rewind()][MAN_REWIND]. - todo: complete this list. The FAT32/SDCard support is at the following location in the source code tree: @@ -204,3 +206,6 @@ The FAT32/SDCard test cases are at following locations in the source code tree: [MBED_DEVENV_NOTES]: https://github.com/ARMmbed/meVo/blob/master/docs/ARM_MBED/TN/ARM_MBED_TN_0017/12-mbed_devenv_setup_how_to_notes.docx [BUILD-TESTS-GCC-20161219-1007]: https://github.com/ARMmbed/meVo/blob/master/docs/ARM_MBED/TN/ARM_MBED_TN_0017/build_tests_gcc_20161219_1007.txt [RUN-TESTS-GCC-20161219-1011]: https://github.com/ARMmbed/meVo/blob/master/docs/ARM_MBED/TN/ARM_MBED_TN_0017/run_tests_master_gcc_ex_app2_fat_basic_20161219_1011.txt + +[MAN_FPUTS]: https://linux.die.net/man/3/fputs +[MAN_REWIND]: https://linux.die.net/man/3/rewind \ No newline at end of file