CheckUtf8Test.java
Go to the documentation of this file.
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 // * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 package com.google.protobuf;
32 
33 import proto2_test_check_utf8.TestCheckUtf8.BytesWrapper;
34 import proto2_test_check_utf8.TestCheckUtf8.StringWrapper;
35 import proto2_test_check_utf8_size.TestCheckUtf8Size.BytesWrapperSize;
36 import proto2_test_check_utf8_size.TestCheckUtf8Size.StringWrapperSize;
37 import java.io.ByteArrayInputStream;
38 import junit.framework.TestCase;
39 
46 public class CheckUtf8Test extends TestCase {
47 
48  private static final String UTF8_BYTE_STRING_TEXT = "some text";
49  private static final ByteString UTF8_BYTE_STRING = ByteString.copyFromUtf8(UTF8_BYTE_STRING_TEXT);
50  private static final ByteString NON_UTF8_BYTE_STRING =
51  ByteString.copyFrom(new byte[] {(byte) 0x80}); // A lone continuation byte.
52 
53  public void testBuildRequiredStringWithGoodUtf8() throws Exception {
54  assertEquals(
55  UTF8_BYTE_STRING_TEXT, StringWrapper.newBuilder().setReqBytes(UTF8_BYTE_STRING).getReq());
56  }
57 
58  public void testParseRequiredStringWithGoodUtf8() throws Exception {
59  ByteString serialized =
60  BytesWrapper.newBuilder().setReq(UTF8_BYTE_STRING).build().toByteString();
61  assertEquals(UTF8_BYTE_STRING_TEXT, StringWrapper.parser().parseFrom(serialized).getReq());
62  }
63 
64  public void testBuildRequiredStringWithBadUtf8() throws Exception {
65  try {
66  StringWrapper.newBuilder().setReqBytes(NON_UTF8_BYTE_STRING);
67  fail("Expected IllegalArgumentException for non UTF-8 byte string.");
68  } catch (IllegalArgumentException exception) {
69  assertEquals("Byte string is not UTF-8.", exception.getMessage());
70  }
71  }
72 
73  public void testBuildOptionalStringWithBadUtf8() throws Exception {
74  try {
75  StringWrapper.newBuilder().setOptBytes(NON_UTF8_BYTE_STRING);
76  fail("Expected IllegalArgumentException for non UTF-8 byte string.");
77  } catch (IllegalArgumentException exception) {
78  assertEquals("Byte string is not UTF-8.", exception.getMessage());
79  }
80  }
81 
82  public void testBuildRepeatedStringWithBadUtf8() throws Exception {
83  try {
84  StringWrapper.newBuilder().addRepBytes(NON_UTF8_BYTE_STRING);
85  fail("Expected IllegalArgumentException for non UTF-8 byte string.");
86  } catch (IllegalArgumentException exception) {
87  assertEquals("Byte string is not UTF-8.", exception.getMessage());
88  }
89  }
90 
91  public void testParseRequiredStringWithBadUtf8() throws Exception {
92  byte[] serialized =
93  BytesWrapper.newBuilder().setReq(NON_UTF8_BYTE_STRING).build().toByteArray();
94  assertParseBadUtf8(StringWrapper.getDefaultInstance(), serialized);
95  }
96 
97  public void testBuildRequiredStringWithBadUtf8Size() throws Exception {
98  try {
99  StringWrapperSize.newBuilder().setReqBytes(NON_UTF8_BYTE_STRING);
100  fail("Expected IllegalArgumentException for non UTF-8 byte string.");
101  } catch (IllegalArgumentException exception) {
102  assertEquals("Byte string is not UTF-8.", exception.getMessage());
103  }
104  }
105 
106  public void testBuildOptionalStringWithBadUtf8Size() throws Exception {
107  try {
108  StringWrapperSize.newBuilder().setOptBytes(NON_UTF8_BYTE_STRING);
109  fail("Expected IllegalArgumentException for non UTF-8 byte string.");
110  } catch (IllegalArgumentException exception) {
111  assertEquals("Byte string is not UTF-8.", exception.getMessage());
112  }
113  }
114 
115  public void testBuildRepeatedStringWithBadUtf8Size() throws Exception {
116  try {
117  StringWrapperSize.newBuilder().addRepBytes(NON_UTF8_BYTE_STRING);
118  fail("Expected IllegalArgumentException for non UTF-8 byte string.");
119  } catch (IllegalArgumentException exception) {
120  assertEquals("Byte string is not UTF-8.", exception.getMessage());
121  }
122  }
123 
124  public void testParseRequiredStringWithBadUtf8Size() throws Exception {
125  byte[] serialized =
126  BytesWrapperSize.newBuilder().setReq(NON_UTF8_BYTE_STRING).build().toByteArray();
127  assertParseBadUtf8(StringWrapperSize.getDefaultInstance(), serialized);
128  }
129 
130  private void assertParseBadUtf8(MessageLite defaultInstance, byte[] data) throws Exception {
131  // Check combinations of (parser vs. builder) x (byte[] vs. InputStream)
132  try {
133  defaultInstance.getParserForType().parseFrom(data);
134  fail("Expected InvalidProtocolBufferException for non UTF-8 byte string.");
135  } catch (InvalidProtocolBufferException exception) {
136  assertEquals("Protocol message had invalid UTF-8.", exception.getMessage());
137  }
138  try {
139  defaultInstance.newBuilderForType().mergeFrom(data);
140  fail("Expected InvalidProtocolBufferException for non UTF-8 byte string.");
141  } catch (InvalidProtocolBufferException exception) {
142  assertEquals("Protocol message had invalid UTF-8.", exception.getMessage());
143  }
144  try {
145  defaultInstance.getParserForType().parseFrom(new ByteArrayInputStream(data));
146  fail("Expected InvalidProtocolBufferException for non UTF-8 byte string.");
147  } catch (InvalidProtocolBufferException exception) {
148  assertEquals("Protocol message had invalid UTF-8.", exception.getMessage());
149  }
150  try {
151  defaultInstance.newBuilderForType().mergeFrom(new ByteArrayInputStream(data));
152  fail("Expected InvalidProtocolBufferException for non UTF-8 byte string.");
153  } catch (InvalidProtocolBufferException exception) {
154  assertEquals("Protocol message had invalid UTF-8.", exception.getMessage());
155  }
156  }
157 }
com.google.protobuf.CheckUtf8Test.assertParseBadUtf8
void assertParseBadUtf8(MessageLite defaultInstance, byte[] data)
Definition: CheckUtf8Test.java:130
com.google.protobuf.CheckUtf8Test.UTF8_BYTE_STRING
static final ByteString UTF8_BYTE_STRING
Definition: CheckUtf8Test.java:49
com.google.protobuf.CheckUtf8Test.testBuildRequiredStringWithBadUtf8Size
void testBuildRequiredStringWithBadUtf8Size()
Definition: CheckUtf8Test.java:97
com.google.protobuf.CheckUtf8Test.testParseRequiredStringWithBadUtf8Size
void testParseRequiredStringWithBadUtf8Size()
Definition: CheckUtf8Test.java:124
com.google.protobuf.CheckUtf8Test.testBuildRequiredStringWithGoodUtf8
void testBuildRequiredStringWithGoodUtf8()
Definition: CheckUtf8Test.java:53
com.google.protobuf.CheckUtf8Test.testParseRequiredStringWithGoodUtf8
void testParseRequiredStringWithGoodUtf8()
Definition: CheckUtf8Test.java:58
com.google.protobuf.CheckUtf8Test.NON_UTF8_BYTE_STRING
static final ByteString NON_UTF8_BYTE_STRING
Definition: CheckUtf8Test.java:50
byte
SETUP_TEARDOWN_TESTCONTEXT typedef uint8_t byte
Definition: test_stream.cpp:12
com.google.protobuf.CheckUtf8Test
Definition: CheckUtf8Test.java:46
com.google.protobuf.CheckUtf8Test.testBuildOptionalStringWithBadUtf8Size
void testBuildOptionalStringWithBadUtf8Size()
Definition: CheckUtf8Test.java:106
java
com.google.protobuf.CheckUtf8Test.testBuildOptionalStringWithBadUtf8
void testBuildOptionalStringWithBadUtf8()
Definition: CheckUtf8Test.java:73
com.google.protobuf.CheckUtf8Test.testBuildRequiredStringWithBadUtf8
void testBuildRequiredStringWithBadUtf8()
Definition: CheckUtf8Test.java:64
data
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: glcorearb.h:2879
com.google.protobuf.InvalidProtocolBufferException
Definition: InvalidProtocolBufferException.java:41
gmock_test_utils.TestCase
TestCase
Definition: gmock_test_utils.py:97
com.google.protobuf.CheckUtf8Test.testParseRequiredStringWithBadUtf8
void testParseRequiredStringWithBadUtf8()
Definition: CheckUtf8Test.java:91
com.google.protobuf.CheckUtf8Test.testBuildRepeatedStringWithBadUtf8
void testBuildRepeatedStringWithBadUtf8()
Definition: CheckUtf8Test.java:82
com.google.protobuf.CheckUtf8Test.UTF8_BYTE_STRING_TEXT
static final String UTF8_BYTE_STRING_TEXT
Definition: CheckUtf8Test.java:48
com.google.protobuf.MessageLite
Definition: MessageLite.java:65
com.google.protobuf.CheckUtf8Test.testBuildRepeatedStringWithBadUtf8Size
void testBuildRepeatedStringWithBadUtf8Size()
Definition: CheckUtf8Test.java:115
com.google.protobuf.ByteString
Definition: ByteString.java:67


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:48