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.

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

mercurial