View Javadoc
1    /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.commons.crypto.jna;
19  
20  import static org.junit.jupiter.api.Assumptions.assumeTrue;
21  
22  import java.io.ByteArrayOutputStream;
23  import java.util.concurrent.TimeUnit;
24  
25  import org.apache.commons.crypto.cipher.AbstractCipherTest;
26  import org.apache.commons.crypto.stream.AbstractCipherStreamTest;
27  import org.junit.jupiter.api.BeforeEach;
28  import org.junit.jupiter.api.Test;
29  import org.junit.jupiter.api.Timeout;
30  
31  public abstract class AbstractCipherJnaStreamTest extends AbstractCipherStreamTest {
32  
33      private static final String CIPHER_OPENSSL_JNA = OpenSslJna.getCipherClass().getName();
34  
35      @BeforeEach
36      public void init() {
37          assumeTrue(OpenSslJna.isEnabled());
38      }
39  
40      /** Test byte buffer read with different buffer size. */
41      @Override
42      @Timeout(value = 120000, unit = TimeUnit.MILLISECONDS)
43      public void testByteBufferRead() throws Exception {
44          doByteBufferRead(CIPHER_OPENSSL_JNA, false);
45  
46          doByteBufferRead(CIPHER_OPENSSL_JNA, true);
47      }
48  
49      /** Test byte buffer write. */
50      @Override
51      @Timeout(value = 120000, unit = TimeUnit.MILLISECONDS)
52      public void testByteBufferWrite() throws Exception {
53          final ByteArrayOutputStream baos = new ByteArrayOutputStream();
54          doByteBufferWrite(CIPHER_OPENSSL_JNA, baos, false);
55  
56          doByteBufferWrite(CIPHER_OPENSSL_JNA, baos, true);
57      }
58  
59      @Override
60      @Test
61      public void testReadWrite() throws Exception {
62          doReadWriteTest(0, CIPHER_OPENSSL_JNA, CIPHER_OPENSSL_JNA, iv);
63          doReadWriteTest(count, CIPHER_OPENSSL_JNA, CIPHER_OPENSSL_JNA, iv);
64          doReadWriteTest(count, AbstractCipherTest.JCE_CIPHER_CLASSNAME, CIPHER_OPENSSL_JNA, iv);
65          doReadWriteTest(count, CIPHER_OPENSSL_JNA, AbstractCipherTest.JCE_CIPHER_CLASSNAME, iv);
66          // Overflow test, IV: xx xx xx xx xx xx xx xx ff ff ff ff ff ff ff ff
67          for (int i = 0; i < 8; i++) {
68              iv[8 + i] = (byte) 0xff;
69          }
70          doReadWriteTest(count, CIPHER_OPENSSL_JNA, CIPHER_OPENSSL_JNA, iv);
71          doReadWriteTest(count, AbstractCipherTest.JCE_CIPHER_CLASSNAME, CIPHER_OPENSSL_JNA, iv);
72          doReadWriteTest(count, CIPHER_OPENSSL_JNA, AbstractCipherTest.JCE_CIPHER_CLASSNAME, iv);
73      }
74  
75      /** Test skip. */
76      @Override
77      @Test
78      @Timeout(value = 120000, unit = TimeUnit.MILLISECONDS)
79      public void testSkip() throws Exception {
80          doSkipTest(CIPHER_OPENSSL_JNA, false);
81  
82          doSkipTest(CIPHER_OPENSSL_JNA, true);
83      }
84  }