1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
/* Simulate IO errors after each byte in a stream.
* Verifies proper error propagation.
*/
#include <stdio.h>
#include <pb_decode.h>
#include <pb_encode.h>
#include "alltypes.pb.h"
#include "test_helpers.h"
typedef struct
{
uint8_t *buffer;
size_t fail_after;
} faulty_stream_t;
bool read_callback(pb_istream_t *stream, uint8_t *buf, size_t count)
{
faulty_stream_t *state = stream->state;
while (count--)
{
if (state->fail_after == 0)
PB_RETURN_ERROR(stream, "simulated");
state->fail_after--;
*buf++ = *state->buffer++;
}
return true;
}
bool write_callback(pb_ostream_t *stream, const uint8_t *buf, size_t count)
{
faulty_stream_t *state = stream->state;
while (count--)
{
if (state->fail_after == 0)
PB_RETURN_ERROR(stream, "simulated");
state->fail_after--;
*state->buffer++ = *buf++;
}
return true;
}
int main()
{
uint8_t buffer[2048];
size_t msglen;
AllTypes msg = AllTypes_init_zero;
/* Get some base data to run the tests with */
SET_BINARY_MODE(stdin);
msglen = fread(buffer, 1, sizeof(buffer), stdin);
/* Test IO errors on decoding */
{
bool status;
pb_istream_t stream = {&read_callback, NULL, SIZE_MAX};
faulty_stream_t fs;
size_t i;
for (i = 0; i < msglen; i++)
{
stream.bytes_left = msglen;
stream.state = &fs;
fs.buffer = buffer;
fs.fail_after = i;
status = pb_decode(&stream, AllTypes_fields, &msg);
if (status != false)
{
fprintf(stderr, "Unexpected success in decode\n");
return 2;
}
else if (strcmp(stream.errmsg, "simulated") != 0)
{
fprintf(stderr, "Wrong error in decode: %s\n", stream.errmsg);
return 3;
}
}
stream.bytes_left = msglen;
stream.state = &fs;
fs.buffer = buffer;
fs.fail_after = msglen;
status = pb_decode(&stream, AllTypes_fields, &msg);
if (!status)
{
fprintf(stderr, "Decoding failed: %s\n", stream.errmsg);
return 4;
}
}
/* Test IO errors on encoding */
{
bool status;
pb_ostream_t stream = {&write_callback, NULL, SIZE_MAX, 0};
faulty_stream_t fs;
size_t i;
for (i = 0; i < msglen; i++)
{
stream.max_size = msglen;
stream.bytes_written = 0;
stream.state = &fs;
fs.buffer = buffer;
fs.fail_after = i;
status = pb_encode(&stream, AllTypes_fields, &msg);
if (status != false)
{
fprintf(stderr, "Unexpected success in encode\n");
return 5;
}
else if (strcmp(stream.errmsg, "simulated") != 0)
{
fprintf(stderr, "Wrong error in encode: %s\n", stream.errmsg);
return 6;
}
}
stream.max_size = msglen;
stream.bytes_written = 0;
stream.state = &fs;
fs.buffer = buffer;
fs.fail_after = msglen;
status = pb_encode(&stream, AllTypes_fields, &msg);
if (!status)
{
fprintf(stderr, "Encoding failed: %s\n", stream.errmsg);
return 7;
}
}
return 0;
}
|