storage/test/test_StatementCache.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 "mozilla/storage/StatementCache.h"
    10 using namespace mozilla::storage;
    12 /**
    13  * This file test our statement cache in StatementCache.h.
    14  */
    16 ////////////////////////////////////////////////////////////////////////////////
    17 //// Helpers
    19 class SyncCache : public StatementCache<mozIStorageStatement>
    20 {
    21 public:
    22   SyncCache(nsCOMPtr<mozIStorageConnection>& aConnection)
    23   : StatementCache<mozIStorageStatement>(aConnection)
    24   {
    25   }
    26 };
    28 class AsyncCache : public StatementCache<mozIStorageAsyncStatement>
    29 {
    30 public:
    31   AsyncCache(nsCOMPtr<mozIStorageConnection>& aConnection)
    32   : StatementCache<mozIStorageAsyncStatement>(aConnection)
    33   {
    34   }
    35 };
    37 /**
    38  * Wraps nsCString so we can not implement the same functions twice for each
    39  * type.
    40  */
    41 class StringWrapper : public nsCString
    42 {
    43 public:
    44   StringWrapper(const char* aOther)
    45   {
    46     this->Assign(aOther);
    47   }
    48 };
    50 ////////////////////////////////////////////////////////////////////////////////
    51 //// Test Functions
    53 template<typename StringType>
    54 void
    55 test_GetCachedStatement()
    56 {
    57   nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase());
    58   SyncCache cache(db);
    60   StringType sql = "SELECT * FROM sqlite_master";
    62   // Make sure we get a statement back with the right state.
    63   nsCOMPtr<mozIStorageStatement> stmt = cache.GetCachedStatement(sql);
    64   do_check_true(stmt);
    65   int32_t state;
    66   do_check_success(stmt->GetState(&state));
    67   do_check_eq(mozIStorageBaseStatement::MOZ_STORAGE_STATEMENT_READY, state);
    69   // Check to make sure we get the same copy the second time we ask.
    70   nsCOMPtr<mozIStorageStatement> stmt2 = cache.GetCachedStatement(sql);
    71   do_check_true(stmt2);
    72   do_check_eq(stmt.get(), stmt2.get());
    73 }
    75 template <typename StringType>
    76 void
    77 test_FinalizeStatements()
    78 {
    79   nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase());
    80   SyncCache cache(db);
    82   StringType sql = "SELECT * FROM sqlite_master";
    84   // Get a statement, and then tell the cache to finalize.
    85   nsCOMPtr<mozIStorageStatement> stmt = cache.GetCachedStatement(sql);
    86   do_check_true(stmt);
    88   cache.FinalizeStatements();
    90   // We should be in an invalid state at this point.
    91   int32_t state;
    92   do_check_success(stmt->GetState(&state));
    93   do_check_eq(mozIStorageBaseStatement::MOZ_STORAGE_STATEMENT_INVALID, state);
    95   // Should be able to close the database now too.
    96   do_check_success(db->Close());
    97 }
    99 template<typename StringType>
   100 void
   101 test_GetCachedAsyncStatement()
   102 {
   103   nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase());
   104   AsyncCache cache(db);
   106   StringType sql = "SELECT * FROM sqlite_master";
   108   // Make sure we get a statement back with the right state.
   109   nsCOMPtr<mozIStorageAsyncStatement> stmt = cache.GetCachedStatement(sql);
   110   do_check_true(stmt);
   111   int32_t state;
   112   do_check_success(stmt->GetState(&state));
   113   do_check_eq(mozIStorageBaseStatement::MOZ_STORAGE_STATEMENT_READY, state);
   115   // Check to make sure we get the same copy the second time we ask.
   116   nsCOMPtr<mozIStorageAsyncStatement> stmt2 = cache.GetCachedStatement(sql);
   117   do_check_true(stmt2);
   118   do_check_eq(stmt.get(), stmt2.get());
   119 }
   121 template <typename StringType>
   122 void
   123 test_FinalizeAsyncStatements()
   124 {
   125   nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase());
   126   AsyncCache cache(db);
   128   StringType sql = "SELECT * FROM sqlite_master";
   130   // Get a statement, and then tell the cache to finalize.
   131   nsCOMPtr<mozIStorageAsyncStatement> stmt = cache.GetCachedStatement(sql);
   132   do_check_true(stmt);
   134   cache.FinalizeStatements();
   136   // We should be in an invalid state at this point.
   137   int32_t state;
   138   do_check_success(stmt->GetState(&state));
   139   do_check_eq(mozIStorageBaseStatement::MOZ_STORAGE_STATEMENT_INVALID, state);
   141   // Should be able to close the database now too.
   142   do_check_success(db->AsyncClose(nullptr));
   143 }
   145 ////////////////////////////////////////////////////////////////////////////////
   146 //// Test Harness Stuff
   148 void (*gTests[])(void) = {
   149   test_GetCachedStatement<const char []>,
   150   test_GetCachedStatement<StringWrapper>,
   151   test_FinalizeStatements<const char []>,
   152   test_FinalizeStatements<StringWrapper>,
   153   test_GetCachedAsyncStatement<const char []>,
   154   test_GetCachedAsyncStatement<StringWrapper>,
   155   test_FinalizeAsyncStatements<const char []>,
   156   test_FinalizeAsyncStatements<StringWrapper>,
   157 };
   159 const char *file = __FILE__;
   160 #define TEST_NAME "StatementCache"
   161 #define TEST_FILE file
   162 #include "storage_test_harness_tail.h"

mercurial