From d86db3f76f03a63c56e6dd9f5531d3b8a78980f6 Mon Sep 17 00:00:00 2001 From: Yi Liu Date: Thu, 5 Jun 2014 01:30:40 +0000 Subject: [PATCH] HADOOP-10662. NullPointerException in CryptoInputStream while wrapped stream is not ByteBufferReadable. Add tests using normal stream. Contributed by Yi Liu git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/branches/fs-encryption@1600553 13f79535-47bb-0310-9956-ffa450edef68 --- .../hadoop-common/CHANGES-fs-encryption.txt | 3 + .../hadoop/crypto/CryptoInputStream.java | 2 + .../crypto/TestCryptoStreamsNormal.java | 123 ++++++++++++++++++ 3 files changed, 128 insertions(+) create mode 100644 hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/crypto/TestCryptoStreamsNormal.java diff --git a/hadoop-common-project/hadoop-common/CHANGES-fs-encryption.txt b/hadoop-common-project/hadoop-common/CHANGES-fs-encryption.txt index c99436606e9..66b55b0f508 100644 --- a/hadoop-common-project/hadoop-common/CHANGES-fs-encryption.txt +++ b/hadoop-common-project/hadoop-common/CHANGES-fs-encryption.txt @@ -24,6 +24,9 @@ fs-encryption (Unreleased) HADOOP-10653. Add a new constructor for CryptoInputStream that receives current position of wrapped stream. (Yi Liu) + HADOOP-10662. NullPointerException in CryptoInputStream while wrapped + stream is not ByteBufferReadable. Add tests using normal stream. (Yi Liu) + OPTIMIZATIONS BUG FIXES diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/crypto/CryptoInputStream.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/crypto/CryptoInputStream.java index fe663f2cc9f..55c891a0ace 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/crypto/CryptoInputStream.java +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/crypto/CryptoInputStream.java @@ -172,6 +172,8 @@ public class CryptoInputStream extends FilterInputStream implements } catch (UnsupportedOperationException e) { usingByteBufferRead = Boolean.FALSE; } + } else { + usingByteBufferRead = Boolean.FALSE; } if (!usingByteBufferRead) { n = readFromUnderlyingStream(inBuffer); diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/crypto/TestCryptoStreamsNormal.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/crypto/TestCryptoStreamsNormal.java new file mode 100644 index 00000000000..e9c313fde36 --- /dev/null +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/crypto/TestCryptoStreamsNormal.java @@ -0,0 +1,123 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.crypto; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +import org.apache.hadoop.conf.Configuration; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Ignore; +import org.junit.Test; + +/** + * Test crypto streams using normal stream which does not support the + * additional interfaces that the Hadoop FileSystem streams implement + * (Seekable, PositionedReadable, ByteBufferReadable, HasFileDescriptor, + * CanSetDropBehind, CanSetReadahead, HasEnhancedByteBufferAccess, Syncable, + * CanSetDropBehind) + */ +public class TestCryptoStreamsNormal extends CryptoStreamsTestBase { + /** + * Data storage. + * {@link #getOutputStream(int, byte[], byte[])} will write to this buffer. + * {@link #getInputStream(int, byte[], byte[])} will read from this buffer. + */ + private byte[] buffer; + private int bufferLen; + + @BeforeClass + public static void init() throws Exception { + Configuration conf = new Configuration(); + codec = CryptoCodec.getInstance(conf); + } + + @AfterClass + public static void shutdown() throws Exception { + } + + @Override + protected OutputStream getOutputStream(int bufferSize, byte[] key, byte[] iv) + throws IOException { + OutputStream out = new ByteArrayOutputStream() { + @Override + public void flush() throws IOException { + buffer = buf; + bufferLen = count; + } + @Override + public void close() throws IOException { + buffer = buf; + bufferLen = count; + } + }; + return new CryptoOutputStream(out, codec, bufferSize, key, iv); + } + + @Override + protected InputStream getInputStream(int bufferSize, byte[] key, byte[] iv) + throws IOException { + ByteArrayInputStream in = new ByteArrayInputStream(buffer, 0, bufferLen); + return new CryptoInputStream(in, codec, bufferSize, + key, iv); + } + + @Ignore("Wrapped stream doesn't support Syncable") + @Override + @Test(timeout=1000) + public void testSyncable() throws IOException {} + + @Ignore("Wrapped stream doesn't support PositionedRead") + @Override + @Test(timeout=1000) + public void testPositionedRead() throws IOException {} + + @Ignore("Wrapped stream doesn't support ReadFully") + @Override + @Test(timeout=1000) + public void testReadFully() throws IOException {} + + @Ignore("Wrapped stream doesn't support Seek") + @Override + @Test(timeout=1000) + public void testSeek() throws IOException {} + + @Ignore("Wrapped stream doesn't support ByteBufferRead") + @Override + @Test(timeout=1000) + public void testByteBufferRead() throws IOException {} + + @Ignore("Wrapped stream doesn't support ByteBufferRead, Seek") + @Override + @Test(timeout=1000) + public void testCombinedOp() throws IOException {} + + @Ignore("Wrapped stream doesn't support SeekToNewSource") + @Override + @Test(timeout=1000) + public void testSeekToNewSource() throws IOException {} + + @Ignore("Wrapped stream doesn't support HasEnhancedByteBufferAccess") + @Override + @Test(timeout=1000) + public void testHasEnhancedByteBufferAccess() throws IOException {} +}