summaryrefslogtreecommitdiffstats
path: root/toolkit/mozapps/defaultagent/tests/gtest/CacheTest.cpp
blob: a5a618ff235c1b322391b0ee28dc1ca8f908ee53 (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
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et cindent: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "gtest/gtest.h"

#include <string>

#include "Cache.h"
#include "common.h"
#include "Registry.h"
#include "UtfConvert.h"

#include "mozilla/Result.h"
#include "mozilla/UniquePtr.h"
#include "mozilla/WinHeaderOnlyUtils.h"

class WDBACacheTest : public ::testing::Test {
 protected:
  std::wstring mCacheRegKey;

  void SetUp() override {
    // Create a unique registry key to put the cache in for each test.
    const ::testing::TestInfo* const testInfo =
        ::testing::UnitTest::GetInstance()->current_test_info();
    Utf8ToUtf16Result testCaseResult = Utf8ToUtf16(testInfo->test_case_name());
    ASSERT_TRUE(testCaseResult.isOk());
    mCacheRegKey = testCaseResult.unwrap();

    Utf8ToUtf16Result testNameResult = Utf8ToUtf16(testInfo->name());
    ASSERT_TRUE(testNameResult.isOk());
    mCacheRegKey += L'.';
    mCacheRegKey += testNameResult.unwrap();

    FilePathResult uuidResult = GenerateUUIDStr();
    ASSERT_TRUE(uuidResult.isOk());
    mCacheRegKey += L'.';
    mCacheRegKey += uuidResult.unwrap();
  }

  void TearDown() override {
    // It seems like the TearDown probably doesn't run if SetUp doesn't
    // succeed, but I can't find any documentation saying that. And we don't
    // want to accidentally clobber the entirety of AGENT_REGKEY_NAME.
    if (!mCacheRegKey.empty()) {
      std::wstring regKey = AGENT_REGKEY_NAME;
      regKey += L'\\';
      regKey += mCacheRegKey;
      RegDeleteTreeW(HKEY_CURRENT_USER, regKey.c_str());
    }
  }
};

TEST_F(WDBACacheTest, BasicFunctionality) {
  Cache cache(mCacheRegKey.c_str());
  VoidResult result = cache.Init();
  ASSERT_TRUE(result.isOk());

  // Test that the cache starts empty
  Cache::MaybeEntryResult entryResult = cache.Dequeue();
  ASSERT_TRUE(entryResult.isOk());
  Cache::MaybeEntry entry = entryResult.unwrap();
  ASSERT_TRUE(entry.isNothing());

  // Test that the cache stops accepting items when it is full.
  ASSERT_EQ(Cache::kDefaultCapacity, 2U);
  Cache::Entry toWrite = Cache::Entry{
      .notificationType = "string1",
      .notificationShown = "string2",
      .notificationAction = "string3",
      .prevNotificationAction = "string4",
  };
  result = cache.Enqueue(toWrite);
  ASSERT_TRUE(result.isOk());
  toWrite = Cache::Entry{
      .notificationType = "string5",
      .notificationShown = "string6",
      .notificationAction = "string7",
      .prevNotificationAction = "string8",
  };
  result = cache.Enqueue(toWrite);
  ASSERT_TRUE(result.isOk());
  toWrite = Cache::Entry{
      .notificationType = "string9",
      .notificationShown = "string10",
      .notificationAction = "string11",
      .prevNotificationAction = "string12",
  };
  result = cache.Enqueue(toWrite);
  ASSERT_TRUE(result.isErr());

  // Read the two cache entries back out and test that they match the expected
  // values.
  entryResult = cache.Dequeue();
  ASSERT_TRUE(entryResult.isOk());
  entry = entryResult.unwrap();
  ASSERT_TRUE(entry.isSome());
  ASSERT_EQ(entry.value().entryVersion, 2U);
  ASSERT_EQ(entry.value().notificationType, "string1");
  ASSERT_EQ(entry.value().notificationShown, "string2");
  ASSERT_EQ(entry.value().notificationAction, "string3");
  ASSERT_TRUE(entry.value().prevNotificationAction.isSome());
  ASSERT_EQ(entry.value().prevNotificationAction.value(), "string4");

  entryResult = cache.Dequeue();
  ASSERT_TRUE(entryResult.isOk());
  entry = entryResult.unwrap();
  ASSERT_TRUE(entry.isSome());
  ASSERT_EQ(entry.value().entryVersion, 2U);
  ASSERT_EQ(entry.value().notificationType, "string5");
  ASSERT_EQ(entry.value().notificationShown, "string6");
  ASSERT_EQ(entry.value().notificationAction, "string7");
  ASSERT_TRUE(entry.value().prevNotificationAction.isSome());
  ASSERT_EQ(entry.value().prevNotificationAction.value(), "string8");

  entryResult = cache.Dequeue();
  ASSERT_TRUE(entryResult.isOk());
  entry = entryResult.unwrap();
  ASSERT_TRUE(entry.isNothing());
}

TEST_F(WDBACacheTest, Version1Migration) {
  // Set up 2 version 1 cache entries
  VoidResult result = RegistrySetValueString(
      IsPrefixed::Unprefixed, L"PingCacheNotificationType0", "string1");
  ASSERT_TRUE(result.isOk());
  result = RegistrySetValueString(IsPrefixed::Unprefixed,
                                  L"PingCacheNotificationShown0", "string2");
  ASSERT_TRUE(result.isOk());
  result = RegistrySetValueString(IsPrefixed::Unprefixed,
                                  L"PingCacheNotificationAction0", "string3");
  ASSERT_TRUE(result.isOk());
  result = RegistrySetValueString(IsPrefixed::Unprefixed,
                                  L"PingCacheNotificationType1", "string4");
  ASSERT_TRUE(result.isOk());
  result = RegistrySetValueString(IsPrefixed::Unprefixed,
                                  L"PingCacheNotificationShown1", "string5");
  ASSERT_TRUE(result.isOk());
  result = RegistrySetValueString(IsPrefixed::Unprefixed,
                                  L"PingCacheNotificationAction1", "string6");
  ASSERT_TRUE(result.isOk());

  Cache cache(mCacheRegKey.c_str());
  result = cache.Init();
  ASSERT_TRUE(result.isOk());

  Cache::MaybeEntryResult entryResult = cache.Dequeue();
  ASSERT_TRUE(entryResult.isOk());
  Cache::MaybeEntry entry = entryResult.unwrap();
  ASSERT_TRUE(entry.isSome());
  ASSERT_EQ(entry.value().entryVersion, 1U);
  ASSERT_EQ(entry.value().notificationType, "string1");
  ASSERT_EQ(entry.value().notificationShown, "string2");
  ASSERT_EQ(entry.value().notificationAction, "string3");
  ASSERT_TRUE(entry.value().prevNotificationAction.isNothing());

  // Insert a new item to test coexistence of different versions
  Cache::Entry toWrite = Cache::Entry{
      .notificationType = "string7",
      .notificationShown = "string8",
      .notificationAction = "string9",
      .prevNotificationAction = "string10",
  };
  result = cache.Enqueue(toWrite);
  ASSERT_TRUE(result.isOk());

  entryResult = cache.Dequeue();
  ASSERT_TRUE(entryResult.isOk());
  entry = entryResult.unwrap();
  ASSERT_TRUE(entry.isSome());
  ASSERT_EQ(entry.value().entryVersion, 1U);
  ASSERT_EQ(entry.value().notificationType, "string4");
  ASSERT_EQ(entry.value().notificationShown, "string5");
  ASSERT_EQ(entry.value().notificationAction, "string6");
  ASSERT_TRUE(entry.value().prevNotificationAction.isNothing());

  entryResult = cache.Dequeue();
  ASSERT_TRUE(entryResult.isOk());
  entry = entryResult.unwrap();
  ASSERT_TRUE(entry.isSome());
  ASSERT_EQ(entry.value().entryVersion, 2U);
  ASSERT_EQ(entry.value().notificationType, "string7");
  ASSERT_EQ(entry.value().notificationShown, "string8");
  ASSERT_EQ(entry.value().notificationAction, "string9");
  ASSERT_TRUE(entry.value().prevNotificationAction.isSome());
  ASSERT_EQ(entry.value().prevNotificationAction.value(), "string10");

  entryResult = cache.Dequeue();
  ASSERT_TRUE(entryResult.isOk());
  entry = entryResult.unwrap();
  ASSERT_TRUE(entry.isNothing());
}

TEST_F(WDBACacheTest, ForwardsCompatibility) {
  // Set up a cache that might have been made by a future version with a larger
  // capacity set and more keys per entry.
  std::wstring settingsKey = mCacheRegKey + L"\\version2";
  VoidResult result = RegistrySetValueDword(
      IsPrefixed::Unprefixed, Cache::kCapacityRegName, 8, settingsKey.c_str());
  ASSERT_TRUE(result.isOk());
  // We're going to insert the future version's entry at index 6 so there's
  // space for 1 more before we loop back to index 0. Then we are going to
  // enqueue 2 new values to test that this works properly.
  result = RegistrySetValueDword(IsPrefixed::Unprefixed, Cache::kFrontRegName,
                                 6, settingsKey.c_str());
  ASSERT_TRUE(result.isOk());
  result = RegistrySetValueDword(IsPrefixed::Unprefixed, Cache::kSizeRegName, 1,
                                 settingsKey.c_str());
  ASSERT_TRUE(result.isOk());

  // Insert an entry as if it was inserted by a future version
  std::wstring entryRegKey = settingsKey + L"\\6";
  result =
      RegistrySetValueDword(IsPrefixed::Unprefixed, Cache::kEntryVersionKey,
                            9999, entryRegKey.c_str());
  ASSERT_TRUE(result.isOk());
  result = RegistrySetValueString(IsPrefixed::Unprefixed,
                                  Cache::kNotificationTypeKey, "string1",
                                  entryRegKey.c_str());
  ASSERT_TRUE(result.isOk());
  result = RegistrySetValueString(IsPrefixed::Unprefixed,
                                  Cache::kNotificationShownKey, "string2",
                                  entryRegKey.c_str());
  ASSERT_TRUE(result.isOk());
  result = RegistrySetValueString(IsPrefixed::Unprefixed,
                                  Cache::kNotificationActionKey, "string3",
                                  entryRegKey.c_str());
  ASSERT_TRUE(result.isOk());
  result = RegistrySetValueString(IsPrefixed::Unprefixed,
                                  Cache::kPrevNotificationActionKey, "string4",
                                  entryRegKey.c_str());
  ASSERT_TRUE(result.isOk());
  result = RegistrySetValueString(IsPrefixed::Unprefixed, L"UnknownFutureKey",
                                  "string5", entryRegKey.c_str());
  ASSERT_TRUE(result.isOk());

  Cache cache(mCacheRegKey.c_str());
  result = cache.Init();
  ASSERT_TRUE(result.isOk());

  // Insert 2 new items to test that these features work with a different
  // capacity.
  Cache::Entry toWrite = Cache::Entry{
      .notificationType = "string6",
      .notificationShown = "string7",
      .notificationAction = "string8",
      .prevNotificationAction = "string9",
  };
  result = cache.Enqueue(toWrite);
  ASSERT_TRUE(result.isOk());
  toWrite = Cache::Entry{
      .notificationType = "string10",
      .notificationShown = "string11",
      .notificationAction = "string12",
      .prevNotificationAction = "string13",
  };
  result = cache.Enqueue(toWrite);
  ASSERT_TRUE(result.isOk());

  // Read cache and verify the output
  Cache::MaybeEntryResult entryResult = cache.Dequeue();
  ASSERT_TRUE(entryResult.isOk());
  Cache::MaybeEntry entry = entryResult.unwrap();
  ASSERT_TRUE(entry.isSome());
  ASSERT_EQ(entry.value().entryVersion, 9999U);
  ASSERT_EQ(entry.value().notificationType, "string1");
  ASSERT_EQ(entry.value().notificationShown, "string2");
  ASSERT_EQ(entry.value().notificationAction, "string3");
  ASSERT_TRUE(entry.value().prevNotificationAction.isSome());
  ASSERT_EQ(entry.value().prevNotificationAction.value(), "string4");

  entryResult = cache.Dequeue();
  ASSERT_TRUE(entryResult.isOk());
  entry = entryResult.unwrap();
  ASSERT_TRUE(entry.isSome());
  ASSERT_EQ(entry.value().entryVersion, 2U);
  ASSERT_EQ(entry.value().notificationType, "string6");
  ASSERT_EQ(entry.value().notificationShown, "string7");
  ASSERT_EQ(entry.value().notificationAction, "string8");
  ASSERT_TRUE(entry.value().prevNotificationAction.isSome());
  ASSERT_EQ(entry.value().prevNotificationAction.value(), "string9");

  entryResult = cache.Dequeue();
  ASSERT_TRUE(entryResult.isOk());
  entry = entryResult.unwrap();
  ASSERT_TRUE(entry.isSome());
  ASSERT_EQ(entry.value().entryVersion, 2U);
  ASSERT_EQ(entry.value().notificationType, "string10");
  ASSERT_EQ(entry.value().notificationShown, "string11");
  ASSERT_EQ(entry.value().notificationAction, "string12");
  ASSERT_TRUE(entry.value().prevNotificationAction.isSome());
  ASSERT_EQ(entry.value().prevNotificationAction.value(), "string13");

  entryResult = cache.Dequeue();
  ASSERT_TRUE(entryResult.isOk());
  entry = entryResult.unwrap();
  ASSERT_TRUE(entry.isNothing());
}