summaryrefslogtreecommitdiffstats
path: root/services/sync/tests/unit/test_bookmark_validator.js
blob: cc0b3b08fc6c8a7575e81b2beed5b39cab0cb412 (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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

Components.utils.import("resource://services-sync/bookmark_validator.js");
Components.utils.import("resource://services-sync/util.js");

function inspectServerRecords(data) {
  return new BookmarkValidator().inspectServerRecords(data);
}

add_test(function test_isr_rootOnServer() {
  let c = inspectServerRecords([{
    id: 'places',
    type: 'folder',
    children: [],
  }]);
  ok(c.problemData.rootOnServer);
  run_next_test();
});

add_test(function test_isr_empty() {
  let c = inspectServerRecords([]);
  ok(!c.problemData.rootOnServer);
  notEqual(c.root, null);
  run_next_test();
});

add_test(function test_isr_cycles() {
  let c = inspectServerRecords([
    {id: 'C', type: 'folder', children: ['A', 'B'], parentid: 'places'},
    {id: 'A', type: 'folder', children: ['B'], parentid: 'B'},
    {id: 'B', type: 'folder', children: ['A'], parentid: 'A'},
  ]).problemData;

  equal(c.cycles.length, 1);
  ok(c.cycles[0].indexOf('A') >= 0);
  ok(c.cycles[0].indexOf('B') >= 0);
  run_next_test();
});

add_test(function test_isr_orphansMultiParents() {
  let c = inspectServerRecords([
    { id: 'A', type: 'bookmark', parentid: 'D' },
    { id: 'B', type: 'folder', parentid: 'places', children: ['A']},
    { id: 'C', type: 'folder', parentid: 'places', children: ['A']},

  ]).problemData;
  deepEqual(c.orphans, [{ id: "A", parent: "D" }]);
  equal(c.multipleParents.length, 1)
  ok(c.multipleParents[0].parents.indexOf('B') >= 0);
  ok(c.multipleParents[0].parents.indexOf('C') >= 0);
  run_next_test();
});

add_test(function test_isr_orphansMultiParents2() {
  let c = inspectServerRecords([
    { id: 'A', type: 'bookmark', parentid: 'D' },
    { id: 'B', type: 'folder', parentid: 'places', children: ['A']},
  ]).problemData;
  equal(c.orphans.length, 1);
  equal(c.orphans[0].id, 'A');
  equal(c.multipleParents.length, 0);
  run_next_test();
});

add_test(function test_isr_deletedParents() {
  let c = inspectServerRecords([
    { id: 'A', type: 'bookmark', parentid: 'B' },
    { id: 'B', type: 'folder', parentid: 'places', children: ['A']},
    { id: 'B', type: 'item', deleted: true},
  ]).problemData;
  deepEqual(c.deletedParents, ['A'])
  run_next_test();
});

add_test(function test_isr_badChildren() {
  let c = inspectServerRecords([
    { id: 'A', type: 'bookmark', parentid: 'places', children: ['B', 'C'] },
    { id: 'C', type: 'bookmark', parentid: 'A' }
  ]).problemData;
  deepEqual(c.childrenOnNonFolder, ['A'])
  deepEqual(c.missingChildren, [{parent: 'A', child: 'B'}]);
  deepEqual(c.parentNotFolder, ['C']);
  run_next_test();
});


add_test(function test_isr_parentChildMismatches() {
  let c = inspectServerRecords([
    { id: 'A', type: 'folder', parentid: 'places', children: [] },
    { id: 'B', type: 'bookmark', parentid: 'A' }
  ]).problemData;
  deepEqual(c.parentChildMismatches, [{parent: 'A', child: 'B'}]);
  run_next_test();
});

add_test(function test_isr_duplicatesAndMissingIDs() {
  let c = inspectServerRecords([
    {id: 'A', type: 'folder', parentid: 'places', children: []},
    {id: 'A', type: 'folder', parentid: 'places', children: []},
    {type: 'folder', parentid: 'places', children: []}
  ]).problemData;
  equal(c.missingIDs, 1);
  deepEqual(c.duplicates, ['A']);
  run_next_test();
});

add_test(function test_isr_duplicateChildren()  {
  let c = inspectServerRecords([
    {id: 'A', type: 'folder', parentid: 'places', children: ['B', 'B']},
    {id: 'B', type: 'bookmark', parentid: 'A'},
  ]).problemData;
  deepEqual(c.duplicateChildren, ['A']);
  run_next_test();
});

// Each compareServerWithClient test mutates these, so we can't just keep them
// global
function getDummyServerAndClient() {
  let server = [
    {
      id: 'menu',
      parentid: 'places',
      type: 'folder',
      parentName: '',
      title: 'foo',
      children: ['bbbbbbbbbbbb', 'cccccccccccc']
    },
    {
      id: 'bbbbbbbbbbbb',
      type: 'bookmark',
      parentid: 'menu',
      parentName: 'foo',
      title: 'bar',
      bmkUri: 'http://baz.com'
    },
    {
      id: 'cccccccccccc',
      parentid: 'menu',
      parentName: 'foo',
      title: '',
      type: 'query',
      bmkUri: 'place:type=6&sort=14&maxResults=10'
    }
  ];

  let client = {
    "guid": "root________",
    "title": "",
    "id": 1,
    "type": "text/x-moz-place-container",
    "children": [
      {
        "guid": "menu________",
        "title": "foo",
        "id": 1000,
        "type": "text/x-moz-place-container",
        "children": [
          {
            "guid": "bbbbbbbbbbbb",
            "title": "bar",
            "id": 1001,
            "type": "text/x-moz-place",
            "uri": "http://baz.com"
          },
          {
            "guid": "cccccccccccc",
            "title": "",
            "id": 1002,
            "annos": [{
              "name": "Places/SmartBookmark",
              "flags": 0,
              "expires": 4,
              "value": "RecentTags"
            }],
            "type": "text/x-moz-place",
            "uri": "place:type=6&sort=14&maxResults=10"
          }
        ]
      }
    ]
  };
  return {server, client};
}


add_test(function test_cswc_valid() {
  let {server, client} = getDummyServerAndClient();

  let c = new BookmarkValidator().compareServerWithClient(server, client).problemData;
  equal(c.clientMissing.length, 0);
  equal(c.serverMissing.length, 0);
  equal(c.differences.length, 0);
  run_next_test();
});

add_test(function test_cswc_serverMissing() {
  let {server, client} = getDummyServerAndClient();
  // remove c
  server.pop();
  server[0].children.pop();

  let c = new BookmarkValidator().compareServerWithClient(server, client).problemData;
  deepEqual(c.serverMissing, ['cccccccccccc']);
  equal(c.clientMissing.length, 0);
  deepEqual(c.structuralDifferences, [{id: 'menu', differences: ['childGUIDs']}]);
  run_next_test();
});

add_test(function test_cswc_clientMissing() {
  let {server, client} = getDummyServerAndClient();
  client.children[0].children.pop();

  let c = new BookmarkValidator().compareServerWithClient(server, client).problemData;
  deepEqual(c.clientMissing, ['cccccccccccc']);
  equal(c.serverMissing.length, 0);
  deepEqual(c.structuralDifferences, [{id: 'menu', differences: ['childGUIDs']}]);
  run_next_test();
});

add_test(function test_cswc_differences() {
  {
    let {server, client} = getDummyServerAndClient();
    client.children[0].children[0].title = 'asdf';
    let c = new BookmarkValidator().compareServerWithClient(server, client).problemData;
    equal(c.clientMissing.length, 0);
    equal(c.serverMissing.length, 0);
    deepEqual(c.differences, [{id: 'bbbbbbbbbbbb', differences: ['title']}]);
  }

  {
    let {server, client} = getDummyServerAndClient();
    server[2].type = 'bookmark';
    let c = new BookmarkValidator().compareServerWithClient(server, client).problemData;
    equal(c.clientMissing.length, 0);
    equal(c.serverMissing.length, 0);
    deepEqual(c.differences, [{id: 'cccccccccccc', differences: ['type']}]);
  }
  run_next_test();
});

add_test(function test_cswc_serverUnexpected() {
  let {server, client} = getDummyServerAndClient();
  client.children.push({
    "guid": "dddddddddddd",
    "title": "",
    "id": 2000,
    "annos": [{
      "name": "places/excludeFromBackup",
      "flags": 0,
      "expires": 4,
      "value": 1
    }, {
      "name": "PlacesOrganizer/OrganizerFolder",
      "flags": 0,
      "expires": 4,
      "value": 7
    }],
    "type": "text/x-moz-place-container",
    "children": [{
      "guid": "eeeeeeeeeeee",
      "title": "History",
      "annos": [{
        "name": "places/excludeFromBackup",
        "flags": 0,
        "expires": 4,
        "value": 1
      }, {
        "name": "PlacesOrganizer/OrganizerQuery",
        "flags": 0,
        "expires": 4,
        "value": "History"
      }],
      "type": "text/x-moz-place",
      "uri": "place:type=3&sort=4"
    }]
  });
  server.push({
    id: 'dddddddddddd',
    parentid: 'places',
    parentName: '',
    title: '',
    type: 'folder',
    children: ['eeeeeeeeeeee']
  }, {
    id: 'eeeeeeeeeeee',
    parentid: 'dddddddddddd',
    parentName: '',
    title: 'History',
    type: 'query',
    bmkUri: 'place:type=3&sort=4'
  });

  let c = new BookmarkValidator().compareServerWithClient(server, client).problemData;
  equal(c.clientMissing.length, 0);
  equal(c.serverMissing.length, 0);
  equal(c.serverUnexpected.length, 2);
  deepEqual(c.serverUnexpected, ["dddddddddddd", "eeeeeeeeeeee"]);
  run_next_test();
});

function validationPing(server, client, duration) {
  return wait_for_ping(function() {
    // fake this entirely
    Svc.Obs.notify("weave:service:sync:start");
    Svc.Obs.notify("weave:engine:sync:start", null, "bookmarks");
    Svc.Obs.notify("weave:engine:sync:finish", null, "bookmarks");
    let validator = new BookmarkValidator();
    let data = {
      // We fake duration and version just so that we can verify they're passed through.
      duration,
      version: validator.version,
      recordCount: server.length,
      problems: validator.compareServerWithClient(server, client).problemData,
    };
    Svc.Obs.notify("weave:engine:validate:finish", data, "bookmarks");
    Svc.Obs.notify("weave:service:sync:finish");
  }, true); // Allow "failing" pings, since having validation info indicates failure.
}

add_task(function *test_telemetry_integration() {
  let {server, client} = getDummyServerAndClient();
  // remove "c"
  server.pop();
  server[0].children.pop();
  const duration = 50;
  let ping = yield validationPing(server, client, duration);
  ok(ping.engines);
  let bme = ping.engines.find(e => e.name === "bookmarks");
  ok(bme);
  ok(bme.validation);
  ok(bme.validation.problems)
  equal(bme.validation.checked, server.length);
  equal(bme.validation.took, duration);
  bme.validation.problems.sort((a, b) => String.localeCompare(a.name, b.name));
  equal(bme.validation.version, new BookmarkValidator().version);
  deepEqual(bme.validation.problems, [
    { name: "badClientRoots", count: 3 },
    { name: "sdiff:childGUIDs", count: 1 },
    { name: "serverMissing", count: 1 },
    { name: "structuralDifferences", count: 1 },
  ]);
});

function run_test() {
  run_next_test();
}