Keys.kt (3641B)
1 /* 2 * This file is part of LibEuFin. 3 * Copyright (C) 2025 Taler Systems S.A. 4 5 * LibEuFin is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU Affero General Public License as 7 * published by the Free Software Foundation; either version 3, or 8 * (at your option) any later version. 9 10 * LibEuFin is distributed in the hope that it will be useful, but 11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General 13 * Public License for more details. 14 15 * You should have received a copy of the GNU Affero General Public 16 * License along with LibEuFin; see the file COPYING. If not, see 17 * <http://www.gnu.org/licenses/> 18 */ 19 20 import org.junit.Test 21 import tech.libeufin.common.crypto.CryptoUtil 22 import tech.libeufin.common.fmtChunkByTwo 23 import tech.libeufin.ebics.* 24 import kotlin.io.path.Path 25 import kotlin.io.path.deleteIfExists 26 import kotlin.io.path.notExists 27 import kotlin.test.assertEquals 28 import kotlin.test.assertNotNull 29 import kotlin.test.assertNull 30 import kotlin.test.assertTrue 31 32 class PublicKeys { 33 34 // Tests intermittent spaces in public keys fingerprint. 35 @Test 36 fun splitTest() { 37 assertEquals("0099887766".fmtChunkByTwo(), "00 99 88 77 66") // even 38 assertEquals("ZZYYXXWWVVU".fmtChunkByTwo(), "ZZ YY XX WW VV U") // odd 39 } 40 41 // Tests loading the bank public keys from disk. 42 @Test 43 fun loadBankKeys() { 44 // artificially creating the keys. 45 val fileContent = BankPublicKeysFile( 46 accepted = true, 47 bank_authentication_public_key = CryptoUtil.genRSAPublic(2028), 48 bank_encryption_public_key = CryptoUtil.genRSAPublic(2028) 49 ) 50 // storing them on disk. 51 persistBankKeys(fileContent, Path("/tmp/nexus-tests-bank-keys.json")) 52 // loading them and check that values are the same. 53 val fromDisk = loadBankKeys(Path("/tmp/nexus-tests-bank-keys.json")) 54 assertNotNull(fromDisk) 55 assertTrue { 56 fromDisk.accepted && 57 fromDisk.bank_encryption_public_key == fileContent.bank_encryption_public_key && 58 fromDisk.bank_authentication_public_key == fileContent.bank_authentication_public_key 59 } 60 } 61 @Test 62 fun loadNotFound() { 63 assertNull(loadBankKeys(Path("/tmp/highly-unlikely-to-be-found.json"))) 64 } 65 } 66 67 class PrivateKeys { 68 val f = Path("/tmp/nexus-privs-test.json") 69 init { 70 f.deleteIfExists() 71 } 72 73 /** 74 * Tests whether loading keys from disk yields the same 75 * values that were stored to the file. 76 */ 77 @Test 78 fun load() { 79 assert(f.notExists()) 80 val clientKeys = generateNewKeys() 81 persistClientKeys(clientKeys, f) // Artificially storing this to the file. 82 val fromDisk = loadClientKeys(f) // loading it via the tested routine. 83 assertNotNull(fromDisk) 84 // Checking the values from disk match the initial object. 85 assertTrue { 86 clientKeys.authentication_private_key == fromDisk.authentication_private_key && 87 clientKeys.encryption_private_key == fromDisk.encryption_private_key && 88 clientKeys.signature_private_key == fromDisk.signature_private_key && 89 clientKeys.submitted_ini == fromDisk.submitted_ini && 90 clientKeys.submitted_hia == fromDisk.submitted_hia 91 } 92 } 93 94 // Testing failure on file not found. 95 @Test 96 fun loadNotFound() { 97 assertNull(loadClientKeys(Path("/tmp/highly-unlikely-to-be-found.json"))) 98 } 99 }