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 40 /** 41 * Test that protos generated with file option java_string_check_utf8 do in fact perform appropriate 42 * UTF-8 checks. 43 * 44 * @author jbaum@google.com (Jacob Butcher) 45 */ 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 testBuildRequiredStringWithGoodUtf8()53 public void testBuildRequiredStringWithGoodUtf8() throws Exception { 54 assertEquals( 55 UTF8_BYTE_STRING_TEXT, StringWrapper.newBuilder().setReqBytes(UTF8_BYTE_STRING).getReq()); 56 } 57 testParseRequiredStringWithGoodUtf8()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 testBuildRequiredStringWithBadUtf8()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 testBuildOptionalStringWithBadUtf8()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 testBuildRepeatedStringWithBadUtf8()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 testParseRequiredStringWithBadUtf8()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 testBuildRequiredStringWithBadUtf8Size()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 testBuildOptionalStringWithBadUtf8Size()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 testBuildRepeatedStringWithBadUtf8Size()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 testParseRequiredStringWithBadUtf8Size()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 assertParseBadUtf8(MessageLite defaultInstance, byte[] data)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 } 158