socks_bridge_test.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package amneziawgnet
  2. import (
  3. "encoding/json"
  4. "testing"
  5. )
  6. func TestBuildSocksBridge_BridgesAndPreservesSiblings(t *testing.T) {
  7. raw := []byte(`{
  8. "protocol": "amneziawg",
  9. "tag": "awg-hop",
  10. "sendThrough": "0.0.0.0",
  11. "targetStrategy": {"strategy": "UseIPv4"},
  12. "mux": {"enabled": false},
  13. "streamSettings": {"sockopt": {"tcpFastOpen": true}},
  14. "settings": {"secretKey": "x"}
  15. }`)
  16. out, ok := BuildSocksBridge(raw)
  17. if !ok {
  18. t.Fatal("valid entry rejected")
  19. }
  20. var got map[string]any
  21. if err := json.Unmarshal(out, &got); err != nil {
  22. t.Fatal(err)
  23. }
  24. if got["protocol"] != "socks" {
  25. t.Fatalf("protocol = %v", got["protocol"])
  26. }
  27. if got["tag"] != "awg-hop" || got["sendThrough"] != "0.0.0.0" {
  28. t.Fatalf("siblings dropped: %v", got)
  29. }
  30. if _, ok := got["targetStrategy"].(map[string]any); !ok {
  31. t.Fatalf("targetStrategy dropped: %v", got["targetStrategy"])
  32. }
  33. settings, _ := got["settings"].(map[string]any)
  34. if settings == nil || settings["user"] != "awg-hop" || settings["address"] != "127.0.0.1" {
  35. t.Fatalf("bridge settings wrong: %v", settings)
  36. }
  37. }
  38. func TestBuildSocksBridge_RejectsUnusableTags(t *testing.T) {
  39. for name, raw := range map[string][]byte{
  40. "missing tag": []byte(`{"protocol":"amneziawg","settings":{}}`),
  41. "empty tag": []byte(`{"protocol":"amneziawg","tag":"","settings":{}}`),
  42. "non-string tag": []byte(`{"protocol":"amneziawg","tag":123,"settings":{}}`),
  43. "not an object": []byte(`[1,2,3]`),
  44. } {
  45. if _, ok := BuildSocksBridge(raw); ok {
  46. t.Fatalf("%s: expected rejection", name)
  47. }
  48. }
  49. }