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 38 import junit.framework.TestCase; 39 40 /** 41 * Test that protos generated with file option java_string_check_utf8 do in 42 * fact perform appropriate 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 = 50 ByteString.copyFromUtf8(UTF8_BYTE_STRING_TEXT); 51 private static final ByteString NON_UTF8_BYTE_STRING = 52 ByteString.copyFrom(new byte[]{(byte) 0x80}); // A lone continuation byte. 53 testBuildRequiredStringWithGoodUtf8()54 public void testBuildRequiredStringWithGoodUtf8() throws Exception { 55 assertEquals(UTF8_BYTE_STRING_TEXT, 56 StringWrapper.newBuilder().setReqBytes(UTF8_BYTE_STRING).getReq()); 57 } 58 testParseRequiredStringWithGoodUtf8()59 public void testParseRequiredStringWithGoodUtf8() throws Exception { 60 ByteString serialized = 61 BytesWrapper.newBuilder().setReq(UTF8_BYTE_STRING).build().toByteString(); 62 assertEquals(UTF8_BYTE_STRING_TEXT, StringWrapper.parser().parseFrom(serialized).getReq()); 63 } 64 testBuildRequiredStringWithBadUtf8()65 public void testBuildRequiredStringWithBadUtf8() throws Exception { 66 try { 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()); 71 } 72 } 73 testBuildOptionalStringWithBadUtf8()74 public void testBuildOptionalStringWithBadUtf8() throws Exception { 75 try { 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()); 80 } 81 } 82 testBuildRepeatedStringWithBadUtf8()83 public void testBuildRepeatedStringWithBadUtf8() throws Exception { 84 try { 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()); 89 } 90 } 91 testParseRequiredStringWithBadUtf8()92 public void testParseRequiredStringWithBadUtf8() throws Exception { 93 ByteString serialized = 94 BytesWrapper.newBuilder().setReq(NON_UTF8_BYTE_STRING).build().toByteString(); 95 try { 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()); 100 } 101 } 102 testBuildRequiredStringWithBadUtf8Size()103 public void testBuildRequiredStringWithBadUtf8Size() throws Exception { 104 try { 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()); 109 } 110 } 111 testBuildOptionalStringWithBadUtf8Size()112 public void testBuildOptionalStringWithBadUtf8Size() throws Exception { 113 try { 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()); 118 } 119 } 120 testBuildRepeatedStringWithBadUtf8Size()121 public void testBuildRepeatedStringWithBadUtf8Size() throws Exception { 122 try { 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()); 127 } 128 } 129 testParseRequiredStringWithBadUtf8Size()130 public void testParseRequiredStringWithBadUtf8Size() throws Exception { 131 ByteString serialized = 132 BytesWrapperSize.newBuilder().setReq(NON_UTF8_BYTE_STRING).build().toByteString(); 133 try { 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()); 138 } 139 } 140 141 } 142