PageRenderTime 48ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 0ms

/third_party/gofrontend/libgo/go/net/http/cookiejar/jar_test.go

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