Path: blob/master/test/jdk/sun/security/ssl/GenSSLConfigs/Traffic.java
41152 views
/*1* Copyright (c) 1997, 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*/2223import java.io.*;24import java.security.*;2526class Traffic27{28private InputStream in;29private OutputStream out;3031//32// By default, traffic streams are predictable and what comes33// in is compared with what it's expected to be.34//35static private byte fixedSeed [] = { 1, 2, 3, 4};3637private SecureRandom prng;38private boolean compareRandom = true;394041Traffic (InputStream in, OutputStream out)42{43this.in = in;44this.out = out;45try {46prng = SecureRandom.getInstance("SHA1PRNG");47} catch (NoSuchAlgorithmException e) {48throw new RuntimeException(e);49}50prng.setSeed(fixedSeed);51}5253// optionally provide PRNG for "truly" random data.54public void setPRNG (SecureRandom prng)55{56this.prng = prng;57compareRandom = false;58}596061//62// Basic half-duplex testing, as used for RPC-style systems like63// HTTP, RMI, CORBA, ONC, etc.64//65// parameter 'n' is "0" for some fixed data tests, else is the66// number of passes of random data to send.67//6869public void initiate (int n)70throws IOException71{72// System.out.println ("Initiating N = " + n);7374if (n == 0)75initiateConst ();76else if (n < 0)77System.out.println ("** ERROR: initiate forever ??");78else79for ( ; n > 0; n -= 1) {80initiateRandom ();81}82}838485public void respond (int n)86throws IOException87{88if (n == 0)89respondConst ();90else if (n < 0) // n < 0 == respond forever91while (true)92respondRandom ();93else94while (n-- > 0)95respondRandom ();96}979899//100// Test passing of fixed size (and content) data.101//102// For SSL, one test goal is to ensure that all the basic103// block cipher padding sizes get banged on. SSLv3 ciphers104// are all the same block size, but there are larger sizes105// coming along. (Big blocks in hardware can be fast!!)106//107108private static final int MAX_BLOCKSIZE = 8 * 2;109110private void writeConstData (int n)111throws IOException112{113if (n <= 0)114return;115116byte buf [] = new byte [n];117118for (int i = 0; i < n; i++)119buf [i] = (byte) i;120121out.write (buf);122123/*124System.out.println (Thread.currentThread ().getName ()125+ " wrote const data size = " + n);126*/127}128129private void readConstData (int n)130throws IOException131{132if (n <= 0)133return;134135byte buf [] = new byte [n];136137in.read (buf);138139for (int i = 0; i < n; i++)140if (buf [i] != (byte) i)141throw new IOException ("const data was incorrect, "142+ "n = " + n + ", i = " + i);143144/*145System.out.println (Thread.currentThread ().getName ()146+ " read const data size = " + n);147*/148}149150private void initiateConst ()151throws IOException152{153for (int i = 1; i <= MAX_BLOCKSIZE; i++) {154writeConstData (i);155readConstData (i);156}157158}159160private void respondConst ()161throws IOException162{163for (int i = 1; i <= MAX_BLOCKSIZE; i++) {164readConstData (i);165writeConstData (i);166}167}168169170//171// Test passing of random size (and content) data.172//173// For SSL, one test goal is to ensure that all the basic174// record sizes get banged on. Traffic will normally175// be bimodal (small packets, and big ones) and we give176// a half-hearted effort at emulating that -- no real177// statistics to back up this particular distribution.178//179180private static final int MAX_RECORDSIZE = 16384 * 2;181182private int nextRecordSize ()183{184double d = prng.nextGaussian ();185int n;186187// assume 1/3 traffic is "big", less variance188if ((prng.nextInt () % 3) == 0) {189n = (int) (d * 2048);190n += 15 * 1024;191192// ... and the rest is smaller, much variance193} else {194n = (int) (d * 4096);195n += 1024;196}197198if (n < 0)199return nextRecordSize ();200else if (n > MAX_RECORDSIZE)201return MAX_RECORDSIZE;202else203return n;204}205206207private void writeRandomData ()208throws IOException209{210int n = nextRecordSize ();211byte buf [] = new byte [n];212213// System.out.println ("write, size = " + n);214215prng.nextBytes (buf);216217writeInt (n);218out.write (buf);219}220221private void readRandomData ()222throws IOException223{224int n = readInt ();225byte actual [] = new byte [n];226227readFully (actual);228229if (compareRandom) {230byte expected [];231232if (n != nextRecordSize ())233throw new IOException ("wrong record size");234235expected = new byte [n];236prng.nextBytes (expected);237238for (int i = 0; i < n; i++)239if (actual [i] != expected [i])240throw new IOException ("random data was incorrect, "241+ "n = " + n + ", i = " + i);242}243}244245private void initiateRandom ()246throws IOException247{248writeRandomData ();249readRandomData ();250251}252253private void respondRandom ()254throws IOException255{256readRandomData ();257writeRandomData ();258}259260261private void readFully (byte buf [])262throws IOException263{264int len = buf.length;265int offset = 0;266int value;267268while (len > 0) {269value = in.read (buf, offset, len);270if (value == -1)271throw new EOFException ("read buffer");272offset += value;273len -= value;274}275}276277278private int readInt ()279throws IOException280{281int b0, b1, b2, b3;282int n;283284b0 = in.read ();285b1 = in.read ();286b2 = in.read ();287b3 = in.read ();288289if (b0 < 0 || b1 < 0 || b2 < 0 || b3 < 0)290throw new EOFException ();291292/*293System.out.println ("READ: b0 = " + b0 + ", b1 = " + b1294+ ", b2 = " + b2 + ", b3 = " + b3);295*/296297n = (b3 & 0x0ff);298n |= (b2 & 0x0ff) << 8;299n |= (b1 & 0x0ff) << 16;300n |= (b0 & 0x0ff) << 24;301return n;302}303304private void writeInt (int n)305throws IOException306{307int b0, b1, b2, b3;308309b3 = n & 0x0ff;310n >>= 8;311b2 = n & 0x0ff;312n >>= 8;313b1 = n & 0x0ff;314n >>= 8;315b0 = n & 0x0ff;316317/*318System.out.println ("WRITE: b0 = " + b0 + ", b1 = " + b1319+ ", b2 = " + b2 + ", b3 = " + b3);320*/321322out.write (b0);323out.write (b1);324out.write (b2);325out.write (b3);326}327}328329330