router_test.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. TargetTag: &RoutingRule_Tag{
  16. Tag: "test",
  17. },
  18. NetworkList: &net.NetworkList{
  19. Network: []net.Network{net.Network_TCP},
  20. },
  21. },
  22. },
  23. }
  24. mockCtl := gomock.NewController(t)
  25. defer mockCtl.Finish()
  26. mockDns := mocks.NewDNSClient(mockCtl)
  27. r := new(Router)
  28. common.Must(r.Init(config, mockDns, nil))
  29. ctx := withOutbound(&session.Outbound{Target: net.TCPDestination(net.DomainAddress("v2ray.com"), 80)})
  30. tag, err := r.PickRoute(ctx)
  31. common.Must(err)
  32. if tag != "test" {
  33. t.Error("expect tag 'test', bug actually ", tag)
  34. }
  35. }
  36. func TestIPOnDemand(t *testing.T) {
  37. config := &Config{
  38. DomainStrategy: Config_IpOnDemand,
  39. Rule: []*RoutingRule{
  40. {
  41. TargetTag: &RoutingRule_Tag{
  42. Tag: "test",
  43. },
  44. Cidr: []*CIDR{
  45. {
  46. Ip: []byte{192, 168, 0, 0},
  47. Prefix: 16,
  48. },
  49. },
  50. },
  51. },
  52. }
  53. mockCtl := gomock.NewController(t)
  54. defer mockCtl.Finish()
  55. mockDns := mocks.NewDNSClient(mockCtl)
  56. mockDns.EXPECT().LookupIP(gomock.Eq("v2ray.com")).Return([]net.IP{{192, 168, 0, 1}}, nil).AnyTimes()
  57. r := new(Router)
  58. common.Must(r.Init(config, mockDns, nil))
  59. ctx := withOutbound(&session.Outbound{Target: net.TCPDestination(net.DomainAddress("v2ray.com"), 80)})
  60. tag, err := r.PickRoute(ctx)
  61. common.Must(err)
  62. if tag != "test" {
  63. t.Error("expect tag 'test', bug actually ", tag)
  64. }
  65. }