1 #define PB_VALIDATE_UTF8
2 #include "pb_common.c"
3 
4 #include <stdio.h>
5 #include <string.h>
6 #include "unittests.h"
7 #include "../alltypes/alltypes.pb.h"
8 
main()9 int main()
10 {
11     int status = 0;
12 
13     {
14         AllTypes msg;
15         pb_field_iter_t iter;
16 
17         COMMENT("Test field iterator logic on AllTypes");
18 
19         TEST(pb_field_iter_begin(&iter, AllTypes_fields, &msg))
20 
21         TEST(iter.tag == 1  && iter.pData == &msg.req_int32 && !iter.pSize)
22         TEST(pb_field_iter_next(&iter) && iter.tag == 2  && iter.pData == &msg.req_int64    && !iter.pSize)
23         TEST(pb_field_iter_next(&iter) && iter.tag == 3  && iter.pData == &msg.req_uint32   && !iter.pSize)
24         TEST(pb_field_iter_next(&iter) && iter.tag == 4  && iter.pData == &msg.req_uint64   && !iter.pSize)
25         TEST(pb_field_iter_next(&iter) && iter.tag == 5  && iter.pData == &msg.req_sint32   && !iter.pSize)
26         TEST(pb_field_iter_next(&iter) && iter.tag == 6  && iter.pData == &msg.req_sint64   && !iter.pSize)
27         TEST(pb_field_iter_next(&iter) && iter.tag == 7  && iter.pData == &msg.req_bool     && !iter.pSize)
28         TEST(pb_field_iter_next(&iter) && iter.tag == 8  && iter.pData == &msg.req_fixed32  && !iter.pSize)
29         TEST(pb_field_iter_next(&iter) && iter.tag == 9  && iter.pData == &msg.req_sfixed32 && !iter.pSize)
30         TEST(pb_field_iter_next(&iter) && iter.tag == 10 && iter.pData == &msg.req_float    && !iter.pSize)
31         TEST(pb_field_iter_next(&iter) && iter.tag == 11 && iter.pData == &msg.req_fixed64  && !iter.pSize)
32         TEST(pb_field_iter_next(&iter) && iter.tag == 12 && iter.pData == &msg.req_sfixed64 && !iter.pSize)
33         TEST(pb_field_iter_next(&iter) && iter.tag == 13 && iter.pData == &msg.req_double   && !iter.pSize)
34         TEST(pb_field_iter_next(&iter) && iter.tag == 14 && iter.pData == &msg.req_string   && !iter.pSize)
35         TEST(pb_field_iter_next(&iter) && iter.tag == 15 && iter.pData == &msg.req_bytes    && !iter.pSize)
36         TEST(pb_field_iter_next(&iter) && iter.tag == 16 && iter.pData == &msg.req_submsg   && !iter.pSize
37              && iter.submsg_desc == SubMessage_fields)
38         TEST(pb_field_iter_next(&iter) && iter.tag == 17 && iter.pData == &msg.req_enum     && !iter.pSize)
39         TEST(pb_field_iter_next(&iter) && iter.tag == 18 && iter.pData == &msg.req_emptymsg && !iter.pSize)
40         TEST(pb_field_iter_next(&iter) && iter.tag == 19 && iter.pData == &msg.req_fbytes   && !iter.pSize)
41         TEST(iter.required_field_index == 18)
42         TEST(iter.submessage_index == 2)
43 
44         TEST(pb_field_iter_next(&iter) && iter.tag == 21 && iter.pData == &msg.rep_int32    && iter.pSize == &msg.rep_int32_count)
45         TEST(pb_field_iter_next(&iter) && iter.tag == 22 && iter.pData == &msg.rep_int64    && iter.pSize == &msg.rep_int64_count)
46         TEST(pb_field_iter_next(&iter) && iter.tag == 23 && iter.pData == &msg.rep_uint32   && iter.pSize == &msg.rep_uint32_count)
47         TEST(pb_field_iter_next(&iter) && iter.tag == 24 && iter.pData == &msg.rep_uint64   && iter.pSize == &msg.rep_uint64_count)
48         TEST(pb_field_iter_next(&iter) && iter.tag == 25 && iter.pData == &msg.rep_sint32   && iter.pSize == &msg.rep_sint32_count)
49         TEST(pb_field_iter_next(&iter) && iter.tag == 26 && iter.pData == &msg.rep_sint64   && iter.pSize == &msg.rep_sint64_count)
50         TEST(pb_field_iter_next(&iter) && iter.tag == 27 && iter.pData == &msg.rep_bool     && iter.pSize == &msg.rep_bool_count)
51         TEST(pb_field_iter_next(&iter) && iter.tag == 28 && iter.pData == &msg.rep_fixed32  && iter.pSize == &msg.rep_fixed32_count)
52         TEST(pb_field_iter_next(&iter) && iter.tag == 29 && iter.pData == &msg.rep_sfixed32 && iter.pSize == &msg.rep_sfixed32_count)
53         TEST(pb_field_iter_next(&iter) && iter.tag == 30 && iter.pData == &msg.rep_float    && iter.pSize == &msg.rep_float_count)
54         TEST(pb_field_iter_next(&iter) && iter.tag == 31 && iter.pData == &msg.rep_fixed64  && iter.pSize == &msg.rep_fixed64_count)
55         TEST(pb_field_iter_next(&iter) && iter.tag == 32 && iter.pData == &msg.rep_sfixed64 && iter.pSize == &msg.rep_sfixed64_count)
56         TEST(pb_field_iter_next(&iter) && iter.tag == 33 && iter.pData == &msg.rep_double   && iter.pSize == &msg.rep_double_count)
57         TEST(pb_field_iter_next(&iter) && iter.tag == 34 && iter.pData == &msg.rep_string   && iter.pSize == &msg.rep_string_count)
58         TEST(pb_field_iter_next(&iter) && iter.tag == 35 && iter.pData == &msg.rep_bytes    && iter.pSize == &msg.rep_bytes_count)
59         TEST(pb_field_iter_next(&iter) && iter.tag == 36 && iter.pData == &msg.rep_submsg   && iter.pSize == &msg.rep_submsg_count)
60         TEST(pb_field_iter_next(&iter) && iter.tag == 37 && iter.pData == &msg.rep_enum     && iter.pSize == &msg.rep_enum_count)
61         TEST(pb_field_iter_next(&iter) && iter.tag == 38 && iter.pData == &msg.rep_emptymsg && iter.pSize == &msg.rep_emptymsg_count)
62         TEST(pb_field_iter_next(&iter) && iter.tag == 39 && iter.pData == &msg.rep_fbytes   && iter.pSize == &msg.rep_fbytes_count)
63         TEST(pb_field_iter_next(&iter) && iter.tag == 40 && iter.pData == &msg.rep_farray   && iter.pSize == &iter.array_size && iter.array_size == 5)
64         TEST(iter.required_field_index == 19)
65         TEST(iter.submessage_index == 4)
66 
67         TEST(pb_field_iter_next(&iter) && iter.tag == 41 && iter.pData == &msg.opt_int32    && iter.pSize == &msg.has_opt_int32   )
68         TEST(pb_field_iter_next(&iter) && iter.tag == 42 && iter.pData == &msg.opt_int64    && iter.pSize == &msg.has_opt_int64   )
69         TEST(pb_field_iter_next(&iter) && iter.tag == 43 && iter.pData == &msg.opt_uint32   && iter.pSize == &msg.has_opt_uint32  )
70         TEST(pb_field_iter_next(&iter) && iter.tag == 44 && iter.pData == &msg.opt_uint64   && iter.pSize == &msg.has_opt_uint64  )
71         TEST(pb_field_iter_next(&iter) && iter.tag == 45 && iter.pData == &msg.opt_sint32   && iter.pSize == &msg.has_opt_sint32  )
72         TEST(pb_field_iter_next(&iter) && iter.tag == 46 && iter.pData == &msg.opt_sint64   && iter.pSize == &msg.has_opt_sint64  )
73         TEST(pb_field_iter_next(&iter) && iter.tag == 47 && iter.pData == &msg.opt_bool     && iter.pSize == &msg.has_opt_bool    )
74         TEST(pb_field_iter_next(&iter) && iter.tag == 48 && iter.pData == &msg.opt_fixed32  && iter.pSize == &msg.has_opt_fixed32 )
75         TEST(pb_field_iter_next(&iter) && iter.tag == 49 && iter.pData == &msg.opt_sfixed32 && iter.pSize == &msg.has_opt_sfixed32)
76         TEST(pb_field_iter_next(&iter) && iter.tag == 50 && iter.pData == &msg.opt_float    && iter.pSize == &msg.has_opt_float   )
77         TEST(pb_field_iter_next(&iter) && iter.tag == 51 && iter.pData == &msg.opt_fixed64  && iter.pSize == &msg.has_opt_fixed64 )
78         TEST(pb_field_iter_next(&iter) && iter.tag == 52 && iter.pData == &msg.opt_sfixed64 && iter.pSize == &msg.has_opt_sfixed64)
79         TEST(pb_field_iter_next(&iter) && iter.tag == 53 && iter.pData == &msg.opt_double   && iter.pSize == &msg.has_opt_double  )
80         TEST(pb_field_iter_next(&iter) && iter.tag == 54 && iter.pData == &msg.opt_string   && iter.pSize == &msg.has_opt_string  )
81         TEST(pb_field_iter_next(&iter) && iter.tag == 55 && iter.pData == &msg.opt_bytes    && iter.pSize == &msg.has_opt_bytes   )
82         TEST(pb_field_iter_next(&iter) && iter.tag == 56 && iter.pData == &msg.opt_submsg   && iter.pSize == &msg.has_opt_submsg  )
83         TEST(pb_field_iter_next(&iter) && iter.tag == 57 && iter.pData == &msg.opt_enum     && iter.pSize == &msg.has_opt_enum    )
84         TEST(pb_field_iter_next(&iter) && iter.tag == 58 && iter.pData == &msg.opt_emptymsg && iter.pSize == &msg.has_opt_emptymsg)
85         TEST(pb_field_iter_next(&iter) && iter.tag == 59 && iter.pData == &msg.opt_fbytes   && iter.pSize == &msg.has_opt_fbytes)
86         TEST(iter.required_field_index == 19)
87         TEST(iter.submessage_index == 6)
88 
89         TEST(pb_field_iter_next(&iter) && iter.tag == 60 && iter.pData == &msg.oneof.oneof_msg1  && iter.pSize == &msg.which_oneof   )
90         TEST(pb_field_iter_next(&iter) && iter.tag == 61 && iter.pData == &msg.oneof.oneof_msg2  && iter.pSize == &msg.which_oneof   )
91         TEST(pb_field_iter_next(&iter) && iter.tag == 62 && iter.pData == &msg.opt_non_zero_based_enum && iter.pSize == &msg.has_opt_non_zero_based_enum)
92         TEST(pb_field_iter_next(&iter) && iter.tag == 63 && iter.pData == &msg.oneof.static_msg  && iter.pSize == &msg.which_oneof   )
93         TEST(iter.required_field_index == 19)
94         TEST(iter.submessage_index == 8)
95 
96         TEST(pb_field_iter_next(&iter) && iter.tag == 95 && iter.pData == &msg.rep_farray2 && iter.pSize == &iter.array_size && iter.array_size == 3)
97         TEST(iter.required_field_index == 19)
98         TEST(iter.submessage_index == 9)
99 
100         TEST(pb_field_iter_next(&iter) && iter.tag == 96 && iter.pData == &msg.req_intsizes && !iter.pSize)
101         TEST(iter.required_field_index == 19)
102         TEST(iter.submessage_index == 9)
103 
104         TEST(pb_field_iter_next(&iter) && iter.tag == 97 && iter.pData == &msg.req_ds8 && !iter.pSize)
105         TEST(iter.required_field_index == 20)
106         TEST(iter.submessage_index == 10)
107 
108         TEST(pb_field_iter_next(&iter) && iter.tag == 98 && iter.pData == &msg.req_limits && !iter.pSize)
109         TEST(iter.required_field_index == 21)
110         TEST(iter.submessage_index == 11)
111 
112         TEST(pb_field_iter_next(&iter) && iter.tag == 200 && iter.pData == &msg.extensions && !iter.pSize)
113         TEST(iter.required_field_index == 22)
114         TEST(iter.submessage_index == 12)
115 
116         TEST(pb_field_iter_next(&iter) && iter.tag == 999 && iter.pData == &msg.end && !iter.pSize)
117         TEST(iter.required_field_index == 22)
118         TEST(iter.submessage_index == 12)
119 
120         TEST(!pb_field_iter_next(&iter))
121 
122         TEST(iter.tag == 1  && iter.pData == &msg.req_int32 && !iter.pSize)
123         TEST(iter.required_field_index == 0)
124         TEST(iter.submessage_index == 0)
125     }
126 
127     {
128         COMMENT("Test pb_validate_utf8()");
129 
130         TEST(pb_validate_utf8("abcdefg"));
131         TEST(pb_validate_utf8("\xc3\xa4\xc3\xa4\x6b\x6b\xc3\xb6\x6e\x65\x6e\x0a"));
132         TEST(!pb_validate_utf8("\xc3\xa4\xc3\xa4\x6b\x6b\xb6\xc3\x6e\x65\x6e\x0a"));
133         TEST(pb_validate_utf8("\xed\x9f\xbf"));
134         TEST(pb_validate_utf8("\xee\x80\x80"));
135         TEST(pb_validate_utf8("\xef\xbf\xbd"));
136         TEST(pb_validate_utf8("\xf4\x8f\xbf\xbf"));
137         TEST(!pb_validate_utf8("a\x80z"));
138         TEST(!pb_validate_utf8("a\xbfz"));
139         TEST(!pb_validate_utf8("a\xfez"));
140         TEST(!pb_validate_utf8("a\xffz"));
141         TEST(!pb_validate_utf8("a\xc0\xafz"));
142         TEST(!pb_validate_utf8("a\xef\xbf\xbez"));
143     }
144 
145     if (status != 0)
146         fprintf(stdout, "\n\nSome tests FAILED!\n");
147 
148     return status;
149 }
150