summaryrefslogtreecommitdiffstats
path: root/toolkit/components/places/tests/unit/test_hosts_triggers.js
blob: 9c3359e76dd88754e2b7f69aca7fa7ea0f264e47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

/**
 * This file tests the validity of various triggers that add remove hosts from moz_hosts
 */

XPCOMUtils.defineLazyServiceGetter(this, "gHistory",
                                   "@mozilla.org/browser/history;1",
                                   "mozIAsyncHistory");

// add some visits and remove them, add a bookmark,
// change its uri, then remove it, and
// for each change check that moz_hosts has correctly been updated.

function isHostInMozPlaces(aURI)
{
  let stmt = DBConn().createStatement(
    `SELECT url
       FROM moz_places
       WHERE url_hash = hash(:host) AND url = :host`
  );
  let result = false;
  stmt.params.host = aURI.spec;
  while (stmt.executeStep()) {
    if (stmt.row.url == aURI.spec) {
      result = true;
      break;
    }
  }
  stmt.finalize();
  return result;
}

function isHostInMozHosts(aURI, aTyped, aPrefix)
{
  let stmt = DBConn().createStatement(
    `SELECT host, typed, prefix
       FROM moz_hosts
       WHERE host = fixup_url(:host)
       AND frecency NOTNULL`
  );
  let result = false;
  stmt.params.host = aURI.host;
  if (stmt.executeStep()) {
    result = aTyped == stmt.row.typed && aPrefix == stmt.row.prefix;
  }
  stmt.finalize();
  return result;
}

var urls = [{uri: NetUtil.newURI("http://visit1.mozilla.org"),
             expected: "visit1.mozilla.org",
             typed: 0,
             prefix: null
            },
            {uri: NetUtil.newURI("http://visit2.mozilla.org"),
             expected: "visit2.mozilla.org",
             typed: 0,
             prefix: null
            },
            {uri: NetUtil.newURI("http://www.foo.mozilla.org"),
             expected: "foo.mozilla.org",
             typed: 1,
             prefix: "www."
            },
           ];

const NEW_URL = "http://different.mozilla.org/";

add_task(function* test_moz_hosts_update()
{
  let places = [];
  urls.forEach(function(url) {
    let place = { uri: url.uri,
                  title: "test for " + url.url,
                  transition: url.typed ? TRANSITION_TYPED : undefined };
    places.push(place);
  });

  yield PlacesTestUtils.addVisits(places);

  do_check_true(isHostInMozHosts(urls[0].uri, urls[0].typed, urls[0].prefix));
  do_check_true(isHostInMozHosts(urls[1].uri, urls[1].typed, urls[1].prefix));
  do_check_true(isHostInMozHosts(urls[2].uri, urls[2].typed, urls[2].prefix));
});

add_task(function* test_remove_places()
{
  for (let idx in urls) {
    PlacesUtils.history.removePage(urls[idx].uri);
  }

  yield PlacesTestUtils.clearHistory();

  for (let idx in urls) {
    do_check_false(isHostInMozHosts(urls[idx].uri, urls[idx].typed, urls[idx].prefix));
  }
});

add_task(function* test_bookmark_changes()
{
  let testUri = NetUtil.newURI("http://test.mozilla.org");

  let itemId = PlacesUtils.bookmarks.insertBookmark(PlacesUtils.unfiledBookmarksFolderId,
                                                     testUri,
                                                     PlacesUtils.bookmarks.DEFAULT_INDEX,
                                                     "bookmark title");

  do_check_true(isHostInMozPlaces(testUri));

  // Change the hostname
  PlacesUtils.bookmarks.changeBookmarkURI(itemId, NetUtil.newURI(NEW_URL));

  yield PlacesTestUtils.clearHistory();

  let newUri = NetUtil.newURI(NEW_URL);
  do_check_true(isHostInMozPlaces(newUri));
  do_check_true(isHostInMozHosts(newUri, false, null));
  do_check_false(isHostInMozHosts(NetUtil.newURI("http://test.mozilla.org"), false, null));
});

add_task(function* test_bookmark_removal()
{
  let itemId = PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.unfiledBookmarksFolderId,
                                                    PlacesUtils.bookmarks.DEFAULT_INDEX);
  let newUri = NetUtil.newURI(NEW_URL);
  PlacesUtils.bookmarks.removeItem(itemId);
  yield PlacesTestUtils.clearHistory();

  do_check_false(isHostInMozHosts(newUri, false, null));
});

add_task(function* test_moz_hosts_typed_update()
{
  const TEST_URI = NetUtil.newURI("http://typed.mozilla.com");
  let places = [{ uri: TEST_URI
                , title: "test for " + TEST_URI.spec
                },
                { uri: TEST_URI
                , title: "test for " + TEST_URI.spec
                , transition: TRANSITION_TYPED
                }];

  yield PlacesTestUtils.addVisits(places);

  do_check_true(isHostInMozHosts(TEST_URI, true, null));
  yield PlacesTestUtils.clearHistory();
});

add_task(function* test_moz_hosts_www_remove()
{
  function* test_removal(aURIToRemove, aURIToKeep, aCallback) {
    let places = [{ uri: aURIToRemove
                  , title: "test for " + aURIToRemove.spec
                  , transition: TRANSITION_TYPED
                  },
                  { uri: aURIToKeep
                  , title: "test for " + aURIToKeep.spec
                  , transition: TRANSITION_TYPED
                  }];

    yield PlacesTestUtils.addVisits(places);
    print("removing " + aURIToRemove.spec + " keeping " + aURIToKeep);
    dump_table("moz_hosts");
    dump_table("moz_places");
    PlacesUtils.history.removePage(aURIToRemove);
    let prefix = /www/.test(aURIToKeep.spec) ? "www." : null;
    dump_table("moz_hosts");
    dump_table("moz_places");
    do_check_true(isHostInMozHosts(aURIToKeep, true, prefix));
  }

  const TEST_URI = NetUtil.newURI("http://rem.mozilla.com");
  const TEST_WWW_URI = NetUtil.newURI("http://www.rem.mozilla.com");
  yield test_removal(TEST_URI, TEST_WWW_URI);
  yield test_removal(TEST_WWW_URI, TEST_URI);
  yield PlacesTestUtils.clearHistory();
});

add_task(function* test_moz_hosts_ftp_matchall()
{
  const TEST_URI_1 = NetUtil.newURI("ftp://www.mozilla.com/");
  const TEST_URI_2 = NetUtil.newURI("ftp://mozilla.com/");

  yield PlacesTestUtils.addVisits([
    { uri: TEST_URI_1, transition: TRANSITION_TYPED },
    { uri: TEST_URI_2, transition: TRANSITION_TYPED }
  ]);

  do_check_true(isHostInMozHosts(TEST_URI_1, true, "ftp://"));
});

add_task(function* test_moz_hosts_ftp_not_matchall()
{
  const TEST_URI_1 = NetUtil.newURI("http://mozilla.com/");
  const TEST_URI_2 = NetUtil.newURI("ftp://mozilla.com/");

  yield PlacesTestUtils.addVisits([
    { uri: TEST_URI_1, transition: TRANSITION_TYPED },
    { uri: TEST_URI_2, transition: TRANSITION_TYPED }
  ]);

  do_check_true(isHostInMozHosts(TEST_URI_1, true, null));
});

add_task(function* test_moz_hosts_update_2()
{
  // Check that updating trigger takes into account prefixes for different
  // rev_hosts.
  const TEST_URI_1 = NetUtil.newURI("https://www.google.it/");
  const TEST_URI_2 = NetUtil.newURI("https://google.it/");
  let places = [{ uri: TEST_URI_1
                , transition: TRANSITION_TYPED
                },
                { uri: TEST_URI_2
                }];
  yield PlacesTestUtils.addVisits(places);

  do_check_true(isHostInMozHosts(TEST_URI_1, true, "https://www."));
});

function run_test()
{
  run_next_test();
}