@ -0,0 +1,3 @@ |
||||
This directory contains unit test protos adapted from those in |
||||
src/google/protobuf, and C#-specific test protos for regression |
||||
tests against bugs found in the C# codegen or library. |
@ -0,0 +1,379 @@ |
||||
// Protocol Buffers - Google's data interchange format |
||||
// Copyright 2008 Google Inc. All rights reserved. |
||||
// https://developers.google.com/protocol-buffers/ |
||||
// |
||||
// Redistribution and use in source and binary forms, with or without |
||||
// modification, are permitted provided that the following conditions are |
||||
// met: |
||||
// |
||||
// * Redistributions of source code must retain the above copyright |
||||
// notice, this list of conditions and the following disclaimer. |
||||
// * Redistributions in binary form must reproduce the above |
||||
// copyright notice, this list of conditions and the following disclaimer |
||||
// in the documentation and/or other materials provided with the |
||||
// distribution. |
||||
// * Neither the name of Google Inc. nor the names of its |
||||
// contributors may be used to endorse or promote products derived from |
||||
// this software without specific prior written permission. |
||||
// |
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
|
||||
// Author: kenton@google.com (Kenton Varda) |
||||
// Based on original Protocol Buffers design by |
||||
// Sanjay Ghemawat, Jeff Dean, and others. |
||||
// |
||||
// A proto file we will use for unit testing. |
||||
|
||||
syntax = "proto3"; |
||||
|
||||
option csharp_namespace = "Google.Protobuf.TestProtos"; |
||||
|
||||
// Only present so we can test that we can read it (as an example |
||||
// of a non-C# option) |
||||
option java_outer_classname = "UnittestProto"; |
||||
|
||||
import "unittest_import_proto3.proto"; |
||||
|
||||
package protobuf_unittest3; |
||||
|
||||
// This proto includes every type of field in both singular and repeated |
||||
// forms. |
||||
message TestAllTypes { |
||||
message NestedMessage { |
||||
// The field name "b" fails to compile in proto1 because it conflicts with |
||||
// a local variable named "b" in one of the generated methods. Doh. |
||||
// This file needs to compile in proto1 to test backwards-compatibility. |
||||
int32 bb = 1; |
||||
} |
||||
|
||||
enum NestedEnum { |
||||
NESTED_ENUM_UNSPECIFIED = 0; |
||||
FOO = 1; |
||||
BAR = 2; |
||||
BAZ = 3; |
||||
NEG = -1; // Intentionally negative. |
||||
} |
||||
|
||||
// Singular |
||||
int32 single_int32 = 1; |
||||
int64 single_int64 = 2; |
||||
uint32 single_uint32 = 3; |
||||
uint64 single_uint64 = 4; |
||||
sint32 single_sint32 = 5; |
||||
sint64 single_sint64 = 6; |
||||
fixed32 single_fixed32 = 7; |
||||
fixed64 single_fixed64 = 8; |
||||
sfixed32 single_sfixed32 = 9; |
||||
sfixed64 single_sfixed64 = 10; |
||||
float single_float = 11; |
||||
double single_double = 12; |
||||
bool single_bool = 13; |
||||
string single_string = 14; |
||||
bytes single_bytes = 15; |
||||
|
||||
NestedMessage single_nested_message = 18; |
||||
ForeignMessage single_foreign_message = 19; |
||||
protobuf_unittest_import.ImportMessage single_import_message = 20; |
||||
|
||||
NestedEnum single_nested_enum = 21; |
||||
ForeignEnum single_foreign_enum = 22; |
||||
protobuf_unittest_import.ImportEnum single_import_enum = 23; |
||||
|
||||
// Defined in unittest_import_public.proto |
||||
protobuf_unittest_import.PublicImportMessage |
||||
single_public_import_message = 26; |
||||
|
||||
// Repeated |
||||
repeated int32 repeated_int32 = 31; |
||||
repeated int64 repeated_int64 = 32; |
||||
repeated uint32 repeated_uint32 = 33; |
||||
repeated uint64 repeated_uint64 = 34; |
||||
repeated sint32 repeated_sint32 = 35; |
||||
repeated sint64 repeated_sint64 = 36; |
||||
repeated fixed32 repeated_fixed32 = 37; |
||||
repeated fixed64 repeated_fixed64 = 38; |
||||
repeated sfixed32 repeated_sfixed32 = 39; |
||||
repeated sfixed64 repeated_sfixed64 = 40; |
||||
repeated float repeated_float = 41; |
||||
repeated double repeated_double = 42; |
||||
repeated bool repeated_bool = 43; |
||||
repeated string repeated_string = 44; |
||||
repeated bytes repeated_bytes = 45; |
||||
|
||||
repeated NestedMessage repeated_nested_message = 48; |
||||
repeated ForeignMessage repeated_foreign_message = 49; |
||||
repeated protobuf_unittest_import.ImportMessage repeated_import_message = 50; |
||||
|
||||
repeated NestedEnum repeated_nested_enum = 51; |
||||
repeated ForeignEnum repeated_foreign_enum = 52; |
||||
repeated protobuf_unittest_import.ImportEnum repeated_import_enum = 53; |
||||
// Defined in unittest_import_public.proto |
||||
repeated protobuf_unittest_import.PublicImportMessage |
||||
repeated_public_import_message = 54; |
||||
|
||||
// For oneof test |
||||
oneof oneof_field { |
||||
uint32 oneof_uint32 = 111; |
||||
NestedMessage oneof_nested_message = 112; |
||||
string oneof_string = 113; |
||||
bytes oneof_bytes = 114; |
||||
} |
||||
} |
||||
|
||||
// This proto includes a recusively nested message. |
||||
message NestedTestAllTypes { |
||||
NestedTestAllTypes child = 1; |
||||
TestAllTypes payload = 2; |
||||
repeated NestedTestAllTypes repeated_child = 3; |
||||
} |
||||
|
||||
message TestDeprecatedFields { |
||||
int32 deprecated_int32 = 1 [deprecated=true]; |
||||
} |
||||
|
||||
// Define these after TestAllTypes to make sure the compiler can handle |
||||
// that. |
||||
message ForeignMessage { |
||||
int32 c = 1; |
||||
} |
||||
|
||||
enum ForeignEnum { |
||||
FOREIGN_UNSPECIFIED = 0; |
||||
FOREIGN_FOO = 4; |
||||
FOREIGN_BAR = 5; |
||||
FOREIGN_BAZ = 6; |
||||
} |
||||
|
||||
message TestReservedFields { |
||||
reserved 2, 15, 9 to 11; |
||||
reserved "bar", "baz"; |
||||
} |
||||
|
||||
|
||||
// Test that we can use NestedMessage from outside TestAllTypes. |
||||
message TestForeignNested { |
||||
TestAllTypes.NestedMessage foreign_nested = 1; |
||||
} |
||||
|
||||
// Test that really large tag numbers don't break anything. |
||||
message TestReallyLargeTagNumber { |
||||
// The largest possible tag number is 2^28 - 1, since the wire format uses |
||||
// three bits to communicate wire type. |
||||
int32 a = 1; |
||||
int32 bb = 268435455; |
||||
} |
||||
|
||||
message TestRecursiveMessage { |
||||
TestRecursiveMessage a = 1; |
||||
int32 i = 2; |
||||
} |
||||
|
||||
// Test that mutual recursion works. |
||||
message TestMutualRecursionA { |
||||
TestMutualRecursionB bb = 1; |
||||
} |
||||
|
||||
message TestMutualRecursionB { |
||||
TestMutualRecursionA a = 1; |
||||
int32 optional_int32 = 2; |
||||
} |
||||
|
||||
message TestEnumAllowAlias { |
||||
TestEnumWithDupValue value = 1; |
||||
} |
||||
|
||||
// Test an enum that has multiple values with the same number. |
||||
enum TestEnumWithDupValue { |
||||
TEST_ENUM_WITH_DUP_VALUE_UNSPECIFIED = 0; |
||||
option allow_alias = true; |
||||
|
||||
FOO1 = 1; |
||||
BAR1 = 2; |
||||
BAZ = 3; |
||||
FOO2 = 1; |
||||
BAR2 = 2; |
||||
} |
||||
|
||||
// Test an enum with large, unordered values. |
||||
enum TestSparseEnum { |
||||
TEST_SPARSE_ENUM_UNSPECIFIED = 0; |
||||
SPARSE_A = 123; |
||||
SPARSE_B = 62374; |
||||
SPARSE_C = 12589234; |
||||
SPARSE_D = -15; |
||||
SPARSE_E = -53452; |
||||
// In proto3, value 0 must be the first one specified |
||||
// SPARSE_F = 0; |
||||
SPARSE_G = 2; |
||||
} |
||||
|
||||
// Test message with CamelCase field names. This violates Protocol Buffer |
||||
// standard style. |
||||
message TestCamelCaseFieldNames { |
||||
int32 PrimitiveField = 1; |
||||
string StringField = 2; |
||||
ForeignEnum EnumField = 3; |
||||
ForeignMessage MessageField = 4; |
||||
|
||||
repeated int32 RepeatedPrimitiveField = 7; |
||||
repeated string RepeatedStringField = 8; |
||||
repeated ForeignEnum RepeatedEnumField = 9; |
||||
repeated ForeignMessage RepeatedMessageField = 10; |
||||
} |
||||
|
||||
|
||||
// We list fields out of order, to ensure that we're using field number and not |
||||
// field index to determine serialization order. |
||||
message TestFieldOrderings { |
||||
string my_string = 11; |
||||
int64 my_int = 1; |
||||
float my_float = 101; |
||||
message NestedMessage { |
||||
int64 oo = 2; |
||||
// The field name "b" fails to compile in proto1 because it conflicts with |
||||
// a local variable named "b" in one of the generated methods. Doh. |
||||
// This file needs to compile in proto1 to test backwards-compatibility. |
||||
int32 bb = 1; |
||||
} |
||||
|
||||
NestedMessage single_nested_message = 200; |
||||
} |
||||
|
||||
message SparseEnumMessage { |
||||
TestSparseEnum sparse_enum = 1; |
||||
} |
||||
|
||||
// Test String and Bytes: string is for valid UTF-8 strings |
||||
message OneString { |
||||
string data = 1; |
||||
} |
||||
|
||||
message MoreString { |
||||
repeated string data = 1; |
||||
} |
||||
|
||||
message OneBytes { |
||||
bytes data = 1; |
||||
} |
||||
|
||||
message MoreBytes { |
||||
bytes data = 1; |
||||
} |
||||
|
||||
// Test int32, uint32, int64, uint64, and bool are all compatible |
||||
message Int32Message { |
||||
int32 data = 1; |
||||
} |
||||
|
||||
message Uint32Message { |
||||
uint32 data = 1; |
||||
} |
||||
|
||||
message Int64Message { |
||||
int64 data = 1; |
||||
} |
||||
|
||||
message Uint64Message { |
||||
uint64 data = 1; |
||||
} |
||||
|
||||
message BoolMessage { |
||||
bool data = 1; |
||||
} |
||||
|
||||
// Test oneofs. |
||||
message TestOneof { |
||||
oneof foo { |
||||
int32 foo_int = 1; |
||||
string foo_string = 2; |
||||
TestAllTypes foo_message = 3; |
||||
} |
||||
} |
||||
|
||||
// Test messages for packed fields |
||||
|
||||
message TestPackedTypes { |
||||
repeated int32 packed_int32 = 90 [packed = true]; |
||||
repeated int64 packed_int64 = 91 [packed = true]; |
||||
repeated uint32 packed_uint32 = 92 [packed = true]; |
||||
repeated uint64 packed_uint64 = 93 [packed = true]; |
||||
repeated sint32 packed_sint32 = 94 [packed = true]; |
||||
repeated sint64 packed_sint64 = 95 [packed = true]; |
||||
repeated fixed32 packed_fixed32 = 96 [packed = true]; |
||||
repeated fixed64 packed_fixed64 = 97 [packed = true]; |
||||
repeated sfixed32 packed_sfixed32 = 98 [packed = true]; |
||||
repeated sfixed64 packed_sfixed64 = 99 [packed = true]; |
||||
repeated float packed_float = 100 [packed = true]; |
||||
repeated double packed_double = 101 [packed = true]; |
||||
repeated bool packed_bool = 102 [packed = true]; |
||||
repeated ForeignEnum packed_enum = 103 [packed = true]; |
||||
} |
||||
|
||||
// A message with the same fields as TestPackedTypes, but without packing. Used |
||||
// to test packed <-> unpacked wire compatibility. |
||||
message TestUnpackedTypes { |
||||
repeated int32 unpacked_int32 = 90 [packed = false]; |
||||
repeated int64 unpacked_int64 = 91 [packed = false]; |
||||
repeated uint32 unpacked_uint32 = 92 [packed = false]; |
||||
repeated uint64 unpacked_uint64 = 93 [packed = false]; |
||||
repeated sint32 unpacked_sint32 = 94 [packed = false]; |
||||
repeated sint64 unpacked_sint64 = 95 [packed = false]; |
||||
repeated fixed32 unpacked_fixed32 = 96 [packed = false]; |
||||
repeated fixed64 unpacked_fixed64 = 97 [packed = false]; |
||||
repeated sfixed32 unpacked_sfixed32 = 98 [packed = false]; |
||||
repeated sfixed64 unpacked_sfixed64 = 99 [packed = false]; |
||||
repeated float unpacked_float = 100 [packed = false]; |
||||
repeated double unpacked_double = 101 [packed = false]; |
||||
repeated bool unpacked_bool = 102 [packed = false]; |
||||
repeated ForeignEnum unpacked_enum = 103 [packed = false]; |
||||
} |
||||
|
||||
message TestRepeatedScalarDifferentTagSizes { |
||||
// Parsing repeated fixed size values used to fail. This message needs to be |
||||
// used in order to get a tag of the right size; all of the repeated fields |
||||
// in TestAllTypes didn't trigger the check. |
||||
repeated fixed32 repeated_fixed32 = 12; |
||||
// Check for a varint type, just for good measure. |
||||
repeated int32 repeated_int32 = 13; |
||||
|
||||
// These have two-byte tags. |
||||
repeated fixed64 repeated_fixed64 = 2046; |
||||
repeated int64 repeated_int64 = 2047; |
||||
|
||||
// Three byte tags. |
||||
repeated float repeated_float = 262142; |
||||
repeated uint64 repeated_uint64 = 262143; |
||||
} |
||||
|
||||
message TestCommentInjectionMessage { |
||||
// */ <- This should not close the generated doc comment |
||||
string a = 1; |
||||
} |
||||
|
||||
|
||||
// Test that RPC services work. |
||||
message FooRequest {} |
||||
message FooResponse {} |
||||
|
||||
message FooClientMessage {} |
||||
message FooServerMessage{} |
||||
|
||||
service TestService { |
||||
rpc Foo(FooRequest) returns (FooResponse); |
||||
rpc Bar(BarRequest) returns (BarResponse); |
||||
} |
||||
|
||||
|
||||
message BarRequest {} |
||||
message BarResponse {} |
||||
|
@ -0,0 +1,325 @@ |
||||
package com.google.protobuf; |
||||
|
||||
import com.google.protobuf.Utf8.Processor; |
||||
import com.google.protobuf.Utf8.SafeProcessor; |
||||
import com.google.protobuf.Utf8.UnsafeProcessor; |
||||
import java.nio.ByteBuffer; |
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
import java.util.logging.Logger; |
||||
import junit.framework.TestCase; |
||||
|
||||
public class DecodeUtf8Test extends TestCase { |
||||
private static Logger logger = Logger.getLogger(DecodeUtf8Test.class.getName()); |
||||
|
||||
private static final Processor SAFE_PROCESSOR = new SafeProcessor(); |
||||
private static final Processor UNSAFE_PROCESSOR = new UnsafeProcessor(); |
||||
|
||||
public void testRoundTripAllValidChars() throws Exception { |
||||
for (int i = Character.MIN_CODE_POINT; i < Character.MAX_CODE_POINT; i++) { |
||||
if (i < Character.MIN_SURROGATE || i > Character.MAX_SURROGATE) { |
||||
String str = new String(Character.toChars(i)); |
||||
assertRoundTrips(str); |
||||
} |
||||
} |
||||
} |
||||
|
||||
// Test all 1, 2, 3 invalid byte combinations. Valid ones would have been covered above.
|
||||
|
||||
public void testOneByte() throws Exception { |
||||
int valid = 0; |
||||
for (int i = Byte.MIN_VALUE; i <= Byte.MAX_VALUE; i++) { |
||||
ByteString bs = ByteString.copyFrom(new byte[] { (byte) i }); |
||||
if (!bs.isValidUtf8()) { |
||||
assertInvalid(bs.toByteArray()); |
||||
} else { |
||||
valid++; |
||||
} |
||||
} |
||||
assertEquals(IsValidUtf8TestUtil.EXPECTED_ONE_BYTE_ROUNDTRIPPABLE_COUNT, valid); |
||||
} |
||||
|
||||
public void testTwoBytes() throws Exception { |
||||
int valid = 0; |
||||
for (int i = Byte.MIN_VALUE; i <= Byte.MAX_VALUE; i++) { |
||||
for (int j = Byte.MIN_VALUE; j <= Byte.MAX_VALUE; j++) { |
||||
ByteString bs = ByteString.copyFrom(new byte[]{(byte) i, (byte) j}); |
||||
if (!bs.isValidUtf8()) { |
||||
assertInvalid(bs.toByteArray()); |
||||
} else { |
||||
valid++; |
||||
} |
||||
} |
||||
} |
||||
assertEquals(IsValidUtf8TestUtil.EXPECTED_TWO_BYTE_ROUNDTRIPPABLE_COUNT, valid); |
||||
} |
||||
|
||||
public void testThreeBytes() throws Exception { |
||||
// Travis' OOM killer doesn't like this test
|
||||
if (System.getenv("TRAVIS") == null) { |
||||
int count = 0; |
||||
int valid = 0; |
||||
for (int i = Byte.MIN_VALUE; i <= Byte.MAX_VALUE; i++) { |
||||
for (int j = Byte.MIN_VALUE; j <= Byte.MAX_VALUE; j++) { |
||||
for (int k = Byte.MIN_VALUE; k <= Byte.MAX_VALUE; k++) { |
||||
byte[] bytes = new byte[]{(byte) i, (byte) j, (byte) k}; |
||||
ByteString bs = ByteString.copyFrom(bytes); |
||||
if (!bs.isValidUtf8()) { |
||||
assertInvalid(bytes); |
||||
} else { |
||||
valid++; |
||||
} |
||||
count++; |
||||
if (count % 1000000L == 0) { |
||||
logger.info("Processed " + (count / 1000000L) + " million characters"); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
assertEquals(IsValidUtf8TestUtil.EXPECTED_THREE_BYTE_ROUNDTRIPPABLE_COUNT, valid); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Tests that round tripping of a sample of four byte permutations work. |
||||
*/ |
||||
public void testInvalid_4BytesSamples() throws Exception { |
||||
// Bad trailing bytes
|
||||
assertInvalid(0xF0, 0xA4, 0xAD, 0x7F); |
||||
assertInvalid(0xF0, 0xA4, 0xAD, 0xC0); |
||||
|
||||
// Special cases for byte2
|
||||
assertInvalid(0xF0, 0x8F, 0xAD, 0xA2); |
||||
assertInvalid(0xF4, 0x90, 0xAD, 0xA2); |
||||
} |
||||
|
||||
public void testRealStrings() throws Exception { |
||||
// English
|
||||
assertRoundTrips("The quick brown fox jumps over the lazy dog"); |
||||
// German
|
||||
assertRoundTrips("Quizdeltagerne spiste jordb\u00e6r med fl\u00f8de, mens cirkusklovnen"); |
||||
// Japanese
|
||||
assertRoundTrips( |
||||
"\u3044\u308d\u306f\u306b\u307b\u3078\u3068\u3061\u308a\u306c\u308b\u3092"); |
||||
// Hebrew
|
||||
assertRoundTrips( |
||||
"\u05d3\u05d2 \u05e1\u05e7\u05e8\u05df \u05e9\u05d8 \u05d1\u05d9\u05dd " |
||||
+ "\u05de\u05d0\u05d5\u05db\u05d6\u05d1 \u05d5\u05dc\u05e4\u05ea\u05e2" |
||||
+ " \u05de\u05e6\u05d0 \u05dc\u05d5 \u05d7\u05d1\u05e8\u05d4 " |
||||
+ "\u05d0\u05d9\u05da \u05d4\u05e7\u05dc\u05d9\u05d8\u05d4"); |
||||
// Thai
|
||||
assertRoundTrips( |
||||
" \u0e08\u0e07\u0e1d\u0e48\u0e32\u0e1f\u0e31\u0e19\u0e1e\u0e31\u0e12" |
||||
+ "\u0e19\u0e32\u0e27\u0e34\u0e0a\u0e32\u0e01\u0e32\u0e23"); |
||||
// Chinese
|
||||
assertRoundTrips( |
||||
"\u8fd4\u56de\u94fe\u4e2d\u7684\u4e0b\u4e00\u4e2a\u4ee3\u7406\u9879\u9009\u62e9\u5668"); |
||||
// Chinese with 4-byte chars
|
||||
assertRoundTrips("\uD841\uDF0E\uD841\uDF31\uD841\uDF79\uD843\uDC53\uD843\uDC78" |
||||
+ "\uD843\uDC96\uD843\uDCCF\uD843\uDCD5\uD843\uDD15\uD843\uDD7C\uD843\uDD7F" |
||||
+ "\uD843\uDE0E\uD843\uDE0F\uD843\uDE77\uD843\uDE9D\uD843\uDEA2"); |
||||
// Mixed
|
||||
assertRoundTrips( |
||||
"The quick brown \u3044\u308d\u306f\u306b\u307b\u3078\u8fd4\u56de\u94fe" |
||||
+ "\u4e2d\u7684\u4e0b\u4e00"); |
||||
} |
||||
|
||||
public void testOverlong() throws Exception { |
||||
assertInvalid(0xc0, 0xaf); |
||||
assertInvalid(0xe0, 0x80, 0xaf); |
||||
assertInvalid(0xf0, 0x80, 0x80, 0xaf); |
||||
|
||||
// Max overlong
|
||||
assertInvalid(0xc1, 0xbf); |
||||
assertInvalid(0xe0, 0x9f, 0xbf); |
||||
assertInvalid(0xf0 ,0x8f, 0xbf, 0xbf); |
||||
|
||||
// null overlong
|
||||
assertInvalid(0xc0, 0x80); |
||||
assertInvalid(0xe0, 0x80, 0x80); |
||||
assertInvalid(0xf0, 0x80, 0x80, 0x80); |
||||
} |
||||
|
||||
public void testIllegalCodepoints() throws Exception { |
||||
// Single surrogate
|
||||
assertInvalid(0xed, 0xa0, 0x80); |
||||
assertInvalid(0xed, 0xad, 0xbf); |
||||
assertInvalid(0xed, 0xae, 0x80); |
||||
assertInvalid(0xed, 0xaf, 0xbf); |
||||
assertInvalid(0xed, 0xb0, 0x80); |
||||
assertInvalid(0xed, 0xbe, 0x80); |
||||
assertInvalid(0xed, 0xbf, 0xbf); |
||||
|
||||
// Paired surrogates
|
||||
assertInvalid(0xed, 0xa0, 0x80, 0xed, 0xb0, 0x80); |
||||
assertInvalid(0xed, 0xa0, 0x80, 0xed, 0xbf, 0xbf); |
||||
assertInvalid(0xed, 0xad, 0xbf, 0xed, 0xb0, 0x80); |
||||
assertInvalid(0xed, 0xad, 0xbf, 0xed, 0xbf, 0xbf); |
||||
assertInvalid(0xed, 0xae, 0x80, 0xed, 0xb0, 0x80); |
||||
assertInvalid(0xed, 0xae, 0x80, 0xed, 0xbf, 0xbf); |
||||
assertInvalid(0xed, 0xaf, 0xbf, 0xed, 0xb0, 0x80); |
||||
assertInvalid(0xed, 0xaf, 0xbf, 0xed, 0xbf, 0xbf); |
||||
} |
||||
|
||||
public void testBufferSlice() throws Exception { |
||||
String str = "The quick brown fox jumps over the lazy dog"; |
||||
assertRoundTrips(str, 10, 4); |
||||
assertRoundTrips(str, str.length(), 0); |
||||
} |
||||
|
||||
public void testInvalidBufferSlice() throws Exception { |
||||
byte[] bytes = "The quick brown fox jumps over the lazy dog".getBytes(Internal.UTF_8); |
||||
assertInvalidSlice(bytes, bytes.length - 3, 4); |
||||
assertInvalidSlice(bytes, bytes.length, 1); |
||||
assertInvalidSlice(bytes, bytes.length + 1, 0); |
||||
assertInvalidSlice(bytes, 0, bytes.length + 1); |
||||
} |
||||
|
||||
private void assertInvalid(int... bytesAsInt) throws Exception { |
||||
byte[] bytes = new byte[bytesAsInt.length]; |
||||
for (int i = 0; i < bytesAsInt.length; i++) { |
||||
bytes[i] = (byte) bytesAsInt[i]; |
||||
} |
||||
assertInvalid(bytes); |
||||
} |
||||
|
||||
private void assertInvalid(byte[] bytes) throws Exception { |
||||
try { |
||||
UNSAFE_PROCESSOR.decodeUtf8(bytes, 0, bytes.length); |
||||
fail(); |
||||
} catch (InvalidProtocolBufferException e) { |
||||
// Expected.
|
||||
} |
||||
try { |
||||
SAFE_PROCESSOR.decodeUtf8(bytes, 0, bytes.length); |
||||
fail(); |
||||
} catch (InvalidProtocolBufferException e) { |
||||
// Expected.
|
||||
} |
||||
|
||||
ByteBuffer direct = ByteBuffer.allocateDirect(bytes.length); |
||||
direct.put(bytes); |
||||
direct.flip(); |
||||
try { |
||||
UNSAFE_PROCESSOR.decodeUtf8(direct, 0, bytes.length); |
||||
fail(); |
||||
} catch (InvalidProtocolBufferException e) { |
||||
// Expected.
|
||||
} |
||||
try { |
||||
SAFE_PROCESSOR.decodeUtf8(direct, 0, bytes.length); |
||||
fail(); |
||||
} catch (InvalidProtocolBufferException e) { |
||||
// Expected.
|
||||
} |
||||
|
||||
ByteBuffer heap = ByteBuffer.allocate(bytes.length); |
||||
heap.put(bytes); |
||||
heap.flip(); |
||||
try { |
||||
UNSAFE_PROCESSOR.decodeUtf8(heap, 0, bytes.length); |
||||
fail(); |
||||
} catch (InvalidProtocolBufferException e) { |
||||
// Expected.
|
||||
} |
||||
try { |
||||
SAFE_PROCESSOR.decodeUtf8(heap, 0, bytes.length); |
||||
fail(); |
||||
} catch (InvalidProtocolBufferException e) { |
||||
// Expected.
|
||||
} |
||||
} |
||||
|
||||
private void assertInvalidSlice(byte[] bytes, int index, int size) throws Exception { |
||||
try { |
||||
UNSAFE_PROCESSOR.decodeUtf8(bytes, index, size); |
||||
fail(); |
||||
} catch (ArrayIndexOutOfBoundsException e) { |
||||
// Expected.
|
||||
} |
||||
try { |
||||
SAFE_PROCESSOR.decodeUtf8(bytes, index, size); |
||||
fail(); |
||||
} catch (ArrayIndexOutOfBoundsException e) { |
||||
// Expected.
|
||||
} |
||||
|
||||
ByteBuffer direct = ByteBuffer.allocateDirect(bytes.length); |
||||
direct.put(bytes); |
||||
direct.flip(); |
||||
try { |
||||
UNSAFE_PROCESSOR.decodeUtf8(direct, index, size); |
||||
fail(); |
||||
} catch (ArrayIndexOutOfBoundsException e) { |
||||
// Expected.
|
||||
} |
||||
try { |
||||
SAFE_PROCESSOR.decodeUtf8(direct, index, size); |
||||
fail(); |
||||
} catch (ArrayIndexOutOfBoundsException e) { |
||||
// Expected.
|
||||
} |
||||
|
||||
ByteBuffer heap = ByteBuffer.allocate(bytes.length); |
||||
heap.put(bytes); |
||||
heap.flip(); |
||||
try { |
||||
UNSAFE_PROCESSOR.decodeUtf8(heap, index, size); |
||||
fail(); |
||||
} catch (ArrayIndexOutOfBoundsException e) { |
||||
// Expected.
|
||||
} |
||||
try { |
||||
SAFE_PROCESSOR.decodeUtf8(heap, index, size); |
||||
fail(); |
||||
} catch (ArrayIndexOutOfBoundsException e) { |
||||
// Expected.
|
||||
} |
||||
} |
||||
|
||||
private void assertRoundTrips(String str) throws Exception { |
||||
assertRoundTrips(str, 0, -1); |
||||
} |
||||
|
||||
private void assertRoundTrips(String str, int index, int size) throws Exception { |
||||
byte[] bytes = str.getBytes(Internal.UTF_8); |
||||
if (size == -1) { |
||||
size = bytes.length; |
||||
} |
||||
assertDecode(new String(bytes, index, size, Internal.UTF_8), |
||||
UNSAFE_PROCESSOR.decodeUtf8(bytes, index, size)); |
||||
assertDecode(new String(bytes, index, size, Internal.UTF_8), |
||||
SAFE_PROCESSOR.decodeUtf8(bytes, index, size)); |
||||
|
||||
ByteBuffer direct = ByteBuffer.allocateDirect(bytes.length); |
||||
direct.put(bytes); |
||||
direct.flip(); |
||||
assertDecode(new String(bytes, index, size, Internal.UTF_8), |
||||
UNSAFE_PROCESSOR.decodeUtf8(direct, index, size)); |
||||
assertDecode(new String(bytes, index, size, Internal.UTF_8), |
||||
SAFE_PROCESSOR.decodeUtf8(direct, index, size)); |
||||
|
||||
ByteBuffer heap = ByteBuffer.allocate(bytes.length); |
||||
heap.put(bytes); |
||||
heap.flip(); |
||||
assertDecode(new String(bytes, index, size, Internal.UTF_8), |
||||
UNSAFE_PROCESSOR.decodeUtf8(heap, index, size)); |
||||
assertDecode(new String(bytes, index, size, Internal.UTF_8), |
||||
SAFE_PROCESSOR.decodeUtf8(heap, index, size)); |
||||
} |
||||
|
||||
private void assertDecode(String expected, String actual) { |
||||
if (!expected.equals(actual)) { |
||||
fail("Failure: Expected (" + codepoints(expected) + ") Actual (" + codepoints(actual) + ")"); |
||||
} |
||||
} |
||||
|
||||
private List<String> codepoints(String str) { |
||||
List<String> codepoints = new ArrayList<String>(); |
||||
for (int i = 0; i < str.length(); i++) { |
||||
codepoints.add(Long.toHexString(str.charAt(i))); |
||||
} |
||||
return codepoints; |
||||
} |
||||
|
||||
} |
After Width: | Height: | Size: 201 KiB |
After Width: | Height: | Size: 7.1 KiB |
After Width: | Height: | Size: 10 KiB |
After Width: | Height: | Size: 11 KiB |
After Width: | Height: | Size: 13 KiB |
After Width: | Height: | Size: 752 B |
After Width: | Height: | Size: 1.2 KiB |
After Width: | Height: | Size: 1.7 KiB |
After Width: | Height: | Size: 2.7 KiB |
After Width: | Height: | Size: 2.8 KiB |
After Width: | Height: | Size: 3.8 KiB |
After Width: | Height: | Size: 4.0 KiB |
After Width: | Height: | Size: 4.6 KiB |
Before Width: | Height: | Size: 8.4 KiB |
Before Width: | Height: | Size: 17 KiB |
Before Width: | Height: | Size: 8.8 KiB |
Before Width: | Height: | Size: 18 KiB |
Before Width: | Height: | Size: 6.9 KiB |
Before Width: | Height: | Size: 13 KiB |
Before Width: | Height: | Size: 11 KiB |
Before Width: | Height: | Size: 21 KiB |