From 8fb5eb83e9aa09628d4d1ba228acf563f1e83298 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luc=C3=A0s?= Date: Fri, 4 Nov 2022 19:35:38 +0100 Subject: [PATCH] Fin des tests unitaires --- src/cryptography/temp/testDes.java | 132 ----------------------- src/cryptography/temp/testTripleDes.java | 19 ---- test/cryptography/DesTest.java | 23 ++-- test/cryptography/TripleDesTest.java | 55 +++++++++- 4 files changed, 63 insertions(+), 166 deletions(-) delete mode 100644 src/cryptography/temp/testDes.java delete mode 100644 src/cryptography/temp/testTripleDes.java diff --git a/src/cryptography/temp/testDes.java b/src/cryptography/temp/testDes.java deleted file mode 100644 index c7f197c..0000000 --- a/src/cryptography/temp/testDes.java +++ /dev/null @@ -1,132 +0,0 @@ -package cryptography.temp; - -import cryptography.Des; - -import java.util.Arrays; - -public class testDes { - - /** - * @deprecated - */ - public static void testStringToBits() { - System.out.println(Arrays.toString(Des.stringToBits("Bonjour"))); - } - - /** - * @deprecated - */ - public static void testBitsToString() { - System.out.println(Des.bitsToString(Des.stringToBits("Bonjour"))); - } - - public static void testGenerePermutation() { - System.out.println(Arrays.toString(Des.generePermutation(64))); - } - - public static void testPermuation() { - int[] permutation = Des.generePermutation(64); - int[] bloc = new int[permutation.length]; - - for (int i = 0; i < bloc.length; i++) { - bloc[i] = i; - } - System.out.println(Arrays.toString(bloc)); - Des.permutation(permutation, bloc); - System.out.println(Arrays.toString(bloc)); - } - - public static void testInvPermuation() { - int[] permutation = Des.generePermutation(64); - int[] bloc = new int[permutation.length]; - int[] bloc2 = new int[permutation.length]; - for (int i = 0; i < bloc.length; i++) { - bloc[i] = i; - bloc2[i] = i; - } - - System.out.println("Bloc avant permuation: "); - System.out.println(Arrays.toString(bloc)); - - Des.permutation(permutation, bloc); - System.out.println("Bloc apres permuation: "); - System.out.println(Arrays.toString(bloc)); - - Des.invPermutation(permutation, bloc); - System.out.println("Bloc après Inv-permuation: "); - System.out.println(Arrays.toString(bloc)); - - System.out.println(Arrays.equals(bloc, bloc2)); - } - - public static void testDecoupage() { - System.out.println(Arrays.deepToString(Des.decoupage(new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19}, 7))); - } - - public static void testRecollage() { - int[][] decoupage = Des.decoupage(new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19}, 7); - - System.out.println(Arrays.deepToString(decoupage)); - assert decoupage != null; - System.out.println(Arrays.toString(Des.recollageBloc(decoupage))); - } - - public static void testDecaleGauche() { - System.out.println(Arrays.toString(Des.decaleGauche(new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19}, 4))); - } - - /** - * @deprecated - */ - public static void testXor() { - System.out.println(Arrays.toString(Des.xor(new int[]{1, 1, 0, 0}, new int[]{1, 0, 1, 0}))); - } - - public static void testGenereCle() { - Des a = new Des(); - a.genereCle(1); - System.out.println(Arrays.toString(a.table_cles.get(0))); - } - - public static void testFonctionS() { - //System.out.println(Arrays.toString(Des.fonction_S(new int[]{1, 1, 1, 1, 1, 1}))); - } - - public static void testFonctionF() { - //Des des = new Des(); - //System.out.println(Arrays.toString(des.fonction_F())); - } - - private static void testCrypte() { - Des des = new Des(); - int[] msg = des.crypte("Bonjour"); - System.out.println("en bit : " + Arrays.toString(msg)); - System.out.println("en string :" + Des.bitsToString(msg)); - } - - private static void testDecrypte() { - Des des = new Des(); - int[] msg = des.crypte("Bonjour, je suis super heureux de vous voir"); - System.out.println("Message crypté : " + Des.bitsToString(msg)); - System.out.println(des.decrypte(msg)); - } - - public static void main(String[] args) { -// testStringToBits(); //Il marche -// testBitsToString();//Il marche -// testGenerePermutation();//Il marche -// testPermuation();//Il marche -// testInvPermuation();//Il marche -// testDecoupage();//Il marche (même avec un nombre de bloc qui divise pas la taille du bloc de base) -// testRecollage();//Il marche -// testDecaleGauche();//Il marche -// testXor();//Il marche -// testGenereCle();// Aucune idée, ça créé une clé random mais je sais pas si c'est comme ça -// testFonctionS();// corrigé -// testFonctionF(); // il a l'air bon -// testCrypte(); //Il a l'air bon (si on regarde decoupe[0]et [1] à chaque itérations on voit que c'est cohérent) - testDecrypte(); // Un truc est normal c'est que jusqu'à la deniere itération sur les 16 , - // les clés sont randoms jusqu'à la dernière itération ou elle est tout le temps la meme - } - -} diff --git a/src/cryptography/temp/testTripleDes.java b/src/cryptography/temp/testTripleDes.java deleted file mode 100644 index 69d4f98..0000000 --- a/src/cryptography/temp/testTripleDes.java +++ /dev/null @@ -1,19 +0,0 @@ -package cryptography.temp; - -import cryptography.Des; -import cryptography.TripleDes; - -public class testTripleDes { - - public static void main(String[] args) { - TripleDes tripleDes = new TripleDes(); - - String message = "Hello"; - - int[] messageCrypte = tripleDes.crypte(message); - String messageDecrypte = tripleDes.decrypte(messageCrypte); - - System.out.printf("\"%s\" -> \"%s\" -> \"%s\"", message, Des.bitsToString(messageCrypte), messageDecrypte); - } - -} diff --git a/test/cryptography/DesTest.java b/test/cryptography/DesTest.java index 70e29bf..d6483c5 100644 --- a/test/cryptography/DesTest.java +++ b/test/cryptography/DesTest.java @@ -437,25 +437,24 @@ class DesTest { @Test void fonction_S() { - // TODO - Des des = new Des(); + for (Des des : FIXTURE) { + des.genereS(0); - des.genereS(0); - int resultat_attendu_int = des.tableS.get(3).get(9); + String coordonneeStr = Integer.toString(des.table_S.get(0)[3][9], 2); + int resultat_attendu_int = Integer.parseInt(coordonneeStr); - int[] resultat_attendu = new int[4]; - for (int i = 0; i < resultat_attendu.length; i++, resultat_attendu_int /= 10) { - resultat_attendu[resultat_attendu.length - i - 1] = resultat_attendu_int % 10; + int[] resultat_attendu = new int[4]; + for (int i = 0; i < resultat_attendu.length; i++, resultat_attendu_int /= 10) + resultat_attendu[resultat_attendu.length - i - 1] = resultat_attendu_int % 10; + + assertArrayEquals(resultat_attendu, des.fonction_S(new int[]{1, 1, 0, 0, 1, 1}, 0)); } - - - assertArrayEquals(resultat_attendu, des.fonction_S(new int[] {1,1,0,0,1,1})); - } @Test void fonction_F() { - // System.out.println(Arrays.toString(des.fonction_F(1, new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17}))); + // Impossible à tester, dépend fortement du Random +// System.out.println(Arrays.toString(des.fonction_F(1, new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17}))); } @Test diff --git a/test/cryptography/TripleDesTest.java b/test/cryptography/TripleDesTest.java index a99bc52..aea872f 100644 --- a/test/cryptography/TripleDesTest.java +++ b/test/cryptography/TripleDesTest.java @@ -1,14 +1,63 @@ package cryptography; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import java.util.ArrayList; + +import static org.junit.jupiter.api.Assertions.assertEquals; + class TripleDesTest { - @Test - void crypte() { + private final ArrayList FIXTURE = new ArrayList<>(); + + @BeforeEach + public void init() { + int[][][] MASTER_KEYS = + {{{ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + }, { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + }, { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + }}, {{ + 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, + 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, + 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, + 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, + }, { + 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, + 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, + 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, + 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, + }, { + 1, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, + 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, + 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, + 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, + }} + }; + + for (int[][] masterKeys : MASTER_KEYS) FIXTURE.add(new TripleDes(masterKeys)); } @Test - void decrypte() { + @DisplayName("crypte() & decrypte()") + void crypteDecrypte() { + for (TripleDes tripleDes : FIXTURE) { + String message = "Hello World!"; + int[] messageCrypte = tripleDes.crypte(message); + assertEquals(message, tripleDes.decrypte(messageCrypte)); + } } } \ No newline at end of file