| 1 |
package mqtt |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"crypto/tls" |
| 6 |
"errors" |
| 7 |
"net" |
| 8 |
"net/url" |
| 9 |
"sync" |
| 10 |
|
| 11 |
"github.com/eclipse/paho.golang/autopaho" |
| 12 |
|
| 13 |
ginkgo "github.com/onsi/ginkgo/v2" |
| 14 |
gomega "github.com/onsi/gomega" |
| 15 |
|
| 16 |
"github.com/nicholas-fedor/shoutrrr/pkg/services/standard" |
| 17 |
"github.com/nicholas-fedor/shoutrrr/pkg/types" |
| 18 |
) |
| 19 |
|
| 20 |
var _ = ginkgo.Describe("Service", func() { |
| 21 |
var ( |
| 22 |
service *Service |
| 23 |
testURL *url.URL |
| 24 |
logger types.StdLogger |
| 25 |
) |
| 26 |
|
| 27 |
ginkgo.BeforeEach(func() { |
| 28 |
service = &Service{} |
| 29 |
logger = ginkgo.GinkgoT() |
| 30 |
}) |
| 31 |
|
| 32 |
ginkgo.Describe("GetID", func() { |
| 33 |
ginkgo.It("should return 'mqtt'", func() { |
| 34 |
gomega.Expect(service.GetID()).To(gomega.Equal("mqtt")) |
| 35 |
}) |
| 36 |
|
| 37 |
ginkgo.It("should return consistent ID across multiple calls", func() { |
| 38 |
id1 := service.GetID() |
| 39 |
id2 := service.GetID() |
| 40 |
gomega.Expect(id1).To(gomega.Equal(id2)) |
| 41 |
gomega.Expect(id1).To(gomega.Equal("mqtt")) |
| 42 |
}) |
| 43 |
}) |
| 44 |
|
| 45 |
ginkgo.Describe("getDefaultPortForScheme", func() { |
| 46 |
ginkgo.It("should return 1883 for mqtt scheme", func() { |
| 47 |
gomega.Expect(service.getDefaultPortForScheme("mqtt")).To(gomega.Equal(1883)) |
| 48 |
}) |
| 49 |
|
| 50 |
ginkgo.It("should return 8883 for mqtts scheme", func() { |
| 51 |
gomega.Expect(service.getDefaultPortForScheme("mqtts")).To(gomega.Equal(8883)) |
| 52 |
}) |
| 53 |
|
| 54 |
ginkgo.It("should return 1883 for unknown scheme", func() { |
| 55 |
gomega.Expect(service.getDefaultPortForScheme("unknown")).To(gomega.Equal(1883)) |
| 56 |
}) |
| 57 |
|
| 58 |
ginkgo.It("should return 1883 for empty scheme", func() { |
| 59 |
gomega.Expect(service.getDefaultPortForScheme("")).To(gomega.Equal(1883)) |
| 60 |
}) |
| 61 |
|
| 62 |
ginkgo.It("should return 1883 for http scheme (treated as non-secure)", func() { |
| 63 |
gomega.Expect(service.getDefaultPortForScheme("http")).To(gomega.Equal(1883)) |
| 64 |
}) |
| 65 |
|
| 66 |
ginkgo.It("should be case-sensitive for mqtts scheme", func() { |
| 67 |
// "MQTTS" is not the same as "mqtts", so it returns default |
| 68 |
gomega.Expect(service.getDefaultPortForScheme("MQTTS")).To(gomega.Equal(1883)) |
| 69 |
}) |
| 70 |
}) |
| 71 |
|
| 72 |
ginkgo.Describe("Initialize", func() { |
| 73 |
ginkgo.BeforeEach(func() { |
| 74 |
var err error |
| 75 |
|
| 76 |
testURL, err = url.Parse("mqtt://broker.example.com:1883/notifications/alerts") |
| 77 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 78 |
}) |
| 79 |
|
| 80 |
ginkgo.It("should parse valid URL successfully", func() { |
| 81 |
err := service.Initialize(testURL, logger) |
| 82 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 83 |
gomega.Expect(service.Config).NotTo(gomega.BeNil()) |
| 84 |
}) |
| 85 |
|
| 86 |
ginkgo.It("should set default port for mqtt scheme when port specified in URL", func() { |
| 87 |
mqttURL, err := url.Parse("mqtt://broker.example.com:1883/test/topic") |
| 88 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 89 |
|
| 90 |
err = service.Initialize(mqttURL, logger) |
| 91 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 92 |
gomega.Expect(service.Config.Port).To(gomega.Equal(1883)) |
| 93 |
}) |
| 94 |
|
| 95 |
ginkgo.It("should set default port for mqtts scheme when port specified in URL", func() { |
| 96 |
mqttsURL, err := url.Parse("mqtts://broker.example.com:8883/test/topic") |
| 97 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 98 |
|
| 99 |
err = service.Initialize(mqttsURL, logger) |
| 100 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 101 |
gomega.Expect(service.Config.Port).To(gomega.Equal(8883)) |
| 102 |
}) |
| 103 |
|
| 104 |
ginkgo.It("should parse query parameters", func() { |
| 105 |
serviceURL, err := url.Parse( |
| 106 |
"mqtt://broker.example.com:1883/test/topic?clientid=myclient&qos=1&retained=yes", |
| 107 |
) |
| 108 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 109 |
|
| 110 |
err = service.Initialize(serviceURL, logger) |
| 111 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 112 |
gomega.Expect(service.Config.ClientID).To(gomega.Equal("myclient")) |
| 113 |
gomega.Expect(service.Config.QoS).To(gomega.Equal(QoSAtLeastOnce)) |
| 114 |
gomega.Expect(service.Config.Retained).To(gomega.BeTrue()) |
| 115 |
}) |
| 116 |
|
| 117 |
ginkgo.It("should store config correctly", func() { |
| 118 |
serviceURL, err := url.Parse( |
| 119 |
"mqtt://user:[email protected]:1883/notifications/alerts", |
| 120 |
) |
| 121 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 122 |
|
| 123 |
err = service.Initialize(serviceURL, logger) |
| 124 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 125 |
gomega.Expect(service.Config.Host).To(gomega.Equal("broker.example.com")) |
| 126 |
gomega.Expect(service.Config.Port).To(gomega.Equal(1883)) |
| 127 |
gomega.Expect(service.Config.Topic).To(gomega.Equal("notifications/alerts")) |
| 128 |
gomega.Expect(service.Config.Username).To(gomega.Equal("user")) |
| 129 |
gomega.Expect(service.Config.Password).To(gomega.Equal("pass")) |
| 130 |
}) |
| 131 |
|
| 132 |
ginkgo.It("should set logger", func() { |
| 133 |
err := service.Initialize(testURL, logger) |
| 134 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 135 |
// Logger is set via SetLogger, we verify no error occurred |
| 136 |
}) |
| 137 |
|
| 138 |
ginkgo.It("should parse URL with username only", func() { |
| 139 |
serviceURL, err := url.Parse("mqtt://[email protected]:1883/test/topic") |
| 140 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 141 |
|
| 142 |
err = service.Initialize(serviceURL, logger) |
| 143 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 144 |
gomega.Expect(service.Config.Username).To(gomega.Equal("user")) |
| 145 |
gomega.Expect(service.Config.Password).To(gomega.BeEmpty()) |
| 146 |
}) |
| 147 |
|
| 148 |
ginkgo.It("should parse URL without credentials", func() { |
| 149 |
serviceURL, err := url.Parse("mqtt://broker.example.com:1883/test/topic") |
| 150 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 151 |
|
| 152 |
err = service.Initialize(serviceURL, logger) |
| 153 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 154 |
gomega.Expect(service.Config.Username).To(gomega.BeEmpty()) |
| 155 |
gomega.Expect(service.Config.Password).To(gomega.BeEmpty()) |
| 156 |
}) |
| 157 |
|
| 158 |
ginkgo.It("should parse URL with custom port", func() { |
| 159 |
serviceURL, err := url.Parse("mqtt://broker.example.com:9999/test/topic") |
| 160 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 161 |
|
| 162 |
err = service.Initialize(serviceURL, logger) |
| 163 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 164 |
gomega.Expect(service.Config.Port).To(gomega.Equal(9999)) |
| 165 |
}) |
| 166 |
|
| 167 |
ginkgo.It("should apply default values from struct tags", func() { |
| 168 |
serviceURL, err := url.Parse("mqtt://broker.example.com:1883/test/topic") |
| 169 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 170 |
|
| 171 |
err = service.Initialize(serviceURL, logger) |
| 172 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 173 |
gomega.Expect(service.Config.ClientID).To(gomega.Equal("shoutrrr")) |
| 174 |
gomega.Expect(service.Config.QoS).To(gomega.Equal(QoSAtMostOnce)) |
| 175 |
gomega.Expect(service.Config.Retained).To(gomega.BeFalse()) |
| 176 |
gomega.Expect(service.Config.CleanSession).To(gomega.BeTrue()) |
| 177 |
}) |
| 178 |
|
| 179 |
ginkgo.It("should parse URL with DisableTLS parameter", func() { |
| 180 |
serviceURL, err := url.Parse("mqtts://broker.example.com:8883/test/topic?disabletls=yes") |
| 181 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 182 |
|
| 183 |
err = service.Initialize(serviceURL, logger) |
| 184 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 185 |
gomega.Expect(service.Config.DisableTLS).To(gomega.BeTrue()) |
| 186 |
}) |
| 187 |
|
| 188 |
ginkgo.It("should parse URL with DisableTLSVerification parameter", func() { |
| 189 |
serviceURL, err := url.Parse( |
| 190 |
"mqtts://broker.example.com:8883/test/topic?disabletlsverification=yes", |
| 191 |
) |
| 192 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 193 |
|
| 194 |
err = service.Initialize(serviceURL, logger) |
| 195 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 196 |
gomega.Expect(service.Config.DisableTLSVerification).To(gomega.BeTrue()) |
| 197 |
}) |
| 198 |
|
| 199 |
ginkgo.It("should parse URL with CleanSession=false", func() { |
| 200 |
serviceURL, err := url.Parse("mqtt://broker.example.com:1883/test/topic?cleansession=no") |
| 201 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 202 |
|
| 203 |
err = service.Initialize(serviceURL, logger) |
| 204 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 205 |
gomega.Expect(service.Config.CleanSession).To(gomega.BeFalse()) |
| 206 |
}) |
| 207 |
|
| 208 |
ginkgo.It("should handle deeply nested topic paths", func() { |
| 209 |
serviceURL, err := url.Parse( |
| 210 |
"mqtt://broker.example.com:1883/level1/level2/level3/level4/level5", |
| 211 |
) |
| 212 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 213 |
|
| 214 |
err = service.Initialize(serviceURL, logger) |
| 215 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 216 |
gomega.Expect(service.Config.Topic). |
| 217 |
To(gomega.Equal("level1/level2/level3/level4/level5")) |
| 218 |
}) |
| 219 |
|
| 220 |
ginkgo.It("should initialize pkr field", func() { |
| 221 |
err := service.Initialize(testURL, logger) |
| 222 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 223 |
// pkr is unexported, but we can verify it works via the config |
| 224 |
gomega.Expect(service.Config).NotTo(gomega.BeNil()) |
| 225 |
}) |
| 226 |
}) |
| 227 |
|
| 228 |
ginkgo.Describe("getCancel", func() { |
| 229 |
ginkgo.It("should return valid cancel function", func() { |
| 230 |
cancel := service.getCancel() |
| 231 |
gomega.Expect(cancel).NotTo(gomega.BeNil()) |
| 232 |
}) |
| 233 |
|
| 234 |
ginkgo.It("should initialize context field", func() { |
| 235 |
service.getCancel() |
| 236 |
// ctx is unexported, but we verify it was set by checking cancel is not nil |
| 237 |
gomega.Expect(service.cancel).NotTo(gomega.BeNil()) |
| 238 |
}) |
| 239 |
|
| 240 |
ginkgo.It( |
| 241 |
"should be thread-safe and return same cancel function on multiple calls", |
| 242 |
func() { |
| 243 |
var wg sync.WaitGroup |
| 244 |
|
| 245 |
cancels := make([]context.CancelFunc, 10) |
| 246 |
|
| 247 |
for i := range 10 { |
| 248 |
wg.Add(1) |
| 249 |
|
| 250 |
go func(idx int) { |
| 251 |
defer wg.Done() |
| 252 |
|
| 253 |
cancels[idx] = service.getCancel() |
| 254 |
}(i) |
| 255 |
} |
| 256 |
|
| 257 |
wg.Wait() |
| 258 |
|
| 259 |
// All cancel functions should be non-nil |
| 260 |
for i := range 10 { |
| 261 |
gomega.Expect(cancels[i]).NotTo(gomega.BeNil()) |
| 262 |
} |
| 263 |
}, |
| 264 |
) |
| 265 |
|
| 266 |
ginkgo.It("should allow cancel to be called without panic", func() { |
| 267 |
cancel := service.getCancel() |
| 268 |
|
| 269 |
gomega.Expect(func() { cancel() }).NotTo(gomega.Panic()) |
| 270 |
}) |
| 271 |
|
| 272 |
ginkgo.It("should allow cancel to be called multiple times safely", func() { |
| 273 |
cancel := service.getCancel() |
| 274 |
cancel() |
| 275 |
gomega.Expect(func() { cancel() }).NotTo(gomega.Panic()) |
| 276 |
}) |
| 277 |
}) |
| 278 |
|
| 279 |
ginkgo.Describe("createTLSConfig", func() { |
| 280 |
ginkgo.BeforeEach(func() { |
| 281 |
// Initialize the standard logger to avoid nil pointer dereference |
| 282 |
service.Standard = standard.Standard{} |
| 283 |
service.SetLogger(logger) |
| 284 |
service.Config = &Config{} |
| 285 |
}) |
| 286 |
|
| 287 |
ginkgo.It("should create TLS config with verification enabled by default", func() { |
| 288 |
service.Config.DisableTLSVerification = false |
| 289 |
|
| 290 |
tlsConfig := service.createTLSConfig() |
| 291 |
|
| 292 |
gomega.Expect(tlsConfig).NotTo(gomega.BeNil()) |
| 293 |
gomega.Expect(tlsConfig.InsecureSkipVerify).To(gomega.BeFalse()) |
| 294 |
}) |
| 295 |
|
| 296 |
ginkgo.It("should create TLS config without verification when disabled", func() { |
| 297 |
service.Config.DisableTLSVerification = true |
| 298 |
|
| 299 |
tlsConfig := service.createTLSConfig() |
| 300 |
|
| 301 |
gomega.Expect(tlsConfig).NotTo(gomega.BeNil()) |
| 302 |
gomega.Expect(tlsConfig.InsecureSkipVerify).To(gomega.BeTrue()) |
| 303 |
}) |
| 304 |
|
| 305 |
ginkgo.It("should set minimum TLS version to 1.2", func() { |
| 306 |
tlsConfig := service.createTLSConfig() |
| 307 |
|
| 308 |
gomega.Expect(tlsConfig).NotTo(gomega.BeNil()) |
| 309 |
gomega.Expect(tlsConfig.MinVersion).To(gomega.Equal(uint16(tls.VersionTLS12))) |
| 310 |
}) |
| 311 |
|
| 312 |
ginkgo.It("should return config even with nil Config.DisableTLSVerification", func() { |
| 313 |
// DisableTLSVerification defaults to false |
| 314 |
tlsConfig := service.createTLSConfig() |
| 315 |
|
| 316 |
gomega.Expect(tlsConfig).NotTo(gomega.BeNil()) |
| 317 |
}) |
| 318 |
|
| 319 |
ginkgo.It("should create independent TLS configs on multiple calls", func() { |
| 320 |
tlsConfig1 := service.createTLSConfig() |
| 321 |
tlsConfig2 := service.createTLSConfig() |
| 322 |
|
| 323 |
// Each call creates a new config |
| 324 |
gomega.Expect(tlsConfig1).NotTo(gomega.BeIdenticalTo(tlsConfig2)) |
| 325 |
}) |
| 326 |
}) |
| 327 |
|
| 328 |
ginkgo.Describe("Close", func() { |
| 329 |
ginkgo.BeforeEach(func() { |
| 330 |
// Initialize the standard logger to avoid nil pointer dereference |
| 331 |
service.Standard = standard.Standard{} |
| 332 |
service.SetLogger(logger) |
| 333 |
}) |
| 334 |
|
| 335 |
ginkgo.It("should handle nil connection manager gracefully", func() { |
| 336 |
service.connectionManager = nil |
| 337 |
service.closeOnce = sync.Once{} // Reset for this test |
| 338 |
|
| 339 |
err := service.Close() |
| 340 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 341 |
}) |
| 342 |
|
| 343 |
ginkgo.It("should be idempotent - safe to call multiple times", func() { |
| 344 |
service.connectionManager = nil |
| 345 |
service.closeOnce = sync.Once{} // Reset |
| 346 |
|
| 347 |
err1 := service.Close() |
| 348 |
err2 := service.Close() |
| 349 |
err3 := service.Close() |
| 350 |
|
| 351 |
gomega.Expect(err1).NotTo(gomega.HaveOccurred()) |
| 352 |
gomega.Expect(err2).NotTo(gomega.HaveOccurred()) |
| 353 |
gomega.Expect(err3).NotTo(gomega.HaveOccurred()) |
| 354 |
}) |
| 355 |
|
| 356 |
ginkgo.It("should return same error on subsequent calls", func() { |
| 357 |
service.connectionManager = nil |
| 358 |
service.closeOnce = sync.Once{} // Reset |
| 359 |
service.closeErr = nil // Reset |
| 360 |
|
| 361 |
// First close |
| 362 |
_ = service.Close() |
| 363 |
|
| 364 |
// Subsequent closes should return the same result (nil) |
| 365 |
err1 := service.Close() |
| 366 |
err2 := service.Close() |
| 367 |
|
| 368 |
gomega.Expect(err1).ToNot(gomega.HaveOccurred()) |
| 369 |
gomega.Expect(err2).ToNot(gomega.HaveOccurred()) |
| 370 |
}) |
| 371 |
|
| 372 |
ginkgo.It("should handle close without initialized cancel function", func() { |
| 373 |
service.connectionManager = nil |
| 374 |
service.cancel = nil |
| 375 |
service.closeOnce = sync.Once{} // Reset |
| 376 |
|
| 377 |
err := service.Close() |
| 378 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 379 |
}) |
| 380 |
|
| 381 |
ginkgo.It("should cancel context when cancel function exists", func() { |
| 382 |
service.connectionManager = nil |
| 383 |
service.closeOnce = sync.Once{} // Reset |
| 384 |
|
| 385 |
// Initialize the cancel function |
| 386 |
_ = service.getCancel() |
| 387 |
gomega.Expect(service.cancel).NotTo(gomega.BeNil()) |
| 388 |
|
| 389 |
// Close should call cancel without error |
| 390 |
err := service.Close() |
| 391 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 392 |
}) |
| 393 |
}) |
| 394 |
|
| 395 |
ginkgo.Describe("Service Constants", func() { |
| 396 |
ginkgo.It("should have correct Scheme constant", func() { |
| 397 |
gomega.Expect(Scheme).To(gomega.Equal("mqtt")) |
| 398 |
}) |
| 399 |
|
| 400 |
ginkgo.It("should have correct SecureScheme constant", func() { |
| 401 |
gomega.Expect(SchemeTLS).To(gomega.Equal("mqtts")) |
| 402 |
}) |
| 403 |
}) |
| 404 |
|
| 405 |
ginkgo.Describe("Service Struct", func() { |
| 406 |
ginkgo.It("should initialize with zero values", func() { |
| 407 |
svc := &Service{} |
| 408 |
gomega.Expect(svc.Config).To(gomega.BeNil()) |
| 409 |
gomega.Expect(svc.connectionManager).To(gomega.BeNil()) |
| 410 |
gomega.Expect(svc.connectionInitialized).To(gomega.BeFalse()) |
| 411 |
}) |
| 412 |
|
| 413 |
ginkgo.It("should have correct default state after initialization", func() { |
| 414 |
svc := &Service{} |
| 415 |
serviceURL, err := url.Parse("mqtt://broker.example.com:1883/test/topic") |
| 416 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 417 |
|
| 418 |
err = svc.Initialize(serviceURL, logger) |
| 419 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 420 |
gomega.Expect(svc.Config).NotTo(gomega.BeNil()) |
| 421 |
gomega.Expect(svc.connectionInitialized).To(gomega.BeFalse()) |
| 422 |
gomega.Expect(svc.connectionManager).To(gomega.BeNil()) |
| 423 |
}) |
| 424 |
}) |
| 425 |
|
| 426 |
ginkgo.Describe("SetDialContext", func() { |
| 427 |
ginkgo.It("should satisfy DialContextSetter", func() { |
| 428 |
var setter types.DialContextSetter = service |
| 429 |
gomega.Expect(setter).NotTo(gomega.BeNil()) |
| 430 |
}) |
| 431 |
|
| 432 |
ginkgo.It("should store the custom dial function", func() { |
| 433 |
dial := func(_ context.Context, _, _ string) (net.Conn, error) { |
| 434 |
return nil, errors.New("unused") |
| 435 |
} |
| 436 |
service.SetDialContext(dial) |
| 437 |
gomega.Expect(service.dialContext).NotTo(gomega.BeNil()) |
| 438 |
}) |
| 439 |
}) |
| 440 |
|
| 441 |
ginkgo.Describe("attemptConnection", func() { |
| 442 |
ginkgo.It("should error when no custom dialer is configured", func() { |
| 443 |
brokerURL, err := url.Parse("mqtt://broker.example.com:1883") |
| 444 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 445 |
|
| 446 |
_, err = service.attemptConnection( |
| 447 |
context.Background(), |
| 448 |
autopaho.ClientConfig{}, |
| 449 |
brokerURL, |
| 450 |
) |
| 451 |
gomega.Expect(err).To(gomega.MatchError(ErrNoDialContext)) |
| 452 |
}) |
| 453 |
|
| 454 |
ginkgo.It("should dial tcp at the broker host", func() { |
| 455 |
blocked := errors.New("destination blocked") |
| 456 |
|
| 457 |
var gotNetwork, gotAddr string |
| 458 |
|
| 459 |
service.SetDialContext(func(_ context.Context, network, addr string) (net.Conn, error) { |
| 460 |
gotNetwork = network |
| 461 |
gotAddr = addr |
| 462 |
|
| 463 |
return nil, blocked |
| 464 |
}) |
| 465 |
|
| 466 |
brokerURL, err := url.Parse("mqtt://broker.example.com:1883") |
| 467 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 468 |
|
| 469 |
_, err = service.attemptConnection( |
| 470 |
context.Background(), |
| 471 |
autopaho.ClientConfig{}, |
| 472 |
brokerURL, |
| 473 |
) |
| 474 |
gomega.Expect(err).To(gomega.MatchError(blocked)) |
| 475 |
gomega.Expect(gotNetwork).To(gomega.Equal("tcp")) |
| 476 |
gomega.Expect(gotAddr).To(gomega.Equal("broker.example.com:1883")) |
| 477 |
}) |
| 478 |
|
| 479 |
ginkgo.It("should return a plain connection when TLS is not configured", func() { |
| 480 |
listener, err := net.Listen("tcp", "127.0.0.1:0") |
| 481 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 482 |
|
| 483 |
defer listener.Close() |
| 484 |
|
| 485 |
accepted := make(chan net.Conn, 1) |
| 486 |
|
| 487 |
go func() { |
| 488 |
conn, acceptErr := listener.Accept() |
| 489 |
if acceptErr != nil { |
| 490 |
return |
| 491 |
} |
| 492 |
|
| 493 |
accepted <- conn |
| 494 |
}() |
| 495 |
|
| 496 |
service.SetDialContext((&net.Dialer{}).DialContext) |
| 497 |
|
| 498 |
brokerURL, err := url.Parse("mqtt://" + listener.Addr().String()) |
| 499 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 500 |
|
| 501 |
conn, err := service.attemptConnection( |
| 502 |
context.Background(), |
| 503 |
autopaho.ClientConfig{}, |
| 504 |
brokerURL, |
| 505 |
) |
| 506 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 507 |
gomega.Expect(conn).NotTo(gomega.BeNil()) |
| 508 |
gomega.Expect(conn.Close()).To(gomega.Succeed()) |
| 509 |
|
| 510 |
peer := <-accepted |
| 511 |
gomega.Expect(peer.Close()).To(gomega.Succeed()) |
| 512 |
}) |
| 513 |
|
| 514 |
ginkgo.It("should fail the TLS handshake against a plaintext listener", func() { |
| 515 |
listener, err := net.Listen("tcp", "127.0.0.1:0") |
| 516 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 517 |
|
| 518 |
defer listener.Close() |
| 519 |
|
| 520 |
go func() { |
| 521 |
conn, acceptErr := listener.Accept() |
| 522 |
if acceptErr != nil { |
| 523 |
return |
| 524 |
} |
| 525 |
defer conn.Close() |
| 526 |
|
| 527 |
_, _ = conn.Read(make([]byte, 1)) |
| 528 |
}() |
| 529 |
|
| 530 |
service.SetDialContext((&net.Dialer{}).DialContext) |
| 531 |
|
| 532 |
brokerURL, err := url.Parse("mqtts://" + listener.Addr().String()) |
| 533 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 534 |
|
| 535 |
_, err = service.attemptConnection( |
| 536 |
context.Background(), |
| 537 |
autopaho.ClientConfig{ |
| 538 |
TlsCfg: &tls.Config{ |
| 539 |
InsecureSkipVerify: true, |
| 540 |
MinVersion: tls.VersionTLS12, |
| 541 |
}, |
| 542 |
}, |
| 543 |
brokerURL, |
| 544 |
) |
| 545 |
gomega.Expect(err).To(gomega.HaveOccurred()) |
| 546 |
gomega.Expect(err.Error()).To(gomega.ContainSubstring("TLS handshake")) |
| 547 |
}) |
| 548 |
}) |
| 549 |
}) |