1 /*
2 * Copyright (c) 2018 Intel Corporation.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/ztest.h>
8 #include <string.h>
9 #include <zephyr/fs/fs.h>
10 #include "test_common.h"
11
test_file_open(struct fs_file_t * filep,const char * file_path)12 int test_file_open(struct fs_file_t *filep, const char *file_path)
13 {
14 int res;
15
16 TC_PRINT("\nOpen tests:\n");
17
18 if (check_file_dir_exists(file_path)) {
19 TC_PRINT("Opening existing file %s\n", file_path);
20 } else {
21 TC_PRINT("Creating new file %s\n", file_path);
22 }
23
24 /* Verify fs_open() */
25 res = fs_open(filep, file_path, FS_O_CREATE | FS_O_RDWR);
26 if (res) {
27 TC_PRINT("Failed opening file [%d]\n", res);
28 return res;
29 }
30
31 TC_PRINT("Opened file %s\n", file_path);
32
33 return res;
34 }
35
test_file_write(struct fs_file_t * filep,const char * test_str)36 int test_file_write(struct fs_file_t *filep, const char *test_str)
37 {
38 ssize_t brw;
39 int res;
40
41 TC_PRINT("\nWrite tests:\n");
42
43 /* Verify fs_seek() */
44 res = fs_seek(filep, 0, FS_SEEK_SET);
45 if (res) {
46 TC_PRINT("fs_seek failed [%d]\n", res);
47 fs_close(filep);
48 return res;
49 }
50
51 TC_PRINT("Data written:\"%s\"\n\n", test_str);
52
53 /* Verify fs_write() */
54 brw = fs_write(filep, (char *)test_str, strlen(test_str));
55 if (brw < 0) {
56 TC_PRINT("Failed writing to file [%zd]\n", brw);
57 fs_close(filep);
58 return brw;
59 }
60
61 if (brw < strlen(test_str)) {
62 TC_PRINT("Unable to complete write. Volume full.\n");
63 TC_PRINT("Number of bytes written: [%zd]\n", brw);
64 fs_close(filep);
65 return TC_FAIL;
66 }
67
68 TC_PRINT("Data successfully written!\n");
69
70 return res;
71 }
72
test_file_read(struct fs_file_t * filep,const char * test_str)73 int test_file_read(struct fs_file_t *filep, const char *test_str)
74 {
75 ssize_t brw;
76 int res;
77 char read_buff[80];
78 size_t sz = strlen(test_str);
79
80 TC_PRINT("\nRead tests:\n");
81
82 res = fs_seek(filep, 0, FS_SEEK_SET);
83 if (res) {
84 TC_PRINT("fs_seek failed [%d]\n", res);
85 fs_close(filep);
86 return res;
87 }
88
89 /* Verify fs_read() */
90 brw = fs_read(filep, read_buff, sz);
91 if (brw < 0) {
92 TC_PRINT("Failed reading file [%zd]\n", brw);
93 fs_close(filep);
94 return brw;
95 }
96
97 read_buff[brw] = 0;
98
99 TC_PRINT("Data read:\"%s\"\n\n", read_buff);
100
101 if (strcmp(test_str, read_buff)) {
102 TC_PRINT("Error - Data read does not match data written\n");
103 TC_PRINT("Data read:\"%s\"\n\n", read_buff);
104 return TC_FAIL;
105 }
106
107 TC_PRINT("Data read matches data written\n");
108
109 return res;
110 }
111
test_file_close(struct fs_file_t * filep)112 int test_file_close(struct fs_file_t *filep)
113 {
114 int res;
115
116 TC_PRINT("\nClose tests:\n");
117
118 res = fs_close(filep);
119 if (res) {
120 TC_PRINT("Error closing file [%d]\n", res);
121 return res;
122 }
123
124
125 return res;
126 }
127
test_file_delete(const char * file_path)128 int test_file_delete(const char *file_path)
129 {
130 int res;
131
132
133 TC_PRINT("\nDelete tests:\n");
134
135 /* Verify fs_unlink() */
136 res = fs_unlink(file_path);
137 if (res) {
138 TC_PRINT("Error deleting file [%d]\n", res);
139 return res;
140 }
141
142 /* Check if file was deleted */
143 if (check_file_dir_exists(file_path)) {
144 TC_PRINT("Failed deleting %s\n", file_path);
145 return TC_FAIL;
146 }
147
148 TC_PRINT("File (%s) deleted successfully!\n", file_path);
149
150 return res;
151 }
152