auth_test.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package mtproto_test
  2. import (
  3. "bytes"
  4. "crypto/rand"
  5. "testing"
  6. "github.com/google/go-cmp/cmp"
  7. "github.com/xtls/xray-core/common"
  8. . "github.com/xtls/xray-core/proxy/mtproto"
  9. )
  10. func TestInverse(t *testing.T) {
  11. const size = 64
  12. b := make([]byte, 64)
  13. for b[0] == b[size-1] {
  14. common.Must2(rand.Read(b))
  15. }
  16. bi := Inverse(b)
  17. if b[0] == bi[0] {
  18. t.Fatal("seems bytes are not inversed: ", b[0], "vs", bi[0])
  19. }
  20. bii := Inverse(bi)
  21. if r := cmp.Diff(bii, b); r != "" {
  22. t.Fatal(r)
  23. }
  24. }
  25. func TestAuthenticationReadWrite(t *testing.T) {
  26. a := NewAuthentication(DefaultSessionContext())
  27. b := bytes.NewReader(a.Header[:])
  28. a2, err := ReadAuthentication(b)
  29. common.Must(err)
  30. if r := cmp.Diff(a.EncodingKey[:], a2.DecodingKey[:]); r != "" {
  31. t.Error("decoding key: ", r)
  32. }
  33. if r := cmp.Diff(a.EncodingNonce[:], a2.DecodingNonce[:]); r != "" {
  34. t.Error("decoding nonce: ", r)
  35. }
  36. if r := cmp.Diff(a.DecodingKey[:], a2.EncodingKey[:]); r != "" {
  37. t.Error("encoding key: ", r)
  38. }
  39. if r := cmp.Diff(a.DecodingNonce[:], a2.EncodingNonce[:]); r != "" {
  40. t.Error("encoding nonce: ", r)
  41. }
  42. }