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 : * ***** BEGIN LICENSE BLOCK *****
4 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 : *
6 : * The contents of this file are subject to the Mozilla Public License Version
7 : * 1.1 (the "License"); you may not use this file except in compliance with
8 : * the License. You may obtain a copy of the License at
9 : * http://www.mozilla.org/MPL/
10 : *
11 : * Software distributed under the License is distributed on an "AS IS" basis,
12 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 : * for the specific language governing rights and limitations under the
14 : * License.
15 : *
16 : * The Original Code is storage test code.
17 : *
18 : * The Initial Developer of the Original Code is
19 : * Mozilla Foundation.
20 : * Portions created by the Initial Developer are Copyright (C) 2010
21 : * the Initial Developer. All Rights Reserved.
22 : *
23 : * Contributor(s):
24 : * Daniel Witte <dwitte@mozilla.com> (Original Author)
25 : *
26 : * Alternatively, the contents of this file may be used under the terms of
27 : * either the GNU General Public License Version 2 or later (the "GPL"), or
28 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 : * in which case the provisions of the GPL or the LGPL are applicable instead
30 : * of those above. If you wish to allow use of your version of this file only
31 : * under the terms of either the GPL or the LGPL, and not to allow others to
32 : * use your version of this file under the terms of the MPL, indicate your
33 : * decision by deleting the provisions above and replace them with the notice
34 : * and other provisions required by the GPL or the LGPL. If you do not delete
35 : * the provisions above, a recipient may use your version of this file under
36 : * the terms of any one of the MPL, the GPL or the LGPL.
37 : *
38 : * ***** END LICENSE BLOCK ***** */
39 :
40 : #include "storage_test_harness.h"
41 :
42 : #include "mozStorageHelper.h"
43 :
44 : using namespace mozilla;
45 :
46 : /**
47 : * This file tests binding and reading out string parameters through the
48 : * mozIStorageStatement API.
49 : */
50 :
51 : void
52 1 : test_ASCIIString()
53 : {
54 2 : nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase());
55 :
56 : // Create table with a single string column.
57 2 : (void)db->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
58 : "CREATE TABLE test (str STRING)"
59 1 : ));
60 :
61 : // Create statements to INSERT and SELECT the string.
62 2 : nsCOMPtr<mozIStorageStatement> insert, select;
63 2 : (void)db->CreateStatement(NS_LITERAL_CSTRING(
64 : "INSERT INTO test (str) VALUES (?1)"
65 2 : ), getter_AddRefs(insert));
66 2 : (void)db->CreateStatement(NS_LITERAL_CSTRING(
67 : "SELECT str FROM test"
68 2 : ), getter_AddRefs(select));
69 :
70 : // Roundtrip a string through the table, and ensure it comes out as expected.
71 2 : nsCAutoString inserted("I'm an ASCII string");
72 : {
73 2 : mozStorageStatementScoper scoper(insert);
74 : bool hasResult;
75 1 : do_check_true(NS_SUCCEEDED(insert->BindUTF8StringByIndex(0, inserted)));
76 1 : do_check_true(NS_SUCCEEDED(insert->ExecuteStep(&hasResult)));
77 1 : do_check_false(hasResult);
78 : }
79 :
80 2 : nsCAutoString result;
81 : {
82 2 : mozStorageStatementScoper scoper(select);
83 : bool hasResult;
84 1 : do_check_true(NS_SUCCEEDED(select->ExecuteStep(&hasResult)));
85 1 : do_check_true(hasResult);
86 1 : do_check_true(NS_SUCCEEDED(select->GetUTF8String(0, result)));
87 : }
88 :
89 1 : do_check_true(result == inserted);
90 :
91 1 : (void)db->ExecuteSimpleSQL(NS_LITERAL_CSTRING("DELETE FROM test"));
92 1 : }
93 :
94 : void
95 1 : test_CString()
96 : {
97 2 : nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase());
98 :
99 : // Create table with a single string column.
100 2 : (void)db->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
101 : "CREATE TABLE test (str STRING)"
102 1 : ));
103 :
104 : // Create statements to INSERT and SELECT the string.
105 2 : nsCOMPtr<mozIStorageStatement> insert, select;
106 2 : (void)db->CreateStatement(NS_LITERAL_CSTRING(
107 : "INSERT INTO test (str) VALUES (?1)"
108 2 : ), getter_AddRefs(insert));
109 2 : (void)db->CreateStatement(NS_LITERAL_CSTRING(
110 : "SELECT str FROM test"
111 2 : ), getter_AddRefs(select));
112 :
113 : // Roundtrip a string through the table, and ensure it comes out as expected.
114 : static const char sCharArray[] =
115 : "I'm not a \xff\x00\xac\xde\xbb ASCII string!";
116 2 : nsCAutoString inserted(sCharArray, ArrayLength(sCharArray) - 1);
117 1 : do_check_true(inserted.Length() == ArrayLength(sCharArray) - 1);
118 : {
119 2 : mozStorageStatementScoper scoper(insert);
120 : bool hasResult;
121 1 : do_check_true(NS_SUCCEEDED(insert->BindUTF8StringByIndex(0, inserted)));
122 1 : do_check_true(NS_SUCCEEDED(insert->ExecuteStep(&hasResult)));
123 1 : do_check_false(hasResult);
124 : }
125 :
126 : {
127 2 : nsCAutoString result;
128 :
129 2 : mozStorageStatementScoper scoper(select);
130 : bool hasResult;
131 1 : do_check_true(NS_SUCCEEDED(select->ExecuteStep(&hasResult)));
132 1 : do_check_true(hasResult);
133 1 : do_check_true(NS_SUCCEEDED(select->GetUTF8String(0, result)));
134 :
135 1 : do_check_true(result == inserted);
136 : }
137 :
138 1 : (void)db->ExecuteSimpleSQL(NS_LITERAL_CSTRING("DELETE FROM test"));
139 1 : }
140 :
141 : void
142 1 : test_UTFStrings()
143 : {
144 2 : nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase());
145 :
146 : // Create table with a single string column.
147 2 : (void)db->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
148 : "CREATE TABLE test (str STRING)"
149 1 : ));
150 :
151 : // Create statements to INSERT and SELECT the string.
152 2 : nsCOMPtr<mozIStorageStatement> insert, select;
153 2 : (void)db->CreateStatement(NS_LITERAL_CSTRING(
154 : "INSERT INTO test (str) VALUES (?1)"
155 2 : ), getter_AddRefs(insert));
156 2 : (void)db->CreateStatement(NS_LITERAL_CSTRING(
157 : "SELECT str FROM test"
158 2 : ), getter_AddRefs(select));
159 :
160 : // Roundtrip a UTF8 string through the table, using UTF8 input and output.
161 : static const char sCharArray[] =
162 : "I'm a \xc3\xbb\xc3\xbc\xc3\xa2\xc3\xa4\xc3\xa7 UTF8 string!";
163 2 : nsCAutoString insertedUTF8(sCharArray, ArrayLength(sCharArray) - 1);
164 1 : do_check_true(insertedUTF8.Length() == ArrayLength(sCharArray) - 1);
165 2 : NS_ConvertUTF8toUTF16 insertedUTF16(insertedUTF8);
166 1 : do_check_true(insertedUTF8 == NS_ConvertUTF16toUTF8(insertedUTF16));
167 : {
168 2 : mozStorageStatementScoper scoper(insert);
169 : bool hasResult;
170 1 : do_check_true(NS_SUCCEEDED(insert->BindUTF8StringByIndex(0, insertedUTF8)));
171 1 : do_check_true(NS_SUCCEEDED(insert->ExecuteStep(&hasResult)));
172 1 : do_check_false(hasResult);
173 : }
174 :
175 : {
176 2 : nsCAutoString result;
177 :
178 2 : mozStorageStatementScoper scoper(select);
179 : bool hasResult;
180 1 : do_check_true(NS_SUCCEEDED(select->ExecuteStep(&hasResult)));
181 1 : do_check_true(hasResult);
182 1 : do_check_true(NS_SUCCEEDED(select->GetUTF8String(0, result)));
183 :
184 1 : do_check_true(result == insertedUTF8);
185 : }
186 :
187 : // Use UTF8 input and UTF16 output.
188 : {
189 2 : nsAutoString result;
190 :
191 2 : mozStorageStatementScoper scoper(select);
192 : bool hasResult;
193 1 : do_check_true(NS_SUCCEEDED(select->ExecuteStep(&hasResult)));
194 1 : do_check_true(hasResult);
195 1 : do_check_true(NS_SUCCEEDED(select->GetString(0, result)));
196 :
197 1 : do_check_true(result == insertedUTF16);
198 : }
199 :
200 1 : (void)db->ExecuteSimpleSQL(NS_LITERAL_CSTRING("DELETE FROM test"));
201 :
202 : // Roundtrip the same string using UTF16 input and UTF8 output.
203 : {
204 2 : mozStorageStatementScoper scoper(insert);
205 : bool hasResult;
206 1 : do_check_true(NS_SUCCEEDED(insert->BindStringByIndex(0, insertedUTF16)));
207 1 : do_check_true(NS_SUCCEEDED(insert->ExecuteStep(&hasResult)));
208 1 : do_check_false(hasResult);
209 : }
210 :
211 : {
212 2 : nsCAutoString result;
213 :
214 2 : mozStorageStatementScoper scoper(select);
215 : bool hasResult;
216 1 : do_check_true(NS_SUCCEEDED(select->ExecuteStep(&hasResult)));
217 1 : do_check_true(hasResult);
218 1 : do_check_true(NS_SUCCEEDED(select->GetUTF8String(0, result)));
219 :
220 1 : do_check_true(result == insertedUTF8);
221 : }
222 :
223 : // Use UTF16 input and UTF16 output.
224 : {
225 2 : nsAutoString result;
226 :
227 2 : mozStorageStatementScoper scoper(select);
228 : bool hasResult;
229 1 : do_check_true(NS_SUCCEEDED(select->ExecuteStep(&hasResult)));
230 1 : do_check_true(hasResult);
231 1 : do_check_true(NS_SUCCEEDED(select->GetString(0, result)));
232 :
233 1 : do_check_true(result == insertedUTF16);
234 : }
235 :
236 1 : (void)db->ExecuteSimpleSQL(NS_LITERAL_CSTRING("DELETE FROM test"));
237 1 : }
238 :
239 : void (*gTests[])(void) = {
240 : test_ASCIIString,
241 : test_CString,
242 : test_UTFStrings,
243 : };
244 :
245 : const char *file = __FILE__;
246 : #define TEST_NAME "binding string params"
247 : #define TEST_FILE file
248 : #include "storage_test_harness_tail.h"
|