121
127
# ###########################################################################
124
$q->serialize_list(),
126
"Serialize empty list"
130
$q->serialize_list(''),
132
"Serialize 1 empty string",
136
$q->serialize_list('', '', ''),
138
"Serialize 3 empty strings",
142
$q->serialize_list(undef),
144
"Serialize undef string",
148
$q->deserialize_list(undef),
150
"Deserialize undef string",
153
my @serialize_tests = (
130
$q->serialize_list( () ),
132
'Serialize empty list returns undef'
135
binmode(STDOUT, ':utf8')
136
or die "Can't binmode(STDOUT, ':utf8'): $OS_ERROR";
137
binmode(STDERR, ':utf8')
138
or die "Can't binmode(STDERR, ':utf8'): $OS_ERROR";
140
# Prevent "Wide character in print at Test/Builder.pm" warnings.
141
binmode Test::More->builder->$_(), ':encoding(UTF-8)'
142
for qw(output failure_output);
144
my @latin1_serialize_tests = (
156
[ "a,\\\nc\nas", 'b', ],
159
[ 'a\\\\\,aa', 'c', ],
160
[ 'a\\\\\\,aa', 'c', ],
161
[ 'a\\\,a,a', 'c,d,e,d,', ],
162
[ "\\\,\x{e8},a", '!!!!__!*`,`\\', ], # Latin-1
163
[ "\x{30cb}\\\,\x{e8},a", '!!!!__!*`,`\\', ], # UTF-8
164
[ ",,,,,,,,,,,,,,", ",", ],
165
[ "\\,\\,\\,\\,\\,\\,\\,\\,\\,\\,\\,,,,\\", ":(", ],
166
[ "asdfa", "\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\,a", ],
147
[ 'a,', 'b', ], # trailing comma
148
[ ',a', 'b', ], # leading comma
153
[ '' ], # emptry string
176
my $dp = new DSNParser(opts=>$dsn_opts);
177
my $sb = new Sandbox(basedir => '/tmp', DSNParser => $dp);
178
my $dbh = $sb->get_dbh_for('master');
158
[ '\N' ], # literal \N
159
[ "un caf\x{e9} na\x{ef}ve" ], # Latin-1
162
[ q/"abc\\", 'def'/ ], # Brian's pathalogical case
165
my @utf8_serialize_tests = (
166
[ "\x{30cb} \x{e8}" ], # UTF-8
180
skip 'Cannot connect to sandbox master', scalar @serialize_tests unless $dbh;
182
# Prevent "Wide character in print at Test/Builder.pm" warnings.
183
binmode Test::More->builder->$_(), ':encoding(UTF-8)'
184
for qw(output failure_output);
170
skip 'Cannot connect to sandbox master', scalar @latin1_serialize_tests
186
173
$dbh->do('CREATE DATABASE IF NOT EXISTS serialize_test');
187
174
$dbh->do('DROP TABLE IF EXISTS serialize_test.serialize');
188
$dbh->do('CREATE TABLE serialize_test.serialize (id INT, foo TEXT)');
190
my $sth = $dbh->prepare(
191
"INSERT INTO serialize_test.serialize (id, foo) VALUES (?, ?)"
193
my $selsth = $dbh->prepare(
194
"SELECT foo FROM serialize_test.serialize WHERE id=? LIMIT 1"
197
for my $test_index ( 0..$#serialize_tests ) {
198
my $ser = $q->serialize_list( @{$serialize_tests[$test_index]} );
200
# Bit of a hack, but we want to test both of Perl's internal encodings
202
local $dbh->{'mysql_enable_utf8'} = 1 if utf8::is_utf8($ser);
204
$sth->execute($test_index, $ser);
205
$selsth->execute($test_index);
207
my $flat_string = "["
209
map { defined($_) ? $_ : '' } @{$serialize_tests[$test_index]}
212
$flat_string =~ s/\n/\\n/g;
216
skip "DBD::mysql version $DBD::mysql::VERSION has utf8 bugs. "
217
. "See https://bugs.launchpad.net/percona-toolkit/+bug/932327",
218
1 if $DBD::mysql::VERSION lt '4' && $test_index == 9;
220
[ $q->deserialize_list($selsth->fetchrow_array()) ],
221
$serialize_tests[$test_index],
222
"Serialize $flat_string"
230
$dbh->do("DROP DATABASE serialize_test");
175
$dbh->do('CREATE TABLE serialize_test.serialize (id INT, textval TEXT, blobval BLOB)');
177
my $sth = $dbh->prepare(
178
"INSERT INTO serialize_test.serialize VALUES (?, ?, ?)"
181
for my $test_index ( 0..$#latin1_serialize_tests ) {
183
# Flat, friendly name for the test string
187
map { defined($_) ? $_ : 'undef' }
188
@{$latin1_serialize_tests[$test_index]})
190
$flat_string =~ s/\n/\\n/g;
192
# INSERT the serialized list of values.
193
my $ser = $q->serialize_list( @{$latin1_serialize_tests[$test_index]} );
194
$sth->execute($test_index, $ser, $ser);
196
# SELECT back the values and deserialize them.
197
my ($text_string) = $dbh->selectrow_array(
198
"SELECT textval FROM serialize_test.serialize WHERE id=$test_index");
199
my @text_parts = $q->deserialize_list($text_string);
203
$latin1_serialize_tests[$test_index],
204
"Serialize $flat_string"
205
) or diag(Dumper($text_string, \@text_parts));
209
my $utf8_dbh = $sb->get_dbh_for('master');
210
$utf8_dbh->{mysql_enable_utf8} = 1;
211
$utf8_dbh->do("SET NAMES 'utf8'");
213
skip 'Cannot connect to sandbox master', scalar @utf8_serialize_tests
215
skip 'DBD::mysql 3.0007 has UTF-8 bug', scalar @utf8_serialize_tests
216
if $DBD::mysql::VERSION le '3.0007';
218
$utf8_dbh->do("DROP TABLE serialize_test.serialize");
219
$utf8_dbh->do("CREATE TABLE serialize_test.serialize (id INT, textval TEXT, blobval BLOB) CHARSET='utf8'");
221
my $sth = $utf8_dbh->prepare(
222
"INSERT INTO serialize_test.serialize VALUES (?, ?, ?)"
225
for my $test_index ( 0..$#utf8_serialize_tests ) {
227
# Flat, friendly name for the test string
231
map { defined($_) ? $_ : 'undef' }
232
@{$utf8_serialize_tests[$test_index]})
234
$flat_string =~ s/\n/\\n/g;
236
# INSERT the serialized list of values.
237
my $ser = $q->serialize_list( @{$utf8_serialize_tests[$test_index]} );
238
$sth->execute($test_index, $ser, $ser);
240
# SELECT back the values and deserialize them.
241
my ($text_string) = $utf8_dbh->selectrow_array(
242
"SELECT textval FROM serialize_test.serialize WHERE id=$test_index");
243
my @text_parts = $q->deserialize_list($text_string);
247
$utf8_serialize_tests[$test_index],
248
"Serialize UTF-8 $flat_string"
249
) or diag(Dumper($text_string, \@text_parts));
252
$utf8_dbh->disconnect();
255
# ###########################################################################
257
# ###########################################################################
231
259
$sb->wipe_clean($dbh);
232
260
$dbh->disconnect();
235
# ###########################################################################
237
# ###########################################################################
238
262
ok($sb->ok(), "Sandbox servers") or BAIL_OUT(__FILE__ . " broke the sandbox");