J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2003 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | * accompanied this code). |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License version |
| 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
| 19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /* @test |
| 25 | * @bug 4806007 |
| 26 | * @summary Checks for vague exceptions from writeUTF/readUTF |
| 27 | */ |
| 28 | |
| 29 | import java.io.*; |
| 30 | import java.util.*; |
| 31 | |
| 32 | public class ReadUTF { |
| 33 | |
| 34 | private static Random generator = new Random(); |
| 35 | |
| 36 | private static final int TEST_ITERATIONS = 1000; |
| 37 | |
| 38 | private static final int A_NUMBER_NEAR_65535 = 60000; |
| 39 | |
| 40 | private static final int MAX_CORRUPTIONS_PER_CYCLE = 3; |
| 41 | |
| 42 | public static final void main(String[] args) throws Exception { |
| 43 | for (int i=0; i<TEST_ITERATIONS; i++) { |
| 44 | try { |
| 45 | writeAndReadAString(); |
| 46 | } catch (UTFDataFormatException utfdfe) { |
| 47 | if (utfdfe.getMessage() == null) |
| 48 | throw new RuntimeException("vague exception thrown"); |
| 49 | } catch (EOFException eofe) { |
| 50 | // These are rare and beyond the scope of the test |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | private static void writeAndReadAString() throws Exception { |
| 56 | // Write out a string whose UTF-8 encoding is quite possibly |
| 57 | // longer than 65535 bytes |
| 58 | int length = generator.nextInt(A_NUMBER_NEAR_65535) + 1; |
| 59 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 60 | StringBuffer testBuffer = new StringBuffer(); |
| 61 | for (int i=0; i<length; i++) |
| 62 | testBuffer.append((char)generator.nextInt()); |
| 63 | String testString = testBuffer.toString(); |
| 64 | DataOutputStream dos = new DataOutputStream(baos); |
| 65 | dos.writeUTF(testString); |
| 66 | |
| 67 | // Corrupt the data to produce malformed characters |
| 68 | byte[] testBytes = baos.toByteArray(); |
| 69 | int dataLength = testBytes.length; |
| 70 | int corruptions = generator.nextInt(MAX_CORRUPTIONS_PER_CYCLE); |
| 71 | for (int i=0; i<corruptions; i++) { |
| 72 | int index = generator.nextInt(dataLength); |
| 73 | testBytes[index] = (byte)generator.nextInt(); |
| 74 | } |
| 75 | |
| 76 | // Pay special attention to mangling the end to produce |
| 77 | // partial characters at end |
| 78 | testBytes[dataLength-1] = (byte)generator.nextInt(); |
| 79 | testBytes[dataLength-2] = (byte)generator.nextInt(); |
| 80 | |
| 81 | // Attempt to decode the bytes back into a String |
| 82 | ByteArrayInputStream bais = new ByteArrayInputStream(testBytes); |
| 83 | DataInputStream dis = new DataInputStream(bais); |
| 84 | dis.readUTF(); |
| 85 | } |
| 86 | } |