-
Notifications
You must be signed in to change notification settings - Fork 19.9k
/
Copy pathDiffieHellmanTest.java
38 lines (31 loc) · 1.86 KB
/
DiffieHellmanTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.thealgorithms.ciphers;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.math.BigInteger;
import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
public class DiffieHellmanTest {
// Test for public value calculation using instance methods
@ParameterizedTest
@MethodSource("provideTestData")
public void testCalculatePublicValue(BigInteger base, BigInteger secret, BigInteger prime, BigInteger publicExpected, BigInteger sharedExpected) {
DiffieHellman dh = new DiffieHellman(base, secret, prime); // Create an instance of DiffieHellman
assertEquals(publicExpected, dh.calculatePublicValue()); // Call instance method
}
// Test for shared secret calculation using instance methods
@ParameterizedTest
@MethodSource("provideTestData")
public void testCalculateSharedSecret(BigInteger base, BigInteger secret, BigInteger prime, BigInteger publicExpected, BigInteger sharedExpected) {
DiffieHellman dh = new DiffieHellman(base, secret, prime); // Create an instance of DiffieHellman
assertEquals(sharedExpected, dh.calculateSharedSecret(publicExpected)); // Call instance method
}
// Provide test data for both public key and shared secret calculation
private static Stream<Arguments> provideTestData() {
return Stream.of(createTestArgs(5, 6, 23, 8, 13), createTestArgs(2, 5, 13, 6, 2));
}
// Helper method for arguments
private static Arguments createTestArgs(long base, long secret, long prime, long publicExpected, long sharedExpected) {
return Arguments.of(BigInteger.valueOf(base), BigInteger.valueOf(secret), BigInteger.valueOf(prime), BigInteger.valueOf(publicExpected), BigInteger.valueOf(sharedExpected));
}
}