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