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