tgbot_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package service
  2. import (
  3. "io"
  4. "net"
  5. "reflect"
  6. "testing"
  7. "time"
  8. )
  9. func TestLoginAttemptDoesNotCarryPassword(t *testing.T) {
  10. typ := reflect.TypeFor[LoginAttempt]()
  11. if _, ok := typ.FieldByName("Password"); ok {
  12. t.Fatal("LoginAttempt must not carry attempted passwords")
  13. }
  14. }
  15. func TestIsSupportedBotProxyScheme(t *testing.T) {
  16. supported := []string{
  17. "socks5://127.0.0.1:1080",
  18. "http://127.0.0.1:8080",
  19. "https://127.0.0.1:8080",
  20. }
  21. for _, p := range supported {
  22. if !isSupportedBotProxyScheme(p) {
  23. t.Errorf("expected %q to be supported", p)
  24. }
  25. }
  26. unsupported := []string{"", "ftp://x", "127.0.0.1:1080", "socks4://1.2.3.4:1080"}
  27. for _, p := range unsupported {
  28. if isSupportedBotProxyScheme(p) {
  29. t.Errorf("expected %q to be unsupported", p)
  30. }
  31. }
  32. }
  33. func recordingDialTarget(t *testing.T, n int) (addr string, got chan []byte) {
  34. t.Helper()
  35. ln, err := net.Listen("tcp", "127.0.0.1:0")
  36. if err != nil {
  37. t.Fatal(err)
  38. }
  39. got = make(chan []byte, 1)
  40. t.Cleanup(func() { _ = ln.Close() })
  41. go func() {
  42. conn, err := ln.Accept()
  43. if err != nil {
  44. return
  45. }
  46. defer conn.Close()
  47. _ = conn.SetReadDeadline(time.Now().Add(2 * time.Second))
  48. buf := make([]byte, n)
  49. m, _ := io.ReadFull(conn, buf)
  50. got <- buf[:m]
  51. }()
  52. return ln.Addr().String(), got
  53. }
  54. func TestTgbotProxyDialerSelectsHTTPForHTTPScheme(t *testing.T) {
  55. addr, got := recordingDialTarget(t, len("CONNECT "))
  56. tg := &Tgbot{}
  57. client := tg.createRobustFastHTTPClient("http://" + addr)
  58. if client.Dial == nil {
  59. t.Fatal("Dial must be set for an http:// proxy")
  60. }
  61. go func() { _, _ = client.Dial("example.com:443") }()
  62. select {
  63. case b := <-got:
  64. if string(b) != "CONNECT " {
  65. t.Fatalf("expected HTTP CONNECT to the proxy, got %q", b)
  66. }
  67. case <-time.After(3 * time.Second):
  68. t.Fatal("proxy never received a connection")
  69. }
  70. }
  71. func TestTgbotProxyDialerSelectsSOCKSForSocks5Scheme(t *testing.T) {
  72. addr, got := recordingDialTarget(t, 1)
  73. tg := &Tgbot{}
  74. client := tg.createRobustFastHTTPClient("socks5://" + addr)
  75. if client.Dial == nil {
  76. t.Fatal("Dial must be set for a socks5:// proxy")
  77. }
  78. go func() { _, _ = client.Dial("example.com:443") }()
  79. select {
  80. case b := <-got:
  81. if len(b) != 1 || b[0] != 0x05 {
  82. t.Fatalf("expected SOCKS5 greeting (0x05), got %v", b)
  83. }
  84. case <-time.After(3 * time.Second):
  85. t.Fatal("proxy never received a connection")
  86. }
  87. }
  88. func TestTgbotProxyDialerNoneWhenEmpty(t *testing.T) {
  89. tg := &Tgbot{}
  90. client := tg.createRobustFastHTTPClient("")
  91. if client.Dial != nil {
  92. t.Fatal("Dial must be nil when no proxy is configured")
  93. }
  94. }