github.com/ck00004/CobaltStrikeParser-Go@v1.0.14/lib/http/cookiejar/jar_test.go (about)

     1  // Copyright 2013 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package cookiejar
     6  
     7  import (
     8  	"fmt"
     9  	"sort"
    10  	"strings"
    11  	"testing"
    12  	"time"
    13  
    14  	"github.com/ck00004/CobaltStrikeParser-Go/lib/url"
    15  
    16  	"github.com/ck00004/CobaltStrikeParser-Go/lib/http"
    17  )
    18  
    19  // tNow is the synthetic current time used as now during testing.
    20  var tNow = time.Date(2013, 1, 1, 12, 0, 0, 0, time.UTC)
    21  
    22  // testPSL implements PublicSuffixList with just two rules: "co.uk"
    23  // and the default rule "*".
    24  // The implementation has two intentional bugs:
    25  //    PublicSuffix("www.buggy.psl") == "xy"
    26  //    PublicSuffix("www2.buggy.psl") == "com"
    27  type testPSL struct{}
    28  
    29  func (testPSL) String() string {
    30  	return "testPSL"
    31  }
    32  func (testPSL) PublicSuffix(d string) string {
    33  	if d == "co.uk" || strings.HasSuffix(d, ".co.uk") {
    34  		return "co.uk"
    35  	}
    36  	if d == "www.buggy.psl" {
    37  		return "xy"
    38  	}
    39  	if d == "www2.buggy.psl" {
    40  		return "com"
    41  	}
    42  	return d[strings.LastIndex(d, ".")+1:]
    43  }
    44  
    45  // newTestJar creates an empty Jar with testPSL as the public suffix list.
    46  func newTestJar() *Jar {
    47  	jar, err := New(&Options{PublicSuffixList: testPSL{}})
    48  	if err != nil {
    49  		panic(err)
    50  	}
    51  	return jar
    52  }
    53  
    54  var hasDotSuffixTests = [...]struct {
    55  	s, suffix string
    56  }{
    57  	{"", ""},
    58  	{"", "."},
    59  	{"", "x"},
    60  	{".", ""},
    61  	{".", "."},
    62  	{".", ".."},
    63  	{".", "x"},
    64  	{".", "x."},
    65  	{".", ".x"},
    66  	{".", ".x."},
    67  	{"x", ""},
    68  	{"x", "."},
    69  	{"x", ".."},
    70  	{"x", "x"},
    71  	{"x", "x."},
    72  	{"x", ".x"},
    73  	{"x", ".x."},
    74  	{".x", ""},
    75  	{".x", "."},
    76  	{".x", ".."},
    77  	{".x", "x"},
    78  	{".x", "x."},
    79  	{".x", ".x"},
    80  	{".x", ".x."},
    81  	{"x.", ""},
    82  	{"x.", "."},
    83  	{"x.", ".."},
    84  	{"x.", "x"},
    85  	{"x.", "x."},
    86  	{"x.", ".x"},
    87  	{"x.", ".x."},
    88  	{"com", ""},
    89  	{"com", "m"},
    90  	{"com", "om"},
    91  	{"com", "com"},
    92  	{"com", ".com"},
    93  	{"com", "x.com"},
    94  	{"com", "xcom"},
    95  	{"com", "xorg"},
    96  	{"com", "org"},
    97  	{"com", "rg"},
    98  	{"foo.com", ""},
    99  	{"foo.com", "m"},
   100  	{"foo.com", "om"},
   101  	{"foo.com", "com"},
   102  	{"foo.com", ".com"},
   103  	{"foo.com", "o.com"},
   104  	{"foo.com", "oo.com"},
   105  	{"foo.com", "foo.com"},
   106  	{"foo.com", ".foo.com"},
   107  	{"foo.com", "x.foo.com"},
   108  	{"foo.com", "xfoo.com"},
   109  	{"foo.com", "xfoo.org"},
   110  	{"foo.com", "foo.org"},
   111  	{"foo.com", "oo.org"},
   112  	{"foo.com", "o.org"},
   113  	{"foo.com", ".org"},
   114  	{"foo.com", "org"},
   115  	{"foo.com", "rg"},
   116  }
   117  
   118  func TestHasDotSuffix(t *testing.T) {
   119  	for _, tc := range hasDotSuffixTests {
   120  		got := hasDotSuffix(tc.s, tc.suffix)
   121  		want := strings.HasSuffix(tc.s, "."+tc.suffix)
   122  		if got != want {
   123  			t.Errorf("s=%q, suffix=%q: got %v, want %v", tc.s, tc.suffix, got, want)
   124  		}
   125  	}
   126  }
   127  
   128  var canonicalHostTests = map[string]string{
   129  	"www.example.com":         "www.example.com",
   130  	"WWW.EXAMPLE.COM":         "www.example.com",
   131  	"wWw.eXAmple.CoM":         "www.example.com",
   132  	"www.example.com:80":      "www.example.com",
   133  	"192.168.0.10":            "192.168.0.10",
   134  	"192.168.0.5:8080":        "192.168.0.5",
   135  	"2001:4860:0:2001::68":    "2001:4860:0:2001::68",
   136  	"[2001:4860:0:::68]:8080": "2001:4860:0:::68",
   137  	"www.bücher.de":           "www.xn--bcher-kva.de",
   138  	"www.example.com.":        "www.example.com",
   139  	// TODO: Fix canonicalHost so that all of the following malformed
   140  	// domain names trigger an error. (This list is not exhaustive, e.g.
   141  	// malformed internationalized domain names are missing.)
   142  	".":                       "",
   143  	"..":                      ".",
   144  	"...":                     "..",
   145  	".net":                    ".net",
   146  	".net.":                   ".net",
   147  	"a..":                     "a.",
   148  	"b.a..":                   "b.a.",
   149  	"weird.stuff...":          "weird.stuff..",
   150  	"[bad.unmatched.bracket:": "error",
   151  }
   152  
   153  func TestCanonicalHost(t *testing.T) {
   154  	for h, want := range canonicalHostTests {
   155  		got, err := canonicalHost(h)
   156  		if want == "error" {
   157  			if err == nil {
   158  				t.Errorf("%q: got %q and nil error, want non-nil", h, got)
   159  			}
   160  			continue
   161  		}
   162  		if err != nil {
   163  			t.Errorf("%q: %v", h, err)
   164  			continue
   165  		}
   166  		if got != want {
   167  			t.Errorf("%q: got %q, want %q", h, got, want)
   168  			continue
   169  		}
   170  	}
   171  }
   172  
   173  var hasPortTests = map[string]bool{
   174  	"www.example.com":      false,
   175  	"www.example.com:80":   true,
   176  	"127.0.0.1":            false,
   177  	"127.0.0.1:8080":       true,
   178  	"2001:4860:0:2001::68": false,
   179  	"[2001::0:::68]:80":    true,
   180  }
   181  
   182  func TestHasPort(t *testing.T) {
   183  	for host, want := range hasPortTests {
   184  		if got := hasPort(host); got != want {
   185  			t.Errorf("%q: got %t, want %t", host, got, want)
   186  		}
   187  	}
   188  }
   189  
   190  var jarKeyTests = map[string]string{
   191  	"foo.www.example.com": "example.com",
   192  	"www.example.com":     "example.com",
   193  	"example.com":         "example.com",
   194  	"com":                 "com",
   195  	"foo.www.bbc.co.uk":   "bbc.co.uk",
   196  	"www.bbc.co.uk":       "bbc.co.uk",
   197  	"bbc.co.uk":           "bbc.co.uk",
   198  	"co.uk":               "co.uk",
   199  	"uk":                  "uk",
   200  	"192.168.0.5":         "192.168.0.5",
   201  	"www.buggy.psl":       "www.buggy.psl",
   202  	"www2.buggy.psl":      "buggy.psl",
   203  	// The following are actual outputs of canonicalHost for
   204  	// malformed inputs to canonicalHost (see above).
   205  	"":              "",
   206  	".":             ".",
   207  	"..":            ".",
   208  	".net":          ".net",
   209  	"a.":            "a.",
   210  	"b.a.":          "a.",
   211  	"weird.stuff..": ".",
   212  }
   213  
   214  func TestJarKey(t *testing.T) {
   215  	for host, want := range jarKeyTests {
   216  		if got := jarKey(host, testPSL{}); got != want {
   217  			t.Errorf("%q: got %q, want %q", host, got, want)
   218  		}
   219  	}
   220  }
   221  
   222  var jarKeyNilPSLTests = map[string]string{
   223  	"foo.www.example.com": "example.com",
   224  	"www.example.com":     "example.com",
   225  	"example.com":         "example.com",
   226  	"com":                 "com",
   227  	"foo.www.bbc.co.uk":   "co.uk",
   228  	"www.bbc.co.uk":       "co.uk",
   229  	"bbc.co.uk":           "co.uk",
   230  	"co.uk":               "co.uk",
   231  	"uk":                  "uk",
   232  	"192.168.0.5":         "192.168.0.5",
   233  	// The following are actual outputs of canonicalHost for
   234  	// malformed inputs to canonicalHost.
   235  	"":              "",
   236  	".":             ".",
   237  	"..":            "..",
   238  	".net":          ".net",
   239  	"a.":            "a.",
   240  	"b.a.":          "a.",
   241  	"weird.stuff..": "stuff..",
   242  }
   243  
   244  func TestJarKeyNilPSL(t *testing.T) {
   245  	for host, want := range jarKeyNilPSLTests {
   246  		if got := jarKey(host, nil); got != want {
   247  			t.Errorf("%q: got %q, want %q", host, got, want)
   248  		}
   249  	}
   250  }
   251  
   252  var isIPTests = map[string]bool{
   253  	"127.0.0.1":            true,
   254  	"1.2.3.4":              true,
   255  	"2001:4860:0:2001::68": true,
   256  	"example.com":          false,
   257  	"1.1.1.300":            false,
   258  	"www.foo.bar.net":      false,
   259  	"123.foo.bar.net":      false,
   260  }
   261  
   262  func TestIsIP(t *testing.T) {
   263  	for host, want := range isIPTests {
   264  		if got := isIP(host); got != want {
   265  			t.Errorf("%q: got %t, want %t", host, got, want)
   266  		}
   267  	}
   268  }
   269  
   270  var defaultPathTests = map[string]string{
   271  	"/":           "/",
   272  	"/abc":        "/",
   273  	"/abc/":       "/abc",
   274  	"/abc/xyz":    "/abc",
   275  	"/abc/xyz/":   "/abc/xyz",
   276  	"/a/b/c.html": "/a/b",
   277  	"":            "/",
   278  	"strange":     "/",
   279  	"//":          "/",
   280  	"/a//b":       "/a/",
   281  	"/a/./b":      "/a/.",
   282  	"/a/../b":     "/a/..",
   283  }
   284  
   285  func TestDefaultPath(t *testing.T) {
   286  	for path, want := range defaultPathTests {
   287  		if got := defaultPath(path); got != want {
   288  			t.Errorf("%q: got %q, want %q", path, got, want)
   289  		}
   290  	}
   291  }
   292  
   293  var domainAndTypeTests = [...]struct {
   294  	host         string // host Set-Cookie header was received from
   295  	domain       string // domain attribute in Set-Cookie header
   296  	wantDomain   string // expected domain of cookie
   297  	wantHostOnly bool   // expected host-cookie flag
   298  	wantErr      error  // expected error
   299  }{
   300  	{"www.example.com", "", "www.example.com", true, nil},
   301  	{"127.0.0.1", "", "127.0.0.1", true, nil},
   302  	{"2001:4860:0:2001::68", "", "2001:4860:0:2001::68", true, nil},
   303  	{"www.example.com", "example.com", "example.com", false, nil},
   304  	{"www.example.com", ".example.com", "example.com", false, nil},
   305  	{"www.example.com", "www.example.com", "www.example.com", false, nil},
   306  	{"www.example.com", ".www.example.com", "www.example.com", false, nil},
   307  	{"foo.sso.example.com", "sso.example.com", "sso.example.com", false, nil},
   308  	{"bar.co.uk", "bar.co.uk", "bar.co.uk", false, nil},
   309  	{"foo.bar.co.uk", ".bar.co.uk", "bar.co.uk", false, nil},
   310  	{"127.0.0.1", "127.0.0.1", "", false, errNoHostname},
   311  	{"2001:4860:0:2001::68", "2001:4860:0:2001::68", "2001:4860:0:2001::68", false, errNoHostname},
   312  	{"www.example.com", ".", "", false, errMalformedDomain},
   313  	{"www.example.com", "..", "", false, errMalformedDomain},
   314  	{"www.example.com", "other.com", "", false, errIllegalDomain},
   315  	{"www.example.com", "com", "", false, errIllegalDomain},
   316  	{"www.example.com", ".com", "", false, errIllegalDomain},
   317  	{"foo.bar.co.uk", ".co.uk", "", false, errIllegalDomain},
   318  	{"127.www.0.0.1", "127.0.0.1", "", false, errIllegalDomain},
   319  	{"com", "", "com", true, nil},
   320  	{"com", "com", "com", true, nil},
   321  	{"com", ".com", "com", true, nil},
   322  	{"co.uk", "", "co.uk", true, nil},
   323  	{"co.uk", "co.uk", "co.uk", true, nil},
   324  	{"co.uk", ".co.uk", "co.uk", true, nil},
   325  }
   326  
   327  func TestDomainAndType(t *testing.T) {
   328  	jar := newTestJar()
   329  	for _, tc := range domainAndTypeTests {
   330  		domain, hostOnly, err := jar.domainAndType(tc.host, tc.domain)
   331  		if err != tc.wantErr {
   332  			t.Errorf("%q/%q: got %q error, want %q",
   333  				tc.host, tc.domain, err, tc.wantErr)
   334  			continue
   335  		}
   336  		if err != nil {
   337  			continue
   338  		}
   339  		if domain != tc.wantDomain || hostOnly != tc.wantHostOnly {
   340  			t.Errorf("%q/%q: got %q/%t want %q/%t",
   341  				tc.host, tc.domain, domain, hostOnly,
   342  				tc.wantDomain, tc.wantHostOnly)
   343  		}
   344  	}
   345  }
   346  
   347  // expiresIn creates an expires attribute delta seconds from tNow.
   348  func expiresIn(delta int) string {
   349  	t := tNow.Add(time.Duration(delta) * time.Second)
   350  	return "expires=" + t.Format(time.RFC1123)
   351  }
   352  
   353  // mustParseURL parses s to an URL and panics on error.
   354  func mustParseURL(s string) *url.URL {
   355  	u, err := url.Parse(s)
   356  	if err != nil || u.Scheme == "" || u.Host == "" {
   357  		panic(fmt.Sprintf("Unable to parse URL %s.", s))
   358  	}
   359  	return u
   360  }
   361  
   362  // jarTest encapsulates the following actions on a jar:
   363  //   1. Perform SetCookies with fromURL and the cookies from setCookies.
   364  //      (Done at time tNow + 0 ms.)
   365  //   2. Check that the entries in the jar matches content.
   366  //      (Done at time tNow + 1001 ms.)
   367  //   3. For each query in tests: Check that Cookies with toURL yields the
   368  //      cookies in want.
   369  //      (Query n done at tNow + (n+2)*1001 ms.)
   370  type jarTest struct {
   371  	description string   // The description of what this test is supposed to test
   372  	fromURL     string   // The full URL of the request from which Set-Cookie headers where received
   373  	setCookies  []string // All the cookies received from fromURL
   374  	content     string   // The whole (non-expired) content of the jar
   375  	queries     []query  // Queries to test the Jar.Cookies method
   376  }
   377  
   378  // query contains one test of the cookies returned from Jar.Cookies.
   379  type query struct {
   380  	toURL string // the URL in the Cookies call
   381  	want  string // the expected list of cookies (order matters)
   382  }
   383  
   384  // run runs the jarTest.
   385  func (test jarTest) run(t *testing.T, jar *Jar) {
   386  	now := tNow
   387  
   388  	// Populate jar with cookies.
   389  	setCookies := make([]*http.Cookie, len(test.setCookies))
   390  	for i, cs := range test.setCookies {
   391  		cookies := (&http.Response{Header: http.Header{"Set-Cookie": {cs}}}).Cookies()
   392  		if len(cookies) != 1 {
   393  			panic(fmt.Sprintf("Wrong cookie line %q: %#v", cs, cookies))
   394  		}
   395  		setCookies[i] = cookies[0]
   396  	}
   397  	jar.setCookies(mustParseURL(test.fromURL), setCookies, now)
   398  	now = now.Add(1001 * time.Millisecond)
   399  
   400  	// Serialize non-expired entries in the form "name1=val1 name2=val2".
   401  	var cs []string
   402  	for _, submap := range jar.entries {
   403  		for _, cookie := range submap {
   404  			if !cookie.Expires.After(now) {
   405  				continue
   406  			}
   407  			cs = append(cs, cookie.Name+"="+cookie.Value)
   408  		}
   409  	}
   410  	sort.Strings(cs)
   411  	got := strings.Join(cs, " ")
   412  
   413  	// Make sure jar content matches our expectations.
   414  	if got != test.content {
   415  		t.Errorf("Test %q Content\ngot  %q\nwant %q",
   416  			test.description, got, test.content)
   417  	}
   418  
   419  	// Test different calls to Cookies.
   420  	for i, query := range test.queries {
   421  		now = now.Add(1001 * time.Millisecond)
   422  		var s []string
   423  		for _, c := range jar.cookies(mustParseURL(query.toURL), now) {
   424  			s = append(s, c.Name+"="+c.Value)
   425  		}
   426  		if got := strings.Join(s, " "); got != query.want {
   427  			t.Errorf("Test %q #%d\ngot  %q\nwant %q", test.description, i, got, query.want)
   428  		}
   429  	}
   430  }
   431  
   432  // basicsTests contains fundamental tests. Each jarTest has to be performed on
   433  // a fresh, empty Jar.
   434  var basicsTests = [...]jarTest{
   435  	{
   436  		"Retrieval of a plain host cookie.",
   437  		"http://www.host.test/",
   438  		[]string{"A=a"},
   439  		"A=a",
   440  		[]query{
   441  			{"http://www.host.test", "A=a"},
   442  			{"http://www.host.test/", "A=a"},
   443  			{"http://www.host.test/some/path", "A=a"},
   444  			{"https://www.host.test", "A=a"},
   445  			{"https://www.host.test/", "A=a"},
   446  			{"https://www.host.test/some/path", "A=a"},
   447  			{"ftp://www.host.test", ""},
   448  			{"ftp://www.host.test/", ""},
   449  			{"ftp://www.host.test/some/path", ""},
   450  			{"http://www.other.org", ""},
   451  			{"http://sibling.host.test", ""},
   452  			{"http://deep.www.host.test", ""},
   453  		},
   454  	},
   455  	{
   456  		"Secure cookies are not returned to http.",
   457  		"http://www.host.test/",
   458  		[]string{"A=a; secure"},
   459  		"A=a",
   460  		[]query{
   461  			{"http://www.host.test", ""},
   462  			{"http://www.host.test/", ""},
   463  			{"http://www.host.test/some/path", ""},
   464  			{"https://www.host.test", "A=a"},
   465  			{"https://www.host.test/", "A=a"},
   466  			{"https://www.host.test/some/path", "A=a"},
   467  		},
   468  	},
   469  	{
   470  		"Explicit path.",
   471  		"http://www.host.test/",
   472  		[]string{"A=a; path=/some/path"},
   473  		"A=a",
   474  		[]query{
   475  			{"http://www.host.test", ""},
   476  			{"http://www.host.test/", ""},
   477  			{"http://www.host.test/some", ""},
   478  			{"http://www.host.test/some/", ""},
   479  			{"http://www.host.test/some/path", "A=a"},
   480  			{"http://www.host.test/some/paths", ""},
   481  			{"http://www.host.test/some/path/foo", "A=a"},
   482  			{"http://www.host.test/some/path/foo/", "A=a"},
   483  		},
   484  	},
   485  	{
   486  		"Implicit path #1: path is a directory.",
   487  		"http://www.host.test/some/path/",
   488  		[]string{"A=a"},
   489  		"A=a",
   490  		[]query{
   491  			{"http://www.host.test", ""},
   492  			{"http://www.host.test/", ""},
   493  			{"http://www.host.test/some", ""},
   494  			{"http://www.host.test/some/", ""},
   495  			{"http://www.host.test/some/path", "A=a"},
   496  			{"http://www.host.test/some/paths", ""},
   497  			{"http://www.host.test/some/path/foo", "A=a"},
   498  			{"http://www.host.test/some/path/foo/", "A=a"},
   499  		},
   500  	},
   501  	{
   502  		"Implicit path #2: path is not a directory.",
   503  		"http://www.host.test/some/path/index.html",
   504  		[]string{"A=a"},
   505  		"A=a",
   506  		[]query{
   507  			{"http://www.host.test", ""},
   508  			{"http://www.host.test/", ""},
   509  			{"http://www.host.test/some", ""},
   510  			{"http://www.host.test/some/", ""},
   511  			{"http://www.host.test/some/path", "A=a"},
   512  			{"http://www.host.test/some/paths", ""},
   513  			{"http://www.host.test/some/path/foo", "A=a"},
   514  			{"http://www.host.test/some/path/foo/", "A=a"},
   515  		},
   516  	},
   517  	{
   518  		"Implicit path #3: no path in URL at all.",
   519  		"http://www.host.test",
   520  		[]string{"A=a"},
   521  		"A=a",
   522  		[]query{
   523  			{"http://www.host.test", "A=a"},
   524  			{"http://www.host.test/", "A=a"},
   525  			{"http://www.host.test/some/path", "A=a"},
   526  		},
   527  	},
   528  	{
   529  		"Cookies are sorted by path length.",
   530  		"http://www.host.test/",
   531  		[]string{
   532  			"A=a; path=/foo/bar",
   533  			"B=b; path=/foo/bar/baz/qux",
   534  			"C=c; path=/foo/bar/baz",
   535  			"D=d; path=/foo"},
   536  		"A=a B=b C=c D=d",
   537  		[]query{
   538  			{"http://www.host.test/foo/bar/baz/qux", "B=b C=c A=a D=d"},
   539  			{"http://www.host.test/foo/bar/baz/", "C=c A=a D=d"},
   540  			{"http://www.host.test/foo/bar", "A=a D=d"},
   541  		},
   542  	},
   543  	{
   544  		"Creation time determines sorting on same length paths.",
   545  		"http://www.host.test/",
   546  		[]string{
   547  			"A=a; path=/foo/bar",
   548  			"X=x; path=/foo/bar",
   549  			"Y=y; path=/foo/bar/baz/qux",
   550  			"B=b; path=/foo/bar/baz/qux",
   551  			"C=c; path=/foo/bar/baz",
   552  			"W=w; path=/foo/bar/baz",
   553  			"Z=z; path=/foo",
   554  			"D=d; path=/foo"},
   555  		"A=a B=b C=c D=d W=w X=x Y=y Z=z",
   556  		[]query{
   557  			{"http://www.host.test/foo/bar/baz/qux", "Y=y B=b C=c W=w A=a X=x Z=z D=d"},
   558  			{"http://www.host.test/foo/bar/baz/", "C=c W=w A=a X=x Z=z D=d"},
   559  			{"http://www.host.test/foo/bar", "A=a X=x Z=z D=d"},
   560  		},
   561  	},
   562  	{
   563  		"Sorting of same-name cookies.",
   564  		"http://www.host.test/",
   565  		[]string{
   566  			"A=1; path=/",
   567  			"A=2; path=/path",
   568  			"A=3; path=/quux",
   569  			"A=4; path=/path/foo",
   570  			"A=5; domain=.host.test; path=/path",
   571  			"A=6; domain=.host.test; path=/quux",
   572  			"A=7; domain=.host.test; path=/path/foo",
   573  		},
   574  		"A=1 A=2 A=3 A=4 A=5 A=6 A=7",
   575  		[]query{
   576  			{"http://www.host.test/path", "A=2 A=5 A=1"},
   577  			{"http://www.host.test/path/foo", "A=4 A=7 A=2 A=5 A=1"},
   578  		},
   579  	},
   580  	{
   581  		"Disallow domain cookie on public suffix.",
   582  		"http://www.bbc.co.uk",
   583  		[]string{
   584  			"a=1",
   585  			"b=2; domain=co.uk",
   586  		},
   587  		"a=1",
   588  		[]query{{"http://www.bbc.co.uk", "a=1"}},
   589  	},
   590  	{
   591  		"Host cookie on IP.",
   592  		"http://192.168.0.10",
   593  		[]string{"a=1"},
   594  		"a=1",
   595  		[]query{{"http://192.168.0.10", "a=1"}},
   596  	},
   597  	{
   598  		"Port is ignored #1.",
   599  		"http://www.host.test/",
   600  		[]string{"a=1"},
   601  		"a=1",
   602  		[]query{
   603  			{"http://www.host.test", "a=1"},
   604  			{"http://www.host.test:8080/", "a=1"},
   605  		},
   606  	},
   607  	{
   608  		"Port is ignored #2.",
   609  		"http://www.host.test:8080/",
   610  		[]string{"a=1"},
   611  		"a=1",
   612  		[]query{
   613  			{"http://www.host.test", "a=1"},
   614  			{"http://www.host.test:8080/", "a=1"},
   615  			{"http://www.host.test:1234/", "a=1"},
   616  		},
   617  	},
   618  }
   619  
   620  func TestBasics(t *testing.T) {
   621  	for _, test := range basicsTests {
   622  		jar := newTestJar()
   623  		test.run(t, jar)
   624  	}
   625  }
   626  
   627  // updateAndDeleteTests contains jarTests which must be performed on the same
   628  // Jar.
   629  var updateAndDeleteTests = [...]jarTest{
   630  	{
   631  		"Set initial cookies.",
   632  		"http://www.host.test",
   633  		[]string{
   634  			"a=1",
   635  			"b=2; secure",
   636  			"c=3; httponly",
   637  			"d=4; secure; httponly"},
   638  		"a=1 b=2 c=3 d=4",
   639  		[]query{
   640  			{"http://www.host.test", "a=1 c=3"},
   641  			{"https://www.host.test", "a=1 b=2 c=3 d=4"},
   642  		},
   643  	},
   644  	{
   645  		"Update value via http.",
   646  		"http://www.host.test",
   647  		[]string{
   648  			"a=w",
   649  			"b=x; secure",
   650  			"c=y; httponly",
   651  			"d=z; secure; httponly"},
   652  		"a=w b=x c=y d=z",
   653  		[]query{
   654  			{"http://www.host.test", "a=w c=y"},
   655  			{"https://www.host.test", "a=w b=x c=y d=z"},
   656  		},
   657  	},
   658  	{
   659  		"Clear Secure flag from a http.",
   660  		"http://www.host.test/",
   661  		[]string{
   662  			"b=xx",
   663  			"d=zz; httponly"},
   664  		"a=w b=xx c=y d=zz",
   665  		[]query{{"http://www.host.test", "a=w b=xx c=y d=zz"}},
   666  	},
   667  	{
   668  		"Delete all.",
   669  		"http://www.host.test/",
   670  		[]string{
   671  			"a=1; max-Age=-1",                    // delete via MaxAge
   672  			"b=2; " + expiresIn(-10),             // delete via Expires
   673  			"c=2; max-age=-1; " + expiresIn(-10), // delete via both
   674  			"d=4; max-age=-1; " + expiresIn(10)}, // MaxAge takes precedence
   675  		"",
   676  		[]query{{"http://www.host.test", ""}},
   677  	},
   678  	{
   679  		"Refill #1.",
   680  		"http://www.host.test",
   681  		[]string{
   682  			"A=1",
   683  			"A=2; path=/foo",
   684  			"A=3; domain=.host.test",
   685  			"A=4; path=/foo; domain=.host.test"},
   686  		"A=1 A=2 A=3 A=4",
   687  		[]query{{"http://www.host.test/foo", "A=2 A=4 A=1 A=3"}},
   688  	},
   689  	{
   690  		"Refill #2.",
   691  		"http://www.google.com",
   692  		[]string{
   693  			"A=6",
   694  			"A=7; path=/foo",
   695  			"A=8; domain=.google.com",
   696  			"A=9; path=/foo; domain=.google.com"},
   697  		"A=1 A=2 A=3 A=4 A=6 A=7 A=8 A=9",
   698  		[]query{
   699  			{"http://www.host.test/foo", "A=2 A=4 A=1 A=3"},
   700  			{"http://www.google.com/foo", "A=7 A=9 A=6 A=8"},
   701  		},
   702  	},
   703  	{
   704  		"Delete A7.",
   705  		"http://www.google.com",
   706  		[]string{"A=; path=/foo; max-age=-1"},
   707  		"A=1 A=2 A=3 A=4 A=6 A=8 A=9",
   708  		[]query{
   709  			{"http://www.host.test/foo", "A=2 A=4 A=1 A=3"},
   710  			{"http://www.google.com/foo", "A=9 A=6 A=8"},
   711  		},
   712  	},
   713  	{
   714  		"Delete A4.",
   715  		"http://www.host.test",
   716  		[]string{"A=; path=/foo; domain=host.test; max-age=-1"},
   717  		"A=1 A=2 A=3 A=6 A=8 A=9",
   718  		[]query{
   719  			{"http://www.host.test/foo", "A=2 A=1 A=3"},
   720  			{"http://www.google.com/foo", "A=9 A=6 A=8"},
   721  		},
   722  	},
   723  	{
   724  		"Delete A6.",
   725  		"http://www.google.com",
   726  		[]string{"A=; max-age=-1"},
   727  		"A=1 A=2 A=3 A=8 A=9",
   728  		[]query{
   729  			{"http://www.host.test/foo", "A=2 A=1 A=3"},
   730  			{"http://www.google.com/foo", "A=9 A=8"},
   731  		},
   732  	},
   733  	{
   734  		"Delete A3.",
   735  		"http://www.host.test",
   736  		[]string{"A=; domain=host.test; max-age=-1"},
   737  		"A=1 A=2 A=8 A=9",
   738  		[]query{
   739  			{"http://www.host.test/foo", "A=2 A=1"},
   740  			{"http://www.google.com/foo", "A=9 A=8"},
   741  		},
   742  	},
   743  	{
   744  		"No cross-domain delete.",
   745  		"http://www.host.test",
   746  		[]string{
   747  			"A=; domain=google.com; max-age=-1",
   748  			"A=; path=/foo; domain=google.com; max-age=-1"},
   749  		"A=1 A=2 A=8 A=9",
   750  		[]query{
   751  			{"http://www.host.test/foo", "A=2 A=1"},
   752  			{"http://www.google.com/foo", "A=9 A=8"},
   753  		},
   754  	},
   755  	{
   756  		"Delete A8 and A9.",
   757  		"http://www.google.com",
   758  		[]string{
   759  			"A=; domain=google.com; max-age=-1",
   760  			"A=; path=/foo; domain=google.com; max-age=-1"},
   761  		"A=1 A=2",
   762  		[]query{
   763  			{"http://www.host.test/foo", "A=2 A=1"},
   764  			{"http://www.google.com/foo", ""},
   765  		},
   766  	},
   767  }
   768  
   769  func TestUpdateAndDelete(t *testing.T) {
   770  	jar := newTestJar()
   771  	for _, test := range updateAndDeleteTests {
   772  		test.run(t, jar)
   773  	}
   774  }
   775  
   776  func TestExpiration(t *testing.T) {
   777  	jar := newTestJar()
   778  	jarTest{
   779  		"Expiration.",
   780  		"http://www.host.test",
   781  		[]string{
   782  			"a=1",
   783  			"b=2; max-age=3",
   784  			"c=3; " + expiresIn(3),
   785  			"d=4; max-age=5",
   786  			"e=5; " + expiresIn(5),
   787  			"f=6; max-age=100",
   788  		},
   789  		"a=1 b=2 c=3 d=4 e=5 f=6", // executed at t0 + 1001 ms
   790  		[]query{
   791  			{"http://www.host.test", "a=1 b=2 c=3 d=4 e=5 f=6"}, // t0 + 2002 ms
   792  			{"http://www.host.test", "a=1 d=4 e=5 f=6"},         // t0 + 3003 ms
   793  			{"http://www.host.test", "a=1 d=4 e=5 f=6"},         // t0 + 4004 ms
   794  			{"http://www.host.test", "a=1 f=6"},                 // t0 + 5005 ms
   795  			{"http://www.host.test", "a=1 f=6"},                 // t0 + 6006 ms
   796  		},
   797  	}.run(t, jar)
   798  }
   799  
   800  //
   801  // Tests derived from Chromium's cookie_store_unittest.h.
   802  //
   803  
   804  // See http://src.chromium.org/viewvc/chrome/trunk/src/net/cookies/cookie_store_unittest.h?revision=159685&content-type=text/plain
   805  // Some of the original tests are in a bad condition (e.g.
   806  // DomainWithTrailingDotTest) or are not RFC 6265 conforming (e.g.
   807  // TestNonDottedAndTLD #1 and #6) and have not been ported.
   808  
   809  // chromiumBasicsTests contains fundamental tests. Each jarTest has to be
   810  // performed on a fresh, empty Jar.
   811  var chromiumBasicsTests = [...]jarTest{
   812  	{
   813  		"DomainWithTrailingDotTest.",
   814  		"http://www.google.com/",
   815  		[]string{
   816  			"a=1; domain=.www.google.com.",
   817  			"b=2; domain=.www.google.com.."},
   818  		"",
   819  		[]query{
   820  			{"http://www.google.com", ""},
   821  		},
   822  	},
   823  	{
   824  		"ValidSubdomainTest #1.",
   825  		"http://a.b.c.d.com",
   826  		[]string{
   827  			"a=1; domain=.a.b.c.d.com",
   828  			"b=2; domain=.b.c.d.com",
   829  			"c=3; domain=.c.d.com",
   830  			"d=4; domain=.d.com"},
   831  		"a=1 b=2 c=3 d=4",
   832  		[]query{
   833  			{"http://a.b.c.d.com", "a=1 b=2 c=3 d=4"},
   834  			{"http://b.c.d.com", "b=2 c=3 d=4"},
   835  			{"http://c.d.com", "c=3 d=4"},
   836  			{"http://d.com", "d=4"},
   837  		},
   838  	},
   839  	{
   840  		"ValidSubdomainTest #2.",
   841  		"http://a.b.c.d.com",
   842  		[]string{
   843  			"a=1; domain=.a.b.c.d.com",
   844  			"b=2; domain=.b.c.d.com",
   845  			"c=3; domain=.c.d.com",
   846  			"d=4; domain=.d.com",
   847  			"X=bcd; domain=.b.c.d.com",
   848  			"X=cd; domain=.c.d.com"},
   849  		"X=bcd X=cd a=1 b=2 c=3 d=4",
   850  		[]query{
   851  			{"http://b.c.d.com", "b=2 c=3 d=4 X=bcd X=cd"},
   852  			{"http://c.d.com", "c=3 d=4 X=cd"},
   853  		},
   854  	},
   855  	{
   856  		"InvalidDomainTest #1.",
   857  		"http://foo.bar.com",
   858  		[]string{
   859  			"a=1; domain=.yo.foo.bar.com",
   860  			"b=2; domain=.foo.com",
   861  			"c=3; domain=.bar.foo.com",
   862  			"d=4; domain=.foo.bar.com.net",
   863  			"e=5; domain=ar.com",
   864  			"f=6; domain=.",
   865  			"g=7; domain=/",
   866  			"h=8; domain=http://foo.bar.com",
   867  			"i=9; domain=..foo.bar.com",
   868  			"j=10; domain=..bar.com",
   869  			"k=11; domain=.foo.bar.com?blah",
   870  			"l=12; domain=.foo.bar.com/blah",
   871  			"m=12; domain=.foo.bar.com:80",
   872  			"n=14; domain=.foo.bar.com:",
   873  			"o=15; domain=.foo.bar.com#sup",
   874  		},
   875  		"", // Jar is empty.
   876  		[]query{{"http://foo.bar.com", ""}},
   877  	},
   878  	{
   879  		"InvalidDomainTest #2.",
   880  		"http://foo.com.com",
   881  		[]string{"a=1; domain=.foo.com.com.com"},
   882  		"",
   883  		[]query{{"http://foo.bar.com", ""}},
   884  	},
   885  	{
   886  		"DomainWithoutLeadingDotTest #1.",
   887  		"http://manage.hosted.filefront.com",
   888  		[]string{"a=1; domain=filefront.com"},
   889  		"a=1",
   890  		[]query{{"http://www.filefront.com", "a=1"}},
   891  	},
   892  	{
   893  		"DomainWithoutLeadingDotTest #2.",
   894  		"http://www.google.com",
   895  		[]string{"a=1; domain=www.google.com"},
   896  		"a=1",
   897  		[]query{
   898  			{"http://www.google.com", "a=1"},
   899  			{"http://sub.www.google.com", "a=1"},
   900  			{"http://something-else.com", ""},
   901  		},
   902  	},
   903  	{
   904  		"CaseInsensitiveDomainTest.",
   905  		"http://www.google.com",
   906  		[]string{
   907  			"a=1; domain=.GOOGLE.COM",
   908  			"b=2; domain=.www.gOOgLE.coM"},
   909  		"a=1 b=2",
   910  		[]query{{"http://www.google.com", "a=1 b=2"}},
   911  	},
   912  	{
   913  		"TestIpAddress #1.",
   914  		"http://1.2.3.4/foo",
   915  		[]string{"a=1; path=/"},
   916  		"a=1",
   917  		[]query{{"http://1.2.3.4/foo", "a=1"}},
   918  	},
   919  	{
   920  		"TestIpAddress #2.",
   921  		"http://1.2.3.4/foo",
   922  		[]string{
   923  			"a=1; domain=.1.2.3.4",
   924  			"b=2; domain=.3.4"},
   925  		"",
   926  		[]query{{"http://1.2.3.4/foo", ""}},
   927  	},
   928  	{
   929  		"TestIpAddress #3.",
   930  		"http://1.2.3.4/foo",
   931  		[]string{"a=1; domain=1.2.3.4"},
   932  		"",
   933  		[]query{{"http://1.2.3.4/foo", ""}},
   934  	},
   935  	{
   936  		"TestNonDottedAndTLD #2.",
   937  		"http://com./index.html",
   938  		[]string{"a=1"},
   939  		"a=1",
   940  		[]query{
   941  			{"http://com./index.html", "a=1"},
   942  			{"http://no-cookies.com./index.html", ""},
   943  		},
   944  	},
   945  	{
   946  		"TestNonDottedAndTLD #3.",
   947  		"http://a.b",
   948  		[]string{
   949  			"a=1; domain=.b",
   950  			"b=2; domain=b"},
   951  		"",
   952  		[]query{{"http://bar.foo", ""}},
   953  	},
   954  	{
   955  		"TestNonDottedAndTLD #4.",
   956  		"http://google.com",
   957  		[]string{
   958  			"a=1; domain=.com",
   959  			"b=2; domain=com"},
   960  		"",
   961  		[]query{{"http://google.com", ""}},
   962  	},
   963  	{
   964  		"TestNonDottedAndTLD #5.",
   965  		"http://google.co.uk",
   966  		[]string{
   967  			"a=1; domain=.co.uk",
   968  			"b=2; domain=.uk"},
   969  		"",
   970  		[]query{
   971  			{"http://google.co.uk", ""},
   972  			{"http://else.co.com", ""},
   973  			{"http://else.uk", ""},
   974  		},
   975  	},
   976  	{
   977  		"TestHostEndsWithDot.",
   978  		"http://www.google.com",
   979  		[]string{
   980  			"a=1",
   981  			"b=2; domain=.www.google.com."},
   982  		"a=1",
   983  		[]query{{"http://www.google.com", "a=1"}},
   984  	},
   985  	{
   986  		"PathTest",
   987  		"http://www.google.izzle",
   988  		[]string{"a=1; path=/wee"},
   989  		"a=1",
   990  		[]query{
   991  			{"http://www.google.izzle/wee", "a=1"},
   992  			{"http://www.google.izzle/wee/", "a=1"},
   993  			{"http://www.google.izzle/wee/war", "a=1"},
   994  			{"http://www.google.izzle/wee/war/more/more", "a=1"},
   995  			{"http://www.google.izzle/weehee", ""},
   996  			{"http://www.google.izzle/", ""},
   997  		},
   998  	},
   999  }
  1000  
  1001  func TestChromiumBasics(t *testing.T) {
  1002  	for _, test := range chromiumBasicsTests {
  1003  		jar := newTestJar()
  1004  		test.run(t, jar)
  1005  	}
  1006  }
  1007  
  1008  // chromiumDomainTests contains jarTests which must be executed all on the
  1009  // same Jar.
  1010  var chromiumDomainTests = [...]jarTest{
  1011  	{
  1012  		"Fill #1.",
  1013  		"http://www.google.izzle",
  1014  		[]string{"A=B"},
  1015  		"A=B",
  1016  		[]query{{"http://www.google.izzle", "A=B"}},
  1017  	},
  1018  	{
  1019  		"Fill #2.",
  1020  		"http://www.google.izzle",
  1021  		[]string{"C=D; domain=.google.izzle"},
  1022  		"A=B C=D",
  1023  		[]query{{"http://www.google.izzle", "A=B C=D"}},
  1024  	},
  1025  	{
  1026  		"Verify A is a host cookie and not accessible from subdomain.",
  1027  		"http://unused.nil",
  1028  		[]string{},
  1029  		"A=B C=D",
  1030  		[]query{{"http://foo.www.google.izzle", "C=D"}},
  1031  	},
  1032  	{
  1033  		"Verify domain cookies are found on proper domain.",
  1034  		"http://www.google.izzle",
  1035  		[]string{"E=F; domain=.www.google.izzle"},
  1036  		"A=B C=D E=F",
  1037  		[]query{{"http://www.google.izzle", "A=B C=D E=F"}},
  1038  	},
  1039  	{
  1040  		"Leading dots in domain attributes are optional.",
  1041  		"http://www.google.izzle",
  1042  		[]string{"G=H; domain=www.google.izzle"},
  1043  		"A=B C=D E=F G=H",
  1044  		[]query{{"http://www.google.izzle", "A=B C=D E=F G=H"}},
  1045  	},
  1046  	{
  1047  		"Verify domain enforcement works #1.",
  1048  		"http://www.google.izzle",
  1049  		[]string{"K=L; domain=.bar.www.google.izzle"},
  1050  		"A=B C=D E=F G=H",
  1051  		[]query{{"http://bar.www.google.izzle", "C=D E=F G=H"}},
  1052  	},
  1053  	{
  1054  		"Verify domain enforcement works #2.",
  1055  		"http://unused.nil",
  1056  		[]string{},
  1057  		"A=B C=D E=F G=H",
  1058  		[]query{{"http://www.google.izzle", "A=B C=D E=F G=H"}},
  1059  	},
  1060  }
  1061  
  1062  func TestChromiumDomain(t *testing.T) {
  1063  	jar := newTestJar()
  1064  	for _, test := range chromiumDomainTests {
  1065  		test.run(t, jar)
  1066  	}
  1067  
  1068  }
  1069  
  1070  // chromiumDeletionTests must be performed all on the same Jar.
  1071  var chromiumDeletionTests = [...]jarTest{
  1072  	{
  1073  		"Create session cookie a1.",
  1074  		"http://www.google.com",
  1075  		[]string{"a=1"},
  1076  		"a=1",
  1077  		[]query{{"http://www.google.com", "a=1"}},
  1078  	},
  1079  	{
  1080  		"Delete sc a1 via MaxAge.",
  1081  		"http://www.google.com",
  1082  		[]string{"a=1; max-age=-1"},
  1083  		"",
  1084  		[]query{{"http://www.google.com", ""}},
  1085  	},
  1086  	{
  1087  		"Create session cookie b2.",
  1088  		"http://www.google.com",
  1089  		[]string{"b=2"},
  1090  		"b=2",
  1091  		[]query{{"http://www.google.com", "b=2"}},
  1092  	},
  1093  	{
  1094  		"Delete sc b2 via Expires.",
  1095  		"http://www.google.com",
  1096  		[]string{"b=2; " + expiresIn(-10)},
  1097  		"",
  1098  		[]query{{"http://www.google.com", ""}},
  1099  	},
  1100  	{
  1101  		"Create persistent cookie c3.",
  1102  		"http://www.google.com",
  1103  		[]string{"c=3; max-age=3600"},
  1104  		"c=3",
  1105  		[]query{{"http://www.google.com", "c=3"}},
  1106  	},
  1107  	{
  1108  		"Delete pc c3 via MaxAge.",
  1109  		"http://www.google.com",
  1110  		[]string{"c=3; max-age=-1"},
  1111  		"",
  1112  		[]query{{"http://www.google.com", ""}},
  1113  	},
  1114  	{
  1115  		"Create persistent cookie d4.",
  1116  		"http://www.google.com",
  1117  		[]string{"d=4; max-age=3600"},
  1118  		"d=4",
  1119  		[]query{{"http://www.google.com", "d=4"}},
  1120  	},
  1121  	{
  1122  		"Delete pc d4 via Expires.",
  1123  		"http://www.google.com",
  1124  		[]string{"d=4; " + expiresIn(-10)},
  1125  		"",
  1126  		[]query{{"http://www.google.com", ""}},
  1127  	},
  1128  }
  1129  
  1130  func TestChromiumDeletion(t *testing.T) {
  1131  	jar := newTestJar()
  1132  	for _, test := range chromiumDeletionTests {
  1133  		test.run(t, jar)
  1134  	}
  1135  }
  1136  
  1137  // domainHandlingTests tests and documents the rules for domain handling.
  1138  // Each test must be performed on an empty new Jar.
  1139  var domainHandlingTests = [...]jarTest{
  1140  	{
  1141  		"Host cookie",
  1142  		"http://www.host.test",
  1143  		[]string{"a=1"},
  1144  		"a=1",
  1145  		[]query{
  1146  			{"http://www.host.test", "a=1"},
  1147  			{"http://host.test", ""},
  1148  			{"http://bar.host.test", ""},
  1149  			{"http://foo.www.host.test", ""},
  1150  			{"http://other.test", ""},
  1151  			{"http://test", ""},
  1152  		},
  1153  	},
  1154  	{
  1155  		"Domain cookie #1",
  1156  		"http://www.host.test",
  1157  		[]string{"a=1; domain=host.test"},
  1158  		"a=1",
  1159  		[]query{
  1160  			{"http://www.host.test", "a=1"},
  1161  			{"http://host.test", "a=1"},
  1162  			{"http://bar.host.test", "a=1"},
  1163  			{"http://foo.www.host.test", "a=1"},
  1164  			{"http://other.test", ""},
  1165  			{"http://test", ""},
  1166  		},
  1167  	},
  1168  	{
  1169  		"Domain cookie #2",
  1170  		"http://www.host.test",
  1171  		[]string{"a=1; domain=.host.test"},
  1172  		"a=1",
  1173  		[]query{
  1174  			{"http://www.host.test", "a=1"},
  1175  			{"http://host.test", "a=1"},
  1176  			{"http://bar.host.test", "a=1"},
  1177  			{"http://foo.www.host.test", "a=1"},
  1178  			{"http://other.test", ""},
  1179  			{"http://test", ""},
  1180  		},
  1181  	},
  1182  	{
  1183  		"Host cookie on IDNA domain #1",
  1184  		"http://www.bücher.test",
  1185  		[]string{"a=1"},
  1186  		"a=1",
  1187  		[]query{
  1188  			{"http://www.bücher.test", "a=1"},
  1189  			{"http://www.xn--bcher-kva.test", "a=1"},
  1190  			{"http://bücher.test", ""},
  1191  			{"http://xn--bcher-kva.test", ""},
  1192  			{"http://bar.bücher.test", ""},
  1193  			{"http://bar.xn--bcher-kva.test", ""},
  1194  			{"http://foo.www.bücher.test", ""},
  1195  			{"http://foo.www.xn--bcher-kva.test", ""},
  1196  			{"http://other.test", ""},
  1197  			{"http://test", ""},
  1198  		},
  1199  	},
  1200  	{
  1201  		"Host cookie on IDNA domain #2",
  1202  		"http://www.xn--bcher-kva.test",
  1203  		[]string{"a=1"},
  1204  		"a=1",
  1205  		[]query{
  1206  			{"http://www.bücher.test", "a=1"},
  1207  			{"http://www.xn--bcher-kva.test", "a=1"},
  1208  			{"http://bücher.test", ""},
  1209  			{"http://xn--bcher-kva.test", ""},
  1210  			{"http://bar.bücher.test", ""},
  1211  			{"http://bar.xn--bcher-kva.test", ""},
  1212  			{"http://foo.www.bücher.test", ""},
  1213  			{"http://foo.www.xn--bcher-kva.test", ""},
  1214  			{"http://other.test", ""},
  1215  			{"http://test", ""},
  1216  		},
  1217  	},
  1218  	{
  1219  		"Domain cookie on IDNA domain #1",
  1220  		"http://www.bücher.test",
  1221  		[]string{"a=1; domain=xn--bcher-kva.test"},
  1222  		"a=1",
  1223  		[]query{
  1224  			{"http://www.bücher.test", "a=1"},
  1225  			{"http://www.xn--bcher-kva.test", "a=1"},
  1226  			{"http://bücher.test", "a=1"},
  1227  			{"http://xn--bcher-kva.test", "a=1"},
  1228  			{"http://bar.bücher.test", "a=1"},
  1229  			{"http://bar.xn--bcher-kva.test", "a=1"},
  1230  			{"http://foo.www.bücher.test", "a=1"},
  1231  			{"http://foo.www.xn--bcher-kva.test", "a=1"},
  1232  			{"http://other.test", ""},
  1233  			{"http://test", ""},
  1234  		},
  1235  	},
  1236  	{
  1237  		"Domain cookie on IDNA domain #2",
  1238  		"http://www.xn--bcher-kva.test",
  1239  		[]string{"a=1; domain=xn--bcher-kva.test"},
  1240  		"a=1",
  1241  		[]query{
  1242  			{"http://www.bücher.test", "a=1"},
  1243  			{"http://www.xn--bcher-kva.test", "a=1"},
  1244  			{"http://bücher.test", "a=1"},
  1245  			{"http://xn--bcher-kva.test", "a=1"},
  1246  			{"http://bar.bücher.test", "a=1"},
  1247  			{"http://bar.xn--bcher-kva.test", "a=1"},
  1248  			{"http://foo.www.bücher.test", "a=1"},
  1249  			{"http://foo.www.xn--bcher-kva.test", "a=1"},
  1250  			{"http://other.test", ""},
  1251  			{"http://test", ""},
  1252  		},
  1253  	},
  1254  	{
  1255  		"Host cookie on TLD.",
  1256  		"http://com",
  1257  		[]string{"a=1"},
  1258  		"a=1",
  1259  		[]query{
  1260  			{"http://com", "a=1"},
  1261  			{"http://any.com", ""},
  1262  			{"http://any.test", ""},
  1263  		},
  1264  	},
  1265  	{
  1266  		"Domain cookie on TLD becomes a host cookie.",
  1267  		"http://com",
  1268  		[]string{"a=1; domain=com"},
  1269  		"a=1",
  1270  		[]query{
  1271  			{"http://com", "a=1"},
  1272  			{"http://any.com", ""},
  1273  			{"http://any.test", ""},
  1274  		},
  1275  	},
  1276  	{
  1277  		"Host cookie on public suffix.",
  1278  		"http://co.uk",
  1279  		[]string{"a=1"},
  1280  		"a=1",
  1281  		[]query{
  1282  			{"http://co.uk", "a=1"},
  1283  			{"http://uk", ""},
  1284  			{"http://some.co.uk", ""},
  1285  			{"http://foo.some.co.uk", ""},
  1286  			{"http://any.uk", ""},
  1287  		},
  1288  	},
  1289  	{
  1290  		"Domain cookie on public suffix is ignored.",
  1291  		"http://some.co.uk",
  1292  		[]string{"a=1; domain=co.uk"},
  1293  		"",
  1294  		[]query{
  1295  			{"http://co.uk", ""},
  1296  			{"http://uk", ""},
  1297  			{"http://some.co.uk", ""},
  1298  			{"http://foo.some.co.uk", ""},
  1299  			{"http://any.uk", ""},
  1300  		},
  1301  	},
  1302  }
  1303  
  1304  func TestDomainHandling(t *testing.T) {
  1305  	for _, test := range domainHandlingTests {
  1306  		jar := newTestJar()
  1307  		test.run(t, jar)
  1308  	}
  1309  }
  1310  
  1311  func TestIssue19384(t *testing.T) {
  1312  	cookies := []*http.Cookie{{Name: "name", Value: "value"}}
  1313  	for _, host := range []string{"", ".", "..", "..."} {
  1314  		jar, _ := New(nil)
  1315  		u := &url.URL{Scheme: "http", Host: host, Path: "/"}
  1316  		if got := jar.Cookies(u); len(got) != 0 {
  1317  			t.Errorf("host %q, got %v", host, got)
  1318  		}
  1319  		jar.SetCookies(u, cookies)
  1320  		if got := jar.Cookies(u); len(got) != 1 || got[0].Value != "value" {
  1321  			t.Errorf("host %q, got %v", host, got)
  1322  		}
  1323  	}
  1324  }