extensions/cookie/test/unit/test_cookies_read.js

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:65c1e87fae19
1 /* Any copyright is dedicated to the Public Domain.
2 http://creativecommons.org/publicdomain/zero/1.0/ */
3
4 // test cookie database asynchronous read operation.
5
6 let test_generator = do_run_test();
7
8 let CMAX = 1000; // # of cookies to create
9
10 function run_test() {
11 do_test_pending();
12 test_generator.next();
13 }
14
15 function finish_test() {
16 do_execute_soon(function() {
17 test_generator.close();
18 do_test_finished();
19 });
20 }
21
22 function do_run_test() {
23 // Set up a profile.
24 let profile = do_get_profile();
25
26 // Allow all cookies.
27 Services.prefs.setIntPref("network.cookie.cookieBehavior", 0);
28
29 // Start the cookieservice, to force creation of a database.
30 Services.cookies;
31
32 // Open a database connection now, after synchronous initialization has
33 // completed. We may not be able to open one later once asynchronous writing
34 // begins.
35 do_check_true(do_get_cookie_file(profile).exists());
36 let db = new CookieDatabaseConnection(do_get_cookie_file(profile), 4);
37
38 for (let i = 0; i < CMAX; ++i) {
39 let uri = NetUtil.newURI("http://" + i + ".com/");
40 Services.cookies.setCookieString(uri, null, "oh=hai; max-age=1000", null);
41 }
42
43 do_check_eq(do_count_cookies(), CMAX);
44
45 // Wait until all CMAX cookies have been written out to the database.
46 while (do_count_cookies_in_db(db.db) < CMAX) {
47 do_execute_soon(function() {
48 do_run_generator(test_generator);
49 });
50 yield;
51 }
52
53 // Check the WAL file size. We set it to 16 pages of 32k, which means it
54 // should be around 500k.
55 let file = db.db.databaseFile;
56 do_check_true(file.exists());
57 do_check_true(file.fileSize < 1e6);
58 db.close();
59
60 // fake a profile change
61 do_close_profile(test_generator);
62 yield;
63 do_load_profile();
64
65 // test a few random cookies
66 do_check_eq(Services.cookiemgr.countCookiesFromHost("999.com"), 1);
67 do_check_eq(Services.cookiemgr.countCookiesFromHost("abc.com"), 0);
68 do_check_eq(Services.cookiemgr.countCookiesFromHost("100.com"), 1);
69 do_check_eq(Services.cookiemgr.countCookiesFromHost("400.com"), 1);
70 do_check_eq(Services.cookiemgr.countCookiesFromHost("xyz.com"), 0);
71
72 // force synchronous load of everything
73 do_check_eq(do_count_cookies(), CMAX);
74
75 // check that everything's precisely correct
76 for (let i = 0; i < CMAX; ++i) {
77 let host = i.toString() + ".com";
78 do_check_eq(Services.cookiemgr.countCookiesFromHost(host), 1);
79 }
80
81 // reload again, to make sure the additions were written correctly
82 do_close_profile(test_generator);
83 yield;
84 do_load_profile();
85
86 // remove some of the cookies, in both reverse and forward order
87 for (let i = 100; i-- > 0; ) {
88 let host = i.toString() + ".com";
89 Services.cookiemgr.remove(host, "oh", "/", false);
90 }
91 for (let i = CMAX - 100; i < CMAX; ++i) {
92 let host = i.toString() + ".com";
93 Services.cookiemgr.remove(host, "oh", "/", false);
94 }
95
96 // check the count
97 do_check_eq(do_count_cookies(), CMAX - 200);
98
99 // reload again, to make sure the removals were written correctly
100 do_close_profile(test_generator);
101 yield;
102 do_load_profile();
103
104 // check the count
105 do_check_eq(do_count_cookies(), CMAX - 200);
106
107 // reload again, but wait for async read completion
108 do_close_profile(test_generator);
109 yield;
110 do_load_profile(test_generator);
111 yield;
112
113 // check that everything's precisely correct
114 do_check_eq(do_count_cookies(), CMAX - 200);
115 for (let i = 100; i < CMAX - 100; ++i) {
116 let host = i.toString() + ".com";
117 do_check_eq(Services.cookiemgr.countCookiesFromHost(host), 1);
118 }
119
120 finish_test();
121 }
122

mercurial