@@ -11,9 +11,11 @@ package main
11
11
12
12
import (
13
13
"fmt"
14
+ "testing"
15
+ "time"
16
+
14
17
"github.com/ibm-messaging/mq-golang-jms20/mqjms"
15
18
"github.com/stretchr/testify/assert"
16
- "testing"
17
19
)
18
20
19
21
// This test file contains tests that demonstrate how to create TLS connections
@@ -66,6 +68,75 @@ func TestAnonymousTLSConnection(t *testing.T) {
66
68
67
69
}
68
70
71
+ /*
72
+ * Test that we can connect successfully if we provide the correct anonymous
73
+ * ("ony way") TLS configuration.
74
+ */
75
+ func TestTLSAlreadyInitialized (t * testing.T ) {
76
+
77
+ cf , err := mqjms .CreateConnectionFactoryFromDefaultJSONFiles ()
78
+ assert .Nil (t , err )
79
+
80
+ // Override the connection settings to point to the anonymous ("one way") TLS
81
+ // channel (must be configured on the queue manager)
82
+ cf .ChannelName = "TLS.ANON.SVRCONN"
83
+
84
+ // Set the channel settings that tells the client what TLS configuration to use
85
+ // to connect to the queue manager.
86
+ cf .TLSCipherSpec = "TLS_RSA_WITH_AES_128_CBC_SHA256" // ANY_TLS12
87
+ cf .KeyRepository = "./tls-samples/anon-tls" // points to .kdb file
88
+
89
+ // Creates a connection to the queue manager, using defer to close it automatically
90
+ // at the end of the function (if it was created successfully)
91
+ context , errCtx := cf .CreateContext ()
92
+ if context != nil {
93
+ defer context .Close ()
94
+ }
95
+
96
+ if errCtx != nil && (errCtx .GetReason () == "MQRC_UNKNOWN_CHANNEL_NAME" ||
97
+ errCtx .GetReason () == "MQRC_CHANNEL_CONFIG_ERROR" ) {
98
+ // See ./tls-samples/README.md for details on how to configure the required channel.
99
+ fmt .Println ("Skipping TestTLSAlreadyInitialized as required channel is not defined." )
100
+ return
101
+ }
102
+
103
+ if errCtx != nil && errCtx .GetReason () == "MQRC_NOT_AUTHORIZED" {
104
+ // See ./tls-samples/README.md for details on how to configure the required channel.
105
+ fmt .Println ("TLS connection was successfully negotiated, but client was blocked from connecting." )
106
+ // Allow test to fail below.
107
+ }
108
+
109
+ // This connection should have been created successfully.
110
+ assert .Nil (t , errCtx )
111
+
112
+ // Above this is just to create a standard TLS connection to the queue manager.
113
+
114
+ // Now try to set up a connection using different connection parameters
115
+ // We are aiming to trigger an MQRC_SSL_ALREADY_INITIALIZED response, which is a Warning that <also>
116
+ // returns a valid connection.
117
+ cf2 , err2 := mqjms .CreateConnectionFactoryFromDefaultJSONFiles ()
118
+ assert .Nil (t , err2 )
119
+ cf2 .ChannelName = "TLS.ANON.SVRCONN"
120
+ cf2 .TLSCipherSpec = "ANY_TLS12"
121
+ cf2 .TLSClientAuth = mqjms .TLSClientAuth_REQUIRED
122
+ cf2 .CertificateLabel = "SampleClientA" // point to the client certificate
123
+ cf2 .KeyRepository = "./tls-samples/mutual-tls" // points to .kdb file
124
+
125
+ context2 , errCtx2 := cf2 .CreateContext ()
126
+ if context2 != nil {
127
+ defer context2 .Close ()
128
+ }
129
+
130
+ assert .NotNil (t , errCtx2 )
131
+ if errCtx2 != nil {
132
+ assert .Equal (t , "MQRC_SSL_ALREADY_INITIALIZED" , errCtx2 .GetReason ())
133
+ }
134
+
135
+ assert .NotNil (t , context2 ) // should ALSO get a connection back.
136
+ time .Sleep (time .Second )
137
+
138
+ }
139
+
69
140
/*
70
141
* Test that we can connect successfully if we provide the correct mutual
71
142
* TLS configuration.
0 commit comments