common_unittests.c 11 KB

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