|
| 1 | +/* |
| 2 | + * $Id$ |
| 3 | + */ |
| 4 | + |
| 5 | +/* |
| 6 | +
|
| 7 | +Copyright (c) 2000-2016 Board of Trustees of Leland Stanford Jr. University, |
| 8 | +all rights reserved. |
| 9 | +
|
| 10 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 11 | +of this software and associated documentation files (the "Software"), to deal |
| 12 | +in the Software without restriction, including without limitation the rights |
| 13 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 14 | +copies of the Software, and to permit persons to whom the Software is |
| 15 | +furnished to do so, subject to the following conditions: |
| 16 | +
|
| 17 | +The above copyright notice and this permission notice shall be included in |
| 18 | +all copies or substantial portions of the Software. |
| 19 | +
|
| 20 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 21 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 22 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 23 | +STANFORD UNIVERSITY BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 24 | +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR |
| 25 | +IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 26 | +
|
| 27 | +Except as contained in this notice, the name of Stanford University shall not |
| 28 | +be used in advertising or otherwise to promote the sale, use or other dealings |
| 29 | +in this Software without prior written authorization from Stanford University. |
| 30 | +
|
| 31 | +*/ |
| 32 | + |
| 33 | +package org.lockss.util; |
| 34 | +import java.io.*; |
| 35 | +import java.security.*; |
| 36 | +import org.lockss.test.*; |
| 37 | + |
| 38 | +public class TestHashedOutputStream extends LockssTestCase { |
| 39 | + |
| 40 | + private MockMessageDigest makeMessageDigest() { |
| 41 | + return new MockMessageDigest(); |
| 42 | + } |
| 43 | + |
| 44 | + public void testNullArgumentsToConstructor() { |
| 45 | + MessageDigest md = makeMessageDigest(); |
| 46 | + OutputStream out = new ByteArrayOutputStream(); |
| 47 | + try { |
| 48 | + new HashedOutputStream(null, md); |
| 49 | + fail("Calling the constructor with null OutputStream should throw"); |
| 50 | + } catch (IllegalArgumentException e) { |
| 51 | + } |
| 52 | + try { |
| 53 | + new HashedOutputStream(out, null); |
| 54 | + fail("Calling the constructor with null MessageDigest should throw"); |
| 55 | + } catch (IllegalArgumentException e) { |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + byte[] getBytes(String s) throws UnsupportedEncodingException { |
| 60 | + return s.getBytes(Constants.DEFAULT_ENCODING); |
| 61 | + } |
| 62 | + |
| 63 | + public void testWrite() throws IOException { |
| 64 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 65 | + MockMessageDigest md = makeMessageDigest(); |
| 66 | + HashedOutputStream hos = new HashedOutputStream(out, md); |
| 67 | + hos.write('a'); |
| 68 | + assertEquals(getBytes("a"), md.getUpdatedBytes()); |
| 69 | + hos.write(getBytes("bcdefghijk")); |
| 70 | + hos.write(getBytes("1234567890"), 3, 4); |
| 71 | + assertEquals(getBytes("bcdefghijk4567"), md.getUpdatedBytes()); |
| 72 | + assertEquals("abcdefghijk4567", out.toString()); |
| 73 | + } |
| 74 | + |
| 75 | +} |
0 commit comments