tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_statement_scoper.cpp (2917B)


      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/. */
      6 
      7 #include "storage_test_harness.h"
      8 
      9 #include "mozStorageHelper.h"
     10 
     11 /**
     12 * This file test our statement scoper in mozStorageHelper.h.
     13 */
     14 
     15 TEST(storage_statement_scoper, automatic_reset)
     16 {
     17  nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase());
     18 
     19  // Need to create a table to populate sqlite_master with an entry.
     20  (void)db->ExecuteSimpleSQL("CREATE TABLE test (id INTEGER PRIMARY KEY)"_ns);
     21 
     22  nsCOMPtr<mozIStorageStatement> stmt;
     23  (void)db->CreateStatement("SELECT * FROM sqlite_master"_ns,
     24                            getter_AddRefs(stmt));
     25 
     26  // Reality check - make sure we start off in the right state.
     27  int32_t state = -1;
     28  (void)stmt->GetState(&state);
     29  do_check_true(state == mozIStorageStatement::MOZ_STORAGE_STATEMENT_READY);
     30 
     31  // Start executing the statement, which will put it into an executing state.
     32  {
     33    mozStorageStatementScoper scoper(stmt);
     34    bool hasMore;
     35    do_check_true(NS_SUCCEEDED(stmt->ExecuteStep(&hasMore)));
     36 
     37    // Reality check that we are executing.
     38    state = -1;
     39    (void)stmt->GetState(&state);
     40    do_check_true(state ==
     41                  mozIStorageStatement::MOZ_STORAGE_STATEMENT_EXECUTING);
     42  }
     43 
     44  // And we should be ready again.
     45  state = -1;
     46  (void)stmt->GetState(&state);
     47  do_check_true(state == mozIStorageStatement::MOZ_STORAGE_STATEMENT_READY);
     48 }
     49 
     50 TEST(storage_statement_scoper, Abandon)
     51 {
     52  nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase());
     53 
     54  // Need to create a table to populate sqlite_master with an entry.
     55  (void)db->ExecuteSimpleSQL("CREATE TABLE test (id INTEGER PRIMARY KEY)"_ns);
     56 
     57  nsCOMPtr<mozIStorageStatement> stmt;
     58  (void)db->CreateStatement("SELECT * FROM sqlite_master"_ns,
     59                            getter_AddRefs(stmt));
     60 
     61  // Reality check - make sure we start off in the right state.
     62  int32_t state = -1;
     63  (void)stmt->GetState(&state);
     64  do_check_true(state == mozIStorageStatement::MOZ_STORAGE_STATEMENT_READY);
     65 
     66  // Start executing the statement, which will put it into an executing state.
     67  {
     68    mozStorageStatementScoper scoper(stmt);
     69    bool hasMore;
     70    do_check_true(NS_SUCCEEDED(stmt->ExecuteStep(&hasMore)));
     71 
     72    // Reality check that we are executing.
     73    state = -1;
     74    (void)stmt->GetState(&state);
     75    do_check_true(state ==
     76                  mozIStorageStatement::MOZ_STORAGE_STATEMENT_EXECUTING);
     77 
     78    // And call Abandon.  We should not reset now when we fall out of scope.
     79    scoper.Abandon();
     80  }
     81 
     82  // And we should still be executing.
     83  state = -1;
     84  (void)stmt->GetState(&state);
     85  do_check_true(state == mozIStorageStatement::MOZ_STORAGE_STATEMENT_EXECUTING);
     86 }