Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | SOFTWARE & RESEARCH4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * PROJECT METADATA8 * -----------------------------------------------------------------------------9 * Project : Immo-Ka Android10 * File : app/src/test/kotlin/com/spboucher/immoka/SessionVaultTest.kt11 * Description : garde de la session — chiffrement AES-GCM (même code que12 * sur l'appareil, avec une clé logicielle à la place de13 * celle du Keystore), rien en clair sur le disque, fichier14 * abîmé ou clé perdue.15 * Module : test16 * Repository : https://git.spboucher.ai/immo-ka-android17 * Project Website : https://www.immo-ka.com18 * Created : 2026-11-3019 * Last Modified : 2026-11-3020 *21 * =============================================================================22 * AUTHOR & PROFESSIONAL AFFILIATION23 * =============================================================================24 *25 * Author : Simon-Pierre Boucher26 * Position : Professor of Finance27 * Institution : Université du Québec en Outaouais (UQO)28 * Department : Département des sciences administratives29 * Organization : SP Boucher30 *31 * Website : https://spboucher.ai32 * Git : https://git.spboucher.ai33 *34 * =============================================================================35 * OFFICIAL CONTACT DIRECTORY36 * =============================================================================37 *38 * General : info@spboucher.ai39 * Professional : contact@spboucher.ai40 * Direct : simon@spboucher.ai41 * Research : research@spboucher.ai42 * Development : dev@spboucher.ai43 * Support : support@spboucher.ai44 * Security/Admin : admin@spboucher.ai45 * Notifications : notifications@spboucher.ai46 *47 * Preferred Contact: contact@spboucher.ai48 *49 * =============================================================================50 * COPYRIGHT & INTELLECTUAL PROPERTY51 * =============================================================================52 *53 * Copyright (c) 202654 * Simon-Pierre Boucher55 *56 * All rights reserved where applicable.57 *58 * This file is governed by the applicable project license,59 * copyright notices, institutional agreements and relevant law.60 *61 * Unless expressly authorized by the applicable license or62 * rights holder, copying, redistribution, sublicensing,63 * publication and commercial exploitation are not permitted.64 *65 * Existing third-party rights and notices remain unaffected.66 *67 * =============================================================================68 * CONFIDENTIALITY & SECURITY69 * =============================================================================70 *71 * Classification : Public72 * Distribution : Public repository; use is governed by the project license73 * Security Contact : admin@spboucher.ai74 *75 * Access to this source does not independently grant permission76 * to redistribute or disclose confidential materials.77 *78 * Do not embed credentials, private keys, access tokens,79 * certificates or confidential personal information.80 *81 * =============================================================================82 * LICENSE & MAINTENANCE83 * =============================================================================84 *85 * License : None specified (no license file in this repository)86 * Maintainer : Simon-Pierre Boucher87 * Technical Contact: dev@spboucher.ai88 *89 * =============================================================================90 * END OF SP BOUCHER SOURCE HEADER91 * =============================================================================92 */9394package com.spboucher.immoka9596import com.spboucher.immoka.core.account.AccountUser97import com.spboucher.immoka.core.account.AesGcmCipher98import com.spboucher.immoka.core.account.EncryptedSessionVault99import com.spboucher.immoka.core.account.MemorySessionVault100import com.spboucher.immoka.core.account.StoredSession101import org.junit.Assert.assertEquals102import org.junit.Assert.assertFalse103import org.junit.Assert.assertNotEquals104import org.junit.Assert.assertNull105import org.junit.Assert.assertTrue106import org.junit.Rule107import org.junit.Test108import org.junit.rules.TemporaryFolder109import java.io.File110import javax.crypto.KeyGenerator111import javax.crypto.SecretKey112113class SessionVaultTest {114 @get:Rule val tmp = TemporaryFolder()115 private fun key(): SecretKey = KeyGenerator.getInstance("AES").apply { init(256) }.generateKey()116 private val session = StoredSession("jeton-tres-secret-0123456789abcdef", "https://www.immo-ka.com", AccountUser("u1", "lea@example.org", "Léa", true, 1796000000), 1803776000)117118 private fun contains(haystack: ByteArray, needle: String): Boolean = String(haystack, Charsets.ISO_8859_1).contains(String(needle.toByteArray(Charsets.UTF_8), Charsets.ISO_8859_1))119120 @Test fun `la session est relue telle quelle et rien n'est en clair dans le fichier`() {121 val k = key()122 val f = File(tmp.root, "session.bin")123 EncryptedSessionVault(f, AesGcmCipher { k }).save(session)124 val bytes = f.readBytes()125 assertFalse(contains(bytes, session.token)); assertFalse(contains(bytes, "lea@example.org")); assertFalse(contains(bytes, "token"))126 assertEquals(session, EncryptedSessionVault(f, AesGcmCipher { k }).load())127 assertFalse(File(tmp.root, "session.bin.tmp").exists())128 }129130 @Test fun `deux ecritures de la meme session ne donnent pas le meme fichier`() {131 val k = key()132 val f = File(tmp.root, "session.bin")133 val vault = EncryptedSessionVault(f, AesGcmCipher { k })134 vault.save(session); val a = f.readBytes()135 vault.save(session); val b = f.readBytes()136 assertFalse(a.contentEquals(b))137 assertEquals(session, vault.load())138 }139140 @Test fun `fichier abime ou tronque - pas de session, le fichier est efface`() {141 val k = key()142 val f = File(tmp.root, "session.bin")143 val vault = EncryptedSessionVault(f, AesGcmCipher { k })144 vault.save(session)145 val bytes = f.readBytes()146 bytes[bytes.size - 3] = (bytes[bytes.size - 3] + 1).toByte() // le sceau d'authenticité ne tient plus147 f.writeBytes(bytes)148 assertNull(vault.load()); assertFalse(f.exists())149 f.writeBytes(byteArrayOf(1, 12, 3)); assertNull(vault.load()); assertFalse(f.exists())150 f.writeText("{\"token\":\"en clair\"}"); assertNull(vault.load()); assertFalse(f.exists())151 }152153 @Test fun `cle perdue (autre appareil, Keystore remis a zero) - pas de session`() {154 val f = File(tmp.root, "session.bin")155 EncryptedSessionVault(f, AesGcmCipher { key() }).save(session)156 assertNull(EncryptedSessionVault(f, AesGcmCipher { key() }).load())157 assertFalse(f.exists())158 }159160 @Test fun `chiffrement impossible - rien n'est ecrit, jamais de repli en clair`() {161 val f = File(tmp.root, "session.bin")162 val vault = EncryptedSessionVault(f, AesGcmCipher { error("Keystore indisponible") })163 vault.save(session)164 assertFalse(f.exists()); assertNull(vault.load())165 }166167 @Test fun `effacement et garde en memoire`() {168 val k = key()169 val f = File(tmp.root, "session.bin")170 val vault = EncryptedSessionVault(f, AesGcmCipher { k })171 vault.save(session); assertTrue(f.isFile)172 vault.save(null); assertFalse(f.exists()); assertNull(vault.load())173 val mem = MemorySessionVault()174 assertNull(mem.load()); mem.save(session); assertEquals(session, mem.load()); mem.save(null); assertNull(mem.load())175 assertNotEquals(session, session.copy(token = "autre"))176 }177}178