OLD | NEW |
1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
3 // https://developers.google.com/protocol-buffers/ | 3 // https://developers.google.com/protocol-buffers/ |
4 // | 4 // |
5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
7 // met: | 7 // met: |
8 // | 8 // |
9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
(...skipping 1120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1131 Utf8ValidationTest() {} | 1131 Utf8ValidationTest() {} |
1132 virtual ~Utf8ValidationTest() {} | 1132 virtual ~Utf8ValidationTest() {} |
1133 virtual void SetUp() { | 1133 virtual void SetUp() { |
1134 } | 1134 } |
1135 | 1135 |
1136 }; | 1136 }; |
1137 | 1137 |
1138 TEST_F(Utf8ValidationTest, WriteInvalidUTF8String) { | 1138 TEST_F(Utf8ValidationTest, WriteInvalidUTF8String) { |
1139 string wire_buffer; | 1139 string wire_buffer; |
1140 protobuf_unittest::OneString input; | 1140 protobuf_unittest::OneString input; |
1141 vector<string> errors; | 1141 std::vector<string> errors; |
1142 { | 1142 { |
1143 ScopedMemoryLog log; | 1143 ScopedMemoryLog log; |
1144 WriteMessage(kInvalidUTF8String, &input, &wire_buffer); | 1144 WriteMessage(kInvalidUTF8String, &input, &wire_buffer); |
1145 errors = log.GetMessages(ERROR); | 1145 errors = log.GetMessages(ERROR); |
1146 } | 1146 } |
1147 #ifdef GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED | 1147 #ifdef GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED |
1148 ASSERT_EQ(1, errors.size()); | 1148 ASSERT_EQ(1, errors.size()); |
1149 EXPECT_TRUE(StartsWith(errors[0], | 1149 EXPECT_TRUE(StartsWith(errors[0], |
1150 "String field 'protobuf_unittest.OneString.data' " | 1150 "String field 'protobuf_unittest.OneString.data' " |
1151 "contains invalid UTF-8 data when " | 1151 "contains invalid UTF-8 data when " |
1152 "serializing a protocol buffer. Use the " | 1152 "serializing a protocol buffer. Use the " |
1153 "'bytes' type if you intend to send raw bytes.")); | 1153 "'bytes' type if you intend to send raw bytes.")); |
1154 #else | 1154 #else |
1155 ASSERT_EQ(0, errors.size()); | 1155 ASSERT_EQ(0, errors.size()); |
1156 #endif // GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED | 1156 #endif // GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED |
1157 } | 1157 } |
1158 | 1158 |
1159 | 1159 |
1160 TEST_F(Utf8ValidationTest, ReadInvalidUTF8String) { | 1160 TEST_F(Utf8ValidationTest, ReadInvalidUTF8String) { |
1161 string wire_buffer; | 1161 string wire_buffer; |
1162 protobuf_unittest::OneString input; | 1162 protobuf_unittest::OneString input; |
1163 WriteMessage(kInvalidUTF8String, &input, &wire_buffer); | 1163 WriteMessage(kInvalidUTF8String, &input, &wire_buffer); |
1164 protobuf_unittest::OneString output; | 1164 protobuf_unittest::OneString output; |
1165 vector<string> errors; | 1165 std::vector<string> errors; |
1166 { | 1166 { |
1167 ScopedMemoryLog log; | 1167 ScopedMemoryLog log; |
1168 ReadMessage(wire_buffer, &output); | 1168 ReadMessage(wire_buffer, &output); |
1169 errors = log.GetMessages(ERROR); | 1169 errors = log.GetMessages(ERROR); |
1170 } | 1170 } |
1171 #ifdef GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED | 1171 #ifdef GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED |
1172 ASSERT_EQ(1, errors.size()); | 1172 ASSERT_EQ(1, errors.size()); |
1173 EXPECT_TRUE(StartsWith(errors[0], | 1173 EXPECT_TRUE(StartsWith(errors[0], |
1174 "String field 'protobuf_unittest.OneString.data' " | 1174 "String field 'protobuf_unittest.OneString.data' " |
1175 "contains invalid UTF-8 data when " | 1175 "contains invalid UTF-8 data when " |
1176 "parsing a protocol buffer. Use the " | 1176 "parsing a protocol buffer. Use the " |
1177 "'bytes' type if you intend to send raw bytes.")); | 1177 "'bytes' type if you intend to send raw bytes.")); |
1178 | 1178 |
1179 #else | 1179 #else |
1180 ASSERT_EQ(0, errors.size()); | 1180 ASSERT_EQ(0, errors.size()); |
1181 #endif // GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED | 1181 #endif // GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED |
1182 } | 1182 } |
1183 | 1183 |
1184 | 1184 |
1185 TEST_F(Utf8ValidationTest, WriteValidUTF8String) { | 1185 TEST_F(Utf8ValidationTest, WriteValidUTF8String) { |
1186 string wire_buffer; | 1186 string wire_buffer; |
1187 protobuf_unittest::OneString input; | 1187 protobuf_unittest::OneString input; |
1188 vector<string> errors; | 1188 std::vector<string> errors; |
1189 { | 1189 { |
1190 ScopedMemoryLog log; | 1190 ScopedMemoryLog log; |
1191 WriteMessage(kValidUTF8String, &input, &wire_buffer); | 1191 WriteMessage(kValidUTF8String, &input, &wire_buffer); |
1192 errors = log.GetMessages(ERROR); | 1192 errors = log.GetMessages(ERROR); |
1193 } | 1193 } |
1194 ASSERT_EQ(0, errors.size()); | 1194 ASSERT_EQ(0, errors.size()); |
1195 } | 1195 } |
1196 | 1196 |
1197 TEST_F(Utf8ValidationTest, ReadValidUTF8String) { | 1197 TEST_F(Utf8ValidationTest, ReadValidUTF8String) { |
1198 string wire_buffer; | 1198 string wire_buffer; |
1199 protobuf_unittest::OneString input; | 1199 protobuf_unittest::OneString input; |
1200 WriteMessage(kValidUTF8String, &input, &wire_buffer); | 1200 WriteMessage(kValidUTF8String, &input, &wire_buffer); |
1201 protobuf_unittest::OneString output; | 1201 protobuf_unittest::OneString output; |
1202 vector<string> errors; | 1202 std::vector<string> errors; |
1203 { | 1203 { |
1204 ScopedMemoryLog log; | 1204 ScopedMemoryLog log; |
1205 ReadMessage(wire_buffer, &output); | 1205 ReadMessage(wire_buffer, &output); |
1206 errors = log.GetMessages(ERROR); | 1206 errors = log.GetMessages(ERROR); |
1207 } | 1207 } |
1208 ASSERT_EQ(0, errors.size()); | 1208 ASSERT_EQ(0, errors.size()); |
1209 EXPECT_EQ(input.data(), output.data()); | 1209 EXPECT_EQ(input.data(), output.data()); |
1210 } | 1210 } |
1211 | 1211 |
1212 // Bytes: anything can pass as bytes, use invalid UTF-8 string to test | 1212 // Bytes: anything can pass as bytes, use invalid UTF-8 string to test |
1213 TEST_F(Utf8ValidationTest, WriteArbitraryBytes) { | 1213 TEST_F(Utf8ValidationTest, WriteArbitraryBytes) { |
1214 string wire_buffer; | 1214 string wire_buffer; |
1215 protobuf_unittest::OneBytes input; | 1215 protobuf_unittest::OneBytes input; |
1216 vector<string> errors; | 1216 std::vector<string> errors; |
1217 { | 1217 { |
1218 ScopedMemoryLog log; | 1218 ScopedMemoryLog log; |
1219 WriteMessage(kInvalidUTF8String, &input, &wire_buffer); | 1219 WriteMessage(kInvalidUTF8String, &input, &wire_buffer); |
1220 errors = log.GetMessages(ERROR); | 1220 errors = log.GetMessages(ERROR); |
1221 } | 1221 } |
1222 ASSERT_EQ(0, errors.size()); | 1222 ASSERT_EQ(0, errors.size()); |
1223 } | 1223 } |
1224 | 1224 |
1225 TEST_F(Utf8ValidationTest, ReadArbitraryBytes) { | 1225 TEST_F(Utf8ValidationTest, ReadArbitraryBytes) { |
1226 string wire_buffer; | 1226 string wire_buffer; |
1227 protobuf_unittest::OneBytes input; | 1227 protobuf_unittest::OneBytes input; |
1228 WriteMessage(kInvalidUTF8String, &input, &wire_buffer); | 1228 WriteMessage(kInvalidUTF8String, &input, &wire_buffer); |
1229 protobuf_unittest::OneBytes output; | 1229 protobuf_unittest::OneBytes output; |
1230 vector<string> errors; | 1230 std::vector<string> errors; |
1231 { | 1231 { |
1232 ScopedMemoryLog log; | 1232 ScopedMemoryLog log; |
1233 ReadMessage(wire_buffer, &output); | 1233 ReadMessage(wire_buffer, &output); |
1234 errors = log.GetMessages(ERROR); | 1234 errors = log.GetMessages(ERROR); |
1235 } | 1235 } |
1236 ASSERT_EQ(0, errors.size()); | 1236 ASSERT_EQ(0, errors.size()); |
1237 EXPECT_EQ(input.data(), output.data()); | 1237 EXPECT_EQ(input.data(), output.data()); |
1238 } | 1238 } |
1239 | 1239 |
1240 TEST_F(Utf8ValidationTest, ParseRepeatedString) { | 1240 TEST_F(Utf8ValidationTest, ParseRepeatedString) { |
1241 protobuf_unittest::MoreBytes input; | 1241 protobuf_unittest::MoreBytes input; |
1242 input.add_data(kValidUTF8String); | 1242 input.add_data(kValidUTF8String); |
1243 input.add_data(kInvalidUTF8String); | 1243 input.add_data(kInvalidUTF8String); |
1244 input.add_data(kInvalidUTF8String); | 1244 input.add_data(kInvalidUTF8String); |
1245 string wire_buffer = input.SerializeAsString(); | 1245 string wire_buffer = input.SerializeAsString(); |
1246 | 1246 |
1247 protobuf_unittest::MoreString output; | 1247 protobuf_unittest::MoreString output; |
1248 vector<string> errors; | 1248 std::vector<string> errors; |
1249 { | 1249 { |
1250 ScopedMemoryLog log; | 1250 ScopedMemoryLog log; |
1251 ReadMessage(wire_buffer, &output); | 1251 ReadMessage(wire_buffer, &output); |
1252 errors = log.GetMessages(ERROR); | 1252 errors = log.GetMessages(ERROR); |
1253 } | 1253 } |
1254 #ifdef GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED | 1254 #ifdef GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED |
1255 ASSERT_EQ(2, errors.size()); | 1255 ASSERT_EQ(2, errors.size()); |
1256 #else | 1256 #else |
1257 ASSERT_EQ(0, errors.size()); | 1257 ASSERT_EQ(0, errors.size()); |
1258 #endif // GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED | 1258 #endif // GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED |
1259 EXPECT_EQ(wire_buffer, output.SerializeAsString()); | 1259 EXPECT_EQ(wire_buffer, output.SerializeAsString()); |
1260 } | 1260 } |
1261 | 1261 |
1262 // Test the old VerifyUTF8String() function, which may still be called by old | 1262 // Test the old VerifyUTF8String() function, which may still be called by old |
1263 // generated code. | 1263 // generated code. |
1264 TEST_F(Utf8ValidationTest, OldVerifyUTF8String) { | 1264 TEST_F(Utf8ValidationTest, OldVerifyUTF8String) { |
1265 string data(kInvalidUTF8String); | 1265 string data(kInvalidUTF8String); |
1266 | 1266 |
1267 vector<string> errors; | 1267 std::vector<string> errors; |
1268 { | 1268 { |
1269 ScopedMemoryLog log; | 1269 ScopedMemoryLog log; |
1270 WireFormat::VerifyUTF8String(data.data(), data.size(), | 1270 WireFormat::VerifyUTF8String(data.data(), data.size(), |
1271 WireFormat::SERIALIZE); | 1271 WireFormat::SERIALIZE); |
1272 errors = log.GetMessages(ERROR); | 1272 errors = log.GetMessages(ERROR); |
1273 } | 1273 } |
1274 #ifdef GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED | 1274 #ifdef GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED |
1275 ASSERT_EQ(1, errors.size()); | 1275 ASSERT_EQ(1, errors.size()); |
1276 EXPECT_TRUE(StartsWith(errors[0], | 1276 EXPECT_TRUE(StartsWith(errors[0], |
1277 "String field contains invalid UTF-8 data when " | 1277 "String field contains invalid UTF-8 data when " |
1278 "serializing a protocol buffer. Use the " | 1278 "serializing a protocol buffer. Use the " |
1279 "'bytes' type if you intend to send raw bytes.")); | 1279 "'bytes' type if you intend to send raw bytes.")); |
1280 #else | 1280 #else |
1281 ASSERT_EQ(0, errors.size()); | 1281 ASSERT_EQ(0, errors.size()); |
1282 #endif | 1282 #endif |
1283 } | 1283 } |
1284 | 1284 |
1285 | 1285 |
1286 } // namespace | 1286 } // namespace |
1287 } // namespace internal | 1287 } // namespace internal |
1288 } // namespace protobuf | 1288 } // namespace protobuf |
1289 } // namespace google | 1289 } // namespace google |
OLD | NEW |