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

mercurial