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 .cassandra .io .compress ;
19
+
20
+ import java .io .IOException ;
21
+ import java .nio .ByteBuffer ;
22
+ import java .nio .channels .FileChannel ;
23
+ import java .nio .file .StandardOpenOption ;
24
+ import java .util .HashMap ;
25
+ import java .util .Map ;
26
+ import java .util .concurrent .ThreadLocalRandom ;
27
+
28
+ import org .junit .BeforeClass ;
29
+ import org .junit .Test ;
30
+
31
+ import org .apache .cassandra .config .DatabaseDescriptor ;
32
+ import org .apache .cassandra .io .util .File ;
33
+ import org .apache .cassandra .io .util .FileUtils ;
34
+ import org .apache .cassandra .io .util .SequentialWriterOption ;
35
+ import org .apache .cassandra .schema .CompressionParams ;
36
+
37
+ import static org .junit .Assert .*;
38
+
39
+ public class EncryptedSequentialWriterReadableTest
40
+ {
41
+ static final ICompressor AESEncryptor ;
42
+ static final CompressionParams AESEncryptorParams ;
43
+
44
+ static
45
+ {
46
+ DatabaseDescriptor .daemonInitialization ();
47
+
48
+ Map <String , String > opts = new HashMap <>();
49
+ opts .put (CompressionParams .CLASS , Encryptor .class .getName ());
50
+ opts .put (EncryptionConfig .CIPHER_ALGORITHM , "AES/CBC/PKCS5Padding" );
51
+ opts .put (EncryptionConfig .SECRET_KEY_STRENGTH , Integer .toString (128 ));
52
+ opts .put (EncryptionConfig .KEY_PROVIDER , EncryptorTest .KeyProviderFactoryStub .class .getName ());
53
+ AESEncryptor = Encryptor .create (opts );
54
+ AESEncryptorParams = CompressionParams .fromMap (opts );
55
+ }
56
+
57
+ @ BeforeClass
58
+ public static void setupDD ()
59
+ {
60
+ // Already initialized in static block
61
+ }
62
+
63
+ @ Test
64
+ public void testEncryptedWriterWithReadableChannel () throws IOException
65
+ {
66
+ File tempFile = FileUtils .createTempFile ("encrypted" , "test" );
67
+ tempFile .tryDelete ();
68
+
69
+ byte [] testData = new byte [8192 ];
70
+ ThreadLocalRandom .current ().nextBytes (testData );
71
+
72
+ ICompressor encryptor = AESEncryptor ;
73
+ SequentialWriterOption option = SequentialWriterOption .newBuilder ()
74
+ .bufferSize (1024 )
75
+ .build ();
76
+
77
+ try (EncryptedSequentialWriter writer = new EncryptedSequentialWriter (tempFile , option , encryptor ))
78
+ {
79
+ writer .write (testData );
80
+ writer .finish ();
81
+
82
+ assertTrue (tempFile .exists ());
83
+ assertTrue (tempFile .length () > 0 );
84
+
85
+ try (FileChannel channel = FileChannel .open (tempFile .toPath (), StandardOpenOption .READ ))
86
+ {
87
+ ByteBuffer headerBuffer = ByteBuffer .allocate (128 );
88
+ int bytesRead = channel .read (headerBuffer );
89
+ assertTrue (bytesRead > 0 );
90
+ }
91
+ }
92
+ finally
93
+ {
94
+ tempFile .tryDelete ();
95
+ }
96
+ }
97
+
98
+ @ Test
99
+ public void testEncryptedWriterConstructorPassesReadableTrue () throws IOException
100
+ {
101
+ File tempFile = FileUtils .createTempFile ("encrypted2" , "test" );
102
+ tempFile .tryDelete ();
103
+
104
+ ICompressor encryptor = AESEncryptor ;
105
+ SequentialWriterOption option = SequentialWriterOption .newBuilder ()
106
+ .bufferSize (1024 )
107
+ .finishOnClose (true )
108
+ .build ();
109
+
110
+ byte [] testData = "test data for encryption" .getBytes ();
111
+
112
+ try (EncryptedSequentialWriter writer = new EncryptedSequentialWriter (tempFile , option , encryptor ))
113
+ {
114
+ writer .write (testData );
115
+ }
116
+
117
+ assertTrue (tempFile .exists ());
118
+ assertTrue (tempFile .length () > testData .length );
119
+
120
+ tempFile .tryDelete ();
121
+ }
122
+
123
+ }
0 commit comments