| 1 | package gateway |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "io" |
| 6 | "strconv" |
| 7 | "testing" |
| 8 | |
| 9 | "github.com/matterbridge-org/matterbridge/bridge/config" |
| 10 | "github.com/matterbridge-org/matterbridge/gateway/bridgemap" |
| 11 | "github.com/sirupsen/logrus" |
| 12 | "github.com/stretchr/testify/assert" |
| 13 | "github.com/stretchr/testify/suite" |
| 14 | ) |
| 15 | |
| 16 | var testconfig = []byte(` |
| 17 | [irc.freenode] |
| 18 | server="" |
| 19 | [mattermost.test] |
| 20 | server="" |
| 21 | [discord.test] |
| 22 | server="" |
| 23 | [slack.test] |
| 24 | server="" |
| 25 | |
| 26 | [[gateway]] |
| 27 | name = "bridge1" |
| 28 | enable=true |
| 29 | |
| 30 | [[gateway.inout]] |
| 31 | account = "irc.freenode" |
| 32 | channel = "#wimtesting" |
| 33 | |
| 34 | [[gateway.inout]] |
| 35 | account = "discord.test" |
| 36 | channel = "general" |
| 37 | |
| 38 | [[gateway.inout]] |
| 39 | account="slack.test" |
| 40 | channel="testing" |
| 41 | `) |
| 42 | |
| 43 | var testconfig2 = []byte(` |
| 44 | [irc.freenode] |
| 45 | server="" |
| 46 | [mattermost.test] |
| 47 | server="" |
| 48 | [discord.test] |
| 49 | server="" |
| 50 | [slack.test] |
| 51 | server="" |
| 52 | |
| 53 | [[gateway]] |
| 54 | name = "bridge1" |
| 55 | enable=true |
| 56 | |
| 57 | [[gateway.in]] |
| 58 | account = "irc.freenode" |
| 59 | channel = "#wimtesting" |
| 60 | |
| 61 | [[gateway.inout]] |
| 62 | account = "discord.test" |
| 63 | channel = "general" |
| 64 | |
| 65 | [[gateway.out]] |
| 66 | account="slack.test" |
| 67 | channel="testing" |
| 68 | [[gateway]] |
| 69 | name = "bridge2" |
| 70 | enable=true |
| 71 | |
| 72 | [[gateway.in]] |
| 73 | account = "irc.freenode" |
| 74 | channel = "#wimtesting2" |
| 75 | |
| 76 | [[gateway.out]] |
| 77 | account = "discord.test" |
| 78 | channel = "general2" |
| 79 | `) |
| 80 | |
| 81 | var testconfig3 = []byte(` |
| 82 | [irc.zzz] |
| 83 | server="" |
| 84 | [telegram.zzz] |
| 85 | server="" |
| 86 | [slack.zzz] |
| 87 | server="" |
| 88 | [[gateway]] |
| 89 | name="bridge" |
| 90 | enable=true |
| 91 | |
| 92 | [[gateway.inout]] |
| 93 | account="irc.zzz" |
| 94 | channel="#main" |
| 95 | |
| 96 | [[gateway.inout]] |
| 97 | account="telegram.zzz" |
| 98 | channel="-1111111111111" |
| 99 | |
| 100 | [[gateway.inout]] |
| 101 | account="slack.zzz" |
| 102 | channel="irc" |
| 103 | |
| 104 | [[gateway]] |
| 105 | name="announcements" |
| 106 | enable=true |
| 107 | |
| 108 | [[gateway.in]] |
| 109 | account="telegram.zzz" |
| 110 | channel="-2222222222222" |
| 111 | |
| 112 | [[gateway.out]] |
| 113 | account="irc.zzz" |
| 114 | channel="#main" |
| 115 | |
| 116 | [[gateway.out]] |
| 117 | account="irc.zzz" |
| 118 | channel="#main-help" |
| 119 | |
| 120 | [[gateway.out]] |
| 121 | account="telegram.zzz" |
| 122 | channel="--333333333333" |
| 123 | |
| 124 | [[gateway.out]] |
| 125 | account="slack.zzz" |
| 126 | channel="general" |
| 127 | |
| 128 | [[gateway]] |
| 129 | name="bridge2" |
| 130 | enable=true |
| 131 | |
| 132 | [[gateway.inout]] |
| 133 | account="irc.zzz" |
| 134 | channel="#main-help" |
| 135 | |
| 136 | [[gateway.inout]] |
| 137 | account="telegram.zzz" |
| 138 | channel="--444444444444" |
| 139 | |
| 140 | |
| 141 | [[gateway]] |
| 142 | name="bridge3" |
| 143 | enable=true |
| 144 | |
| 145 | [[gateway.inout]] |
| 146 | account="irc.zzz" |
| 147 | channel="#main-telegram" |
| 148 | |
| 149 | [[gateway.inout]] |
| 150 | account="telegram.zzz" |
| 151 | channel="--333333333333" |
| 152 | `) |
| 153 | |
| 154 | const ( |
| 155 | ircTestAccount = "irc.zzz" |
| 156 | tgTestAccount = "telegram.zzz" |
| 157 | slackTestAccount = "slack.zzz" |
| 158 | ) |
| 159 | |
| 160 | func maketestRouter(input []byte) *Router { |
| 161 | logger := logrus.New() |
| 162 | logger.SetOutput(io.Discard) |
| 163 | cfg := config.NewConfigFromString(logger, input) |
| 164 | r, err := NewRouter(logger, cfg, bridgemap.FullMap) |
| 165 | if err != nil { |
| 166 | fmt.Println(err) |
| 167 | } |
| 168 | return r |
| 169 | } |
| 170 | |
| 171 | func TestNewRouter(t *testing.T) { |
| 172 | r := maketestRouter(testconfig) |
| 173 | assert.Equal(t, 1, len(r.Gateways)) |
| 174 | assert.Equal(t, 3, len(r.Gateways["bridge1"].Bridges)) |
| 175 | assert.Equal(t, 3, len(r.Gateways["bridge1"].Channels)) |
| 176 | r = maketestRouter(testconfig2) |
| 177 | assert.Equal(t, 2, len(r.Gateways)) |
| 178 | assert.Equal(t, 3, len(r.Gateways["bridge1"].Bridges)) |
| 179 | assert.Equal(t, 2, len(r.Gateways["bridge2"].Bridges)) |
| 180 | assert.Equal(t, 3, len(r.Gateways["bridge1"].Channels)) |
| 181 | assert.Equal(t, 2, len(r.Gateways["bridge2"].Channels)) |
| 182 | assert.Equal(t, &config.ChannelInfo{ |
| 183 | Name: "general", |
| 184 | Direction: "inout", |
| 185 | ID: "generaldiscord.test", |
| 186 | Account: "discord.test", |
| 187 | SameChannel: map[string]bool{"bridge1": false}, |
| 188 | }, r.Gateways["bridge1"].Channels["generaldiscord.test"]) |
| 189 | } |
| 190 | |
| 191 | func TestGetDestChannel(t *testing.T) { |
| 192 | r := maketestRouter(testconfig2) |
| 193 | msg := &config.Message{Text: "test", Channel: "general", Account: "discord.test", Gateway: "bridge1", Protocol: "discord", Username: "test"} |
| 194 | for _, br := range r.Gateways["bridge1"].Bridges { |
| 195 | switch br.Account { |
| 196 | case "discord.test": |
| 197 | assert.Equal(t, []config.ChannelInfo{{ |
| 198 | Name: "general", |
| 199 | Account: "discord.test", |
| 200 | Direction: "inout", |
| 201 | ID: "generaldiscord.test", |
| 202 | SameChannel: map[string]bool{"bridge1": false}, |
| 203 | Options: config.ChannelOptions{Key: ""}, |
| 204 | }}, r.Gateways["bridge1"].getDestChannel(msg, *br)) |
| 205 | case "slack.test": |
| 206 | assert.Equal(t, []config.ChannelInfo{{ |
| 207 | Name: "testing", |
| 208 | Account: "slack.test", |
| 209 | Direction: "out", |
| 210 | ID: "testingslack.test", |
| 211 | SameChannel: map[string]bool{"bridge1": false}, |
| 212 | Options: config.ChannelOptions{Key: ""}, |
| 213 | }}, r.Gateways["bridge1"].getDestChannel(msg, *br)) |
| 214 | case "irc.freenode": |
| 215 | assert.Equal(t, []config.ChannelInfo(nil), r.Gateways["bridge1"].getDestChannel(msg, *br)) |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | func TestGetDestChannelAdvanced(t *testing.T) { |
| 221 | r := maketestRouter(testconfig3) |
| 222 | var msgs []*config.Message |
| 223 | i := 0 |
| 224 | for _, gw := range r.Gateways { |
| 225 | for _, channel := range gw.Channels { |
| 226 | msgs = append(msgs, &config.Message{Text: "text" + strconv.Itoa(i), Channel: channel.Name, Account: channel.Account, Gateway: gw.Name, Username: "user" + strconv.Itoa(i)}) |
| 227 | i++ |
| 228 | } |
| 229 | } |
| 230 | hits := make(map[string]int) |
| 231 | for _, gw := range r.Gateways { |
| 232 | for _, br := range gw.Bridges { |
| 233 | for _, msg := range msgs { |
| 234 | channels := gw.getDestChannel(msg, *br) |
| 235 | if gw.Name != msg.Gateway { |
| 236 | assert.Equal(t, []config.ChannelInfo(nil), channels) |
| 237 | continue |
| 238 | } |
| 239 | switch gw.Name { |
| 240 | case "bridge": |
| 241 | if (msg.Channel == "#main" || msg.Channel == "-1111111111111" || msg.Channel == "irc") && |
| 242 | (msg.Account == ircTestAccount || msg.Account == tgTestAccount || msg.Account == slackTestAccount) { |
| 243 | hits[gw.Name]++ |
| 244 | switch br.Account { |
| 245 | case ircTestAccount: |
| 246 | assert.Equal(t, []config.ChannelInfo{{ |
| 247 | Name: "#main", |
| 248 | Account: ircTestAccount, |
| 249 | Direction: "inout", |
| 250 | ID: "#mainirc.zzz", |
| 251 | SameChannel: map[string]bool{"bridge": false}, |
| 252 | Options: config.ChannelOptions{Key: ""}, |
| 253 | }}, channels) |
| 254 | case tgTestAccount: |
| 255 | assert.Equal(t, []config.ChannelInfo{{ |
| 256 | Name: "-1111111111111", |
| 257 | Account: tgTestAccount, |
| 258 | Direction: "inout", |
| 259 | ID: "-1111111111111telegram.zzz", |
| 260 | SameChannel: map[string]bool{"bridge": false}, |
| 261 | Options: config.ChannelOptions{Key: ""}, |
| 262 | }}, channels) |
| 263 | case slackTestAccount: |
| 264 | assert.Equal(t, []config.ChannelInfo{{ |
| 265 | Name: "irc", |
| 266 | Account: slackTestAccount, |
| 267 | Direction: "inout", |
| 268 | ID: "ircslack.zzz", |
| 269 | SameChannel: map[string]bool{"bridge": false}, |
| 270 | Options: config.ChannelOptions{Key: ""}, |
| 271 | }}, channels) |
| 272 | } |
| 273 | } |
| 274 | case "bridge2": |
| 275 | if (msg.Channel == "#main-help" || msg.Channel == "--444444444444") && |
| 276 | (msg.Account == ircTestAccount || msg.Account == tgTestAccount) { |
| 277 | hits[gw.Name]++ |
| 278 | switch br.Account { |
| 279 | case ircTestAccount: |
| 280 | assert.Equal(t, []config.ChannelInfo{{ |
| 281 | Name: "#main-help", |
| 282 | Account: ircTestAccount, |
| 283 | Direction: "inout", |
| 284 | ID: "#main-helpirc.zzz", |
| 285 | SameChannel: map[string]bool{"bridge2": false}, |
| 286 | Options: config.ChannelOptions{Key: ""}, |
| 287 | }}, channels) |
| 288 | case tgTestAccount: |
| 289 | assert.Equal(t, []config.ChannelInfo{{ |
| 290 | Name: "--444444444444", |
| 291 | Account: tgTestAccount, |
| 292 | Direction: "inout", |
| 293 | ID: "--444444444444telegram.zzz", |
| 294 | SameChannel: map[string]bool{"bridge2": false}, |
| 295 | Options: config.ChannelOptions{Key: ""}, |
| 296 | }}, channels) |
| 297 | } |
| 298 | } |
| 299 | case "bridge3": |
| 300 | if (msg.Channel == "#main-telegram" || msg.Channel == "--333333333333") && |
| 301 | (msg.Account == ircTestAccount || msg.Account == tgTestAccount) { |
| 302 | hits[gw.Name]++ |
| 303 | switch br.Account { |
| 304 | case ircTestAccount: |
| 305 | assert.Equal(t, []config.ChannelInfo{{ |
| 306 | Name: "#main-telegram", |
| 307 | Account: ircTestAccount, |
| 308 | Direction: "inout", |
| 309 | ID: "#main-telegramirc.zzz", |
| 310 | SameChannel: map[string]bool{"bridge3": false}, |
| 311 | Options: config.ChannelOptions{Key: ""}, |
| 312 | }}, channels) |
| 313 | case tgTestAccount: |
| 314 | assert.Equal(t, []config.ChannelInfo{{ |
| 315 | Name: "--333333333333", |
| 316 | Account: tgTestAccount, |
| 317 | Direction: "inout", |
| 318 | ID: "--333333333333telegram.zzz", |
| 319 | SameChannel: map[string]bool{"bridge3": false}, |
| 320 | Options: config.ChannelOptions{Key: ""}, |
| 321 | }}, channels) |
| 322 | } |
| 323 | } |
| 324 | case "announcements": |
| 325 | if msg.Channel != "-2222222222222" && msg.Account != "telegram" { |
| 326 | assert.Equal(t, []config.ChannelInfo(nil), channels) |
| 327 | continue |
| 328 | } |
| 329 | hits[gw.Name]++ |
| 330 | switch br.Account { |
| 331 | case ircTestAccount: |
| 332 | assert.Len(t, channels, 2) |
| 333 | assert.Contains(t, channels, config.ChannelInfo{ |
| 334 | Name: "#main", |
| 335 | Account: ircTestAccount, |
| 336 | Direction: "out", |
| 337 | ID: "#mainirc.zzz", |
| 338 | SameChannel: map[string]bool{"announcements": false}, |
| 339 | Options: config.ChannelOptions{Key: ""}, |
| 340 | }) |
| 341 | assert.Contains(t, channels, config.ChannelInfo{ |
| 342 | Name: "#main-help", |
| 343 | Account: ircTestAccount, |
| 344 | Direction: "out", |
| 345 | ID: "#main-helpirc.zzz", |
| 346 | SameChannel: map[string]bool{"announcements": false}, |
| 347 | Options: config.ChannelOptions{Key: ""}, |
| 348 | }) |
| 349 | case slackTestAccount: |
| 350 | assert.Equal(t, []config.ChannelInfo{{ |
| 351 | Name: "general", |
| 352 | Account: slackTestAccount, |
| 353 | Direction: "out", |
| 354 | ID: "generalslack.zzz", |
| 355 | SameChannel: map[string]bool{"announcements": false}, |
| 356 | Options: config.ChannelOptions{Key: ""}, |
| 357 | }}, channels) |
| 358 | case tgTestAccount: |
| 359 | assert.Equal(t, []config.ChannelInfo{{ |
| 360 | Name: "--333333333333", |
| 361 | Account: tgTestAccount, |
| 362 | Direction: "out", |
| 363 | ID: "--333333333333telegram.zzz", |
| 364 | SameChannel: map[string]bool{"announcements": false}, |
| 365 | Options: config.ChannelOptions{Key: ""}, |
| 366 | }}, channels) |
| 367 | } |
| 368 | } |
| 369 | } |
| 370 | } |
| 371 | } |
| 372 | assert.Equal(t, map[string]int{"bridge3": 4, "bridge": 9, "announcements": 3, "bridge2": 4}, hits) |
| 373 | } |
| 374 | |
| 375 | type ignoreTestSuite struct { |
| 376 | suite.Suite |
| 377 | |
| 378 | gw *Gateway |
| 379 | } |
| 380 | |
| 381 | func TestIgnoreSuite(t *testing.T) { |
| 382 | s := &ignoreTestSuite{} |
| 383 | suite.Run(t, s) |
| 384 | } |
| 385 | |
| 386 | func (s *ignoreTestSuite) SetupSuite() { |
| 387 | logger := logrus.New() |
| 388 | logger.SetOutput(io.Discard) |
| 389 | s.gw = &Gateway{logger: logrus.NewEntry(logger)} |
| 390 | } |
| 391 | |
| 392 | func (s *ignoreTestSuite) TestIgnoreTextEmpty() { |
| 393 | extraFile := make(map[string][]interface{}) |
| 394 | extraAttach := make(map[string][]interface{}) |
| 395 | extraFailure := make(map[string][]interface{}) |
| 396 | extraFile["file"] = append(extraFile["file"], config.FileInfo{}) |
| 397 | extraAttach["attachments"] = append(extraAttach["attachments"], []string{}) |
| 398 | extraFailure[config.EventFileFailureSize] = append(extraFailure[config.EventFileFailureSize], config.FileInfo{}) |
| 399 | |
| 400 | msgTests := map[string]struct { |
| 401 | input *config.Message |
| 402 | output bool |
| 403 | }{ |
| 404 | "usertyping": { |
| 405 | input: &config.Message{Event: config.EventUserTyping}, |
| 406 | output: false, |
| 407 | }, |
| 408 | "file attach": { |
| 409 | input: &config.Message{Extra: extraFile}, |
| 410 | output: false, |
| 411 | }, |
| 412 | "attachments": { |
| 413 | input: &config.Message{Extra: extraAttach}, |
| 414 | output: false, |
| 415 | }, |
| 416 | config.EventFileFailureSize: { |
| 417 | input: &config.Message{Extra: extraFailure}, |
| 418 | output: false, |
| 419 | }, |
| 420 | "nil extra": { |
| 421 | input: &config.Message{Extra: nil}, |
| 422 | output: true, |
| 423 | }, |
| 424 | "empty": { |
| 425 | input: &config.Message{}, |
| 426 | output: true, |
| 427 | }, |
| 428 | } |
| 429 | for testname, testcase := range msgTests { |
| 430 | output := s.gw.ignoreTextEmpty(testcase.input) |
| 431 | s.Assert().Equalf(testcase.output, output, "case '%s' failed", testname) |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | func (s *ignoreTestSuite) TestIgnoreTexts() { |
| 436 | msgTests := map[string]struct { |
| 437 | input string |
| 438 | re []string |
| 439 | output bool |
| 440 | }{ |
| 441 | "no regex": { |
| 442 | input: "a text message", |
| 443 | re: []string{}, |
| 444 | output: false, |
| 445 | }, |
| 446 | "simple regex": { |
| 447 | input: "a text message", |
| 448 | re: []string{"text"}, |
| 449 | output: true, |
| 450 | }, |
| 451 | "multiple regex fail": { |
| 452 | input: "a text message", |
| 453 | re: []string{"abc", "123$"}, |
| 454 | output: false, |
| 455 | }, |
| 456 | "multiple regex pass": { |
| 457 | input: "a text message", |
| 458 | re: []string{"lala", "sage$"}, |
| 459 | output: true, |
| 460 | }, |
| 461 | } |
| 462 | for testname, testcase := range msgTests { |
| 463 | output := s.gw.ignoreText(testcase.input, testcase.re) |
| 464 | s.Assert().Equalf(testcase.output, output, "case '%s' failed", testname) |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | func (s *ignoreTestSuite) TestIgnoreNicks() { |
| 469 | msgTests := map[string]struct { |
| 470 | input string |
| 471 | re []string |
| 472 | output bool |
| 473 | }{ |
| 474 | "no entry": { |
| 475 | input: "user", |
| 476 | re: []string{}, |
| 477 | output: false, |
| 478 | }, |
| 479 | "one entry": { |
| 480 | input: "user", |
| 481 | re: []string{"user"}, |
| 482 | output: true, |
| 483 | }, |
| 484 | "multiple entries": { |
| 485 | input: "user", |
| 486 | re: []string{"abc", "user"}, |
| 487 | output: true, |
| 488 | }, |
| 489 | "multiple entries fail": { |
| 490 | input: "user", |
| 491 | re: []string{"abc", "def"}, |
| 492 | output: false, |
| 493 | }, |
| 494 | } |
| 495 | for testname, testcase := range msgTests { |
| 496 | output := s.gw.ignoreText(testcase.input, testcase.re) |
| 497 | s.Assert().Equalf(testcase.output, output, "case '%s' failed", testname) |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | func BenchmarkTengo(b *testing.B) { |
| 502 | msg := &config.Message{Username: "user", Text: "blah testing", Account: "protocol.account", Channel: "mychannel"} |
| 503 | for n := 0; n < b.N; n++ { |
| 504 | err := modifyInMessageTengo("bench.tengo", msg) |
| 505 | if err != nil { |
| 506 | return |
| 507 | } |
| 508 | } |
| 509 | } |
| 510 | |