1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
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
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.
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.
31 package com.google.protobuf;
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 junit.framework.TestCase;
40 * Test that protos generated with file option java_string_check_utf8 do in
41 * fact perform appropriate UTF-8 checks.
43 * @author jbaum@google.com (Jacob Butcher)
45 public class CheckUtf8Test extends TestCase {
47 private static final String UTF8_BYTE_STRING_TEXT = "some text";
48 private static final ByteString UTF8_BYTE_STRING =
49 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.
53 public void testBuildRequiredStringWithGoodUtf8() throws Exception {
54 assertEquals(UTF8_BYTE_STRING_TEXT,
55 StringWrapper.newBuilder().setReqBytes(UTF8_BYTE_STRING).getReq());
58 public void testParseRequiredStringWithGoodUtf8() throws Exception {
59 ByteString serialized =
60 BytesWrapper.newBuilder().setReq(UTF8_BYTE_STRING).build().toByteString();
61 assertEquals(UTF8_BYTE_STRING_TEXT,
62 StringWrapper.PARSER.parseFrom(serialized).getReq());
65 public void testBuildRequiredStringWithBadUtf8() throws Exception {
67 StringWrapper.newBuilder().setReqBytes(NON_UTF8_BYTE_STRING);
68 fail("Expected IllegalArgumentException for non UTF-8 byte string.");
69 } catch (IllegalArgumentException exception) {
70 assertEquals("Byte string is not UTF-8.", exception.getMessage());
74 public void testBuildOptionalStringWithBadUtf8() throws Exception {
76 StringWrapper.newBuilder().setOptBytes(NON_UTF8_BYTE_STRING);
77 fail("Expected IllegalArgumentException for non UTF-8 byte string.");
78 } catch (IllegalArgumentException exception) {
79 assertEquals("Byte string is not UTF-8.", exception.getMessage());
83 public void testBuildRepeatedStringWithBadUtf8() throws Exception {
85 StringWrapper.newBuilder().addRepBytes(NON_UTF8_BYTE_STRING);
86 fail("Expected IllegalArgumentException for non UTF-8 byte string.");
87 } catch (IllegalArgumentException exception) {
88 assertEquals("Byte string is not UTF-8.", exception.getMessage());
92 public void testParseRequiredStringWithBadUtf8() throws Exception {
93 ByteString serialized =
94 BytesWrapper.newBuilder().setReq(NON_UTF8_BYTE_STRING).build().toByteString();
96 StringWrapper.PARSER.parseFrom(serialized);
97 fail("Expected InvalidProtocolBufferException for non UTF-8 byte string.");
98 } catch (InvalidProtocolBufferException exception) {
99 assertEquals("Protocol message had invalid UTF-8.", exception.getMessage());
103 public void testBuildRequiredStringWithBadUtf8Size() throws Exception {
105 StringWrapperSize.newBuilder().setReqBytes(NON_UTF8_BYTE_STRING);
106 fail("Expected IllegalArgumentException for non UTF-8 byte string.");
107 } catch (IllegalArgumentException exception) {
108 assertEquals("Byte string is not UTF-8.", exception.getMessage());
112 public void testBuildOptionalStringWithBadUtf8Size() throws Exception {
114 StringWrapperSize.newBuilder().setOptBytes(NON_UTF8_BYTE_STRING);
115 fail("Expected IllegalArgumentException for non UTF-8 byte string.");
116 } catch (IllegalArgumentException exception) {
117 assertEquals("Byte string is not UTF-8.", exception.getMessage());
121 public void testBuildRepeatedStringWithBadUtf8Size() throws Exception {
123 StringWrapperSize.newBuilder().addRepBytes(NON_UTF8_BYTE_STRING);
124 fail("Expected IllegalArgumentException for non UTF-8 byte string.");
125 } catch (IllegalArgumentException exception) {
126 assertEquals("Byte string is not UTF-8.", exception.getMessage());
130 public void testParseRequiredStringWithBadUtf8Size() throws Exception {
131 ByteString serialized =
132 BytesWrapperSize.newBuilder().setReq(NON_UTF8_BYTE_STRING).build().toByteString();
134 StringWrapperSize.PARSER.parseFrom(serialized);
135 fail("Expected InvalidProtocolBufferException for non UTF-8 byte string.");
136 } catch (InvalidProtocolBufferException exception) {
137 assertEquals("Protocol message had invalid UTF-8.", exception.getMessage());