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
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

init_test.go 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package obfuscated2_test
  2. import (
  3. "encoding/base64"
  4. "encoding/json"
  5. "fmt"
  6. "os"
  7. "path/filepath"
  8. "strings"
  9. )
  10. type snapshotBytes struct {
  11. data []byte
  12. }
  13. func (s snapshotBytes) MarshalText() ([]byte, error) {
  14. if len(s.data) == 0 {
  15. return nil, nil
  16. }
  17. return []byte(base64.RawStdEncoding.EncodeToString(s.data)), nil
  18. }
  19. func (s *snapshotBytes) UnmarshalText(data []byte) error {
  20. val, err := base64.RawStdEncoding.DecodeString(string(data))
  21. if err != nil {
  22. return fmt.Errorf("cannot unmarshal %v: %w", len(val), err)
  23. }
  24. s.data = val
  25. return nil
  26. }
  27. type Obfuscated2Snapshot struct {
  28. Secret snapshotBytes `json:"secret"`
  29. Frame snapshotBytes `json:"frame"`
  30. DC int16 `json:"dc"`
  31. Encrypted struct {
  32. Text snapshotBytes `json:"text"`
  33. Cipher snapshotBytes `json:"cipher"`
  34. } `json:"encrypted"`
  35. Decrypted struct {
  36. Text snapshotBytes `json:"text"`
  37. Cipher snapshotBytes `json:"cipher"`
  38. } `json:"decrypted"`
  39. }
  40. type SnapshotTestSuite struct {
  41. snapshots map[string]*Obfuscated2Snapshot
  42. }
  43. func (suite *SnapshotTestSuite) IngestSnapshots(dirname, namePrefix string) error {
  44. suite.snapshots = map[string]*Obfuscated2Snapshot{}
  45. files, err := os.ReadDir(filepath.Join("testdata", dirname))
  46. if err != nil {
  47. return fmt.Errorf("cannot ingest snapshots: %w", err)
  48. }
  49. for _, v := range files {
  50. if !strings.HasPrefix(v.Name(), namePrefix) {
  51. continue
  52. }
  53. filename := filepath.Join("testdata", dirname, v.Name())
  54. contents, err := os.ReadFile(filename)
  55. if err != nil {
  56. return fmt.Errorf("cannot read %s: %w", filename, err)
  57. }
  58. value := &Obfuscated2Snapshot{}
  59. if err := json.Unmarshal(contents, value); err != nil {
  60. return fmt.Errorf("cannot unmarshal %s: %w", filename, err)
  61. }
  62. suite.snapshots[v.Name()] = value
  63. }
  64. return nil
  65. }