Debian Codegraph @

pkg/services/chat/xmpp/xmpp_client_test.go

raw
1 package xmpp
2
3 import (
4 "context"
5 "crypto/tls"
6 "errors"
7 "net"
8
9 "github.com/onsi/ginkgo/v2"
10 "github.com/onsi/gomega"
11 )
12
13 var _ = ginkgo.Describe("client", func() {
14 ginkgo.Describe("wrapNegotiateError", func() {
15 ginkgo.It("should wrap SASL not-authorized as authentication failed", func() {
16 err := wrapNegotiateError(errors.New("not-authorized"))
17 gomega.Expect(err).To(gomega.MatchError(ErrAuthenticationFailed))
18 err = wrapNegotiateError(errors.New("Invalid username or password"))
19 gomega.Expect(err).To(gomega.MatchError(ErrAuthenticationFailed))
20 })
21
22 ginkgo.It("should wrap other negotiate errors without the auth sentinel", func() {
23 err := wrapNegotiateError(errors.New("features advertised out of order"))
24 gomega.Expect(err).NotTo(gomega.MatchError(ErrAuthenticationFailed))
25 gomega.Expect(err.Error()).To(gomega.ContainSubstring("negotiating XMPP session"))
26 })
27 })
28
29 ginkgo.Describe("newTLSConfig", func() {
30 ginkgo.It("should copy host and skip-verify onto the TLS config", func() {
31 cfg := &Config{Host: "xmpp.example.com", SkipTLSVerify: true}
32 tlsCfg := newTLSConfig(cfg)
33 gomega.Expect(tlsCfg.ServerName).To(gomega.Equal("xmpp.example.com"))
34 gomega.Expect(tlsCfg.MinVersion).To(gomega.Equal(uint16(tls.VersionTLS12)))
35 gomega.Expect(tlsCfg.InsecureSkipVerify).To(gomega.BeTrue())
36 })
37
38 ginkgo.It("should verify certificates by default", func() {
39 cfg := &Config{Host: "xmpp.example.com"}
40 gomega.Expect(newTLSConfig(cfg).InsecureSkipVerify).To(gomega.BeFalse())
41 })
42 })
43
44 ginkgo.Describe("melliumSession", func() {
45 ginkgo.It("should no-op Close when the session is unset", func() {
46 session := &melliumSession{}
47 gomega.Expect(session.Close()).To(gomega.Succeed())
48 })
49
50 ginkgo.It("should reject an invalid chat JID before encoding", func() {
51 session := &melliumSession{}
52 err := session.SendChat(context.Background(), "alice@", "hello")
53 gomega.Expect(err).To(gomega.MatchError(ErrInvalidRecipientJID))
54 })
55
56 ginkgo.It("should reject an invalid room JID before joining", func() {
57 session := &melliumSession{}
58 err := session.SendToRoom(context.Background(), "alice@", "nick", "", "hello")
59 gomega.Expect(err).To(gomega.MatchError(ErrInvalidRoomJID))
60 })
61 })
62
63 ginkgo.Describe("newMelliumSession", func() {
64 ginkgo.It("should reject an invalid auth JID", func() {
65 cfg := &Config{User: "alice@", Host: "xmpp.example.com", Password: "secret"}
66 _, err := newMelliumSession(context.Background(), cfg, nil)
67 gomega.Expect(err).To(gomega.HaveOccurred())
68 })
69
70 ginkgo.It("should fail implicit TLS handshake on a closed pipe", func() {
71 client, server := net.Pipe()
72 gomega.Expect(server.Close()).To(gomega.Succeed())
73
74 cfg := &Config{
75 User: "alice",
76 Host: "localhost",
77 Password: "secret",
78 scheme: SchemeTLS,
79 SkipTLSVerify: true,
80 }
81 _, err := newMelliumSession(context.Background(), cfg, client)
82 gomega.Expect(err).To(gomega.HaveOccurred())
83 gomega.Expect(err.Error()).To(gomega.ContainSubstring("TLS handshake"))
84 })
85
86 ginkgo.It("should fail plaintext negotiation on a closed pipe", func() {
87 client, server := net.Pipe()
88 gomega.Expect(server.Close()).To(gomega.Succeed())
89
90 cfg := &Config{
91 User: "alice",
92 Host: "localhost",
93 Password: "secret",
94 scheme: Scheme,
95 DisableTLS: true,
96 }
97 _, err := newMelliumSession(context.Background(), cfg, client)
98 gomega.Expect(err).To(gomega.HaveOccurred())
99 gomega.Expect(err.Error()).To(gomega.ContainSubstring("negotiating XMPP session"))
100 })
101
102 ginkgo.It("should fail STARTTLS negotiation on a closed pipe", func() {
103 client, server := net.Pipe()
104 gomega.Expect(server.Close()).To(gomega.Succeed())
105
106 cfg := &Config{
107 User: "alice",
108 Host: "localhost",
109 Password: "secret",
110 scheme: Scheme,
111 }
112 _, err := newMelliumSession(context.Background(), cfg, client)
113 gomega.Expect(err).To(gomega.HaveOccurred())
114 gomega.Expect(err.Error()).To(gomega.ContainSubstring("negotiating XMPP session"))
115 })
116 })
117 })