< prev index next >

test/sun/security/util/DerValue/BadValue.java

Print this page


   1 /*
   2  * Copyright (c) 2009, 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  */


  31 import java.io.*;
  32 import sun.security.util.*;
  33 
  34 public class BadValue {
  35 
  36     public static void main(String[] args) throws Exception {
  37 
  38         // Test IOUtils.readFully
  39 
  40         // We have 4 bytes
  41         InputStream in = new ByteArrayInputStream(new byte[10]);
  42         byte[] bs = IOUtils.readFully(in, 4, true);
  43         if (bs.length != 4 || in.available() != 6) {
  44             throw new Exception("First read error");
  45         }
  46         // But only 6 left
  47         bs = IOUtils.readFully(in, 10, false);
  48         if (bs.length != 6 || in.available() != 0) {
  49             throw new Exception("Second read error");
  50         }
  51         // MAX read as much as it can
  52         in = new ByteArrayInputStream(new byte[10]);

  53         bs = IOUtils.readFully(in, Integer.MAX_VALUE, true);
  54         if (bs.length != 10 || in.available() != 0) {
  55             throw new Exception("Second read error");



  56         }
  57         // MAX ignore readAll
  58         in = new ByteArrayInputStream(new byte[10]);
  59         bs = IOUtils.readFully(in, Integer.MAX_VALUE, false);
  60         if (bs.length != 10 || in.available() != 0) {
  61             throw new Exception("Second read error");




  62         }

  63         // 20>10, readAll means failure
  64         in = new ByteArrayInputStream(new byte[10]);
  65         try {
  66             bs = IOUtils.readFully(in, 20, true);
  67             throw new Exception("Third read error");
  68         } catch (EOFException e) {
  69             // OK
  70         }
  71         int bignum = 10 * 1024 * 1024;
  72         bs = IOUtils.readFully(new SuperSlowStream(bignum), -1, true);
  73         if (bs.length != bignum) {
  74             throw new Exception("Fourth read error");
  75         }
  76 
  77         // Test DerValue
  78         byte[] input = {0x04, (byte)0x84, 0x40, 0x00, 0x42, 0x46, 0x4b};
  79         try {
  80             new DerValue(new ByteArrayInputStream(input));
  81         } catch (IOException ioe) {
  82             // This is OK
  83         }
  84     }
  85 }
  86 
  87 /**
  88  * An InputStream contains a given number of bytes, but only returns one byte
  89  * per read.
  90  */
  91 class SuperSlowStream extends InputStream {
  92     private int p;
  93     /**
  94      * @param Initial capacity


   1 /*
   2  * Copyright (c) 2009, 2017 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  */


  31 import java.io.*;
  32 import sun.security.util.*;
  33 
  34 public class BadValue {
  35 
  36     public static void main(String[] args) throws Exception {
  37 
  38         // Test IOUtils.readFully
  39 
  40         // We have 4 bytes
  41         InputStream in = new ByteArrayInputStream(new byte[10]);
  42         byte[] bs = IOUtils.readFully(in, 4, true);
  43         if (bs.length != 4 || in.available() != 6) {
  44             throw new Exception("First read error");
  45         }
  46         // But only 6 left
  47         bs = IOUtils.readFully(in, 10, false);
  48         if (bs.length != 6 || in.available() != 0) {
  49             throw new Exception("Second read error");
  50         }
  51         // MAX length results in exception
  52         in = new ByteArrayInputStream(new byte[10]);
  53         try {
  54             bs = IOUtils.readFully(in, Integer.MAX_VALUE, true);
  55             throw new Exception("No exception on MAX_VALUE length");
  56         } catch (EOFException ex) {
  57             // this is expected
  58         } catch (Exception ex) {
  59             throw ex;
  60         }
  61         // -1 length results in exception
  62         in = new ByteArrayInputStream(new byte[10]);
  63         try {
  64             bs = IOUtils.readFully(in, -1, true);
  65             throw new Exception("No exception on -1 length");
  66         } catch (IndexOutOfBoundsException ex) {
  67             // this is expected
  68         } catch (Exception ex) {
  69             throw ex;
  70         }
  71 
  72         // 20>10, readAll means failure
  73         in = new ByteArrayInputStream(new byte[10]);
  74         try {
  75             bs = IOUtils.readFully(in, 20, true);
  76             throw new Exception("No exception on EOF");
  77         } catch (EOFException e) {
  78             // OK
  79         }
  80         int bignum = 10 * 1024 * 1024;
  81         bs = IOUtils.readFully(new SuperSlowStream(bignum), bignum, true);
  82         if (bs.length != bignum) {
  83             throw new Exception("Read returned small array");
  84         }
  85 
  86         // Test DerValue
  87         byte[] input = {0x04, (byte)0x84, 0x40, 0x00, 0x42, 0x46, 0x4b};
  88         try {
  89             new DerValue(new ByteArrayInputStream(input));
  90         } catch (IOException ioe) {
  91             // This is OK
  92         }
  93     }
  94 }
  95 
  96 /**
  97  * An InputStream contains a given number of bytes, but only returns one byte
  98  * per read.
  99  */
 100 class SuperSlowStream extends InputStream {
 101     private int p;
 102     /**
 103      * @param Initial capacity


< prev index next >