handler_test.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package outbound_test
  2. import (
  3. "context"
  4. "testing"
  5. core "github.com/v2fly/v2ray-core/v4"
  6. "github.com/v2fly/v2ray-core/v4/app/policy"
  7. . "github.com/v2fly/v2ray-core/v4/app/proxyman/outbound"
  8. "github.com/v2fly/v2ray-core/v4/app/stats"
  9. "github.com/v2fly/v2ray-core/v4/common/net"
  10. "github.com/v2fly/v2ray-core/v4/common/serial"
  11. "github.com/v2fly/v2ray-core/v4/features/outbound"
  12. "github.com/v2fly/v2ray-core/v4/proxy/freedom"
  13. "github.com/v2fly/v2ray-core/v4/transport/internet"
  14. _ "unsafe"
  15. )
  16. func TestInterfaces(t *testing.T) {
  17. _ = (outbound.Handler)(new(Handler))
  18. _ = (outbound.Manager)(new(Manager))
  19. }
  20. //go:linkname toContext github.com/v2fly/v2ray-core/v4.toContext
  21. func toContext(ctx context.Context, v *core.Instance) context.Context
  22. func TestOutboundWithoutStatCounter(t *testing.T) {
  23. config := &core.Config{
  24. App: []*serial.TypedMessage{
  25. serial.ToTypedMessage(&stats.Config{}),
  26. serial.ToTypedMessage(&policy.Config{
  27. System: &policy.SystemPolicy{
  28. Stats: &policy.SystemPolicy_Stats{
  29. InboundUplink: true,
  30. },
  31. },
  32. }),
  33. },
  34. }
  35. v, _ := core.New(config)
  36. v.AddFeature((outbound.Manager)(new(Manager)))
  37. ctx := toContext(context.Background(), v)
  38. h, _ := NewHandler(ctx, &core.OutboundHandlerConfig{
  39. Tag: "tag",
  40. ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
  41. })
  42. conn, _ := h.(*Handler).Dial(ctx, net.TCPDestination(net.DomainAddress("localhost"), 13146))
  43. _, ok := conn.(*internet.StatCouterConnection)
  44. if ok {
  45. t.Errorf("Expected conn to not be StatCouterConnection")
  46. }
  47. }
  48. func TestOutboundWithStatCounter(t *testing.T) {
  49. config := &core.Config{
  50. App: []*serial.TypedMessage{
  51. serial.ToTypedMessage(&stats.Config{}),
  52. serial.ToTypedMessage(&policy.Config{
  53. System: &policy.SystemPolicy{
  54. Stats: &policy.SystemPolicy_Stats{
  55. OutboundUplink: true,
  56. OutboundDownlink: true,
  57. },
  58. },
  59. }),
  60. },
  61. }
  62. v, _ := core.New(config)
  63. v.AddFeature((outbound.Manager)(new(Manager)))
  64. ctx := toContext(context.Background(), v)
  65. h, _ := NewHandler(ctx, &core.OutboundHandlerConfig{
  66. Tag: "tag",
  67. ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
  68. })
  69. conn, _ := h.(*Handler).Dial(ctx, net.TCPDestination(net.DomainAddress("localhost"), 13146))
  70. _, ok := conn.(*internet.StatCouterConnection)
  71. if !ok {
  72. t.Errorf("Expected conn to be StatCouterConnection")
  73. }
  74. }