storage/test/test_mutex.cpp

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
michael@0 2 * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
michael@0 3 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #include "storage_test_harness.h"
michael@0 8
michael@0 9 #include "SQLiteMutex.h"
michael@0 10
michael@0 11 using namespace mozilla;
michael@0 12 using namespace mozilla::storage;
michael@0 13
michael@0 14 /**
michael@0 15 * This file test our sqlite3_mutex wrapper in SQLiteMutex.h.
michael@0 16 */
michael@0 17
michael@0 18 void
michael@0 19 test_AutoLock()
michael@0 20 {
michael@0 21 int lockTypes[] = {
michael@0 22 SQLITE_MUTEX_FAST,
michael@0 23 SQLITE_MUTEX_RECURSIVE,
michael@0 24 };
michael@0 25 for (size_t i = 0; i < ArrayLength(lockTypes); i++) {
michael@0 26 // Get our test mutex (we have to allocate a SQLite mutex of the right type
michael@0 27 // too!).
michael@0 28 SQLiteMutex mutex("TestMutex");
michael@0 29 sqlite3_mutex *inner = sqlite3_mutex_alloc(lockTypes[i]);
michael@0 30 do_check_true(inner);
michael@0 31 mutex.initWithMutex(inner);
michael@0 32
michael@0 33 // And test that our automatic locking wrapper works as expected.
michael@0 34 mutex.assertNotCurrentThreadOwns();
michael@0 35 {
michael@0 36 SQLiteMutexAutoLock lockedScope(mutex);
michael@0 37 mutex.assertCurrentThreadOwns();
michael@0 38 }
michael@0 39 mutex.assertNotCurrentThreadOwns();
michael@0 40
michael@0 41 // Free the wrapped mutex - we don't need it anymore.
michael@0 42 sqlite3_mutex_free(inner);
michael@0 43 }
michael@0 44 }
michael@0 45
michael@0 46 void
michael@0 47 test_AutoUnlock()
michael@0 48 {
michael@0 49 int lockTypes[] = {
michael@0 50 SQLITE_MUTEX_FAST,
michael@0 51 SQLITE_MUTEX_RECURSIVE,
michael@0 52 };
michael@0 53 for (size_t i = 0; i < ArrayLength(lockTypes); i++) {
michael@0 54 // Get our test mutex (we have to allocate a SQLite mutex of the right type
michael@0 55 // too!).
michael@0 56 SQLiteMutex mutex("TestMutex");
michael@0 57 sqlite3_mutex *inner = sqlite3_mutex_alloc(lockTypes[i]);
michael@0 58 do_check_true(inner);
michael@0 59 mutex.initWithMutex(inner);
michael@0 60
michael@0 61 // And test that our automatic unlocking wrapper works as expected.
michael@0 62 {
michael@0 63 SQLiteMutexAutoLock lockedScope(mutex);
michael@0 64
michael@0 65 {
michael@0 66 SQLiteMutexAutoUnlock unlockedScope(mutex);
michael@0 67 mutex.assertNotCurrentThreadOwns();
michael@0 68 }
michael@0 69 mutex.assertCurrentThreadOwns();
michael@0 70 }
michael@0 71
michael@0 72 // Free the wrapped mutex - we don't need it anymore.
michael@0 73 sqlite3_mutex_free(inner);
michael@0 74 }
michael@0 75 }
michael@0 76
michael@0 77 void (*gTests[])(void) = {
michael@0 78 test_AutoLock,
michael@0 79 test_AutoUnlock,
michael@0 80 };
michael@0 81
michael@0 82 const char *file = __FILE__;
michael@0 83 #define TEST_NAME "SQLiteMutex"
michael@0 84 #define TEST_FILE file
michael@0 85 #include "storage_test_harness_tail.h"

mercurial