1# bad blocks with block cycles should be tested in test_relocations
2if = '(int32_t)BLOCK_CYCLES == -1'
3
4[cases.test_badblocks_single]
5defines.ERASE_COUNT = 256 # small bd so test runs faster
6defines.ERASE_CYCLES = 0xffffffff
7defines.ERASE_VALUE = [0x00, 0xff, -1]
8defines.BADBLOCK_BEHAVIOR = [
9    'LFS_EMUBD_BADBLOCK_PROGERROR',
10    'LFS_EMUBD_BADBLOCK_ERASEERROR',
11    'LFS_EMUBD_BADBLOCK_READERROR',
12    'LFS_EMUBD_BADBLOCK_PROGNOOP',
13    'LFS_EMUBD_BADBLOCK_ERASENOOP',
14]
15defines.NAMEMULT = 64
16defines.FILEMULT = 1
17code = '''
18    for (lfs_block_t badblock = 2; badblock < BLOCK_COUNT; badblock++) {
19        lfs_emubd_setwear(cfg, badblock-1, 0) => 0;
20        lfs_emubd_setwear(cfg, badblock, 0xffffffff) => 0;
21
22        lfs_t lfs;
23        lfs_format(&lfs, cfg) => 0;
24
25        lfs_mount(&lfs, cfg) => 0;
26        for (int i = 1; i < 10; i++) {
27            uint8_t buffer[1024];
28            for (int j = 0; j < NAMEMULT; j++) {
29                buffer[j] = '0'+i;
30            }
31            buffer[NAMEMULT] = '\0';
32            lfs_mkdir(&lfs, (char*)buffer) => 0;
33
34            buffer[NAMEMULT] = '/';
35            for (int j = 0; j < NAMEMULT; j++) {
36                buffer[j+NAMEMULT+1] = '0'+i;
37            }
38            buffer[2*NAMEMULT+1] = '\0';
39            lfs_file_t file;
40            lfs_file_open(&lfs, &file, (char*)buffer,
41                    LFS_O_WRONLY | LFS_O_CREAT) => 0;
42
43            lfs_size_t size = NAMEMULT;
44            for (int j = 0; j < i*FILEMULT; j++) {
45                lfs_file_write(&lfs, &file, buffer, size) => size;
46            }
47
48            lfs_file_close(&lfs, &file) => 0;
49        }
50        lfs_unmount(&lfs) => 0;
51
52        lfs_mount(&lfs, cfg) => 0;
53        for (int i = 1; i < 10; i++) {
54            uint8_t buffer[1024];
55            for (int j = 0; j < NAMEMULT; j++) {
56                buffer[j] = '0'+i;
57            }
58            buffer[NAMEMULT] = '\0';
59            struct lfs_info info;
60            lfs_stat(&lfs, (char*)buffer, &info) => 0;
61            info.type => LFS_TYPE_DIR;
62
63            buffer[NAMEMULT] = '/';
64            for (int j = 0; j < NAMEMULT; j++) {
65                buffer[j+NAMEMULT+1] = '0'+i;
66            }
67            buffer[2*NAMEMULT+1] = '\0';
68            lfs_file_t file;
69            lfs_file_open(&lfs, &file, (char*)buffer, LFS_O_RDONLY) => 0;
70
71            int size = NAMEMULT;
72            for (int j = 0; j < i*FILEMULT; j++) {
73                uint8_t rbuffer[1024];
74                lfs_file_read(&lfs, &file, rbuffer, size) => size;
75                memcmp(buffer, rbuffer, size) => 0;
76            }
77
78            lfs_file_close(&lfs, &file) => 0;
79        }
80        lfs_unmount(&lfs) => 0;
81    }
82'''
83
84[cases.test_badblocks_region_corruption] # (causes cascading failures)
85defines.ERASE_COUNT = 256 # small bd so test runs faster
86defines.ERASE_CYCLES = 0xffffffff
87defines.ERASE_VALUE = [0x00, 0xff, -1]
88defines.BADBLOCK_BEHAVIOR = [
89    'LFS_EMUBD_BADBLOCK_PROGERROR',
90    'LFS_EMUBD_BADBLOCK_ERASEERROR',
91    'LFS_EMUBD_BADBLOCK_READERROR',
92    'LFS_EMUBD_BADBLOCK_PROGNOOP',
93    'LFS_EMUBD_BADBLOCK_ERASENOOP',
94]
95defines.NAMEMULT = 64
96defines.FILEMULT = 1
97code = '''
98    for (lfs_block_t i = 0; i < (BLOCK_COUNT-2)/2; i++) {
99        lfs_emubd_setwear(cfg, i+2, 0xffffffff) => 0;
100    }
101
102    lfs_t lfs;
103    lfs_format(&lfs, cfg) => 0;
104
105    lfs_mount(&lfs, cfg) => 0;
106    for (int i = 1; i < 10; i++) {
107        uint8_t buffer[1024];
108        for (int j = 0; j < NAMEMULT; j++) {
109            buffer[j] = '0'+i;
110        }
111        buffer[NAMEMULT] = '\0';
112        lfs_mkdir(&lfs, (char*)buffer) => 0;
113
114        buffer[NAMEMULT] = '/';
115        for (int j = 0; j < NAMEMULT; j++) {
116            buffer[j+NAMEMULT+1] = '0'+i;
117        }
118        buffer[2*NAMEMULT+1] = '\0';
119        lfs_file_t file;
120        lfs_file_open(&lfs, &file, (char*)buffer,
121                LFS_O_WRONLY | LFS_O_CREAT) => 0;
122
123        lfs_size_t size = NAMEMULT;
124        for (int j = 0; j < i*FILEMULT; j++) {
125            lfs_file_write(&lfs, &file, buffer, size) => size;
126        }
127
128        lfs_file_close(&lfs, &file) => 0;
129    }
130    lfs_unmount(&lfs) => 0;
131
132    lfs_mount(&lfs, cfg) => 0;
133    for (int i = 1; i < 10; i++) {
134        uint8_t buffer[1024];
135        for (int j = 0; j < NAMEMULT; j++) {
136            buffer[j] = '0'+i;
137        }
138        buffer[NAMEMULT] = '\0';
139        struct lfs_info info;
140        lfs_stat(&lfs, (char*)buffer, &info) => 0;
141        info.type => LFS_TYPE_DIR;
142
143        buffer[NAMEMULT] = '/';
144        for (int j = 0; j < NAMEMULT; j++) {
145            buffer[j+NAMEMULT+1] = '0'+i;
146        }
147        buffer[2*NAMEMULT+1] = '\0';
148        lfs_file_t file;
149        lfs_file_open(&lfs, &file, (char*)buffer, LFS_O_RDONLY) => 0;
150
151        lfs_size_t size = NAMEMULT;
152        for (int j = 0; j < i*FILEMULT; j++) {
153            uint8_t rbuffer[1024];
154            lfs_file_read(&lfs, &file, rbuffer, size) => size;
155            memcmp(buffer, rbuffer, size) => 0;
156        }
157
158        lfs_file_close(&lfs, &file) => 0;
159    }
160    lfs_unmount(&lfs) => 0;
161'''
162
163[cases.test_badblocks_alternating_corruption] # (causes cascading failures)
164defines.ERASE_COUNT = 256 # small bd so test runs faster
165defines.ERASE_CYCLES = 0xffffffff
166defines.ERASE_VALUE = [0x00, 0xff, -1]
167defines.BADBLOCK_BEHAVIOR = [
168    'LFS_EMUBD_BADBLOCK_PROGERROR',
169    'LFS_EMUBD_BADBLOCK_ERASEERROR',
170    'LFS_EMUBD_BADBLOCK_READERROR',
171    'LFS_EMUBD_BADBLOCK_PROGNOOP',
172    'LFS_EMUBD_BADBLOCK_ERASENOOP',
173]
174defines.NAMEMULT = 64
175defines.FILEMULT = 1
176code = '''
177    for (lfs_block_t i = 0; i < (BLOCK_COUNT-2)/2; i++) {
178        lfs_emubd_setwear(cfg, (2*i) + 2, 0xffffffff) => 0;
179    }
180
181    lfs_t lfs;
182    lfs_format(&lfs, cfg) => 0;
183
184    lfs_mount(&lfs, cfg) => 0;
185    for (int i = 1; i < 10; i++) {
186        uint8_t buffer[1024];
187        for (int j = 0; j < NAMEMULT; j++) {
188            buffer[j] = '0'+i;
189        }
190        buffer[NAMEMULT] = '\0';
191        lfs_mkdir(&lfs, (char*)buffer) => 0;
192
193        buffer[NAMEMULT] = '/';
194        for (int j = 0; j < NAMEMULT; j++) {
195            buffer[j+NAMEMULT+1] = '0'+i;
196        }
197        buffer[2*NAMEMULT+1] = '\0';
198        lfs_file_t file;
199        lfs_file_open(&lfs, &file, (char*)buffer,
200                LFS_O_WRONLY | LFS_O_CREAT) => 0;
201
202        lfs_size_t size = NAMEMULT;
203        for (int j = 0; j < i*FILEMULT; j++) {
204            lfs_file_write(&lfs, &file, buffer, size) => size;
205        }
206
207        lfs_file_close(&lfs, &file) => 0;
208    }
209    lfs_unmount(&lfs) => 0;
210
211    lfs_mount(&lfs, cfg) => 0;
212    for (int i = 1; i < 10; i++) {
213        uint8_t buffer[1024];
214        for (int j = 0; j < NAMEMULT; j++) {
215            buffer[j] = '0'+i;
216        }
217        buffer[NAMEMULT] = '\0';
218        struct lfs_info info;
219        lfs_stat(&lfs, (char*)buffer, &info) => 0;
220        info.type => LFS_TYPE_DIR;
221
222        buffer[NAMEMULT] = '/';
223        for (int j = 0; j < NAMEMULT; j++) {
224            buffer[j+NAMEMULT+1] = '0'+i;
225        }
226        buffer[2*NAMEMULT+1] = '\0';
227        lfs_file_t file;
228        lfs_file_open(&lfs, &file, (char*)buffer, LFS_O_RDONLY) => 0;
229
230        lfs_size_t size = NAMEMULT;
231        for (int j = 0; j < i*FILEMULT; j++) {
232            uint8_t rbuffer[1024];
233            lfs_file_read(&lfs, &file, rbuffer, size) => size;
234            memcmp(buffer, rbuffer, size) => 0;
235        }
236
237        lfs_file_close(&lfs, &file) => 0;
238    }
239    lfs_unmount(&lfs) => 0;
240'''
241
242# other corner cases
243[cases.test_badblocks_superblocks] # (corrupt 1 or 0)
244defines.ERASE_CYCLES = 0xffffffff
245defines.ERASE_VALUE = [0x00, 0xff, -1]
246defines.BADBLOCK_BEHAVIOR = [
247    'LFS_EMUBD_BADBLOCK_PROGERROR',
248    'LFS_EMUBD_BADBLOCK_ERASEERROR',
249    'LFS_EMUBD_BADBLOCK_READERROR',
250    'LFS_EMUBD_BADBLOCK_PROGNOOP',
251    'LFS_EMUBD_BADBLOCK_ERASENOOP',
252]
253code = '''
254    lfs_emubd_setwear(cfg, 0, 0xffffffff) => 0;
255    lfs_emubd_setwear(cfg, 1, 0xffffffff) => 0;
256
257    lfs_t lfs;
258    lfs_format(&lfs, cfg) => LFS_ERR_NOSPC;
259    lfs_mount(&lfs, cfg) => LFS_ERR_CORRUPT;
260'''
261