server_test.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package dns_test
  2. import (
  3. "net"
  4. "testing"
  5. "github.com/v2ray/v2ray-core/app"
  6. "github.com/v2ray/v2ray-core/app/dispatcher"
  7. . "github.com/v2ray/v2ray-core/app/dns"
  8. apptesting "github.com/v2ray/v2ray-core/app/testing"
  9. v2net "github.com/v2ray/v2ray-core/common/net"
  10. netassert "github.com/v2ray/v2ray-core/common/net/testing/assert"
  11. "github.com/v2ray/v2ray-core/proxy/freedom"
  12. v2testing "github.com/v2ray/v2ray-core/testing"
  13. "github.com/v2ray/v2ray-core/testing/assert"
  14. "github.com/v2ray/v2ray-core/transport/ray"
  15. )
  16. type TestDispatcher struct {
  17. freedom *freedom.FreedomConnection
  18. }
  19. func (this *TestDispatcher) DispatchToOutbound(context app.Context, dest v2net.Destination) ray.InboundRay {
  20. direct := ray.NewRay()
  21. go func() {
  22. payload, err := direct.OutboundInput().Read()
  23. if err != nil {
  24. direct.OutboundInput().Release()
  25. direct.OutboundOutput().Release()
  26. return
  27. }
  28. this.freedom.Dispatch(dest, payload, direct)
  29. }()
  30. return direct
  31. }
  32. func TestDnsAdd(t *testing.T) {
  33. v2testing.Current(t)
  34. d := &TestDispatcher{
  35. freedom: &freedom.FreedomConnection{},
  36. }
  37. spaceController := app.NewController()
  38. spaceController.Bind(dispatcher.APP_ID, d)
  39. space := spaceController.ForContext("test")
  40. domain := "local.v2ray.com"
  41. server := NewCacheServer(space, &Config{
  42. NameServers: []v2net.Destination{
  43. v2net.UDPDestination(v2net.IPAddress([]byte{8, 8, 8, 8}), v2net.Port(53)),
  44. },
  45. })
  46. ips := server.Get(&apptesting.Context{
  47. CallerTagValue: "a",
  48. }, domain)
  49. assert.Int(len(ips)).Equals(1)
  50. netassert.IP(ips[0].To4()).Equals(net.IP([]byte{127, 0, 0, 1}))
  51. }