router_test.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package router_test
  2. import (
  3. "testing"
  4. "github.com/golang/mock/gomock"
  5. . "v2ray.com/core/app/router"
  6. "v2ray.com/core/common"
  7. "v2ray.com/core/common/net"
  8. "v2ray.com/core/common/session"
  9. "v2ray.com/core/testing/mocks"
  10. )
  11. func TestSimpleRouter(t *testing.T) {
  12. config := &Config{
  13. Rule: []*RoutingRule{
  14. {
  15. Tag: "test",
  16. NetworkList: &net.NetworkList{
  17. Network: []net.Network{net.Network_TCP},
  18. },
  19. },
  20. },
  21. }
  22. mockCtl := gomock.NewController(t)
  23. defer mockCtl.Finish()
  24. mockDns := mocks.NewMockDNSClient(mockCtl)
  25. r := new(Router)
  26. common.Must(r.Init(config, mockDns))
  27. ctx := withOutbound(&session.Outbound{Target: net.TCPDestination(net.DomainAddress("v2ray.com"), 80)})
  28. tag, err := r.PickRoute(ctx)
  29. common.Must(err)
  30. if tag != "test" {
  31. t.Error("expect tag 'test', bug actually ", tag)
  32. }
  33. }
  34. func TestIPOnDemand(t *testing.T) {
  35. config := &Config{
  36. DomainStrategy: Config_IpOnDemand,
  37. Rule: []*RoutingRule{
  38. {
  39. Tag: "test",
  40. Cidr: []*CIDR{
  41. {
  42. Ip: []byte{192, 168, 0, 0},
  43. Prefix: 16,
  44. },
  45. },
  46. },
  47. },
  48. }
  49. mockCtl := gomock.NewController(t)
  50. defer mockCtl.Finish()
  51. mockDns := mocks.NewMockDNSClient(mockCtl)
  52. mockDns.EXPECT().LookupIP(gomock.Eq("v2ray.com")).Return([]net.IP{{192, 168, 0, 1}}, nil).AnyTimes()
  53. r := new(Router)
  54. common.Must(r.Init(config, mockDns))
  55. ctx := withOutbound(&session.Outbound{Target: net.TCPDestination(net.DomainAddress("v2ray.com"), 80)})
  56. tag, err := r.PickRoute(ctx)
  57. common.Must(err)
  58. if tag != "test" {
  59. t.Error("expect tag 'test', bug actually ", tag)
  60. }
  61. }