handler_test.go 2.2 KB

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