6
6
7
7
8
8
9
-
10
9
struct nv_data {
11
10
bool fired ;
12
11
returncode_t ret ;
13
- int length ;
14
12
int storage_size ;
15
13
};
16
14
@@ -22,31 +20,37 @@ static struct nv_data result = {.fired = false};
22
20
// result.storage_size = number_bytes;
23
21
// }
24
22
25
- // static void write_cb(returncode_t ret, int length) {
26
- // result.fired = true;
27
- // result.ret = ret;
28
- // result.length = length;
29
- // }
23
+ static void write_cb (returncode_t ret ) {
24
+ result .fired = true;
25
+ result .ret = ret ;
26
+ }
30
27
31
- static void read_cb (returncode_t ret , int length ) {
32
- result .fired = true;
33
- result .ret = ret ;
34
- result .length = length ;
28
+ static void read_cb (returncode_t ret ) {
29
+ result .fired = true;
30
+ result .ret = ret ;
31
+ }
32
+
33
+ static void write_done (int ret ,
34
+ __attribute__ ((unused )) int arg1 ,
35
+ __attribute__ ((unused )) int arg2 ,
36
+ void * opaque ) {
37
+ libtock_isolated_nonvolatile_storage_callback_write cb = (libtock_isolated_nonvolatile_storage_callback_write ) opaque ;
38
+ cb (tock_status_to_returncode (ret ));
35
39
}
36
40
37
41
static void read_done (int ret ,
38
- int length ,
42
+ __attribute__ (( unused )) int arg1 ,
39
43
__attribute__ ((unused )) int arg2 ,
40
44
void * opaque ) {
41
45
libtock_isolated_nonvolatile_storage_callback_read cb = (libtock_isolated_nonvolatile_storage_callback_read ) opaque ;
42
- cb (tock_status_to_returncode (ret ), length );
46
+ cb (tock_status_to_returncode (ret ));
43
47
}
44
48
45
49
46
- static uint32_t get_region_size (void ) {
47
- uint32_t num_bytes ;
48
- libtocksync_isolated_nonvolatile_storage_get_number_bytes (& num_bytes );
49
- return num_bytes ;
50
+ static uint64_t get_region_size (void ) {
51
+ uint64_t num_bytes ;
52
+ libtocksync_isolated_nonvolatile_storage_get_number_bytes (& num_bytes );
53
+ return num_bytes ;
50
54
}
51
55
52
56
@@ -61,7 +65,7 @@ static bool test_exists(void) {
61
65
}
62
66
63
67
static bool test_size (void ) {
64
- uint32_t num_bytes ;
68
+ uint64_t num_bytes ;
65
69
returncode_t ret ;
66
70
67
71
ret = libtocksync_isolated_nonvolatile_storage_get_number_bytes (& num_bytes );
@@ -81,13 +85,10 @@ static bool test_read(void) {
81
85
82
86
uint32_t offset = 0 ;
83
87
uint32_t length = 10 ;
84
- uint32_t length_read ;
85
88
86
- ret = libtocksync_isolated_nonvolatile_storage_read (offset , length , readbuf , sizeof ( readbuf ), & length_read );
89
+ ret = libtocksync_isolated_nonvolatile_storage_read (offset , readbuf , length );
87
90
if (ret == RETURNCODE_SUCCESS ) {
88
- if (length_read == length ) {
89
- return true;
90
- }
91
+ return true;
91
92
}
92
93
return false;
93
94
}
@@ -98,49 +99,112 @@ static bool test_read_zerobuffer(void) {
98
99
uint8_t readbuf [512 ];
99
100
100
101
uint32_t offset = 0 ;
101
- uint32_t length = 10 ;
102
- uint32_t length_read ;
103
102
104
- ret = libtocksync_isolated_nonvolatile_storage_read (offset , length , readbuf , 0 , & length_read );
103
+ ret = libtocksync_isolated_nonvolatile_storage_read (offset , readbuf , 0 );
105
104
if (ret == RETURNCODE_ERESERVE ) {
106
- return true;
105
+ return true;
106
+ }
107
+ return false;
108
+ }
109
+
110
+ static bool test_read_fail_noallow (void ) {
111
+ returncode_t ret ;
112
+
113
+ uint32_t offset = 0 ;
114
+
115
+ result .fired = false;
116
+
117
+ ret = libtock_isolated_nonvolatile_storage_set_upcall_read_done (read_done , read_cb );
118
+ if (ret != RETURNCODE_SUCCESS ) return false;
119
+
120
+ ret = libtock_isolated_nonvolatile_storage_set_allow_readwrite_read_buffer (NULL , 0 );
121
+ if (ret != RETURNCODE_SUCCESS ) return false;
122
+
123
+ ret = libtock_isolated_nonvolatile_storage_command_read (offset );
124
+ if (ret != RETURNCODE_SUCCESS ) return false;
125
+
126
+ yield_for (& result .fired );
127
+
128
+ if (result .ret == RETURNCODE_ERESERVE ) {
129
+ return true;
107
130
}
108
131
return false;
109
132
}
110
133
111
- static bool test_read_buffertoosmall (void ) {
134
+ static bool test_read_fail_notwithinregion (void ) {
112
135
returncode_t ret ;
113
136
114
137
uint8_t readbuf [512 ];
115
138
139
+ uint64_t offset ;
140
+ size_t length ;
141
+
142
+ offset = get_region_size () - 5 ;
143
+ length = 10 ;
144
+ ret = libtocksync_isolated_nonvolatile_storage_read (offset , readbuf , length );
145
+ if (ret != RETURNCODE_EINVAL ) return false;
146
+
147
+ offset = get_region_size () + 10 ;
148
+ length = 10 ;
149
+ ret = libtocksync_isolated_nonvolatile_storage_read (offset , readbuf , length );
150
+ if (ret != RETURNCODE_EINVAL ) return false;
151
+
152
+ offset = 0 ;
153
+ length = get_region_size () + 1 ;
154
+ ret = libtocksync_isolated_nonvolatile_storage_read (offset , readbuf , length );
155
+ if (ret != RETURNCODE_EINVAL ) return false;
156
+
157
+ offset = get_region_size () - 1 ;
158
+ length = 2 ;
159
+ ret = libtocksync_isolated_nonvolatile_storage_read (offset , readbuf , length );
160
+ if (ret != RETURNCODE_EINVAL ) return false;
161
+
162
+ return true;
163
+ }
164
+
165
+ static bool test_write (void ) {
166
+ returncode_t ret ;
167
+
168
+ uint8_t writebuf [512 ] = {0x3f };
169
+
116
170
uint32_t offset = 0 ;
117
171
uint32_t length = 10 ;
118
- uint32_t length_read ;
119
172
120
- ret = libtocksync_isolated_nonvolatile_storage_read (offset , length , readbuf , 5 , & length_read );
173
+ ret = libtocksync_isolated_nonvolatile_storage_write (offset , writebuf , length );
121
174
if (ret == RETURNCODE_SUCCESS ) {
122
- if (length_read == 5 ) {
123
- return true;
124
- }
175
+ return true;
125
176
}
126
177
return false;
127
178
}
128
179
129
- static bool test_read_fail_noallow (void ) {
180
+ static bool test_write_zerobuffer (void ) {
181
+ returncode_t ret ;
182
+
183
+ uint8_t writebuf [512 ];
184
+
185
+ uint32_t offset = 0 ;
186
+
187
+ ret = libtocksync_isolated_nonvolatile_storage_read (offset , writebuf , 0 );
188
+ if (ret == RETURNCODE_ERESERVE ) {
189
+ return true;
190
+ }
191
+ return false;
192
+ }
193
+
194
+ static bool test_write_fail_noallow (void ) {
130
195
returncode_t ret ;
131
196
132
197
uint32_t offset = 0 ;
133
- uint32_t length = 10 ;
134
198
135
199
result .fired = false;
136
200
137
- ret = libtock_isolated_nonvolatile_storage_set_upcall_read_done ( read_done , read_cb );
201
+ ret = libtock_isolated_nonvolatile_storage_set_upcall_write_done ( write_done , write_cb );
138
202
if (ret != RETURNCODE_SUCCESS ) return false;
139
203
140
- ret = libtock_isolated_nonvolatile_storage_set_allow_readwrite_read_buffer (NULL , 0 );
204
+ ret = libtock_isolated_nonvolatile_storage_set_allow_readonly_write_buffer (NULL , 0 );
141
205
if (ret != RETURNCODE_SUCCESS ) return false;
142
206
143
- ret = libtock_isolated_nonvolatile_storage_command_read (offset , length );
207
+ ret = libtock_isolated_nonvolatile_storage_command_write (offset );
144
208
if (ret != RETURNCODE_SUCCESS ) return false;
145
209
146
210
yield_for (& result .fired );
@@ -151,38 +215,32 @@ static bool test_read_fail_noallow(void) {
151
215
return false;
152
216
}
153
217
154
- static bool test_read_fail_notwithinregion (void ) {
218
+ static bool test_write_fail_notwithinregion (void ) {
155
219
returncode_t ret ;
156
220
157
- uint8_t readbuf [512 ];
158
-
159
- uint32_t offset ;
160
- uint32_t length ;
161
- uint32_t length_read ;
221
+ uint8_t writebuf [512 ] = {0x99 };
162
222
223
+ uint64_t offset ;
224
+ size_t length ;
163
225
164
- offset = get_region_size () - 5 ;
226
+ offset = get_region_size () - 5 ;
165
227
length = 10 ;
166
- ret = libtocksync_isolated_nonvolatile_storage_read (offset , length , readbuf , sizeof (readbuf ), & length_read );
167
- printf ("returncode %d\n" ,ret );
228
+ ret = libtocksync_isolated_nonvolatile_storage_read (offset , writebuf , length );
168
229
if (ret != RETURNCODE_EINVAL ) return false;
169
230
170
231
offset = get_region_size () + 10 ;
171
232
length = 10 ;
172
- ret = libtocksync_isolated_nonvolatile_storage_read (offset , length , readbuf , sizeof (readbuf ), & length_read );
173
- printf ("returncode %d\n" ,ret );
233
+ ret = libtocksync_isolated_nonvolatile_storage_read (offset , writebuf , length );
174
234
if (ret != RETURNCODE_EINVAL ) return false;
175
235
176
236
offset = 0 ;
177
237
length = get_region_size () + 1 ;
178
- ret = libtocksync_isolated_nonvolatile_storage_read (offset , length , readbuf , sizeof (readbuf ), & length_read );
179
- printf ("returncode %d\n" ,ret );
238
+ ret = libtocksync_isolated_nonvolatile_storage_read (offset , writebuf , length );
180
239
if (ret != RETURNCODE_EINVAL ) return false;
181
240
182
241
offset = get_region_size () - 1 ;
183
242
length = 2 ;
184
- ret = libtocksync_isolated_nonvolatile_storage_read (offset , length , readbuf , sizeof (readbuf ), & length_read );
185
- printf ("returncode %d\n" ,ret );
243
+ ret = libtocksync_isolated_nonvolatile_storage_read (offset , writebuf , length );
186
244
if (ret != RETURNCODE_EINVAL ) return false;
187
245
188
246
return true;
@@ -194,33 +252,74 @@ int main(void) {
194
252
printf ("[TEST] Isolated Nonvolatile Storage - Single\n" );
195
253
196
254
printf ("[INVS TEST] Exists: " );
197
- if (test_exists ()) printf ("Success\n" );
198
- else printf ("Fail\n" );
255
+ if (test_exists ()) {
256
+ printf ("Success\n" );
257
+ } else {
258
+ printf ("Fail\n" );
259
+ }
199
260
200
261
printf ("[INVS TEST] Size: " );
201
- if (test_size ()) printf ("Success\n" );
202
- else printf ("Fail\n" );
262
+ if (test_size ()) {
263
+ printf ("Success\n" );
264
+ } else {
265
+ printf ("Fail\n" );
266
+ }
203
267
204
268
printf ("[INVS TEST] Read: " );
205
- if (test_read ()) printf ("Success\n" );
206
- else printf ("Fail\n" );
269
+ if (test_read ()) {
270
+ printf ("Success\n" );
271
+ } else {
272
+ printf ("Fail\n" );
273
+ }
207
274
208
275
printf ("[INVS TEST] Read Zero Buffer: " );
209
- if (test_read_zerobuffer ()) printf ("Success\n" );
210
- else printf ("Fail\n" );
211
-
212
- printf ("[INVS TEST] Read Buffer Too Small: " );
213
- if (test_read_buffertoosmall ()) printf ("Success\n" );
214
- else printf ("Fail\n" );
276
+ if (test_read_zerobuffer ()) {
277
+ printf ("Success\n" );
278
+ } else {
279
+ printf ("Fail\n" );
280
+ }
215
281
216
282
printf ("[INVS TEST] Read Fail No Allow: " );
217
- if (test_read_fail_noallow ()) printf ("Success\n" );
218
- else printf ("Fail\n" );
283
+ if (test_read_fail_noallow ()) {
284
+ printf ("Success\n" );
285
+ } else {
286
+ printf ("Fail\n" );
287
+ }
219
288
220
289
printf ("[INVS TEST] Read Fail Not Within Region: " );
221
- if (test_read_fail_notwithinregion ()) printf ("Success\n" );
222
- else printf ("Fail\n" );
290
+ if (test_read_fail_notwithinregion ()) {
291
+ printf ("Success\n" );
292
+ } else {
293
+ printf ("Fail\n" );
294
+ }
295
+
296
+ printf ("[INVS TEST] Write: " );
297
+ if (test_write ()) {
298
+ printf ("Success\n" );
299
+ } else {
300
+ printf ("Fail\n" );
301
+ }
223
302
303
+ printf ("[INVS TEST] Write Zero Buffer: " );
304
+ if (test_write_zerobuffer ()) {
305
+ printf ("Success\n" );
306
+ } else {
307
+ printf ("Fail\n" );
308
+ }
309
+
310
+ printf ("[INVS TEST] Write Fail No Allow: " );
311
+ if (test_write_fail_noallow ()) {
312
+ printf ("Success\n" );
313
+ } else {
314
+ printf ("Fail\n" );
315
+ }
316
+
317
+ printf ("[INVS TEST] Write Fail Not Within Region: " );
318
+ if (test_write_fail_notwithinregion ()) {
319
+ printf ("Success\n" );
320
+ } else {
321
+ printf ("Fail\n" );
322
+ }
224
323
225
324
return 0 ;
226
325
}
0 commit comments