summaryrefslogtreecommitdiffstats
path: root/browser/components/migration/tests/unit/test_fx_telemetry.js
blob: a276f52f850f981a941d5d063f27a431ff12ad35 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

/* globals do_get_tempdir */

"use strict";

function run_test() {
  run_next_test();
}

function readFile(file) {
  let stream = Cc["@mozilla.org/network/file-input-stream;1"]
               .createInstance(Ci.nsIFileInputStream);
  stream.init(file, -1, -1, Ci.nsIFileInputStream.CLOSE_ON_EOF);

  let sis = Cc["@mozilla.org/scriptableinputstream;1"]
            .createInstance(Ci.nsIScriptableInputStream);
  sis.init(stream);
  let contents = sis.read(file.fileSize);
  sis.close();
  return contents;
}

function checkDirectoryContains(dir, files) {
  print("checking " + dir.path + " - should contain " + Object.keys(files));
  let seen = new Set();
  let enumerator = dir.directoryEntries;
  while (enumerator.hasMoreElements()) {
    let file = enumerator.getNext().QueryInterface(Ci.nsIFile);
    print("found file: " + file.path);
    Assert.ok(file.leafName in files, file.leafName + " exists, but shouldn't");

    let expectedContents = files[file.leafName];
    if (typeof expectedContents != "string") {
      // it's a subdir - recurse!
      Assert.ok(file.isDirectory(), "should be a subdir");
      let newDir = dir.clone();
      newDir.append(file.leafName);
      checkDirectoryContains(newDir, expectedContents);
    } else {
      Assert.ok(!file.isDirectory(), "should be a regular file");
      let contents = readFile(file);
      Assert.equal(contents, expectedContents);
    }
    seen.add(file.leafName);
  }
  let missing = [];
  for (let x in files) {
    if (!seen.has(x)) {
      missing.push(x);
    }
  }
  Assert.deepEqual(missing, [], "no missing files in " + dir.path);
}

function getTestDirs() {
  // we make a directory structure in a temp dir which mirrors what we are
  // testing.
  let tempDir = do_get_tempdir();
  let srcDir = tempDir.clone();
  srcDir.append("test_source_dir");
  srcDir.createUnique(Ci.nsIFile.DIRECTORY_TYPE, FileUtils.PERMS_DIRECTORY);

  let targetDir = tempDir.clone();
  targetDir.append("test_target_dir");
  targetDir.createUnique(Ci.nsIFile.DIRECTORY_TYPE, FileUtils.PERMS_DIRECTORY);

  // no need to cleanup these dirs - the xpcshell harness will do it for us.
  return [srcDir, targetDir];
}

function writeToFile(dir, leafName, contents) {
  let file = dir.clone();
  file.append(leafName);

  let outputStream = FileUtils.openFileOutputStream(file);
  outputStream.write(contents, contents.length);
  outputStream.close();
}

function createSubDir(dir, subDirName) {
  let subDir = dir.clone();
  subDir.append(subDirName);
  subDir.create(Ci.nsIFile.DIRECTORY_TYPE, FileUtils.PERMS_DIRECTORY);
  return subDir;
}

function promiseMigrator(name, srcDir, targetDir) {
  let migrator = Cc["@mozilla.org/profile/migrator;1?app=browser&type=firefox"]
                 .createInstance(Ci.nsISupports)
                 .wrappedJSObject;
  let migrators = migrator._getResourcesInternal(srcDir, targetDir);
  for (let m of migrators) {
    if (m.name == name) {
      return new Promise(resolve => m.migrate(resolve));
    }
  }
  throw new Error("failed to find the " + name + " migrator");
}

function promiseTelemetryMigrator(srcDir, targetDir) {
  return promiseMigrator("telemetry", srcDir, targetDir);
}

add_task(function* test_empty() {
  let [srcDir, targetDir] = getTestDirs();
  let ok = yield promiseTelemetryMigrator(srcDir, targetDir);
  Assert.ok(ok, "callback should have been true with empty directories");
  // check both are empty
  checkDirectoryContains(srcDir, {});
  checkDirectoryContains(targetDir, {});
});

add_task(function* test_migrate_files() {
  let [srcDir, targetDir] = getTestDirs();

  // Set up datareporting files, some to copy, some not.
  let stateContent = JSON.stringify({
    clientId: "68d5474e-19dc-45c1-8e9a-81fca592707c",
  });
  let sessionStateContent = "foobar 5432";
  let subDir = createSubDir(srcDir, "datareporting");
  writeToFile(subDir, "state.json", stateContent);
  writeToFile(subDir, "session-state.json", sessionStateContent);
  writeToFile(subDir, "other.file", "do not copy");

  let archived = createSubDir(subDir, "archived");
  writeToFile(archived, "other.file", "do not copy");

  // Set up FHR files, they should not be copied.
  writeToFile(srcDir, "healthreport.sqlite", "do not copy");
  writeToFile(srcDir, "healthreport.sqlite-wal", "do not copy");
  subDir = createSubDir(srcDir, "healthreport");
  writeToFile(subDir, "state.json", "do not copy");
  writeToFile(subDir, "other.file", "do not copy");

  // Perform migration.
  let ok = yield promiseTelemetryMigrator(srcDir, targetDir);
  Assert.ok(ok, "callback should have been true with important telemetry files copied");

  checkDirectoryContains(targetDir, {
    "datareporting": {
      "state.json": stateContent,
      "session-state.json": sessionStateContent,
    },
  });
});

add_task(function* test_fallback_fhr_state() {
  let [srcDir, targetDir] = getTestDirs();

  // Test that we fall back to migrating FHR state if the datareporting
  // state file does not exist.
  let stateContent = JSON.stringify({
    clientId: "68d5474e-19dc-45c1-8e9a-81fca592707c",
  });
  let subDir = createSubDir(srcDir, "healthreport");
  writeToFile(subDir, "state.json", stateContent);

  // Perform migration.
  let ok = yield promiseTelemetryMigrator(srcDir, targetDir);
  Assert.ok(ok, "callback should have been true");

  checkDirectoryContains(targetDir, {
    "healthreport": {
      "state.json": stateContent,
    },
  });
});


add_task(function* test_datareporting_not_dir() {
  let [srcDir, targetDir] = getTestDirs();

  writeToFile(srcDir, "datareporting", "I'm a file but should be a directory");

  let ok = yield promiseTelemetryMigrator(srcDir, targetDir);
  Assert.ok(ok, "callback should have been true even though the directory was a file");

  checkDirectoryContains(targetDir, {});
});

add_task(function* test_datareporting_empty() {
  let [srcDir, targetDir] = getTestDirs();

  // Migrate with an empty 'datareporting' subdir.
  createSubDir(srcDir, "datareporting");
  let ok = yield promiseTelemetryMigrator(srcDir, targetDir);
  Assert.ok(ok, "callback should have been true");

  // We should end up with no migrated files.
  checkDirectoryContains(targetDir, {
    "datareporting": {},
  });
});

add_task(function* test_healthreport_empty() {
  let [srcDir, targetDir] = getTestDirs();

  // Migrate with no 'datareporting' and an empty 'healthreport' subdir.
  createSubDir(srcDir, "healthreport");
  let ok = yield promiseTelemetryMigrator(srcDir, targetDir);
  Assert.ok(ok, "callback should have been true");

  // We should end up with no migrated files.
  checkDirectoryContains(targetDir, {});
});

add_task(function* test_datareporting_many() {
  let [srcDir, targetDir] = getTestDirs();

  // Create some datareporting files.
  let subDir = createSubDir(srcDir, "datareporting");
  let shouldBeCopied = "should be copied";
  writeToFile(subDir, "state.json", shouldBeCopied);
  writeToFile(subDir, "session-state.json", shouldBeCopied);
  writeToFile(subDir, "something.else", "should not");
  createSubDir(subDir, "emptyDir");

  let ok = yield promiseTelemetryMigrator(srcDir, targetDir);
  Assert.ok(ok, "callback should have been true");

  checkDirectoryContains(targetDir, {
    "datareporting" : {
      "state.json": shouldBeCopied,
      "session-state.json": shouldBeCopied,
    }
  });
});

add_task(function* test_no_session_state() {
  let [srcDir, targetDir] = getTestDirs();

  // Check that migration still works properly if we only have state.json.
  let subDir = createSubDir(srcDir, "datareporting");
  let stateContent = "abcd984";
  writeToFile(subDir, "state.json", stateContent);

  let ok = yield promiseTelemetryMigrator(srcDir, targetDir);
  Assert.ok(ok, "callback should have been true");

  checkDirectoryContains(targetDir, {
    "datareporting" : {
      "state.json": stateContent,
    }
  });
});

add_task(function* test_no_state() {
  let [srcDir, targetDir] = getTestDirs();

  // Check that migration still works properly if we only have session-state.json.
  let subDir = createSubDir(srcDir, "datareporting");
  let sessionStateContent = "abcd512";
  writeToFile(subDir, "session-state.json", sessionStateContent);

  let ok = yield promiseTelemetryMigrator(srcDir, targetDir);
  Assert.ok(ok, "callback should have been true");

  checkDirectoryContains(targetDir, {
    "datareporting" : {
      "session-state.json": sessionStateContent,
    }
  });
});

add_task(function* test_times_migration() {
  let [srcDir, targetDir] = getTestDirs();

  // create a times.json in the source directory.
  let contents = JSON.stringify({created: 1234});
  writeToFile(srcDir, "times.json", contents);

  let earliest = Date.now();
  let ok = yield promiseMigrator("times", srcDir, targetDir);
  Assert.ok(ok, "callback should have been true");
  let latest = Date.now();

  let timesFile = targetDir.clone();
  timesFile.append("times.json");

  let raw = readFile(timesFile);
  let times = JSON.parse(raw);
  Assert.ok(times.reset >= earliest && times.reset <= latest);
  // and it should have left the creation time alone.
  Assert.equal(times.created, 1234);
});