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