1 /*
   2  * Copyright (c) 2003, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /* @test
  25  * @bug 4806007
  26  * @summary Checks for vague exceptions from writeUTF/readUTF
  27  * @key randomness
  28  */
  29 
  30 import java.io.*;
  31 import java.util.*;
  32 
  33 public class ReadUTF {
  34 
  35     private static Random generator = new Random();
  36 
  37     private static final int TEST_ITERATIONS = 1000;
  38 
  39     private static final int A_NUMBER_NEAR_65535 = 60000;
  40 
  41     private static final int MAX_CORRUPTIONS_PER_CYCLE = 3;
  42 
  43     public static final void main(String[] args) throws Exception {
  44         for (int i=0; i<TEST_ITERATIONS; i++) {
  45             try {
  46                 writeAndReadAString();
  47             } catch (UTFDataFormatException utfdfe) {
  48                 if (utfdfe.getMessage() == null)
  49                     throw new RuntimeException("vague exception thrown");
  50             } catch (EOFException eofe) {
  51                 // These are rare and beyond the scope of the test
  52             }
  53         }
  54     }
  55 
  56     private static void writeAndReadAString() throws Exception {
  57         // Write out a string whose UTF-8 encoding is quite possibly
  58         // longer than 65535 bytes
  59         int length = generator.nextInt(A_NUMBER_NEAR_65535) + 1;
  60         ByteArrayOutputStream baos = new ByteArrayOutputStream();
  61         StringBuffer testBuffer = new StringBuffer();
  62         for (int i=0; i<length; i++)
  63             testBuffer.append((char)generator.nextInt());
  64         String testString = testBuffer.toString();
  65         DataOutputStream dos = new DataOutputStream(baos);
  66         dos.writeUTF(testString);
  67 
  68         // Corrupt the data to produce malformed characters
  69         byte[] testBytes = baos.toByteArray();
  70         int dataLength = testBytes.length;
  71         int corruptions = generator.nextInt(MAX_CORRUPTIONS_PER_CYCLE);
  72         for (int i=0; i<corruptions; i++) {
  73             int index = generator.nextInt(dataLength);
  74             testBytes[index] = (byte)generator.nextInt();
  75         }
  76 
  77         // Pay special attention to mangling the end to produce
  78         // partial characters at end
  79         testBytes[dataLength-1] = (byte)generator.nextInt();
  80         testBytes[dataLength-2] = (byte)generator.nextInt();
  81 
  82         // Attempt to decode the bytes back into a String
  83         ByteArrayInputStream bais = new ByteArrayInputStream(testBytes);
  84         DataInputStream dis = new DataInputStream(bais);
  85         dis.readUTF();
  86     }
  87 }