Skip to content

Commit 129bae4

Browse files
committed
Filesystem: Added test for basic filesystem operation on cpp api
1 parent 274460b commit 129bae4

File tree

1 file changed

+160
-0
lines changed
  • features/TESTS/filesystem/fat_filesystem

1 file changed

+160
-0
lines changed
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2017 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
#include "mbed.h"
17+
#include "greentea-client/test_env.h"
18+
#include "unity.h"
19+
#include "utest.h"
20+
21+
#include "HeapBlockDevice.h"
22+
#include "FATFileSystem.h"
23+
#include <stdlib.h>
24+
#include "retarget.h"
25+
26+
using namespace utest::v1;
27+
28+
#ifndef MBED_EXTENDED_TESTS
29+
#error [NOT_SUPPORTED] Filesystem tests not supported by default
30+
#endif
31+
32+
// Test block device
33+
#define BLOCK_SIZE 512
34+
HeapBlockDevice bd(128*BLOCK_SIZE, BLOCK_SIZE);
35+
36+
37+
// Test formatting
38+
void test_format() {
39+
int err = FATFileSystem::format(&bd);
40+
TEST_ASSERT_EQUAL(0, err);
41+
}
42+
43+
44+
// Simple test for reading/writing files
45+
template <ssize_t TEST_SIZE>
46+
void test_read_write() {
47+
FATFileSystem fs("fat");
48+
49+
int err = fs.mount(&bd);
50+
TEST_ASSERT_EQUAL(0, err);
51+
52+
uint8_t *buffer = (uint8_t *)malloc(TEST_SIZE);
53+
TEST_ASSERT(buffer);
54+
55+
// Fill with random sequence
56+
srand(1);
57+
for (int i = 0; i < TEST_SIZE; i++) {
58+
buffer[i] = 0xff & rand();
59+
}
60+
61+
// write and read file
62+
File file;
63+
err = file.open(&fs, "test_read_write.dat", O_WRONLY | O_CREAT);
64+
TEST_ASSERT_EQUAL(0, err);
65+
ssize_t size = file.write(buffer, TEST_SIZE);
66+
TEST_ASSERT_EQUAL(TEST_SIZE, size);
67+
err = file.close();
68+
TEST_ASSERT_EQUAL(0, err);
69+
70+
err = file.open(&fs, "test_read_write.dat", O_RDONLY);
71+
TEST_ASSERT_EQUAL(0, err);
72+
size = file.read(buffer, TEST_SIZE);
73+
TEST_ASSERT_EQUAL(TEST_SIZE, size);
74+
err = file.close();
75+
TEST_ASSERT_EQUAL(0, err);
76+
77+
// Check that the data was unmodified
78+
srand(1);
79+
for (int i = 0; i < TEST_SIZE; i++) {
80+
TEST_ASSERT_EQUAL(0xff & rand(), buffer[i]);
81+
}
82+
83+
err = fs.unmount();
84+
TEST_ASSERT_EQUAL(0, err);
85+
}
86+
87+
88+
// Simple test for iterating dir entries
89+
void test_read_dir() {
90+
FATFileSystem fs("fat");
91+
92+
int err = fs.mount(&bd);
93+
TEST_ASSERT_EQUAL(0, err);
94+
95+
err = fs.mkdir("test_read_dir", S_IRWXU | S_IRWXG | S_IRWXO);
96+
TEST_ASSERT_EQUAL(0, err);
97+
98+
err = fs.mkdir("test_read_dir/test_dir", S_IRWXU | S_IRWXG | S_IRWXO);
99+
TEST_ASSERT_EQUAL(0, err);
100+
101+
File file;
102+
err = file.open(&fs, "test_read_dir/test_file", O_WRONLY | O_CREAT);
103+
TEST_ASSERT_EQUAL(0, err);
104+
err = file.close();
105+
TEST_ASSERT_EQUAL(0, err);
106+
107+
// Iterate over dir checking for known files
108+
Dir dir;
109+
err = dir.open(&fs, "test_read_dir");
110+
TEST_ASSERT_EQUAL(0, err);
111+
112+
struct dirent *de;
113+
bool test_dir_found = false;
114+
bool test_file_found = true;
115+
116+
while ((de = readdir(&dir))) {
117+
printf("d_name: %.32s, d_type: %x\n", de->d_name, de->d_type);
118+
119+
if (strcmp(de->d_name, "test_dir") == 0) {
120+
test_dir_found = true;
121+
TEST_ASSERT_EQUAL(DT_DIR, de->d_type);
122+
} else if (strcmp(de->d_name, "test_file") == 0) {
123+
test_file_found = true;
124+
TEST_ASSERT_EQUAL(DT_REG, de->d_type);
125+
} else {
126+
char *buf = new char[NAME_MAX];
127+
snprintf(buf, NAME_MAX, "Unexpected file \"%s\"", de->d_name);
128+
TEST_ASSERT_MESSAGE(false, buf);
129+
}
130+
}
131+
132+
TEST_ASSERT_MESSAGE(test_dir_found, "Could not find \"test_dir\"");
133+
TEST_ASSERT_MESSAGE(test_file_found, "Could not find \"test_file\"");
134+
135+
err = dir.close();
136+
TEST_ASSERT_EQUAL(0, err);
137+
138+
err = fs.unmount();
139+
TEST_ASSERT_EQUAL(0, err);
140+
}
141+
142+
143+
// Test setup
144+
utest::v1::status_t test_setup(const size_t number_of_cases) {
145+
GREENTEA_SETUP(10, "default_auto");
146+
return verbose_test_setup_handler(number_of_cases);
147+
}
148+
149+
Case cases[] = {
150+
Case("Testing formating", test_format),
151+
Case("Testing read write < block", test_read_write<BLOCK_SIZE/2>),
152+
Case("Testing read write > block", test_read_write<2*BLOCK_SIZE>),
153+
Case("Testing dir iteration", test_read_dir),
154+
};
155+
156+
Specification specification(test_setup, cases);
157+
158+
int main() {
159+
return !Harness::run(specification);
160+
}

0 commit comments

Comments
 (0)