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