Skip to content

Commit a4b0638

Browse files
committed
Refactor some TLS tests
1 parent 1c5e998 commit a4b0638

File tree

2 files changed

+40
-40
lines changed

2 files changed

+40
-40
lines changed

tls/src/test/java/org/bouncycastle/tls/test/TlsProtocolHybridTest.java

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import org.bouncycastle.tls.NamedGroup;
99
import org.bouncycastle.tls.TlsClientProtocol;
10+
import org.bouncycastle.tls.TlsServer;
1011
import org.bouncycastle.tls.TlsServerProtocol;
1112
import org.bouncycastle.tls.crypto.TlsCrypto;
1213
import org.bouncycastle.util.Arrays;
@@ -35,7 +36,13 @@ public void testMismatchedGroups() throws Exception
3536
TlsClientProtocol clientProtocol = new TlsClientProtocol(clientRead, clientWrite);
3637
TlsServerProtocol serverProtocol = new TlsServerProtocol(serverRead, serverWrite);
3738

38-
ServerThread serverThread = new ServerThread(crypto, serverProtocol, new int[]{ NamedGroup.X25519MLKEM768 }, true);
39+
MockTlsHybridClient client = new MockTlsHybridClient(crypto, null);
40+
MockTlsHybridServer server = new MockTlsHybridServer(crypto);
41+
42+
client.setNamedGroups(new int[]{ NamedGroup.SecP256r1MLKEM768 });
43+
server.setNamedGroups(new int[]{ NamedGroup.X25519MLKEM768 });
44+
45+
ServerThread serverThread = new ServerThread(serverProtocol, server, true);
3946
try
4047
{
4148
serverThread.start();
@@ -44,8 +51,6 @@ public void testMismatchedGroups() throws Exception
4451
{
4552
}
4653

47-
MockTlsHybridClient client = new MockTlsHybridClient(crypto, null);
48-
client.setNamedGroups(new int[]{ NamedGroup.SecP256r1MLKEM768 });
4954
try
5055
{
5156
clientProtocol.connect(client);
@@ -83,11 +88,14 @@ private void implTestClientServer(int hybridGroup) throws Exception
8388
TlsClientProtocol clientProtocol = new TlsClientProtocol(clientRead, clientWrite);
8489
TlsServerProtocol serverProtocol = new TlsServerProtocol(serverRead, serverWrite);
8590

86-
ServerThread serverThread = new ServerThread(crypto, serverProtocol, new int[]{ hybridGroup }, false);
87-
serverThread.start();
88-
8991
MockTlsHybridClient client = new MockTlsHybridClient(crypto, null);
92+
MockTlsHybridServer server = new MockTlsHybridServer(crypto);
93+
9094
client.setNamedGroups(new int[]{ hybridGroup });
95+
server.setNamedGroups(new int[]{ hybridGroup });
96+
97+
ServerThread serverThread = new ServerThread(serverProtocol, server, false);
98+
serverThread.start();
9199

92100
clientProtocol.connect(client);
93101

@@ -114,36 +122,30 @@ private void implTestClientServer(int hybridGroup) throws Exception
114122
static class ServerThread
115123
extends Thread
116124
{
117-
private final TlsCrypto crypto;
118125
private final TlsServerProtocol serverProtocol;
119-
private final int[] namedGroups;
120-
private boolean shouldFail = false;
126+
private final TlsServer server;
127+
private final boolean shouldFail;
121128

122-
ServerThread(TlsCrypto crypto, TlsServerProtocol serverProtocol, int[] namedGroups, boolean shouldFail)
129+
ServerThread(TlsServerProtocol serverProtocol, TlsServer server, boolean shouldFail)
123130
{
124-
this.crypto = crypto;
125131
this.serverProtocol = serverProtocol;
126-
this.namedGroups = namedGroups;
132+
this.server = server;
127133
this.shouldFail = shouldFail;
128134
}
129135

130136
public void run()
131137
{
132138
try
133139
{
134-
MockTlsHybridServer server = new MockTlsHybridServer(crypto);
135-
if (namedGroups != null)
136-
{
137-
server.setNamedGroups(namedGroups);
138-
}
139-
140140
try
141141
{
142142
serverProtocol.accept(server);
143143
if (shouldFail)
144144
{
145145
fail();
146146
}
147+
148+
Streams.pipeAll(serverProtocol.getInputStream(), serverProtocol.getOutputStream());
147149
}
148150
catch (IOException ignored)
149151
{
@@ -153,12 +155,10 @@ public void run()
153155
}
154156
}
155157

156-
Streams.pipeAll(serverProtocol.getInputStream(), serverProtocol.getOutputStream());
157158
serverProtocol.close();
158159
}
159160
catch (Exception e)
160161
{
161-
// throw new RuntimeException(e);
162162
}
163163
}
164164
}

tls/src/test/java/org/bouncycastle/tls/test/TlsProtocolKemTest.java

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import org.bouncycastle.tls.NamedGroup;
99
import org.bouncycastle.tls.TlsClientProtocol;
10+
import org.bouncycastle.tls.TlsServer;
1011
import org.bouncycastle.tls.TlsServerProtocol;
1112
import org.bouncycastle.tls.crypto.TlsCrypto;
1213
import org.bouncycastle.util.Arrays;
@@ -35,7 +36,13 @@ public void testMismatchedGroups() throws Exception
3536
TlsClientProtocol clientProtocol = new TlsClientProtocol(clientRead, clientWrite);
3637
TlsServerProtocol serverProtocol = new TlsServerProtocol(serverRead, serverWrite);
3738

38-
ServerThread serverThread = new ServerThread(crypto, serverProtocol, new int[]{ NamedGroup.MLKEM768 }, true);
39+
MockTlsKemClient client = new MockTlsKemClient(crypto, null);
40+
MockTlsKemServer server = new MockTlsKemServer(crypto);
41+
42+
client.setNamedGroups(new int[]{ NamedGroup.MLKEM512 });
43+
server.setNamedGroups(new int[]{ NamedGroup.MLKEM768 });
44+
45+
ServerThread serverThread = new ServerThread(serverProtocol, server, true);
3946
try
4047
{
4148
serverThread.start();
@@ -44,8 +51,6 @@ public void testMismatchedGroups() throws Exception
4451
{
4552
}
4653

47-
MockTlsKemClient client = new MockTlsKemClient(crypto, null);
48-
client.setNamedGroups(new int[]{ NamedGroup.MLKEM512 });
4954
try
5055
{
5156
clientProtocol.connect(client);
@@ -83,11 +88,14 @@ private void implTestClientServer(int kemGroup) throws Exception
8388
TlsClientProtocol clientProtocol = new TlsClientProtocol(clientRead, clientWrite);
8489
TlsServerProtocol serverProtocol = new TlsServerProtocol(serverRead, serverWrite);
8590

86-
ServerThread serverThread = new ServerThread(crypto, serverProtocol, new int[]{ kemGroup }, false);
87-
serverThread.start();
88-
8991
MockTlsKemClient client = new MockTlsKemClient(crypto, null);
92+
MockTlsKemServer server = new MockTlsKemServer(crypto);
93+
9094
client.setNamedGroups(new int[]{ kemGroup });
95+
server.setNamedGroups(new int[]{ kemGroup });
96+
97+
ServerThread serverThread = new ServerThread(serverProtocol, server, false);
98+
serverThread.start();
9199

92100
clientProtocol.connect(client);
93101

@@ -114,36 +122,30 @@ private void implTestClientServer(int kemGroup) throws Exception
114122
static class ServerThread
115123
extends Thread
116124
{
117-
private final TlsCrypto crypto;
118125
private final TlsServerProtocol serverProtocol;
119-
private final int[] namedGroups;
120-
private boolean shouldFail = false;
126+
private final TlsServer server;
127+
private final boolean shouldFail;
121128

122-
ServerThread(TlsCrypto crypto, TlsServerProtocol serverProtocol, int[] namedGroups, boolean shouldFail)
129+
ServerThread(TlsServerProtocol serverProtocol, TlsServer server, boolean shouldFail)
123130
{
124-
this.crypto = crypto;
125131
this.serverProtocol = serverProtocol;
126-
this.namedGroups = namedGroups;
132+
this.server = server;
127133
this.shouldFail = shouldFail;
128134
}
129135

130136
public void run()
131137
{
132138
try
133139
{
134-
MockTlsKemServer server = new MockTlsKemServer(crypto);
135-
if (namedGroups != null)
136-
{
137-
server.setNamedGroups(namedGroups);
138-
}
139-
140140
try
141141
{
142142
serverProtocol.accept(server);
143143
if (shouldFail)
144144
{
145145
fail();
146146
}
147+
148+
Streams.pipeAll(serverProtocol.getInputStream(), serverProtocol.getOutputStream());
147149
}
148150
catch (IOException ignored)
149151
{
@@ -153,12 +155,10 @@ public void run()
153155
}
154156
}
155157

156-
Streams.pipeAll(serverProtocol.getInputStream(), serverProtocol.getOutputStream());
157158
serverProtocol.close();
158159
}
159160
catch (Exception e)
160161
{
161-
// throw new RuntimeException(e);
162162
}
163163
}
164164
}

0 commit comments

Comments
 (0)