github.com/database64128/shadowsocks-go@v1.10.2-0.20240315062903-143a773533f1/domainset/domainset_test.go (about)

     1  package domainset
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  )
     7  
     8  const testDomainSetText = `# shadowsocks-go domain set capacity hint 1 6 1 1 DSKR
     9  domain:www.example.net
    10  suffix:example.com
    11  suffix:github.com
    12  suffix:cube64128.xyz
    13  suffix:api.ipify.org
    14  suffix:api6.ipify.org
    15  suffix:archlinux.org
    16  keyword:dev
    17  regexp:^adservice\.google\.([a-z]{2}|com?)(\.[a-z]{2})?$
    18  `
    19  
    20  var testDomainSetBuilder = mustDomainSetBuilderFromText(testDomainSetText)
    21  
    22  func mustDomainSetBuilderFromText(s string) Builder {
    23  	dsb, err := BuilderFromText(s)
    24  	if err != nil {
    25  		panic(err)
    26  	}
    27  	return dsb
    28  }
    29  
    30  func testMatch(t *testing.T, ds DomainSet, domain string, expectedResult bool) {
    31  	if ds.Match(domain) != expectedResult {
    32  		t.Errorf("%s should return %v", domain, expectedResult)
    33  	}
    34  }
    35  
    36  func testDomainSet(t *testing.T, ds DomainSet) {
    37  	testMatch(t, ds, "net", false)
    38  	testMatch(t, ds, "example.net", false)
    39  	testMatch(t, ds, "www.example.net", true)
    40  	testMatch(t, ds, "wwww.example.net", false)
    41  	testMatch(t, ds, "test.www.example.net", false)
    42  	testMatch(t, ds, "com", false)
    43  	testMatch(t, ds, "example.com", true)
    44  	testMatch(t, ds, "www.example.com", true)
    45  	testMatch(t, ds, "gobyexample.com", false)
    46  	testMatch(t, ds, "example.org", false)
    47  	testMatch(t, ds, "github.com", true)
    48  	testMatch(t, ds, "api.github.com", true)
    49  	testMatch(t, ds, "raw.githubusercontent.com", false)
    50  	testMatch(t, ds, "github.blog", false)
    51  	testMatch(t, ds, "cube64128.xyz", true)
    52  	testMatch(t, ds, "www.cube64128.xyz", true)
    53  	testMatch(t, ds, "notcube64128.xyz", false)
    54  	testMatch(t, ds, "org", false)
    55  	testMatch(t, ds, "ipify.org", false)
    56  	testMatch(t, ds, "api.ipify.org", true)
    57  	testMatch(t, ds, "api6.ipify.org", true)
    58  	testMatch(t, ds, "api64.ipify.org", false)
    59  	testMatch(t, ds, "www.ipify.org", false)
    60  	testMatch(t, ds, "archlinux.org", true)
    61  	testMatch(t, ds, "aur.archlinux.org", true)
    62  	testMatch(t, ds, "wikipedia.org", false)
    63  	testMatch(t, ds, "dev", true)
    64  	testMatch(t, ds, "go.dev", true)
    65  	testMatch(t, ds, "drewdevault.com", true)
    66  	testMatch(t, ds, "developer.mozilla.org", true)
    67  	testMatch(t, ds, "adservice.google.com", true)
    68  }
    69  
    70  func TestDomainSetFromText(t *testing.T) {
    71  	dsb, err := BuilderFromTextFast(testDomainSetText)
    72  	if err != nil {
    73  		t.Fatal(err)
    74  	}
    75  	ds, err := dsb.DomainSet()
    76  	if err != nil {
    77  		t.Fatal(err)
    78  	}
    79  	testDomainSet(t, ds)
    80  }
    81  
    82  func TestDomainSetFromGob(t *testing.T) {
    83  	var buf bytes.Buffer
    84  	if err := testDomainSetBuilder.WriteGob(&buf); err != nil {
    85  		t.Fatal(err)
    86  	}
    87  	dsb, err := BuilderFromGob(&buf)
    88  	if err != nil {
    89  		t.Fatal(err)
    90  	}
    91  	ds, err := dsb.DomainSet()
    92  	if err != nil {
    93  		t.Fatal(err)
    94  	}
    95  	testDomainSet(t, ds)
    96  }
    97  
    98  func TestBuilderWriteText(t *testing.T) {
    99  	var buf bytes.Buffer
   100  	if err := testDomainSetBuilder.WriteText(&buf); err != nil {
   101  		t.Fatal(err)
   102  	}
   103  	dsb, err := BuilderFromText(buf.String())
   104  	if err != nil {
   105  		t.Fatal(err)
   106  	}
   107  	ds, err := dsb.DomainSet()
   108  	if err != nil {
   109  		t.Fatal(err)
   110  	}
   111  	testDomainSet(t, ds)
   112  }