Fin des tests unitaires

This commit is contained in:
Lucàs
2022-11-04 19:35:38 +01:00
parent 4ca0314703
commit 8fb5eb83e9
4 changed files with 63 additions and 166 deletions
-132
View File
@@ -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
}
}
-19
View File
@@ -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);
}
}
+9 -10
View File
@@ -437,25 +437,24 @@ class DesTest {
@Test
void fonction_S() {
// TODO
Des des = new Des();
for (Des des : FIXTURE) {
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) {
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
+52 -3
View File
@@ -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<TripleDes> 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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, 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));
}
}
}