| 1 |
package xmpp |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"context" |
| 6 |
"errors" |
| 7 |
"io" |
| 8 |
"log" |
| 9 |
"net" |
| 10 |
"sync" |
| 11 |
|
| 12 |
"github.com/onsi/ginkgo/v2" |
| 13 |
"github.com/onsi/gomega" |
| 14 |
|
| 15 |
"github.com/nicholas-fedor/shoutrrr/internal/testutils" |
| 16 |
"github.com/nicholas-fedor/shoutrrr/pkg/types" |
| 17 |
) |
| 18 |
|
| 19 |
type mockSession struct { |
| 20 |
chats [][2]string |
| 21 |
rooms [][4]string |
| 22 |
err error |
| 23 |
failTo string |
| 24 |
} |
| 25 |
|
| 26 |
var _ = ginkgo.Describe("service", func() { |
| 27 |
var service *Service |
| 28 |
|
| 29 |
ginkgo.BeforeEach(func() { |
| 30 |
service = &Service{} |
| 31 |
}) |
| 32 |
|
| 33 |
ginkgo.It("should return xmpp as the service ID", func() { |
| 34 |
gomega.Expect(service.GetID()).To(gomega.Equal(Scheme)) |
| 35 |
}) |
| 36 |
|
| 37 |
ginkgo.It("should initialize from a valid URL", func() { |
| 38 |
err := service.Initialize( |
| 39 |
testutils.URLMust("xmpp://alice:[email protected]/[email protected]"), |
| 40 |
testutils.TestLogger(), |
| 41 |
) |
| 42 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 43 |
gomega.Expect(service.Config.Host).To(gomega.Equal("xmpp.example.com")) |
| 44 |
}) |
| 45 |
|
| 46 |
ginkgo.It("should return an error when initializing an invalid URL", func() { |
| 47 |
err := service.Initialize( |
| 48 |
testutils.URLMust("xmpp://alice:[email protected]/"), |
| 49 |
testutils.TestLogger(), |
| 50 |
) |
| 51 |
gomega.Expect(err).To(gomega.MatchError(ErrMissingTargets)) |
| 52 |
}) |
| 53 |
|
| 54 |
ginkgo.It("should implement DialContextSetter", func() { |
| 55 |
_, ok := any(service).(types.DialContextSetter) |
| 56 |
gomega.Expect(ok).To(gomega.BeTrue()) |
| 57 |
}) |
| 58 |
|
| 59 |
ginkgo.It("should send chat messages through the session", func() { |
| 60 |
session := &mockSession{} |
| 61 |
|
| 62 |
gomega.Expect(service.Initialize( |
| 63 |
testutils.URLMust("xmpp://alice:[email protected]/[email protected]"), |
| 64 |
testutils.TestLogger(), |
| 65 |
)).To(gomega.Succeed()) |
| 66 |
service.SetSessionFactory(func(context.Context, *Config, net.Conn) (Session, error) { |
| 67 |
return session, nil |
| 68 |
}) |
| 69 |
service.SetDialContext(pipeDialer()) |
| 70 |
|
| 71 |
err := service.Send("hello", nil) |
| 72 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 73 |
gomega.Expect(session.chats).To(gomega.Equal([][2]string{{"[email protected]", "hello"}})) |
| 74 |
}) |
| 75 |
|
| 76 |
ginkgo.It("should prepend the title and send to rooms", func() { |
| 77 |
session := &mockSession{} |
| 78 |
|
| 79 |
gomega.Expect(service.Initialize( |
| 80 |
testutils.URLMust( |
| 81 |
"xmpp://alice:[email protected]/"+ |
| 82 |
"[email protected]&title=Alert&roompassword=pw", |
| 83 |
), |
| 84 |
testutils.TestLogger(), |
| 85 |
)).To(gomega.Succeed()) |
| 86 |
service.SetSessionFactory(func(context.Context, *Config, net.Conn) (Session, error) { |
| 87 |
return session, nil |
| 88 |
}) |
| 89 |
service.SetDialContext(pipeDialer()) |
| 90 |
|
| 91 |
err := service.Send("hello", nil) |
| 92 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 93 |
gomega.Expect(session.rooms).To(gomega.Equal([][4]string{{ |
| 94 |
"[email protected]", |
| 95 |
"alice", |
| 96 |
"pw", |
| 97 |
"Alert\nhello", |
| 98 |
}})) |
| 99 |
}) |
| 100 |
|
| 101 |
ginkgo.It("should dial the configured host and port", func() { |
| 102 |
var ( |
| 103 |
mu sync.Mutex |
| 104 |
gotAddr string |
| 105 |
gotNet string |
| 106 |
dialed bool |
| 107 |
) |
| 108 |
|
| 109 |
gomega.Expect(service.Initialize( |
| 110 |
testutils.URLMust("xmpp://alice:[email protected]:5222/[email protected]"), |
| 111 |
testutils.TestLogger(), |
| 112 |
)).To(gomega.Succeed()) |
| 113 |
service.SetSessionFactory(func(context.Context, *Config, net.Conn) (Session, error) { |
| 114 |
return &mockSession{}, nil |
| 115 |
}) |
| 116 |
service.SetDialContext(func(_ context.Context, network, addr string) (net.Conn, error) { |
| 117 |
mu.Lock() |
| 118 |
gotNet = network |
| 119 |
gotAddr = addr |
| 120 |
dialed = true |
| 121 |
mu.Unlock() |
| 122 |
|
| 123 |
return pipeDialer()(context.Background(), network, addr) |
| 124 |
}) |
| 125 |
|
| 126 |
gomega.Expect(service.Send("hello", nil)).To(gomega.Succeed()) |
| 127 |
mu.Lock() |
| 128 |
defer mu.Unlock() |
| 129 |
|
| 130 |
gomega.Expect(dialed).To(gomega.BeTrue()) |
| 131 |
gomega.Expect(gotNet).To(gomega.Equal("tcp")) |
| 132 |
gomega.Expect(gotAddr).To(gomega.Equal("xmpp.example.com:5222")) |
| 133 |
}) |
| 134 |
|
| 135 |
ginkgo.It("should restore the default session factory when nil is set", func() { |
| 136 |
gomega.Expect(service.Initialize( |
| 137 |
testutils.URLMust("xmpp://alice:[email protected]/[email protected]"), |
| 138 |
testutils.TestLogger(), |
| 139 |
)).To(gomega.Succeed()) |
| 140 |
service.SetSessionFactory(func(context.Context, *Config, net.Conn) (Session, error) { |
| 141 |
return &mockSession{}, nil |
| 142 |
}) |
| 143 |
service.SetSessionFactory(nil) |
| 144 |
service.SetDialContext(func(context.Context, string, string) (net.Conn, error) { |
| 145 |
client, server := net.Pipe() |
| 146 |
gomega.Expect(server.Close()).To(gomega.Succeed()) |
| 147 |
|
| 148 |
return client, nil |
| 149 |
}) |
| 150 |
|
| 151 |
err := service.Send("hello", nil) |
| 152 |
gomega.Expect(err).To(gomega.HaveOccurred()) |
| 153 |
gomega.Expect(err.Error()).To(gomega.ContainSubstring("negotiating XMPP session")) |
| 154 |
}) |
| 155 |
|
| 156 |
ginkgo.It("should return a dial error from the default dialer", func() { |
| 157 |
gomega.Expect(service.Initialize( |
| 158 |
testutils.URLMust("xmpp://alice:[email protected]:1/[email protected]"), |
| 159 |
testutils.TestLogger(), |
| 160 |
)).To(gomega.Succeed()) |
| 161 |
|
| 162 |
err := service.Send("hello", nil) |
| 163 |
gomega.Expect(err).To(gomega.HaveOccurred()) |
| 164 |
gomega.Expect(err.Error()).To(gomega.ContainSubstring("dialing")) |
| 165 |
}) |
| 166 |
|
| 167 |
ginkgo.It("should close the connection when session setup fails", func() { |
| 168 |
gomega.Expect(service.Initialize( |
| 169 |
testutils.URLMust("xmpp://alice:[email protected]/[email protected]"), |
| 170 |
testutils.TestLogger(), |
| 171 |
)).To(gomega.Succeed()) |
| 172 |
service.SetDialContext(pipeDialer()) |
| 173 |
service.SetSessionFactory(func(context.Context, *Config, net.Conn) (Session, error) { |
| 174 |
return nil, errors.New("negotiate failed") |
| 175 |
}) |
| 176 |
|
| 177 |
err := service.Send("hello", nil) |
| 178 |
gomega.Expect(err).To(gomega.MatchError("negotiate failed")) |
| 179 |
}) |
| 180 |
|
| 181 |
ginkgo.It("should return chat send errors", func() { |
| 182 |
chatErr := errors.New("chat failed") |
| 183 |
session := &mockSession{err: chatErr} |
| 184 |
|
| 185 |
gomega.Expect(service.Initialize( |
| 186 |
testutils.URLMust("xmpp://alice:[email protected]/[email protected]"), |
| 187 |
testutils.TestLogger(), |
| 188 |
)).To(gomega.Succeed()) |
| 189 |
service.SetSessionFactory(func(context.Context, *Config, net.Conn) (Session, error) { |
| 190 |
return session, nil |
| 191 |
}) |
| 192 |
service.SetDialContext(pipeDialer()) |
| 193 |
|
| 194 |
gomega.Expect(service.Send("hello", nil)).To(gomega.MatchError(chatErr)) |
| 195 |
}) |
| 196 |
|
| 197 |
ginkgo.It("should send remaining targets after a chat failure", func() { |
| 198 |
chatErr := errors.New("chat failed") |
| 199 |
session := &mockSession{failTo: "[email protected]", err: chatErr} |
| 200 |
|
| 201 |
gomega.Expect(service.Initialize( |
| 202 |
testutils.URLMust( |
| 203 |
"xmpp://alice:[email protected]/[email protected],[email protected]", |
| 204 |
), |
| 205 |
testutils.TestLogger(), |
| 206 |
)).To(gomega.Succeed()) |
| 207 |
service.SetSessionFactory(func(context.Context, *Config, net.Conn) (Session, error) { |
| 208 |
return session, nil |
| 209 |
}) |
| 210 |
service.SetDialContext(pipeDialer()) |
| 211 |
|
| 212 |
err := service.Send("hello", nil) |
| 213 |
gomega.Expect(err).To(gomega.MatchError(chatErr)) |
| 214 |
gomega.Expect(session.chats).To(gomega.Equal([][2]string{ |
| 215 |
{"[email protected]", "hello"}, |
| 216 |
{"[email protected]", "hello"}, |
| 217 |
})) |
| 218 |
}) |
| 219 |
|
| 220 |
ginkgo.It("should return MUC send errors", func() { |
| 221 |
mucErr := errors.New("muc failed") |
| 222 |
session := &mockSession{err: mucErr} |
| 223 |
|
| 224 |
gomega.Expect(service.Initialize( |
| 225 |
testutils.URLMust( |
| 226 |
"xmpp://alice:[email protected]/[email protected]", |
| 227 |
), |
| 228 |
testutils.TestLogger(), |
| 229 |
)).To(gomega.Succeed()) |
| 230 |
service.SetSessionFactory(func(context.Context, *Config, net.Conn) (Session, error) { |
| 231 |
return session, nil |
| 232 |
}) |
| 233 |
service.SetDialContext(pipeDialer()) |
| 234 |
|
| 235 |
gomega.Expect(service.Send("hello", nil)).To(gomega.MatchError(mucErr)) |
| 236 |
}) |
| 237 |
|
| 238 |
ginkgo.It("should reject unknown send params", func() { |
| 239 |
gomega.Expect(service.Initialize( |
| 240 |
testutils.URLMust("xmpp://alice:[email protected]/[email protected]"), |
| 241 |
testutils.TestLogger(), |
| 242 |
)).To(gomega.Succeed()) |
| 243 |
|
| 244 |
err := service.Send("hello", &types.Params{"notakey": "1"}) |
| 245 |
gomega.Expect(err).To(gomega.HaveOccurred()) |
| 246 |
gomega.Expect(err.Error()).To(gomega.ContainSubstring("updating config from params")) |
| 247 |
}) |
| 248 |
|
| 249 |
ginkgo.It("should reject params that remove all targets", func() { |
| 250 |
gomega.Expect(service.Initialize( |
| 251 |
testutils.URLMust("xmpp://alice:[email protected]/[email protected]"), |
| 252 |
testutils.TestLogger(), |
| 253 |
)).To(gomega.Succeed()) |
| 254 |
|
| 255 |
err := service.Send("hello", &types.Params{"to": ""}) |
| 256 |
gomega.Expect(err).To(gomega.MatchError(ErrMissingTargets)) |
| 257 |
}) |
| 258 |
|
| 259 |
ginkgo.It("should warn when TLS verification is skipped", func() { |
| 260 |
var buf bytes.Buffer |
| 261 |
gomega.Expect(service.Initialize( |
| 262 |
testutils.URLMust( |
| 263 |
"xmpp://alice:[email protected]/[email protected]&skiptlsverify=yes", |
| 264 |
), |
| 265 |
log.New(&buf, "", 0), |
| 266 |
)).To(gomega.Succeed()) |
| 267 |
service.SetSessionFactory(func(context.Context, *Config, net.Conn) (Session, error) { |
| 268 |
return &mockSession{}, nil |
| 269 |
}) |
| 270 |
service.SetDialContext(pipeDialer()) |
| 271 |
|
| 272 |
gomega.Expect(service.Send("hello", nil)).To(gomega.Succeed()) |
| 273 |
gomega.Expect(buf.String()).To(gomega.ContainSubstring("TLS verification is disabled")) |
| 274 |
}) |
| 275 |
}) |
| 276 |
|
| 277 |
func (*mockSession) Close() error { |
| 278 |
return nil |
| 279 |
} |
| 280 |
|
| 281 |
func (m *mockSession) SendChat(_ context.Context, to, body string) error { |
| 282 |
m.chats = append(m.chats, [2]string{to, body}) |
| 283 |
if m.failTo != "" && to != m.failTo { |
| 284 |
return nil |
| 285 |
} |
| 286 |
|
| 287 |
return m.err |
| 288 |
} |
| 289 |
|
| 290 |
func (m *mockSession) SendToRoom(_ context.Context, room, nick, password, body string) error { |
| 291 |
m.rooms = append(m.rooms, [4]string{room, nick, password, body}) |
| 292 |
|
| 293 |
return m.err |
| 294 |
} |
| 295 |
|
| 296 |
func pipeDialer() types.DialContextFunc { |
| 297 |
return func(context.Context, string, string) (net.Conn, error) { |
| 298 |
client, server := net.Pipe() |
| 299 |
go func() { |
| 300 |
_, _ = io.Copy(io.Discard, server) |
| 301 |
_ = server.Close() |
| 302 |
}() |
| 303 |
|
| 304 |
return client, nil |
| 305 |
} |
| 306 |
} |