This repository has been archived by the owner on Jun 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 102
Eliminate double initialization of SSLSession in GelfTCPSSLSender #182
Closed
alexander-katanov
wants to merge
3
commits into
mp911de:master
from
alexander-katanov:double_initialization_of_sslsession
Closed
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
...t/java/biz/paluch/logging/gelf/intern/sender/GelfTCPSSLSenderConnectIntegrationTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package biz.paluch.logging.gelf.intern.sender; | ||
|
||
import biz.paluch.logging.gelf.intern.ErrorReporter; | ||
import biz.paluch.logging.gelf.intern.GelfMessage; | ||
import biz.paluch.logging.gelf.netty.NettyLocalServer; | ||
import io.netty.channel.Channel; | ||
import io.netty.channel.ChannelInitializer; | ||
import io.netty.channel.socket.nio.NioServerSocketChannel; | ||
import io.netty.handler.ssl.SslContext; | ||
import io.netty.handler.ssl.SslContextBuilder; | ||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import javax.net.ssl.KeyManager; | ||
import javax.net.ssl.KeyManagerFactory; | ||
import javax.net.ssl.SSLContext; | ||
import javax.net.ssl.TrustManagerFactory; | ||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.security.KeyStore; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.jupiter.api.Assumptions.assumeTrue; | ||
|
||
public class GelfTCPSSLSenderConnectIntegrationTests { | ||
|
||
private static NettyLocalServer server = new NettyLocalServer(NioServerSocketChannel.class); | ||
private static SSLContext sslContext; | ||
|
||
@BeforeAll | ||
public static void setupClass() throws Exception { | ||
|
||
File file = new File("work/keystore.jks"); | ||
assumeTrue(file.exists()); | ||
|
||
KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); | ||
keyStore.load(new FileInputStream(file), "changeit".toCharArray()); | ||
KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); | ||
kmf.init(keyStore, "changeit".toCharArray()); | ||
|
||
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); | ||
tmf.init(keyStore); | ||
|
||
final SslContext sslContext = SslContextBuilder.forServer(kmf).build(); | ||
|
||
GelfTCPSSLSenderConnectIntegrationTests.sslContext = SSLContext.getInstance("TLSv1.2"); | ||
GelfTCPSSLSenderConnectIntegrationTests.sslContext.init(new KeyManager[0], tmf.getTrustManagers(), null); | ||
|
||
server.run(new ChannelInitializer<Channel>() { | ||
@Override | ||
protected void initChannel(Channel ch) throws Exception { | ||
|
||
ch.pipeline().addLast(sslContext.newHandler(ch.alloc())); | ||
ch.pipeline().addLast(server.getHandler()); | ||
} | ||
}); | ||
} | ||
|
||
@Test | ||
public void shouldSendTCPMessagesViaSsl() throws Exception { | ||
|
||
final GelfTCPSSLSender tcpsslSender = new GelfTCPSSLSender("localhost", server.getPort(), 1000, 1000, 1, true, | ||
new ErrorReporter() { | ||
@Override | ||
public void reportError(String message, Exception e) { | ||
System.out.println(Thread.currentThread() + " " + message); | ||
if (e != null) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
}, sslContext); | ||
|
||
Thread thread1 = new Thread(new Runnable() { | ||
@Override | ||
public void run() { | ||
GelfMessage gelfMessage = new GelfMessage("short1", "long1", 1, "info"); | ||
gelfMessage.setHost("host"); | ||
tcpsslSender.sendMessage(gelfMessage); | ||
} | ||
}); | ||
|
||
Thread thread2 = new Thread(new Runnable() { | ||
@Override | ||
public void run() { | ||
GelfMessage gelfMessage = new GelfMessage("short2", "long2", 1, "info"); | ||
gelfMessage.setHost("host"); | ||
tcpsslSender.sendMessage(gelfMessage); | ||
} | ||
}); | ||
|
||
thread2.start(); | ||
thread1.start(); | ||
|
||
thread1.join(); | ||
thread2.join(); | ||
|
||
for (int i = 0; i < 100; i++) { | ||
if (!server.getJsonValues().isEmpty()) { | ||
continue; | ||
|
||
} | ||
Thread.sleep(100); | ||
} | ||
|
||
assertThat(server.getJsonValues()).isNotEmpty(); | ||
assertThat(server.getJsonValues()).hasSize(2); | ||
|
||
tcpsslSender.close(); | ||
} | ||
|
||
@AfterAll | ||
public static void afterClass() throws Exception { | ||
server.close(); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This check is superfluous as
super.connect()
does the same check. We can safely remove thisif
block here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
agreed