|
| 1 | +/* |
| 2 | + * This file is part of SimpleTextCrypt. |
| 3 | + * Copyright (c) 2015-2026, Aidin Gharibnavaz <aidin@aidinhut.com> |
| 4 | + * |
| 5 | + * SimpleTextCrypt is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * SimpleTextCrypt is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with SimpleTextCrypt. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | +package com.aidinhut.simpletextcrypt |
| 19 | + |
| 20 | +import android.util.Log |
| 21 | +import androidx.test.ext.junit.runners.AndroidJUnit4 |
| 22 | +import org.junit.Assert.assertEquals |
| 23 | +import org.junit.Test |
| 24 | +import org.junit.runner.RunWith |
| 25 | + |
| 26 | +/** |
| 27 | + * Implements stand-alone tests for Crypter class. |
| 28 | + */ |
| 29 | +@RunWith(AndroidJUnit4::class) |
| 30 | +class CrypterTest { |
| 31 | + |
| 32 | + /** |
| 33 | + * Tests if encrypting and decrypting a data will result |
| 34 | + * in the same data. |
| 35 | + */ |
| 36 | + @Test |
| 37 | + fun testEncryptDecryptValidation() { |
| 38 | + val passwords = arrayOf( |
| 39 | + "uir43@#89djhncAd.,[]-+091jhdncq`", |
| 40 | + "18736", |
| 41 | + "❤️😜🙈🙃🍎🍀🍵🍪", |
| 42 | + "Me L@ve Ch33se! Double, no triple cream! Gonna have a heart attack.", |
| 43 | + "کلید فارسی" |
| 44 | + ) |
| 45 | + val dataList = arrayOf( |
| 46 | + "Hi there! I'm a test data that is going to be encrypted.", |
| 47 | + "I love you! ❤", |
| 48 | + "یک مسیج فارسی سری که برای بقیه بفرستیم", |
| 49 | + "A very long message to be secretly send over the network. It's very very very very very very" + |
| 50 | + " very very long! And has some UTF-8 characters like یک and マンガが大好きです" |
| 51 | + ) |
| 52 | + |
| 53 | + for (password in passwords) { |
| 54 | + for (data in dataList) { |
| 55 | + validateEncryptDecrypt(password, data) |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + private fun validateEncryptDecrypt(password: String, data: String) { |
| 61 | + Log.i("CrypterTest", "password=$password - data=$data") |
| 62 | + |
| 63 | + val encryptedData = Crypter.encrypt(password, data) |
| 64 | + val decryptedData = Crypter.decrypt(password, encryptedData) |
| 65 | + |
| 66 | + assertEquals(data, decryptedData) |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Tests if we can decrypt messages that are encrypted with an old version of the app. |
| 71 | + */ |
| 72 | + @Test |
| 73 | + fun testLegacyAlgorithm() { |
| 74 | + assertEquals( |
| 75 | + "Hi! I'm encrypted with the old algorithm.", |
| 76 | + Crypter.legacyDecrypt( |
| 77 | + "buSD4778@af-bq3299", |
| 78 | + "q:*`Bgd|I[2y9#[SRbsqNBNdEb+7gBaGSUylWNcSHQWw+Sq32XDF6N3avTWiBzUyObviPqdTzkFEDd2o" |
| 79 | + ) |
| 80 | + ) |
| 81 | + assertEquals( |
| 82 | + "Another message for testing. 😀🙃", |
| 83 | + Crypter.legacyDecrypt( |
| 84 | + "1356", |
| 85 | + "g4A!>y{%pz}Q4B\u007FRr9Da4/d/y33wGJichEjYy3Z+uRgKaTz4WvsOfcC/h2HJTDoSCktv2WNFRM5gdXEI" |
| 86 | + ) |
| 87 | + ) |
| 88 | + } |
| 89 | +} |
0 commit comments