Line data Source code
1 : /* Tests the decoding of all types. 2 : * This is the counterpart of test_encode3. 3 : * Run e.g. ./test_encode3 | ./test_decode3 4 : */ 5 : 6 : #include <stdio.h> 7 : #include <string.h> 8 : #include <stdlib.h> 9 : #include <pb_decode.h> 10 : #include "alltypes.pb.h" 11 : #include "unittests.h" 12 : #include "test_helpers.h" 13 : 14 : /* This function is called once from main(), it handles 15 : the decoding and checks the fields. */ 16 5 : bool check_alltypes(pb_istream_t *stream, int mode) 17 : { 18 5 : int status = 0; 19 : 20 : /* Uses _init_default to just make sure that the macro works. */ 21 5 : AllTypes alltypes = AllTypes_init_default; 22 : 23 : /* Fill with garbage to better detect initialization errors */ 24 5 : memset(&alltypes, 0xAA, sizeof(alltypes)); 25 5 : alltypes.extensions = 0; 26 : 27 5 : if (!pb_decode(stream, AllTypes_fields, &alltypes)) 28 0 : return false; 29 : 30 5 : if (mode == 0 || mode == 1) 31 : { 32 4 : TEST(alltypes.req_int32 == -1001); 33 4 : TEST(alltypes.req_int64 == -1002); 34 4 : TEST(alltypes.req_uint32 == 1003); 35 4 : TEST(alltypes.req_uint64 == 1004); 36 4 : TEST(alltypes.req_sint32 == -1005); 37 4 : TEST(alltypes.req_sint64 == -1006); 38 4 : TEST(alltypes.req_bool == true); 39 : 40 4 : TEST(alltypes.req_fixed32 == 1008); 41 4 : TEST(alltypes.req_sfixed32 == -1009); 42 4 : TEST(alltypes.req_float == 1010.0f); 43 : 44 4 : TEST(alltypes.req_fixed64 == 1011); 45 4 : TEST(alltypes.req_sfixed64 == -1012); 46 4 : TEST(alltypes.req_double == 1013.0f); 47 : 48 4 : TEST(strcmp(alltypes.req_string, "1014") == 0); 49 4 : TEST(alltypes.req_bytes.size == 4); 50 4 : TEST(memcmp(alltypes.req_bytes.bytes, "1015", 4) == 0); 51 4 : TEST(strcmp(alltypes.req_submsg.substuff1, "1016") == 0); 52 4 : TEST(alltypes.req_submsg.substuff2 == 1016); 53 4 : TEST(alltypes.req_submsg.substuff3 == 3); 54 4 : TEST(alltypes.req_enum == MyEnum_Truth); 55 4 : TEST(memcmp(alltypes.req_fbytes, "1019", 4) == 0); 56 : 57 4 : TEST(alltypes.rep_int32_count == 5 && alltypes.rep_int32[4] == -2001 && alltypes.rep_int32[0] == 0); 58 4 : TEST(alltypes.rep_int64_count == 5 && alltypes.rep_int64[4] == -2002 && alltypes.rep_int64[0] == 0); 59 4 : TEST(alltypes.rep_uint32_count == 5 && alltypes.rep_uint32[4] == 2003 && alltypes.rep_uint32[0] == 0); 60 4 : TEST(alltypes.rep_uint64_count == 5 && alltypes.rep_uint64[4] == 2004 && alltypes.rep_uint64[0] == 0); 61 4 : TEST(alltypes.rep_sint32_count == 5 && alltypes.rep_sint32[4] == -2005 && alltypes.rep_sint32[0] == 0); 62 4 : TEST(alltypes.rep_sint64_count == 5 && alltypes.rep_sint64[4] == -2006 && alltypes.rep_sint64[0] == 0); 63 4 : TEST(alltypes.rep_bool_count == 5 && alltypes.rep_bool[4] == true && alltypes.rep_bool[0] == false); 64 : 65 4 : TEST(alltypes.rep_fixed32_count == 5 && alltypes.rep_fixed32[4] == 2008 && alltypes.rep_fixed32[0] == 0); 66 4 : TEST(alltypes.rep_sfixed32_count == 5 && alltypes.rep_sfixed32[4] == -2009 && alltypes.rep_sfixed32[0] == 0); 67 4 : TEST(alltypes.rep_float_count == 5 && alltypes.rep_float[4] == 2010.0f && alltypes.rep_float[0] == 0.0f); 68 : 69 4 : TEST(alltypes.rep_fixed64_count == 5 && alltypes.rep_fixed64[4] == 2011 && alltypes.rep_fixed64[0] == 0); 70 4 : TEST(alltypes.rep_sfixed64_count == 5 && alltypes.rep_sfixed64[4] == -2012 && alltypes.rep_sfixed64[0] == 0); 71 4 : TEST(alltypes.rep_double_count == 5 && alltypes.rep_double[4] == 2013.0 && alltypes.rep_double[0] == 0.0); 72 : 73 4 : TEST(alltypes.rep_string_count == 5 && strcmp(alltypes.rep_string[4], "2014") == 0 && alltypes.rep_string[0][0] == '\0'); 74 4 : TEST(alltypes.rep_bytes_count == 5 && alltypes.rep_bytes[4].size == 4 && alltypes.rep_bytes[0].size == 0); 75 4 : TEST(memcmp(alltypes.rep_bytes[4].bytes, "2015", 4) == 0); 76 : 77 4 : TEST(alltypes.rep_submsg_count == 5); 78 4 : TEST(strcmp(alltypes.rep_submsg[4].substuff1, "2016") == 0 && alltypes.rep_submsg[0].substuff1[0] == '\0'); 79 4 : TEST(alltypes.rep_submsg[4].substuff2 == 2016 && alltypes.rep_submsg[0].substuff2 == 0); 80 4 : TEST(alltypes.rep_submsg[4].substuff3 == 2016 && alltypes.rep_submsg[0].substuff3 == 3); 81 : 82 4 : TEST(alltypes.rep_enum_count == 5 && alltypes.rep_enum[4] == MyEnum_Truth && alltypes.rep_enum[0] == MyEnum_Zero); 83 4 : TEST(alltypes.rep_emptymsg_count == 5); 84 4 : TEST(alltypes.rep_fbytes_count == 5); 85 4 : TEST(alltypes.rep_fbytes[0][0] == 0 && alltypes.rep_fbytes[0][3] == 0); 86 4 : TEST(memcmp(alltypes.rep_fbytes[4], "2019", 4) == 0); 87 : 88 4 : TEST(alltypes.rep_farray[0] == 0 && alltypes.rep_farray[4] == 2040); 89 4 : TEST(alltypes.rep_farray2[0] == 0 && alltypes.rep_farray2[2] == 2095); 90 : 91 4 : TEST(alltypes.req_limits.int32_min == INT32_MIN); 92 4 : TEST(alltypes.req_limits.int32_max == INT32_MAX); 93 4 : TEST(alltypes.req_limits.uint32_min == 0); 94 4 : TEST(alltypes.req_limits.uint32_max == UINT32_MAX); 95 4 : TEST(alltypes.req_limits.int64_min == INT64_MIN); 96 4 : TEST(alltypes.req_limits.int64_max == INT64_MAX); 97 4 : TEST(alltypes.req_limits.uint64_min == 0); 98 4 : TEST(alltypes.req_limits.uint64_max == UINT64_MAX); 99 4 : TEST(alltypes.req_limits.enum_min == HugeEnum_Negative); 100 4 : TEST(alltypes.req_limits.enum_max == HugeEnum_Positive); 101 4 : TEST(alltypes.req_limits.largetag == 1001); 102 : 103 4 : TEST(alltypes.req_ds8.first == 9991); 104 4 : TEST(alltypes.req_ds8.second == 9992); 105 : 106 4 : TEST(alltypes.req_intsizes.req_int8 == -128); 107 4 : TEST(alltypes.req_intsizes.req_uint8 == 255); 108 4 : TEST(alltypes.req_intsizes.req_sint8 == -128); 109 4 : TEST(alltypes.req_intsizes.req_int16 == -32768); 110 4 : TEST(alltypes.req_intsizes.req_uint16 == 65535); 111 4 : TEST(alltypes.req_intsizes.req_sint16 == -32768); 112 : } 113 : 114 5 : if (mode == 0) 115 : { 116 : /* Expect default values */ 117 2 : TEST(alltypes.has_opt_int32 == false); 118 2 : TEST(alltypes.opt_int32 == 4041); 119 2 : TEST(alltypes.has_opt_int64 == false); 120 2 : TEST(alltypes.opt_int64 == 4042); 121 2 : TEST(alltypes.has_opt_uint32 == false); 122 2 : TEST(alltypes.opt_uint32 == 4043); 123 2 : TEST(alltypes.has_opt_uint64 == false); 124 2 : TEST(alltypes.opt_uint64 == 4044); 125 2 : TEST(alltypes.has_opt_sint32 == false); 126 2 : TEST(alltypes.opt_sint32 == 4045); 127 2 : TEST(alltypes.has_opt_sint64 == false); 128 2 : TEST(alltypes.opt_sint64 == 4046); 129 2 : TEST(alltypes.has_opt_bool == false); 130 2 : TEST(alltypes.opt_bool == false); 131 : 132 2 : TEST(alltypes.has_opt_fixed32 == false); 133 2 : TEST(alltypes.opt_fixed32 == 4048); 134 2 : TEST(alltypes.has_opt_sfixed32 == false); 135 2 : TEST(alltypes.opt_sfixed32 == 4049); 136 2 : TEST(alltypes.has_opt_float == false); 137 2 : TEST(alltypes.opt_float == 4050.0f); 138 : 139 2 : TEST(alltypes.has_opt_fixed64 == false); 140 2 : TEST(alltypes.opt_fixed64 == 4051); 141 2 : TEST(alltypes.has_opt_sfixed64 == false); 142 2 : TEST(alltypes.opt_sfixed64 == 4052); 143 2 : TEST(alltypes.has_opt_double == false); 144 2 : TEST(alltypes.opt_double == 4053.0); 145 : 146 2 : TEST(alltypes.has_opt_string == false); 147 2 : TEST(strcmp(alltypes.opt_string, "4054") == 0); 148 2 : TEST(alltypes.has_opt_bytes == false); 149 2 : TEST(alltypes.opt_bytes.size == 4); 150 2 : TEST(memcmp(alltypes.opt_bytes.bytes, "\x34\x5C\x00\xff", 4) == 0); 151 2 : TEST(alltypes.has_opt_submsg == false); 152 2 : TEST(strcmp(alltypes.opt_submsg.substuff1, "1") == 0); 153 2 : TEST(alltypes.opt_submsg.substuff2 == 2); 154 2 : TEST(alltypes.opt_submsg.substuff3 == 3); 155 2 : TEST(alltypes.has_opt_enum == false); 156 2 : TEST(alltypes.opt_enum == MyEnum_Second); 157 2 : TEST(alltypes.has_opt_emptymsg == false); 158 2 : TEST(alltypes.has_opt_fbytes == false); 159 2 : TEST(memcmp(alltypes.opt_fbytes, "4059", 4) == 0); 160 : 161 2 : TEST(alltypes.which_oneof == 0); 162 : 163 2 : TEST(alltypes.has_opt_non_zero_based_enum == false); 164 2 : TEST(alltypes.opt_non_zero_based_enum == NonZeroBasedEnum_Two); 165 : } 166 3 : else if (mode == 1) 167 : { 168 : /* Expect filled-in values */ 169 2 : TEST(alltypes.has_opt_int32 == true); 170 2 : TEST(alltypes.opt_int32 == 3041); 171 2 : TEST(alltypes.has_opt_int64 == true); 172 2 : TEST(alltypes.opt_int64 == 3042); 173 2 : TEST(alltypes.has_opt_uint32 == true); 174 2 : TEST(alltypes.opt_uint32 == 3043); 175 2 : TEST(alltypes.has_opt_uint64 == true); 176 2 : TEST(alltypes.opt_uint64 == 3044); 177 2 : TEST(alltypes.has_opt_sint32 == true); 178 2 : TEST(alltypes.opt_sint32 == 3045); 179 2 : TEST(alltypes.has_opt_sint64 == true); 180 2 : TEST(alltypes.opt_sint64 == 3046); 181 2 : TEST(alltypes.has_opt_bool == true); 182 2 : TEST(alltypes.opt_bool == true); 183 : 184 2 : TEST(alltypes.has_opt_fixed32 == true); 185 2 : TEST(alltypes.opt_fixed32 == 3048); 186 2 : TEST(alltypes.has_opt_sfixed32 == true); 187 2 : TEST(alltypes.opt_sfixed32 == 3049); 188 2 : TEST(alltypes.has_opt_float == true); 189 2 : TEST(alltypes.opt_float == 3050.0f); 190 : 191 2 : TEST(alltypes.has_opt_fixed64 == true); 192 2 : TEST(alltypes.opt_fixed64 == 3051); 193 2 : TEST(alltypes.has_opt_sfixed64 == true); 194 2 : TEST(alltypes.opt_sfixed64 == 3052); 195 2 : TEST(alltypes.has_opt_double == true); 196 2 : TEST(alltypes.opt_double == 3053.0); 197 : 198 2 : TEST(alltypes.has_opt_string == true); 199 2 : TEST(strcmp(alltypes.opt_string, "3054") == 0); 200 2 : TEST(alltypes.has_opt_bytes == true); 201 2 : TEST(alltypes.opt_bytes.size == 4); 202 2 : TEST(memcmp(alltypes.opt_bytes.bytes, "3055", 4) == 0); 203 2 : TEST(alltypes.has_opt_submsg == true); 204 2 : TEST(strcmp(alltypes.opt_submsg.substuff1, "3056") == 0); 205 2 : TEST(alltypes.opt_submsg.substuff2 == 3056); 206 2 : TEST(alltypes.opt_submsg.substuff3 == 3); 207 2 : TEST(alltypes.has_opt_enum == true); 208 2 : TEST(alltypes.opt_enum == MyEnum_Truth); 209 2 : TEST(alltypes.has_opt_emptymsg == true); 210 2 : TEST(alltypes.has_opt_fbytes == true); 211 2 : TEST(memcmp(alltypes.opt_fbytes, "3059", 4) == 0); 212 : 213 2 : TEST(alltypes.which_oneof == AllTypes_oneof_msg1_tag); 214 2 : TEST(strcmp(alltypes.oneof.oneof_msg1.substuff1, "4059") == 0); 215 2 : TEST(alltypes.oneof.oneof_msg1.substuff2 == 4059); 216 2 : TEST(alltypes.oneof.oneof_msg1.substuff3 == 3); 217 : 218 2 : TEST(alltypes.has_opt_non_zero_based_enum == true); 219 2 : TEST(alltypes.opt_non_zero_based_enum == NonZeroBasedEnum_Three); 220 : } 221 1 : else if (mode == 2) 222 : { 223 : /* Expect zero values */ 224 1 : TEST(alltypes.req_int32 == 0); 225 1 : TEST(alltypes.req_int64 == 0); 226 1 : TEST(alltypes.req_uint32 == 0); 227 1 : TEST(alltypes.req_uint64 == 0); 228 1 : TEST(alltypes.req_sint32 == 0); 229 1 : TEST(alltypes.req_sint64 == 0); 230 1 : TEST(alltypes.req_bool == false); 231 : 232 1 : TEST(alltypes.req_fixed32 == 0); 233 1 : TEST(alltypes.req_sfixed32 == 0); 234 1 : TEST(alltypes.req_float == 0.0f); 235 : 236 1 : TEST(alltypes.req_fixed64 == 0); 237 1 : TEST(alltypes.req_sfixed64 == 0); 238 1 : TEST(alltypes.req_double == 0.0f); 239 : 240 1 : TEST(strcmp(alltypes.req_string, "") == 0); 241 1 : TEST(alltypes.req_bytes.size == 0); 242 1 : TEST(strcmp(alltypes.req_submsg.substuff1, "") == 0); 243 1 : TEST(alltypes.req_submsg.substuff2 == 0); 244 1 : TEST(alltypes.req_enum == MyEnum_Zero); 245 : 246 1 : TEST(alltypes.rep_int32_count == 0); 247 1 : TEST(alltypes.rep_int64_count == 0); 248 1 : TEST(alltypes.rep_uint32_count == 0); 249 1 : TEST(alltypes.rep_uint64_count == 0); 250 1 : TEST(alltypes.rep_sint32_count == 0); 251 1 : TEST(alltypes.rep_sint64_count == 0); 252 1 : TEST(alltypes.rep_bool_count == 0); 253 : 254 1 : TEST(alltypes.rep_fixed32_count == 0); 255 1 : TEST(alltypes.rep_sfixed32_count == 0); 256 1 : TEST(alltypes.rep_float_count == 0); 257 : 258 1 : TEST(alltypes.rep_fixed64_count == 0); 259 1 : TEST(alltypes.rep_sfixed64_count == 0); 260 1 : TEST(alltypes.rep_double_count == 0); 261 : 262 1 : TEST(alltypes.rep_string_count == 0); 263 1 : TEST(alltypes.rep_bytes_count == 0); 264 : 265 1 : TEST(alltypes.rep_submsg_count == 0); 266 : 267 1 : TEST(alltypes.rep_enum_count == 0); 268 1 : TEST(alltypes.rep_emptymsg_count == 0); 269 1 : TEST(alltypes.rep_fbytes_count == 0); 270 1 : TEST(alltypes.rep_farray[0] == 0 && alltypes.rep_farray[4] == 0); 271 : 272 1 : TEST(alltypes.has_opt_int32 == false); 273 1 : TEST(alltypes.has_opt_int64 == false); 274 1 : TEST(alltypes.has_opt_uint32 == false); 275 1 : TEST(alltypes.has_opt_uint64 == false); 276 1 : TEST(alltypes.has_opt_sint32 == false); 277 1 : TEST(alltypes.has_opt_sint64 == false); 278 1 : TEST(alltypes.has_opt_bool == false); 279 : 280 1 : TEST(alltypes.has_opt_fixed32 == false); 281 1 : TEST(alltypes.has_opt_sfixed32 == false); 282 1 : TEST(alltypes.has_opt_float == false); 283 : 284 1 : TEST(alltypes.has_opt_fixed64 == false); 285 1 : TEST(alltypes.has_opt_sfixed64 == false); 286 1 : TEST(alltypes.has_opt_double == false); 287 : 288 1 : TEST(alltypes.has_opt_string == false); 289 1 : TEST(alltypes.has_opt_bytes == false); 290 1 : TEST(alltypes.has_opt_submsg == false); 291 1 : TEST(alltypes.has_opt_enum == false); 292 1 : TEST(alltypes.has_opt_emptymsg == false); 293 1 : TEST(alltypes.has_opt_fbytes == false); 294 : 295 1 : TEST(alltypes.which_oneof == 0); 296 : 297 1 : TEST(alltypes.has_opt_non_zero_based_enum == false); 298 : } 299 : 300 5 : TEST(alltypes.end == 1099); 301 : 302 5 : return status == 0; 303 : } 304 : 305 : #ifdef __cplusplus 306 : extern "C" 307 : #endif 308 5 : int main(int argc, char **argv) 309 : { 310 : uint8_t buffer[1024]; 311 : size_t count; 312 : pb_istream_t stream; 313 : 314 : /* Whether to expect the optional values or the default values. */ 315 5 : int mode = (argc > 1) ? atoi(argv[1]) : 0; 316 : 317 : /* Read the data into buffer */ 318 : SET_BINARY_MODE(stdin); 319 5 : count = fread(buffer, 1, sizeof(buffer), stdin); 320 : 321 : /* Construct a pb_istream_t for reading from the buffer */ 322 5 : stream = pb_istream_from_buffer(buffer, count); 323 : 324 : /* Decode and print out the stuff */ 325 5 : if (!check_alltypes(&stream, mode)) 326 : { 327 0 : printf("Parsing failed: %s\n", PB_GET_ERROR(&stream)); 328 0 : return 1; 329 : } else { 330 5 : return 0; 331 : } 332 : }