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