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

/**
 * This file only contains the |test_abortedSessionQueued| test. This needs
 * to be in a separate, stand-alone file since we're initializing Telemetry
 * twice, in a non-standard way to simulate incorrect shutdowns. Doing this
 * in other files might interfere with the other tests.
 */

const { TelemetryStorage } = ChromeUtils.importESModule(
  "resource://gre/modules/TelemetryStorage.sys.mjs"
);

const DATAREPORTING_DIR = "datareporting";
const ABORTED_PING_FILE_NAME = "aborted-session-ping";
const ABORTED_SESSION_UPDATE_INTERVAL_MS = 5 * 60 * 1000;

const PING_TYPE_MAIN = "main";
const REASON_ABORTED_SESSION = "aborted-session";
const TEST_PING_TYPE = "test-ping-type";

XPCOMUtils.defineLazyGetter(this, "DATAREPORTING_PATH", function () {
  return PathUtils.join(PathUtils.profileDir, DATAREPORTING_DIR);
});

function sendPing() {
  if (PingServer.started) {
    TelemetrySend.setServer("http://localhost:" + PingServer.port);
  } else {
    TelemetrySend.setServer("http://doesnotexist");
  }

  let options = {
    addClientId: true,
    addEnvironment: true,
  };
  return TelemetryController.submitExternalPing(TEST_PING_TYPE, {}, options);
}

add_task(async function test_setup() {
  do_get_profile();
  PingServer.start();
  Services.prefs.setCharPref(
    TelemetryUtils.Preferences.Server,
    "http://localhost:" + PingServer.port
  );
});

add_task(async function test_abortedSessionQueued() {
  const ABORTED_FILE = PathUtils.join(
    DATAREPORTING_PATH,
    ABORTED_PING_FILE_NAME
  );

  // Make sure the aborted sessions directory does not exist to test its creation.
  await IOUtils.remove(DATAREPORTING_PATH, {
    ignoreAbsent: true,
    recursive: true,
  });

  let schedulerTickCallback = null;
  let now = new Date(2040, 1, 1, 0, 0, 0);
  fakeNow(now);
  // Fake scheduler functions to control aborted-session flow in tests.
  fakeSchedulerTimer(
    callback => (schedulerTickCallback = callback),
    () => {}
  );
  await TelemetryController.testReset();

  Assert.ok(
    await IOUtils.exists(DATAREPORTING_PATH),
    "Telemetry must create the aborted session directory when starting."
  );

  // Fake now again so that the scheduled aborted-session save takes place.
  now = futureDate(now, ABORTED_SESSION_UPDATE_INTERVAL_MS);
  fakeNow(now);
  // The first aborted session checkpoint must take place right after the initialisation.
  Assert.ok(!!schedulerTickCallback);
  // Execute one scheduler tick.
  await schedulerTickCallback();
  // Check that the aborted session is due at the correct time.
  Assert.ok(
    await IOUtils.exists(ABORTED_FILE),
    "There must be an aborted session ping."
  );

  await TelemetryStorage.testClearPendingPings();
  PingServer.clearRequests();
  await TelemetryController.testReset();

  Assert.ok(
    !(await IOUtils.exists(ABORTED_FILE)),
    "The aborted session ping must be removed from the aborted session ping directory."
  );

  // Restarting Telemetry again to trigger sending pings in TelemetrySend.
  await TelemetryController.testReset();

  // We should have received an aborted-session ping.
  const receivedPing = await PingServer.promiseNextPing();
  Assert.equal(
    receivedPing.type,
    PING_TYPE_MAIN,
    "Should have the correct type"
  );
  Assert.equal(
    receivedPing.payload.info.reason,
    REASON_ABORTED_SESSION,
    "Ping should have the correct reason"
  );

  await TelemetryController.testShutdown();
});

/*
 * An aborted-session ping might have been written when Telemetry upload was disabled and
 * the profile had a canary client ID.
 * These pings should not be sent out at a later point when Telemetry is enabled again.
 */
add_task(async function test_abortedSession_canary_clientid() {
  const ABORTED_FILE = PathUtils.join(
    DATAREPORTING_PATH,
    ABORTED_PING_FILE_NAME
  );

  // Make sure the aborted sessions directory does not exist to test its creation.
  await IOUtils.remove(DATAREPORTING_PATH, {
    ignoreAbsent: true,
    recursive: true,
  });

  let schedulerTickCallback = null;
  let now = new Date(2040, 1, 1, 0, 0, 0);
  fakeNow(now);
  // Fake scheduler functions to control aborted-session flow in tests.
  fakeSchedulerTimer(
    callback => (schedulerTickCallback = callback),
    () => {}
  );
  await TelemetryController.testReset();

  Assert.ok(
    await IOUtils.exists(DATAREPORTING_PATH),
    "Telemetry must create the aborted session directory when starting."
  );

  // Fake now again so that the scheduled aborted-session save takes place.
  now = futureDate(now, ABORTED_SESSION_UPDATE_INTERVAL_MS);
  fakeNow(now);
  // The first aborted session checkpoint must take place right after the initialisation.
  Assert.ok(!!schedulerTickCallback);
  // Execute one scheduler tick.
  await schedulerTickCallback();
  // Check that the aborted session is due at the correct time.
  Assert.ok(
    await IOUtils.exists(ABORTED_FILE),
    "There must be an aborted session ping."
  );

  // Set clientID in aborted-session ping to canary value
  let abortedPing = await IOUtils.readJSON(ABORTED_FILE);
  abortedPing.clientId = TelemetryUtils.knownClientID;
  await IOUtils.writeJSON(ABORTED_FILE, abortedPing);

  await TelemetryStorage.testClearPendingPings();
  PingServer.clearRequests();
  await TelemetryController.testReset();

  Assert.ok(
    !(await IOUtils.exists(ABORTED_FILE)),
    "The aborted session ping must be removed from the aborted session ping directory."
  );

  // Restarting Telemetry again to trigger sending pings in TelemetrySend.
  await TelemetryController.testReset();

  // Trigger a test ping, so we can verify the server received something.
  sendPing();

  // We should have received an aborted-session ping.
  const receivedPing = await PingServer.promiseNextPing();
  Assert.equal(
    receivedPing.type,
    TEST_PING_TYPE,
    "Should have received test ping"
  );

  await TelemetryController.testShutdown();
});

add_task(async function stopServer() {
  await PingServer.stop();
});