|
1 | 1 | package xray |
2 | 2 |
|
3 | | -import "testing" |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/xtls/xray-core/infra/conf" |
| 9 | +) |
4 | 10 |
|
5 | 11 | func TestShouldIncludeObservatoryOutbound(t *testing.T) { |
6 | 12 | protocolByTag := map[string]string{ |
@@ -58,3 +64,63 @@ func TestApplyAPISanitizesObservatorySelectors(t *testing.T) { |
58 | 64 | t.Fatalf("unexpected burst observatory selectors: %#v", burst) |
59 | 65 | } |
60 | 66 | } |
| 67 | + |
| 68 | +func TestApplyAPIAddsMalformedDomainGuardWhenBlackholeExists(t *testing.T) { |
| 69 | + cfg := &Config{ |
| 70 | + InboundConfigs: []*Inbound{}, |
| 71 | + OutboundConfigs: []any{ |
| 72 | + map[string]any{"tag": "direct", "protocol": "freedom"}, |
| 73 | + map[string]any{"tag": "Block", "protocol": "blackhole"}, |
| 74 | + }, |
| 75 | + } |
| 76 | + |
| 77 | + if err := cfg.ApplyAPI(10001, 10002); err != nil { |
| 78 | + t.Fatal(err) |
| 79 | + } |
| 80 | + |
| 81 | + if len(cfg.RouterConfig.RuleList) < 2 { |
| 82 | + t.Fatalf("expected API and malformed-domain guard rules, got %d", len(cfg.RouterConfig.RuleList)) |
| 83 | + } |
| 84 | + |
| 85 | + rule := string(cfg.RouterConfig.RuleList[1]) |
| 86 | + if !containsAll(rule, malformedDomainGuardRuleTag, `"outboundTag":"Block"`, `regexp:^.{254,}$`) { |
| 87 | + t.Fatalf("unexpected malformed-domain guard rule: %s", rule) |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +func TestApplyAPIRemovesExistingMalformedDomainGuard(t *testing.T) { |
| 92 | + cfg := &Config{ |
| 93 | + InboundConfigs: []*Inbound{}, |
| 94 | + OutboundConfigs: []any{ |
| 95 | + map[string]any{"tag": "Block", "protocol": "blackhole"}, |
| 96 | + }, |
| 97 | + RouterConfig: &conf.RouterConfig{ |
| 98 | + RuleList: []json.RawMessage{ |
| 99 | + json.RawMessage(`{"type":"field","ruleTag":"PG_NODE_MALFORMED_DOMAIN_GUARD","outboundTag":"old","domain":["regexp:^.{254,}$"]}`), |
| 100 | + }, |
| 101 | + }, |
| 102 | + } |
| 103 | + |
| 104 | + if err := cfg.ApplyAPI(10001, 10002); err != nil { |
| 105 | + t.Fatal(err) |
| 106 | + } |
| 107 | + |
| 108 | + count := 0 |
| 109 | + for _, raw := range cfg.RouterConfig.RuleList { |
| 110 | + if strings.Contains(string(raw), malformedDomainGuardRuleTag) { |
| 111 | + count++ |
| 112 | + } |
| 113 | + } |
| 114 | + if count != 1 { |
| 115 | + t.Fatalf("expected one malformed-domain guard rule, got %d", count) |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +func containsAll(s string, values ...string) bool { |
| 120 | + for _, value := range values { |
| 121 | + if !strings.Contains(s, value) { |
| 122 | + return false |
| 123 | + } |
| 124 | + } |
| 125 | + return true |
| 126 | +} |
0 commit comments