|
14 | 14 | #include "upb/mem/arena.hpp" |
15 | 15 | #include "upb/message/utf8_test.upb.h" |
16 | 16 | #include "upb/message/utf8_test.upb_minitable.h" |
| 17 | +#include "upb/message/utf8_test_proto2.upb.h" |
| 18 | +#include "upb/message/utf8_test_proto2.upb_minitable.h" |
17 | 19 | #include "upb/wire/decode.h" |
18 | 20 |
|
19 | 21 | namespace { |
@@ -72,6 +74,100 @@ TEST(Utf8Test, RepeatedProto3FieldValidates) { |
72 | 74 | ASSERT_EQ(kUpb_DecodeStatus_BadUtf8, status); |
73 | 75 | } |
74 | 76 |
|
| 77 | +TEST(Utf8Test, Proto2BytesValidates) { |
| 78 | + upb::Arena arena; |
| 79 | + size_t size; |
| 80 | + char* data = GetBadUtf8Payload(arena.ptr(), &size); |
| 81 | + |
| 82 | + upb_test_TestUtf8Proto2Bytes* msg = |
| 83 | + upb_test_TestUtf8Proto2Bytes_new(arena.ptr()); |
| 84 | + |
| 85 | + upb_DecodeStatus status; |
| 86 | + status = upb_Decode(data, size, UPB_UPCAST(msg), |
| 87 | + &upb_0test__TestUtf8Proto2Bytes_msg_init, nullptr, 0, |
| 88 | + arena.ptr()); |
| 89 | + |
| 90 | + // Parse succeeds, because proto2 bytes fields don't validate UTF-8. |
| 91 | + ASSERT_EQ(kUpb_DecodeStatus_Ok, status); |
| 92 | +} |
| 93 | + |
| 94 | +TEST(Utf8Test, Proto2RepeatedBytesValidates) { |
| 95 | + upb::Arena arena; |
| 96 | + size_t size; |
| 97 | + char* data = GetBadUtf8Payload(arena.ptr(), &size); |
| 98 | + |
| 99 | + upb_test_TestUtf8RepeatedProto2Bytes* msg = |
| 100 | + upb_test_TestUtf8RepeatedProto2Bytes_new(arena.ptr()); |
| 101 | + |
| 102 | + upb_DecodeStatus status; |
| 103 | + status = upb_Decode(data, size, UPB_UPCAST(msg), |
| 104 | + &upb_0test__TestUtf8RepeatedProto2Bytes_msg_init, nullptr, |
| 105 | + 0, arena.ptr()); |
| 106 | + |
| 107 | + // Parse succeeds, because proto2 bytes fields don't validate UTF-8. |
| 108 | + ASSERT_EQ(kUpb_DecodeStatus_Ok, status); |
| 109 | +} |
| 110 | + |
| 111 | +TEST(Utf8Test, Proto2StringValidates) { |
| 112 | + upb::Arena arena; |
| 113 | + size_t size; |
| 114 | + char* data = GetBadUtf8Payload(arena.ptr(), &size); |
| 115 | + |
| 116 | + upb_test_TestUtf8Proto2String* msg = |
| 117 | + upb_test_TestUtf8Proto2String_new(arena.ptr()); |
| 118 | + |
| 119 | + upb_DecodeStatus status; |
| 120 | + status = upb_Decode(data, size, UPB_UPCAST(msg), |
| 121 | + &upb_0test__TestUtf8Proto2String_msg_init, nullptr, 0, |
| 122 | + arena.ptr()); |
| 123 | + |
| 124 | + // Parse succeeds, because proto2 string fields don't validate UTF-8. |
| 125 | + ASSERT_EQ(kUpb_DecodeStatus_Ok, status); |
| 126 | +} |
| 127 | + |
| 128 | +TEST(Utf8Test, Proto2FieldFailsValidation) { |
| 129 | + upb::Arena arena; |
| 130 | + size_t size; |
| 131 | + char* data = GetBadUtf8Payload(arena.ptr(), &size); |
| 132 | + |
| 133 | + upb_test_TestUtf8Proto2String* msg = |
| 134 | + upb_test_TestUtf8Proto2String_new(arena.ptr()); |
| 135 | + |
| 136 | + upb_DecodeStatus status; |
| 137 | + status = upb_Decode(data, size, UPB_UPCAST(msg), |
| 138 | + &upb_0test__TestUtf8Proto2String_msg_init, nullptr, 0, |
| 139 | + arena.ptr()); |
| 140 | + |
| 141 | + // Parse fails, because we pass in kUpb_DecodeOption_AlwaysValidateUtf8 to |
| 142 | + // force validation of proto2 string fields. |
| 143 | + status = upb_Decode(data, size, UPB_UPCAST(msg), |
| 144 | + &upb_0test__TestUtf8Proto2String_msg_init, nullptr, |
| 145 | + kUpb_DecodeOption_AlwaysValidateUtf8, arena.ptr()); |
| 146 | + ASSERT_EQ(kUpb_DecodeStatus_BadUtf8, status); |
| 147 | +} |
| 148 | + |
| 149 | +TEST(Utf8Test, Proto2RepeatedFieldFailsValidation) { |
| 150 | + upb::Arena arena; |
| 151 | + size_t size; |
| 152 | + char* data = GetBadUtf8Payload(arena.ptr(), &size); |
| 153 | + |
| 154 | + upb_test_TestUtf8RepeatedProto2String* msg = |
| 155 | + upb_test_TestUtf8RepeatedProto2String_new(arena.ptr()); |
| 156 | + |
| 157 | + upb_DecodeStatus status; |
| 158 | + status = upb_Decode(data, size, UPB_UPCAST(msg), |
| 159 | + &upb_0test__TestUtf8RepeatedProto2String_msg_init, |
| 160 | + nullptr, 0, arena.ptr()); |
| 161 | + |
| 162 | + // Parse fails, because we pass in kUpb_DecodeOption_AlwaysValidateUtf8 to |
| 163 | + // force validation of proto2 string fields. |
| 164 | + status = |
| 165 | + upb_Decode(data, size, UPB_UPCAST(msg), |
| 166 | + &upb_0test__TestUtf8RepeatedProto2String_msg_init, nullptr, |
| 167 | + kUpb_DecodeOption_AlwaysValidateUtf8, arena.ptr()); |
| 168 | + ASSERT_EQ(kUpb_DecodeStatus_BadUtf8, status); |
| 169 | +} |
| 170 | + |
75 | 171 | // begin:google_only |
76 | 172 | // TEST(Utf8Test, Proto3MixedFieldValidates) { |
77 | 173 | // upb::Arena arena; |
|
0 commit comments