|
| 1 | +/* |
| 2 | + * Copyright (C) 2024-2025 OnixByte. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package com.onixbyte.devkit.utils; |
| 19 | + |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | + |
| 22 | +import java.nio.charset.StandardCharsets; |
| 23 | +import java.security.GeneralSecurityException; |
| 24 | + |
| 25 | +import static org.junit.jupiter.api.Assertions.*; |
| 26 | + |
| 27 | +class AesUtilTest { |
| 28 | + |
| 29 | + @Test |
| 30 | + void testEncryptAndDecryptByte() throws GeneralSecurityException { |
| 31 | + byte[] secretKey = "43f72073956d4c81".getBytes(StandardCharsets.UTF_8); |
| 32 | + byte[] originalData = "Hello World".getBytes(StandardCharsets.UTF_8); |
| 33 | + |
| 34 | + byte[] encryptedData = AesUtil.encrypt(originalData, secretKey); |
| 35 | + assertNotNull(encryptedData); |
| 36 | + |
| 37 | + byte[] decryptedData = AesUtil.decrypt(encryptedData, secretKey); |
| 38 | + assertNotNull(decryptedData); |
| 39 | + |
| 40 | + assertArrayEquals(originalData, decryptedData); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + void testEncryptAndDecryptString() throws GeneralSecurityException { |
| 45 | + var secret = "43f72073956d4c81"; |
| 46 | + var originalData = "Hello World"; |
| 47 | + |
| 48 | + var encryptedData = AesUtil.encrypt(originalData, secret); |
| 49 | + assertNotNull(encryptedData); |
| 50 | + assertNotEquals(originalData, encryptedData); |
| 51 | + |
| 52 | + var decryptedData = AesUtil.decrypt(encryptedData, secret); |
| 53 | + assertNotNull(decryptedData); |
| 54 | + assertEquals(originalData, decryptedData); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + void testEncryptWithWrongKeyFails() throws GeneralSecurityException { |
| 59 | + var secret = "43f72073956d4c81"; |
| 60 | + var wrongSecret = "0000000000000000"; |
| 61 | + var originalData = "Hello World"; |
| 62 | + |
| 63 | + var encryptedData = AesUtil.encrypt(originalData.getBytes(StandardCharsets.UTF_8), |
| 64 | + secret.getBytes(StandardCharsets.UTF_8)); |
| 65 | + assertNotNull(encryptedData); |
| 66 | + |
| 67 | + // When decrypting with the wrong key, a BadPaddingException or IllegalBlockSizeException is expected to be thrown |
| 68 | + assertThrows(GeneralSecurityException.class, () -> { |
| 69 | + AesUtil.decrypt(encryptedData, wrongSecret.getBytes(StandardCharsets.UTF_8)); |
| 70 | + }); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + void testGenerateRandomSecret() { |
| 75 | + var randomSecret = AesUtil.generateRandomSecret(); |
| 76 | + assertNotNull(randomSecret); |
| 77 | + assertEquals(16, randomSecret.length()); |
| 78 | + } |
| 79 | +} |
0 commit comments