Path: blob/master/test/jdk/java/io/DataInputStream/ReadUTF.java
41149 views
/*1* Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 480600725* @summary Checks for vague exceptions from writeUTF/readUTF26* @key randomness27*/2829import java.io.*;30import java.util.*;3132public class ReadUTF {3334private static Random generator = new Random();3536private static final int TEST_ITERATIONS = 1000;3738private static final int A_NUMBER_NEAR_65535 = 60000;3940private static final int MAX_CORRUPTIONS_PER_CYCLE = 3;4142public static final void main(String[] args) throws Exception {43for (int i=0; i<TEST_ITERATIONS; i++) {44try {45writeAndReadAString();46} catch (UTFDataFormatException utfdfe) {47if (utfdfe.getMessage() == null)48throw new RuntimeException("vague exception thrown");49} catch (EOFException eofe) {50// These are rare and beyond the scope of the test51}52}53}5455private static void writeAndReadAString() throws Exception {56// Write out a string whose UTF-8 encoding is quite possibly57// longer than 65535 bytes58int length = generator.nextInt(A_NUMBER_NEAR_65535) + 1;59ByteArrayOutputStream baos = new ByteArrayOutputStream();60StringBuffer testBuffer = new StringBuffer();61for (int i=0; i<length; i++)62testBuffer.append((char)generator.nextInt());63String testString = testBuffer.toString();64DataOutputStream dos = new DataOutputStream(baos);65dos.writeUTF(testString);6667// Corrupt the data to produce malformed characters68byte[] testBytes = baos.toByteArray();69int dataLength = testBytes.length;70int corruptions = generator.nextInt(MAX_CORRUPTIONS_PER_CYCLE);71for (int i=0; i<corruptions; i++) {72int index = generator.nextInt(dataLength);73testBytes[index] = (byte)generator.nextInt();74}7576// Pay special attention to mangling the end to produce77// partial characters at end78testBytes[dataLength-1] = (byte)generator.nextInt();79testBytes[dataLength-2] = (byte)generator.nextInt();8081// Attempt to decode the bytes back into a String82ByteArrayInputStream bais = new ByteArrayInputStream(testBytes);83DataInputStream dis = new DataInputStream(bais);84dis.readUTF();85}86}878889