Highly-opinionated (ex-bullshit-free) MTPROTO proxy for Telegram. If you use v1.0 or upgrade broke you proxy, please read the chapter Version 2
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package obfuscation
  2. import (
  3. "crypto/aes"
  4. "crypto/cipher"
  5. "encoding/hex"
  6. "testing"
  7. "github.com/9seconds/mtg/v2/essentials"
  8. "github.com/9seconds/mtg/v2/internal/testlib"
  9. "github.com/stretchr/testify/assert"
  10. "github.com/stretchr/testify/mock"
  11. "github.com/stretchr/testify/suite"
  12. )
  13. type ConnTestSuite struct {
  14. suite.Suite
  15. secret []byte
  16. }
  17. func (s *ConnTestSuite) SetupSuite() {
  18. secret := [32]byte{}
  19. s.secret = secret[:]
  20. }
  21. func (s *ConnTestSuite) TestRead() {
  22. testData := map[string]string{
  23. "data1": "b8f4b41993",
  24. "": "",
  25. "___": "83ca9f",
  26. }
  27. for incoming, outgoing := range testData {
  28. s.T().Run(incoming, func(t *testing.T) {
  29. connMock := &testlib.EssentialsConnMock{}
  30. testConn := s.makeConn(connMock)
  31. data := make([]byte, len(incoming))
  32. connMock.On("Read", make([]byte, len(incoming))).Return(len(incoming), nil).Run(func(args mock.Arguments) {
  33. arg := args.Get(0).([]byte)
  34. copy(arg, []byte(incoming))
  35. })
  36. n, err := testConn.Read(data)
  37. assert.Equal(t, len(data), n)
  38. assert.NoError(t, err)
  39. assert.Equal(t, outgoing, hex.EncodeToString(data))
  40. connMock.AssertExpectations(t)
  41. })
  42. }
  43. }
  44. func (s *ConnTestSuite) TestWrite() {
  45. testData := map[string]string{
  46. "b8f4b41993": "data1",
  47. "": "",
  48. "83ca9f": "___",
  49. }
  50. for incoming, outgoing := range testData {
  51. s.T().Run(incoming, func(t *testing.T) {
  52. connMock := &testlib.EssentialsConnMock{}
  53. testConn := s.makeConn(connMock)
  54. toWrite, _ := hex.DecodeString(incoming)
  55. data := make([]byte, len(toWrite))
  56. connMock.On("Write", []byte(outgoing)).Return(len(toWrite), nil)
  57. n, err := testConn.Write(toWrite)
  58. assert.Equal(t, len(data), n)
  59. assert.NoError(t, err)
  60. connMock.AssertExpectations(t)
  61. })
  62. }
  63. }
  64. func (s *ConnTestSuite) makeConn(rawConn *testlib.EssentialsConnMock) essentials.Conn {
  65. rblock, err := aes.NewCipher(s.secret)
  66. if err != nil {
  67. panic(err)
  68. }
  69. wblock, err := aes.NewCipher(s.secret)
  70. if err != nil {
  71. panic(err)
  72. }
  73. return conn{
  74. Conn: rawConn,
  75. sendCipher: cipher.NewCTR(wblock, s.secret[:aes.BlockSize]),
  76. recvCipher: cipher.NewCTR(rblock, s.secret[:aes.BlockSize]),
  77. }
  78. }
  79. func TestConn(t *testing.T) {
  80. t.Parallel()
  81. suite.Run(t, &ConnTestSuite{})
  82. }