Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 | /* * Copyright (c) 2018 Intel Corporation. * * SPDX-License-Identifier: Apache-2.0 */ #include <string.h> #include <posix/unistd.h> #include "test_fs.h" const char test_str[] = "hello world!"; int file; static int test_file_open(void) { int res; TC_PRINT("\nOpen tests:\n"); res = open(TEST_FILE, O_RDWR); if (res < 0) { TC_PRINT("Failed opening file [%d]\n", res); return res; } file = res; TC_PRINT("Opened file %s\n", TEST_FILE); return 0; } int test_file_write(void) { int brw; int res; TC_PRINT("\nWrite tests:\n"); res = lseek(file, 0, SEEK_SET); if (res) { TC_PRINT("lseek failed [%d]\n", res); close(file); return res; } TC_PRINT("Data written:\"%s\"\n\n", test_str); brw = write(file, (char *)test_str, strlen(test_str)); if (brw < 0) { TC_PRINT("Failed writing to file [%d]\n", brw); close(file); return brw; } if (brw < strlen(test_str)) { TC_PRINT("Unable to complete write. Volume full.\n"); TC_PRINT("Number of bytes written: [%d]\n", brw); close(file); return TC_FAIL; } TC_PRINT("Data successfully written!\n"); return res; } static int test_file_read(void) { int brw; int res; char read_buff[80]; size_t sz = strlen(test_str); TC_PRINT("\nRead tests:\n"); res = lseek(file, 0, SEEK_SET); if (res) { TC_PRINT("lseek failed [%d]\n", res); close(file); return res; } brw = read(file, read_buff, sz); if (brw < 0) { TC_PRINT("Failed reading file [%d]\n", brw); close(file); return brw; } read_buff[brw] = 0; TC_PRINT("Data read:\"%s\"\n\n", read_buff); if (strcmp(test_str, read_buff)) { TC_PRINT("Error - Data read does not match data written\n"); TC_PRINT("Data read:\"%s\"\n\n", read_buff); return TC_FAIL; } TC_PRINT("Data read matches data written\n"); return res; } static int test_file_close(void) { int res; TC_PRINT("\nClose tests:\n"); res = close(file); if (res) { TC_PRINT("Error closing file [%d]\n", res); return res; } TC_PRINT("Closed file %s\n", TEST_FILE); return res; } static int test_file_delete(void) { int res; TC_PRINT("\nDelete tests:\n"); res = unlink(TEST_FILE); if (res) { TC_PRINT("Error deleting file [%d]\n", res); return res; } TC_PRINT("File (%s) deleted successfully!\n", TEST_FILE); return res; } /** * @brief Test for POSIX open API * * @details Test opens new file through POSIX open API. */ void test_fs_open(void) { zassert_true(test_file_open() == TC_PASS, NULL); } /** * @brief Test for POSIX write API * * @details Test writes some data through POSIX write API. */ void test_fs_write(void) { zassert_true(test_file_write() == TC_PASS, NULL); } /** * @brief Test for POSIX write API * * @details Test reads data back through POSIX read API. */ void test_fs_read(void) { zassert_true(test_file_read() == TC_PASS, NULL); } /** * @brief Test for POSIX close API * * @details Test closes the open file through POSIX close API. */ void test_fs_close(void) { zassert_true(test_file_close() == TC_PASS, NULL); } /** * @brief Test for POSIX unlink API * * @details Test deletes a file through POSIX unlink API. */ void test_fs_unlink(void) { zassert_true(test_file_delete() == TC_PASS, NULL); } |