Files
palemoon27/security/manager/ssl/tests/gtest/DataStorageTest.cpp
T
roytam1 664ef9175d import changes from `dev' branch of rmottola/Arctic-Fox:
- enable shadow warnings (93e0fb3cf8)
- more bits of  Bug 1198334 (part 1) - Replace the opt-in FAIL_ON_WARNINGS (f07e6eca96)
- Bug 1111892 - Ensure gdk_ungrab_pointer is always called in the correct process r=jimm (8bf40d8add)
- Bug 1220344 - remote some of nsSystemInfo to make it e10s ready on Android r=nchen,froydnj (365e961dfb)
- bug 1190018 - External protocol handlers don't work in e10s r=mrbkap (ff4567d517)
- Bug 1225618 - Do not capture stacks for cycle collection timeline markers; r=vporof (d734ca9d26)
- Bug 1218315 - Replace NS_LITERAL_STRING(...).get() with MOZ_UTF16(...) on xpcom. r=nfroyd (5cff917319)
- Bug 1218315 - Use L"..." instead of MOZ_UTF16(...) for MSVS2015. r=nfroyd (9f8b88f428)
- Bug 1199592 - support TV device type in UA string. r=fabrice. (2eecf7f3b4)
- Bug 1215723 - Part 1: Make DataStorage a singleton for each file name; r=keeler (be2e5947f1)
- Bug 1167693 - Ensure device and vendor IDs are zero-padded. r=mstange (6e583cbf71)
- Bug 1186817 - Replace nsBaseHashtable::EnumerateRead() calls in security/ with iterators. r=keeler (4a14f547a7)
- Bug 1215723 - Part 2: Initialize DataStorage items in the content process from the data in the parent; r=keeler (3a4d3ec7d5)
- Bug 1215723 - Part 3: Propagate updates to DataStorage from the parent process to the content processes; r=keeler (2140b412eb)
- Bug 1215723 - Part 4: Make isSecureHost and isSecureURI usable from the content process; r=keeler (c8b786ca1b)
- Bug 1215723 - Part 5: Add an automated test; r=keeler (012e820385)
- Bug 1154184 - Don't use Linux sandbox gtest dir if not building tests. r=gps (2993e27d44)
- Bug 930258 - Part 1: The file broker, and unit tests for it. r=kang f=froydnj (d3e8cab30d)
- Bug 930258 - Part 2: seccomp-bpf integration. r=kang (a40ef5e566)
- Bug 930258 - Part 3: a file broker policy for the B2G emulator. r=kang (2c0c3d55cf)
- Bug 930258 - Part 4: the PContent changes that connect the broker to its sandboxed client. r=kang r=billm (1206c3a20c)
- Bug 1168251 - Do a partial recursion when doing make -C some/path compile. r=gps (a6733048fc)
- Bug 1194951 - Suppress warnings in third-party code: pocketsphinx and sphinxbase. r=kdavis (4f679d6a1e)
- Bug 1195186 - Build media/pocketsphinx in unified mode. r=kdavis (6e6bb419e0)
- Bug 1203005 - Remove all traces of -Wdeclaration-after-statement. r=glandium. (a66d7a4760)
- bits of  Bug 1211568, land NSS_3_21_RTM (6b81926cd3)
- Bug 947753 - Make sure nsChildView::ComputeShouldAccelerate returns true even if it's called before layout lets the window know about whether it's transparent. r=smichaud (0d2980b44f)
- Bug 1162649 - Don't call setOpaque:NO or setHasShadow:NO for non-popup windows;r=mstange (6353bd2e96)
- Backed out changeset 2e731191c692 (bug 1189565) for causing bug 1201053 a=backout (e4bc37c823)
- Bug 1174703 - Add GLContextEAGL and GLContextProviderEAGL as an OpenGL provider for iOS. r=jgilbert (64c6538d7d)
- Bug 1184402 - Reset format usage authority on context lose/restore. r=jgilbert (3df2278888)
- more dupes... hackish (f432375460)
- Bug 1007975 - div with display:table exposes table semantics r=surkov (f3b389dcce)
- Bug 1159034 - Test uninitialized buffer data. - r=dvander Bug 1147441 - Add SharedArrayBuffer support to WebGL and WebGL 2. r=jgilbert, r=bzbarsky (a151e77beb)
- Bug 1184402 - Part 1: Rename GetInfo() to GetUsage() for obtaining FormatUsageInfo. r=jgilbert (efbfe39a2e)
- Bug 1184402 - Part 2: Added luminance float effective formats (2cb792990d)
- Bug 1184402 - Part 3: Add WebGL1 formats when enabling extensions. r=jgilbert (aba6beb541)
- Bug 1184402 - Part 4: Use WebGLFormat for validation in RenderbufferStorage_base. r=jgilbert (704c86386f)
- Bug 1193070 - Implement GetFramebufferAttachmentParameter. r=jgilbert (ef51efdce1)
- Bug 1188195 - always initialize WebGL2 BlitFramebuffer src/dst formats to some value. r=jgilbert (694a4d87a1)
- Bug 1143876 - Treat invalidate-framebuffer as optional for WebGL2. - r=kamidphish (53d8ef5a3f)
- Bug 1170845 - Remove WebGL2() exclusion on instanced draw divisor check. r=jgilbert (cd8d1375b3)
- Bug 1192989 - Simplify handling by ifdefing in all.js. - r=kamidphish (32a26b46c8)
- Bug 1179556 - Only call EnsureColorAttachPoints with a color attachment. r=jgilbert (8791f2e34a)
- Bug 1170893 - Return null for invalid internal format. r=kamidphish (425657b6b6)
- Bug 1225782 - Removes GlobalPropertiesAreOwn(). r=bz (a6ca6fd503)
- Bug 1225381 - ensure mozCurrentTransform is finite. r=jmuizelaar (c009040dca)
- Bug 1223740 - enforce max surface size for DrawTargetSkia. r=bas (505fe7ac3b)
- Bug 1059014 (part 2) - Remove support for reflow on zoom event pending. r=dbaron. (be2ae37e94)
- Bug 1059014 (part 3) - Remove support for pausing and resuming painting. r=dbaron. (35979215f3)
2023-02-06 08:55:00 +08:00

226 lines
8.8 KiB
C++

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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 "mozilla/DataStorage.h"
#include "nsAppDirectoryServiceDefs.h"
#include "nsDirectoryServiceUtils.h"
#include "nsNetUtil.h"
#include "nsPrintfCString.h"
#include "nsStreamUtils.h"
#include "prtime.h"
using namespace mozilla;
class DataStorageTest : public ::testing::Test
{
protected:
virtual void SetUp()
{
const ::testing::TestInfo* const testInfo =
::testing::UnitTest::GetInstance()->current_test_info();
NS_ConvertUTF8toUTF16 testName(testInfo->name());
storage = DataStorage::Get(testName);
storage->Init(dataWillPersist);
}
RefPtr<DataStorage> storage;
bool dataWillPersist;
};
NS_NAMED_LITERAL_CSTRING(testKey, "test");
NS_NAMED_LITERAL_CSTRING(testValue, "value");
NS_NAMED_LITERAL_CSTRING(privateTestValue, "private");
TEST_F(DataStorageTest, GetPutRemove)
{
EXPECT_TRUE(dataWillPersist);
// Test Put/Get on Persistent data
EXPECT_EQ(NS_OK, storage->Put(testKey, testValue, DataStorage_Persistent));
// Don't re-use testKey / testValue here, to make sure that this works as
// expected with objects that have the same semantic value but are not
// literally the same object.
nsCString result = storage->Get(NS_LITERAL_CSTRING("test"),
DataStorage_Persistent);
EXPECT_STREQ("value", result.get());
// Get on Temporary/Private data with the same key should give nothing
result = storage->Get(testKey, DataStorage_Temporary);
EXPECT_TRUE(result.IsEmpty());
result = storage->Get(testKey, DataStorage_Private);
EXPECT_TRUE(result.IsEmpty());
// Put with Temporary/Private data shouldn't affect Persistent data
NS_NAMED_LITERAL_CSTRING(temporaryTestValue, "temporary");
EXPECT_EQ(NS_OK, storage->Put(testKey, temporaryTestValue,
DataStorage_Temporary));
EXPECT_EQ(NS_OK, storage->Put(testKey, privateTestValue,
DataStorage_Private));
result = storage->Get(testKey, DataStorage_Temporary);
EXPECT_STREQ("temporary", result.get());
result = storage->Get(testKey, DataStorage_Private);
EXPECT_STREQ("private", result.get());
result = storage->Get(testKey, DataStorage_Persistent);
EXPECT_STREQ("value", result.get());
// Put of a previously-present key overwrites it (if of the same type)
NS_NAMED_LITERAL_CSTRING(newValue, "new");
EXPECT_EQ(NS_OK, storage->Put(testKey, newValue, DataStorage_Persistent));
result = storage->Get(testKey, DataStorage_Persistent);
EXPECT_STREQ("new", result.get());
// Removal should work
storage->Remove(testKey, DataStorage_Temporary);
result = storage->Get(testKey, DataStorage_Temporary);
EXPECT_TRUE(result.IsEmpty());
// But removing one type shouldn't affect the others
result = storage->Get(testKey, DataStorage_Private);
EXPECT_STREQ("private", result.get());
result = storage->Get(testKey, DataStorage_Persistent);
EXPECT_STREQ("new", result.get());
// Test removing the other types as well
storage->Remove(testKey, DataStorage_Private);
result = storage->Get(testKey, DataStorage_Private);
EXPECT_TRUE(result.IsEmpty());
storage->Remove(testKey, DataStorage_Persistent);
result = storage->Get(testKey, DataStorage_Persistent);
EXPECT_TRUE(result.IsEmpty());
}
TEST_F(DataStorageTest, InputValidation)
{
EXPECT_TRUE(dataWillPersist);
// Keys may not have tabs or newlines
EXPECT_EQ(NS_ERROR_INVALID_ARG,
storage->Put(NS_LITERAL_CSTRING("key\thas tab"), testValue,
DataStorage_Persistent));
nsCString result = storage->Get(NS_LITERAL_CSTRING("key\thas tab"),
DataStorage_Persistent);
EXPECT_TRUE(result.IsEmpty());
EXPECT_EQ(NS_ERROR_INVALID_ARG,
storage->Put(NS_LITERAL_CSTRING("key has\nnewline"), testValue,
DataStorage_Persistent));
result = storage->Get(NS_LITERAL_CSTRING("keyhas\nnewline"),
DataStorage_Persistent);
EXPECT_TRUE(result.IsEmpty());
// Values may not have newlines
EXPECT_EQ(NS_ERROR_INVALID_ARG,
storage->Put(testKey, NS_LITERAL_CSTRING("value\nhas newline"),
DataStorage_Persistent));
result = storage->Get(testKey, DataStorage_Persistent);
// Values may have tabs
EXPECT_TRUE(result.IsEmpty());
EXPECT_EQ(NS_OK, storage->Put(testKey,
NS_LITERAL_CSTRING("val\thas tab; this is ok"),
DataStorage_Persistent));
result = storage->Get(testKey, DataStorage_Persistent);
EXPECT_STREQ("val\thas tab; this is ok", result.get());
nsCString longKey("a");
for (int i = 0; i < 8; i++) {
longKey.Append(longKey);
}
// A key of length 256 will work
EXPECT_EQ(NS_OK, storage->Put(longKey, testValue, DataStorage_Persistent));
result = storage->Get(longKey, DataStorage_Persistent);
EXPECT_STREQ("value", result.get());
longKey.Append("a");
// A key longer than that will not work
EXPECT_EQ(NS_ERROR_INVALID_ARG,
storage->Put(longKey, testValue, DataStorage_Persistent));
result = storage->Get(longKey, DataStorage_Persistent);
EXPECT_TRUE(result.IsEmpty());
nsCString longValue("a");
for (int i = 0; i < 10; i++) {
longValue.Append(longValue);
}
// A value of length 1024 will work
EXPECT_EQ(NS_OK, storage->Put(testKey, longValue, DataStorage_Persistent));
result = storage->Get(testKey, DataStorage_Persistent);
EXPECT_STREQ(longValue.get(), result.get());
longValue.Append("a");
// A value longer than that will not work
storage->Remove(testKey, DataStorage_Persistent);
EXPECT_EQ(NS_ERROR_INVALID_ARG,
storage->Put(testKey, longValue, DataStorage_Persistent));
result = storage->Get(testKey, DataStorage_Persistent);
EXPECT_TRUE(result.IsEmpty());
}
TEST_F(DataStorageTest, Eviction)
{
EXPECT_TRUE(dataWillPersist);
// Eviction is on a per-table basis. Tables shouldn't affect each other.
EXPECT_EQ(NS_OK, storage->Put(testKey, testValue, DataStorage_Persistent));
for (int i = 0; i < 1025; i++) {
EXPECT_EQ(NS_OK, storage->Put(nsPrintfCString("%d", i),
nsPrintfCString("%d", i),
DataStorage_Temporary));
nsCString result = storage->Get(nsPrintfCString("%d", i),
DataStorage_Temporary);
EXPECT_STREQ(nsPrintfCString("%d", i).get(), result.get());
}
// We don't know which entry got evicted, but we can count them.
int entries = 0;
for (int i = 0; i < 1025; i++) {
nsCString result = storage->Get(nsPrintfCString("%d", i),
DataStorage_Temporary);
if (!result.IsEmpty()) {
entries++;
}
}
EXPECT_EQ(entries, 1024);
nsCString result = storage->Get(testKey, DataStorage_Persistent);
EXPECT_STREQ("value", result.get());
}
TEST_F(DataStorageTest, ClearPrivateData)
{
EXPECT_TRUE(dataWillPersist);
EXPECT_EQ(NS_OK, storage->Put(testKey, privateTestValue,
DataStorage_Private));
nsCString result = storage->Get(testKey, DataStorage_Private);
EXPECT_STREQ("private", result.get());
storage->Observe(nullptr, "last-pb-context-exited", nullptr);
result = storage->Get(testKey, DataStorage_Private);
EXPECT_TRUE(result.IsEmpty());
}
TEST_F(DataStorageTest, Shutdown)
{
EXPECT_TRUE(dataWillPersist);
EXPECT_EQ(NS_OK, storage->Put(testKey, testValue, DataStorage_Persistent));
nsCString result = storage->Get(testKey, DataStorage_Persistent);
EXPECT_STREQ("value", result.get());
// Get "now" (in days) close to when the data was last touched, so we won't
// get intermittent failures with the day not matching.
int64_t microsecondsPerDay = 24 * 60 * 60 * int64_t(PR_USEC_PER_SEC);
int32_t nowInDays = int32_t(PR_Now() / microsecondsPerDay);
storage->Observe(nullptr, "profile-before-change", nullptr);
nsCOMPtr<nsIFile> backingFile;
EXPECT_EQ(NS_OK, NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR,
getter_AddRefs(backingFile)));
const ::testing::TestInfo* const testInfo =
::testing::UnitTest::GetInstance()->current_test_info();
NS_ConvertUTF8toUTF16 testName(testInfo->name());
EXPECT_EQ(NS_OK, backingFile->Append(testName));
nsCOMPtr<nsIInputStream> fileInputStream;
EXPECT_EQ(NS_OK, NS_NewLocalFileInputStream(getter_AddRefs(fileInputStream),
backingFile));
nsCString data;
EXPECT_EQ(NS_OK, NS_ConsumeStream(fileInputStream, UINT32_MAX, data));
// The data will be of the form 'test\t0\t<days since the epoch>\tvalue'
EXPECT_STREQ(nsPrintfCString("test\t0\t%d\tvalue\n", nowInDays).get(),
data.get());
}