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

add_task(async function test_metadata() {
  await PlacesUtils.metadata.set("test/integer", 123);
  Assert.strictEqual(
    await PlacesUtils.metadata.get("test/integer"),
    123,
    "Should store new integer value"
  );

  await PlacesUtils.metadata.set("test/double", 123.45);
  Assert.strictEqual(
    await PlacesUtils.metadata.get("test/double"),
    123.45,
    "Should store new double value"
  );
  await PlacesUtils.metadata.set("test/double", 567.89);
  Assert.strictEqual(
    await PlacesUtils.metadata.get("test/double"),
    567.89,
    "Should update existing double value"
  );

  await PlacesUtils.metadata.set("test/boolean", false);
  Assert.strictEqual(
    await PlacesUtils.metadata.get("test/boolean"),
    false,
    "Should store new Boolean value"
  );
  await PlacesUtils.metadata.set("test/boolean", true);
  Assert.strictEqual(
    await PlacesUtils.metadata.get("test/boolean"),
    true,
    "Should update existing Boolean value"
  );

  await PlacesUtils.metadata.set("test/string", "hi");
  Assert.equal(
    await PlacesUtils.metadata.get("test/string"),
    "hi",
    "Should store new string value"
  );
  await PlacesUtils.metadata.cache.clear();
  Assert.equal(
    await PlacesUtils.metadata.get("test/string"),
    "hi",
    "Should return string value after clearing cache"
  );

  await Assert.rejects(
    PlacesUtils.metadata.get("test/nonexistent"),
    /No data stored for key test\/nonexistent/,
    "Should reject for a non-existent key and no default value."
  );
  Assert.equal(
    await PlacesUtils.metadata.get("test/nonexistent", "defaultValue"),
    "defaultValue",
    "Should return the default value for a non-existent key."
  );

  // Values are untyped; it's OK to store a value of a different type for the
  // same key.
  await PlacesUtils.metadata.set("test/string", 111);
  Assert.strictEqual(
    await PlacesUtils.metadata.get("test/string"),
    111,
    "Should replace string with integer"
  );
  await PlacesUtils.metadata.set("test/string", null);
  await Assert.rejects(
    PlacesUtils.metadata.get("test/string"),
    /No data stored for key test\/string/,
    "Should clear value when setting to NULL"
  );

  await PlacesUtils.metadata.delete("test/string", "test/boolean");
  await Assert.rejects(
    PlacesUtils.metadata.get("test/string"),
    /No data stored for key test\/string/,
    "Should delete string value"
  );
  await Assert.rejects(
    PlacesUtils.metadata.get("test/boolean"),
    /No data stored for key test\/boolean/,
    "Should delete Boolean value"
  );
  Assert.strictEqual(
    await PlacesUtils.metadata.get("test/integer"),
    123,
    "Should keep undeleted integer value"
  );

  await PlacesTestUtils.clearMetadata();
  await Assert.rejects(
    PlacesUtils.metadata.get("test/integer"),
    /No data stored for key test\/integer/,
    "Should clear integer value"
  );
  await Assert.rejects(
    PlacesUtils.metadata.get("test/double"),
    /No data stored for key test\/double/,
    "Should clear double value"
  );
});

add_task(async function test_metadata_canonical_keys() {
  await PlacesUtils.metadata.set("Test/Integer", 123);
  Assert.strictEqual(
    await PlacesUtils.metadata.get("tEsT/integer"),
    123,
    "New keys should be case-insensitive"
  );
  await PlacesUtils.metadata.set("test/integer", 456);
  Assert.strictEqual(
    await PlacesUtils.metadata.get("TEST/INTEGER"),
    456,
    "Existing keys should be case-insensitive"
  );

  await Assert.rejects(
    PlacesUtils.metadata.set("", 123),
    /Invalid metadata key/,
    "Should reject empty keys"
  );
  await Assert.rejects(
    PlacesUtils.metadata.get(123),
    /Invalid metadata key/,
    "Should reject numeric keys"
  );
  await Assert.rejects(
    PlacesUtils.metadata.delete(true),
    /Invalid metadata key/,
    "Should reject Boolean keys"
  );
  await Assert.rejects(
    PlacesUtils.metadata.set({}),
    /Invalid metadata key/,
    "Should reject object keys"
  );
  await Assert.rejects(
    PlacesUtils.metadata.get(null),
    /Invalid metadata key/,
    "Should reject null keys"
  );
  await Assert.rejects(
    PlacesUtils.metadata.delete("!@#$"),
    /Invalid metadata key/,
    "Should reject keys with invalid characters"
  );
});

add_task(async function test_metadata_blobs() {
  let blob = new Uint8Array([1, 2, 3]);
  await PlacesUtils.metadata.set("test/blob", blob);

  let sameBlob = await PlacesUtils.metadata.get("test/blob");
  Assert.equal(
    ChromeUtils.getClassName(sameBlob),
    "Uint8Array",
    "Should cache typed array for blob value"
  );
  Assert.deepEqual(sameBlob, blob, "Should store new blob value");

  info("Remove blob from cache");
  await PlacesUtils.metadata.cache.clear();

  let newBlob = await PlacesUtils.metadata.get("test/blob");
  Assert.equal(
    ChromeUtils.getClassName(newBlob),
    "Uint8Array",
    "Should inflate blob into typed array"
  );
  Assert.deepEqual(
    newBlob,
    blob,
    "Should return same blob after clearing cache"
  );

  await PlacesTestUtils.clearMetadata();
});

add_task(async function test_metadata_arrays() {
  let array = [1, 2, 3, "\u2713 \u00E0 la mode"];
  await PlacesUtils.metadata.set("test/array", array);

  let sameArray = await PlacesUtils.metadata.get("test/array");
  Assert.ok(Array.isArray(sameArray), "Should cache array for array value");
  Assert.deepEqual(sameArray, array, "Should store new array value");

  info("Remove array from cache");
  await PlacesUtils.metadata.cache.clear();

  let newArray = await PlacesUtils.metadata.get("test/array");
  Assert.ok(Array.isArray(newArray), "Should inflate into array");
  Assert.deepEqual(
    newArray,
    array,
    "Should return same array after clearing cache"
  );

  await PlacesTestUtils.clearMetadata();
});

add_task(async function test_metadata_objects() {
  let object = { foo: 123, bar: "test", meow: "\u2713 \u00E0 la mode" };
  await PlacesUtils.metadata.set("test/object", object);

  let sameObject = await PlacesUtils.metadata.get("test/object");
  Assert.equal(
    typeof sameObject,
    "object",
    "Should cache object for object value"
  );
  Assert.deepEqual(sameObject, object, "Should store new object value");

  info("Remove object from cache");
  await PlacesUtils.metadata.cache.clear();

  let newObject = await PlacesUtils.metadata.get("test/object");
  Assert.equal(typeof newObject, "object", "Should inflate into object");
  Assert.deepEqual(
    newObject,
    object,
    "Should return same object after clearing cache"
  );

  await PlacesTestUtils.clearMetadata();
});

add_task(async function test_metadata_unparsable() {
  await PlacesUtils.withConnectionWrapper("test_medata", db => {
    let data = PlacesUtils.metadata._base64Encode("{hjjkhj}");

    return db.execute(`
      INSERT INTO moz_meta (key, value)
      VALUES ("test/unparsable", "data:application/json;base64,${data}")
    `);
  });

  await Assert.rejects(
    PlacesUtils.metadata.get("test/unparsable"),
    /SyntaxError: JSON.parse/,
    "Should reject for an unparsable value with no default"
  );
  Assert.deepEqual(
    await PlacesUtils.metadata.get("test/unparsable", { foo: 1 }),
    { foo: 1 },
    "Should return the default when encountering an unparsable value."
  );

  await PlacesTestUtils.clearMetadata();
});