5
5
#include <libtock-sync/storage/isolated_nonvolatile_storage.h>
6
6
7
7
static int test_all (void );
8
- static int test (uint8_t * readbuf , uint8_t * writebuf , size_t size , size_t offset , size_t len );
8
+ static int test (uint8_t * readbuf , size_t readsize , uint8_t * writebuf , size_t writesize , size_t offset );
9
+
10
+ static size_t min (size_t a , size_t b ) {
11
+ if (a < b ) return a ;
12
+ return b ;
13
+ }
9
14
10
15
int main (void ) {
11
16
printf ("[TEST] Isolated Nonvolatile Storage\n" );
@@ -21,56 +26,56 @@ int main(void) {
21
26
}
22
27
23
28
static int test_all (void ) {
24
- int num_bytes ;
25
- libtocksync_isolated_nonvolatile_storage_get_number_bytes ((uint32_t * ) & num_bytes );
29
+ uint64_t num_bytes_total ;
30
+ libtocksync_isolated_nonvolatile_storage_get_number_bytes (& num_bytes_total );
31
+ int num_bytes = num_bytes_total ;
26
32
printf ("Have %i bytes of nonvolatile storage\n" , num_bytes );
27
33
28
34
int r ;
29
35
uint8_t readbuf [512 ];
30
36
uint8_t writebuf [512 ];
31
37
32
- if ((r = test (readbuf , writebuf , 256 , 0 , 14 )) != 0 ) return r ;
33
- if ((r = test (readbuf , writebuf , 256 , 20 , 14 )) != 0 ) return r ;
34
- if ((r = test (readbuf , writebuf , 512 , 0 , 512 )) != 0 ) return r ;
38
+ if ((r = test (readbuf , 14 , writebuf , 256 , 0 )) != 0 ) return r ;
39
+ if ((r = test (readbuf , 14 , writebuf , 256 , 20 )) != 0 ) return r ;
40
+ if ((r = test (readbuf , 512 , writebuf , 512 , 0 )) != 0 ) return r ;
35
41
36
42
printf ("Write to end of region (offset %d)\n" , num_bytes - 512 );
37
- if ((r = test (readbuf , writebuf , 512 , num_bytes - 512 , 500 )) != 0 ) return r ;
43
+ if ((r = test (readbuf , 512 , writebuf , 500 , num_bytes - 512 )) != 0 ) return r ;
38
44
39
45
printf ("Write beyond end region, should fail (offset %d)\n" , num_bytes );
40
- if ((r = test (readbuf , writebuf , 512 , num_bytes , 501 )) == 0 ) return -1 ;
46
+ if ((r = test (readbuf , 512 , writebuf , 501 , num_bytes )) == 0 ) return -1 ;
41
47
42
48
printf ("Write starts beyond end region, should fail (offset %d)\n" , num_bytes + 1 );
43
- if ((r = test (readbuf , writebuf , 512 , num_bytes + 1 , 1 )) == 0 ) return -1 ;
49
+ if ((r = test (readbuf , 512 , writebuf , 1 , num_bytes + 1 )) == 0 ) return -1 ;
44
50
45
51
printf ("Write starts before start region, should fail (offset %d)\n" , -1 );
46
- if ((r = test (readbuf , writebuf , 512 , -1 , 1 )) == 0 ) return -1 ;
52
+ if ((r = test (readbuf , 512 , writebuf , 512 , -1 )) == 0 ) return -1 ;
47
53
48
54
return 0 ;
49
55
}
50
56
51
- static int test (uint8_t * readbuf , uint8_t * writebuf , size_t size , size_t offset , size_t len ) {
57
+ static int test (uint8_t * readbuf , size_t readsize , uint8_t * writebuf , size_t writesize , size_t offset ) {
52
58
int ret ;
53
- int length_written , length_read ;
54
59
55
- printf ("\tTest with size %d ...\n" , size );
60
+ printf ("\tTest with read size %d and write size %d ...\n" , readsize , writesize );
56
61
57
- for (size_t i = 0 ; i < len ; i ++ ) {
62
+ for (size_t i = 0 ; i < writesize ; i ++ ) {
58
63
writebuf [i ] = i ;
59
64
}
60
65
61
- ret = libtocksync_isolated_nonvolatile_storage_write (offset , len , writebuf , size , & length_written );
66
+ ret = libtocksync_isolated_nonvolatile_storage_write (offset , writebuf , writesize );
62
67
if (ret != RETURNCODE_SUCCESS ) {
63
68
printf ("\tERROR calling write. returncode: %d\n" , ret );
64
69
return ret ;
65
70
}
66
71
67
- ret = libtocksync_isolated_nonvolatile_storage_read (offset , len , readbuf , size , & length_read );
72
+ ret = libtocksync_isolated_nonvolatile_storage_read (offset , readbuf , readsize );
68
73
if (ret != RETURNCODE_SUCCESS ) {
69
74
printf ("\tERROR calling read. returncode: %d\n" , ret );
70
75
return ret ;
71
76
}
72
77
73
- for (size_t i = 0 ; i < len ; i ++ ) {
78
+ for (size_t i = 0 ; i < min ( readsize , writesize ) ; i ++ ) {
74
79
if (readbuf [i ] != writebuf [i ]) {
75
80
printf ("\tInconsistency between data written and read at index %u\n" , i );
76
81
return -1 ;
0 commit comments