tor-browser

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

test_binding_params.cpp (6142B)


      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 using namespace mozilla;
     12 
     13 /**
     14 * This file tests binding and reading out string parameters through the
     15 * mozIStorageStatement API.
     16 */
     17 
     18 TEST(storage_binding_params, ASCIIString)
     19 {
     20  nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase());
     21 
     22  // Create table with a single string column.
     23  (void)db->ExecuteSimpleSQL("CREATE TABLE test (str STRING)"_ns);
     24 
     25  // Create statements to INSERT and SELECT the string.
     26  nsCOMPtr<mozIStorageStatement> insert, select;
     27  (void)db->CreateStatement("INSERT INTO test (str) VALUES (?1)"_ns,
     28                            getter_AddRefs(insert));
     29  (void)db->CreateStatement("SELECT str FROM test"_ns, getter_AddRefs(select));
     30 
     31  // Roundtrip a string through the table, and ensure it comes out as expected.
     32  nsAutoCString inserted("I'm an ASCII string");
     33  {
     34    mozStorageStatementScoper scoper(insert);
     35    bool hasResult;
     36    do_check_true(NS_SUCCEEDED(insert->BindUTF8StringByIndex(0, inserted)));
     37    do_check_true(NS_SUCCEEDED(insert->ExecuteStep(&hasResult)));
     38    do_check_false(hasResult);
     39  }
     40 
     41  nsAutoCString result;
     42  {
     43    mozStorageStatementScoper scoper(select);
     44    bool hasResult;
     45    do_check_true(NS_SUCCEEDED(select->ExecuteStep(&hasResult)));
     46    do_check_true(hasResult);
     47    do_check_true(NS_SUCCEEDED(select->GetUTF8String(0, result)));
     48  }
     49 
     50  do_check_true(result == inserted);
     51 
     52  (void)db->ExecuteSimpleSQL("DELETE FROM test"_ns);
     53 }
     54 
     55 TEST(storage_binding_params, CString)
     56 {
     57  nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase());
     58 
     59  // Create table with a single string column.
     60  (void)db->ExecuteSimpleSQL("CREATE TABLE test (str STRING)"_ns);
     61 
     62  // Create statements to INSERT and SELECT the string.
     63  nsCOMPtr<mozIStorageStatement> insert, select;
     64  (void)db->CreateStatement("INSERT INTO test (str) VALUES (?1)"_ns,
     65                            getter_AddRefs(insert));
     66  (void)db->CreateStatement("SELECT str FROM test"_ns, getter_AddRefs(select));
     67 
     68  // Roundtrip a string through the table, and ensure it comes out as expected.
     69  static const char sCharArray[] =
     70      "I'm not a \xff\x00\xac\xde\xbb ASCII string!";
     71  nsAutoCString inserted(sCharArray, std::size(sCharArray) - 1);
     72  do_check_true(inserted.Length() == std::size(sCharArray) - 1);
     73  {
     74    mozStorageStatementScoper scoper(insert);
     75    bool hasResult;
     76    do_check_true(NS_SUCCEEDED(insert->BindUTF8StringByIndex(0, inserted)));
     77    do_check_true(NS_SUCCEEDED(insert->ExecuteStep(&hasResult)));
     78    do_check_false(hasResult);
     79  }
     80 
     81  {
     82    nsAutoCString result;
     83 
     84    mozStorageStatementScoper scoper(select);
     85    bool hasResult;
     86    do_check_true(NS_SUCCEEDED(select->ExecuteStep(&hasResult)));
     87    do_check_true(hasResult);
     88    do_check_true(NS_SUCCEEDED(select->GetUTF8String(0, result)));
     89 
     90    do_check_true(result == inserted);
     91  }
     92 
     93  (void)db->ExecuteSimpleSQL("DELETE FROM test"_ns);
     94 }
     95 
     96 TEST(storage_binding_params, UTFStrings)
     97 {
     98  nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase());
     99 
    100  // Create table with a single string column.
    101  (void)db->ExecuteSimpleSQL("CREATE TABLE test (str STRING)"_ns);
    102 
    103  // Create statements to INSERT and SELECT the string.
    104  nsCOMPtr<mozIStorageStatement> insert, select;
    105  (void)db->CreateStatement("INSERT INTO test (str) VALUES (?1)"_ns,
    106                            getter_AddRefs(insert));
    107  (void)db->CreateStatement("SELECT str FROM test"_ns, getter_AddRefs(select));
    108 
    109  // Roundtrip a UTF8 string through the table, using UTF8 input and output.
    110  static const char sCharArray[] = R"(I'm a ûüâäç UTF8 string!)";
    111  nsAutoCString insertedUTF8(sCharArray, std::size(sCharArray) - 1);
    112  do_check_true(insertedUTF8.Length() == std::size(sCharArray) - 1);
    113  NS_ConvertUTF8toUTF16 insertedUTF16(insertedUTF8);
    114  do_check_true(insertedUTF8 == NS_ConvertUTF16toUTF8(insertedUTF16));
    115  {
    116    mozStorageStatementScoper scoper(insert);
    117    bool hasResult;
    118    do_check_true(NS_SUCCEEDED(insert->BindUTF8StringByIndex(0, insertedUTF8)));
    119    do_check_true(NS_SUCCEEDED(insert->ExecuteStep(&hasResult)));
    120    do_check_false(hasResult);
    121  }
    122 
    123  {
    124    nsAutoCString result;
    125 
    126    mozStorageStatementScoper scoper(select);
    127    bool hasResult;
    128    do_check_true(NS_SUCCEEDED(select->ExecuteStep(&hasResult)));
    129    do_check_true(hasResult);
    130    do_check_true(NS_SUCCEEDED(select->GetUTF8String(0, result)));
    131 
    132    do_check_true(result == insertedUTF8);
    133  }
    134 
    135  // Use UTF8 input and UTF16 output.
    136  {
    137    nsAutoString result;
    138 
    139    mozStorageStatementScoper scoper(select);
    140    bool hasResult;
    141    do_check_true(NS_SUCCEEDED(select->ExecuteStep(&hasResult)));
    142    do_check_true(hasResult);
    143    do_check_true(NS_SUCCEEDED(select->GetString(0, result)));
    144 
    145    do_check_true(result == insertedUTF16);
    146  }
    147 
    148  (void)db->ExecuteSimpleSQL("DELETE FROM test"_ns);
    149 
    150  // Roundtrip the same string using UTF16 input and UTF8 output.
    151  {
    152    mozStorageStatementScoper scoper(insert);
    153    bool hasResult;
    154    do_check_true(NS_SUCCEEDED(insert->BindStringByIndex(0, insertedUTF16)));
    155    do_check_true(NS_SUCCEEDED(insert->ExecuteStep(&hasResult)));
    156    do_check_false(hasResult);
    157  }
    158 
    159  {
    160    nsAutoCString result;
    161 
    162    mozStorageStatementScoper scoper(select);
    163    bool hasResult;
    164    do_check_true(NS_SUCCEEDED(select->ExecuteStep(&hasResult)));
    165    do_check_true(hasResult);
    166    do_check_true(NS_SUCCEEDED(select->GetUTF8String(0, result)));
    167 
    168    do_check_true(result == insertedUTF8);
    169  }
    170 
    171  // Use UTF16 input and UTF16 output.
    172  {
    173    nsAutoString result;
    174 
    175    mozStorageStatementScoper scoper(select);
    176    bool hasResult;
    177    do_check_true(NS_SUCCEEDED(select->ExecuteStep(&hasResult)));
    178    do_check_true(hasResult);
    179    do_check_true(NS_SUCCEEDED(select->GetString(0, result)));
    180 
    181    do_check_true(result == insertedUTF16);
    182  }
    183 
    184  (void)db->ExecuteSimpleSQL("DELETE FROM test"_ns);
    185 }