| 1 /* |
|
| 2 * Purple - Internet Messaging Library |
|
| 3 * Copyright (C) Pidgin Developers <devel@pidgin.im> |
|
| 4 * |
|
| 5 * This library is free software; you can redistribute it and/or |
|
| 6 * modify it under the terms of the GNU Lesser General Public |
|
| 7 * License as published by the Free Software Foundation; either |
|
| 8 * version 2 of the License, or (at your option) any later version. |
|
| 9 * |
|
| 10 * This library is distributed in the hope that it will be useful, |
|
| 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
| 13 * Lesser General Public License for more details. |
|
| 14 * |
|
| 15 * You should have received a copy of the GNU Lesser General Public |
|
| 16 * License along with this library; if not, see <https://www.gnu.org/licenses/>. |
|
| 17 */ |
|
| 18 |
|
| 19 #include <glib.h> |
|
| 20 |
|
| 21 #include <sqlite3.h> |
|
| 22 |
|
| 23 #include <purple.h> |
|
| 24 |
|
| 25 /****************************************************************************** |
|
| 26 * get schema version tests |
|
| 27 *****************************************************************************/ |
|
| 28 static void |
|
| 29 test_sqlite3_get_schema_version_null(void) { |
|
| 30 if(g_test_subprocess()) { |
|
| 31 GError *error = NULL; |
|
| 32 int version = 0; |
|
| 33 |
|
| 34 version = purple_sqlite3_get_schema_version(NULL, &error); |
|
| 35 g_assert_error(error, PURPLE_SQLITE3_DOMAIN, 0); |
|
| 36 g_clear_error(&error); |
|
| 37 g_assert_cmpint(version, ==, -1); |
|
| 38 } |
|
| 39 |
|
| 40 g_test_trap_subprocess(NULL, 0, 0); |
|
| 41 g_test_trap_assert_failed(); |
|
| 42 g_test_trap_assert_stderr("*assertion*!= NULL*"); |
|
| 43 } |
|
| 44 |
|
| 45 static void |
|
| 46 test_sqlite3_get_schema_version_new(void) { |
|
| 47 GError *error = NULL; |
|
| 48 sqlite3 *db = NULL; |
|
| 49 int rc = 0; |
|
| 50 int version = 0; |
|
| 51 |
|
| 52 rc = sqlite3_open(":memory:", &db); |
|
| 53 g_assert_nonnull(db); |
|
| 54 g_assert_cmpint(rc, ==, SQLITE_OK); |
|
| 55 |
|
| 56 version = purple_sqlite3_get_schema_version(db, &error); |
|
| 57 g_assert_no_error(error); |
|
| 58 g_assert_cmpint(version, ==, 0); |
|
| 59 |
|
| 60 rc = sqlite3_close(db); |
|
| 61 g_assert_cmpint(rc, ==, SQLITE_OK); |
|
| 62 } |
|
| 63 |
|
| 64 /****************************************************************************** |
|
| 65 * string migration tests |
|
| 66 *****************************************************************************/ |
|
| 67 static void |
|
| 68 test_sqlite3_string_migrations_null(void) { |
|
| 69 if(g_test_subprocess()) { |
|
| 70 GError *error = NULL; |
|
| 71 sqlite3 *db = NULL; |
|
| 72 gboolean res = FALSE; |
|
| 73 int rc = 0; |
|
| 74 |
|
| 75 rc = sqlite3_open(":memory:", &db); |
|
| 76 g_assert_nonnull(db); |
|
| 77 g_assert_cmpint(rc, ==, SQLITE_OK); |
|
| 78 |
|
| 79 res = purple_sqlite3_run_migrations_from_strings(db, NULL, &error); |
|
| 80 g_assert_no_error(error); |
|
| 81 g_assert_false(res); |
|
| 82 } |
|
| 83 |
|
| 84 g_test_trap_subprocess(NULL, 0, 0); |
|
| 85 g_test_trap_assert_failed(); |
|
| 86 g_test_trap_assert_stderr("*migrations != NULL*"); |
|
| 87 } |
|
| 88 |
|
| 89 static void |
|
| 90 test_sqlite3_string_migrations_null_terminator(void) { |
|
| 91 GError *error = NULL; |
|
| 92 sqlite3 *db = NULL; |
|
| 93 gboolean res = FALSE; |
|
| 94 int rc = 0; |
|
| 95 int version = -1; |
|
| 96 const char *migrations[] = {NULL}; |
|
| 97 |
|
| 98 rc = sqlite3_open(":memory:", &db); |
|
| 99 g_assert_nonnull(db); |
|
| 100 g_assert_cmpint(rc, ==, SQLITE_OK); |
|
| 101 |
|
| 102 res = purple_sqlite3_run_migrations_from_strings(db, migrations, &error); |
|
| 103 g_assert_no_error(error); |
|
| 104 g_assert_true(res); |
|
| 105 |
|
| 106 version = purple_sqlite3_get_schema_version(db, &error); |
|
| 107 g_assert_no_error(error); |
|
| 108 g_assert_cmpint(version, ==, 0); |
|
| 109 |
|
| 110 rc = sqlite3_close(db); |
|
| 111 g_assert_cmpint(rc, ==, SQLITE_OK); |
|
| 112 } |
|
| 113 |
|
| 114 static void |
|
| 115 test_sqlite3_string_migrations_multiple(void) { |
|
| 116 GError *error = NULL; |
|
| 117 sqlite3 *db = NULL; |
|
| 118 gboolean res = FALSE; |
|
| 119 int rc = 0; |
|
| 120 int version = -1; |
|
| 121 const char *migrations[] = { |
|
| 122 "CREATE TABLE foo(a TEXT); CREATE TABLE bar(b TEXT);", |
|
| 123 "CREATE TABLE baz(c TEXT);", |
|
| 124 NULL |
|
| 125 }; |
|
| 126 |
|
| 127 rc = sqlite3_open(":memory:", &db); |
|
| 128 g_assert_nonnull(db); |
|
| 129 g_assert_cmpint(rc, ==, SQLITE_OK); |
|
| 130 |
|
| 131 res = purple_sqlite3_run_migrations_from_strings(db, migrations, &error); |
|
| 132 g_assert_no_error(error); |
|
| 133 g_assert_true(res); |
|
| 134 |
|
| 135 version = purple_sqlite3_get_schema_version(db, &error); |
|
| 136 g_assert_no_error(error); |
|
| 137 g_assert_cmpint(version, ==, 2); |
|
| 138 |
|
| 139 /* Run the migrations again and make sure we remain at schema version 2. */ |
|
| 140 res = purple_sqlite3_run_migrations_from_strings(db, migrations, &error); |
|
| 141 g_assert_no_error(error); |
|
| 142 g_assert_true(res); |
|
| 143 |
|
| 144 version = purple_sqlite3_get_schema_version(db, &error); |
|
| 145 g_assert_no_error(error); |
|
| 146 g_assert_cmpint(version, ==, 2); |
|
| 147 |
|
| 148 rc = sqlite3_close(db); |
|
| 149 g_assert_cmpint(rc, ==, SQLITE_OK); |
|
| 150 } |
|
| 151 |
|
| 152 static void |
|
| 153 test_sqlite3_string_migrations_syntax_error(void) { |
|
| 154 GError *error = NULL; |
|
| 155 sqlite3 *db = NULL; |
|
| 156 gboolean res = FALSE; |
|
| 157 int rc = 0; |
|
| 158 int version = -1; |
|
| 159 const char *migrations[] = { |
|
| 160 "CREATE TABLE broke(a TEXT", |
|
| 161 NULL |
|
| 162 }; |
|
| 163 |
|
| 164 rc = sqlite3_open(":memory:", &db); |
|
| 165 g_assert_nonnull(db); |
|
| 166 g_assert_cmpint(rc, ==, SQLITE_OK); |
|
| 167 |
|
| 168 res = purple_sqlite3_run_migrations_from_strings(db, migrations, &error); |
|
| 169 g_assert_error(error, PURPLE_SQLITE3_DOMAIN, 0); |
|
| 170 g_clear_error(&error); |
|
| 171 g_assert_false(res); |
|
| 172 |
|
| 173 version = purple_sqlite3_get_schema_version(db, &error); |
|
| 174 g_assert_no_error(error); |
|
| 175 g_assert_cmpint(version, ==, 0); |
|
| 176 |
|
| 177 rc = sqlite3_close(db); |
|
| 178 g_assert_cmpint(rc, ==, SQLITE_OK); |
|
| 179 } |
|
| 180 |
|
| 181 static void |
|
| 182 test_sqlite3_string_migrations_older(void) { |
|
| 183 GError *error = NULL; |
|
| 184 sqlite3 *db = NULL; |
|
| 185 gboolean res = FALSE; |
|
| 186 int rc = 0; |
|
| 187 int version = -1; |
|
| 188 const char *migrations1[] = { |
|
| 189 "CREATE TABLE foo(a TEXT); CREATE TABLE bar(b TEXT);", |
|
| 190 "CREATE TABLE baz(c TEXT);", |
|
| 191 NULL |
|
| 192 }; |
|
| 193 const char *migrations2[] = { |
|
| 194 "CREATE TABLE foo(a TEXT); CREATE TABLE bar(b TEXT);", |
|
| 195 NULL |
|
| 196 }; |
|
| 197 |
|
| 198 rc = sqlite3_open(":memory:", &db); |
|
| 199 g_assert_nonnull(db); |
|
| 200 g_assert_cmpint(rc, ==, SQLITE_OK); |
|
| 201 |
|
| 202 res = purple_sqlite3_run_migrations_from_strings(db, migrations1, &error); |
|
| 203 g_assert_no_error(error); |
|
| 204 g_assert_true(res); |
|
| 205 |
|
| 206 version = purple_sqlite3_get_schema_version(db, &error); |
|
| 207 g_assert_no_error(error); |
|
| 208 g_assert_cmpint(version, ==, 2); |
|
| 209 |
|
| 210 /* Run the older migrations now and verify we get a failure. */ |
|
| 211 res = purple_sqlite3_run_migrations_from_strings(db, migrations2, &error); |
|
| 212 g_assert_error(error, PURPLE_SQLITE3_DOMAIN, 0); |
|
| 213 g_clear_error(&error); |
|
| 214 g_assert_false(res); |
|
| 215 |
|
| 216 version = purple_sqlite3_get_schema_version(db, &error); |
|
| 217 g_assert_no_error(error); |
|
| 218 g_assert_cmpint(version, ==, 2); |
|
| 219 |
|
| 220 rc = sqlite3_close(db); |
|
| 221 g_assert_cmpint(rc, ==, SQLITE_OK); |
|
| 222 } |
|
| 223 |
|
| 224 /****************************************************************************** |
|
| 225 * resource migration tests |
|
| 226 *****************************************************************************/ |
|
| 227 static void |
|
| 228 test_sqlite3_resource_migrations_null_path(void) { |
|
| 229 if(g_test_subprocess()) { |
|
| 230 GError *error = NULL; |
|
| 231 sqlite3 *db = NULL; |
|
| 232 gboolean res = FALSE; |
|
| 233 int rc = 0; |
|
| 234 |
|
| 235 rc = sqlite3_open(":memory:", &db); |
|
| 236 g_assert_nonnull(db); |
|
| 237 g_assert_cmpint(rc, ==, SQLITE_OK); |
|
| 238 |
|
| 239 res = purple_sqlite3_run_migrations_from_resources(db, NULL, NULL, |
|
| 240 &error); |
|
| 241 g_assert_no_error(error); |
|
| 242 g_assert_false(res); |
|
| 243 |
|
| 244 rc = sqlite3_close(db); |
|
| 245 g_assert_cmpint(rc, ==, SQLITE_OK); |
|
| 246 } |
|
| 247 |
|
| 248 g_test_trap_subprocess(NULL, 0, 0); |
|
| 249 g_test_trap_assert_failed(); |
|
| 250 g_test_trap_assert_stderr("*path != NULL*"); |
|
| 251 } |
|
| 252 |
|
| 253 static void |
|
| 254 test_sqlite3_resource_migrations_null_migrations(void) { |
|
| 255 if(g_test_subprocess()) { |
|
| 256 GError *error = NULL; |
|
| 257 sqlite3 *db = NULL; |
|
| 258 gboolean res = FALSE; |
|
| 259 const char *path = "/im/libpidgin/purple/tests/sqlite3/"; |
|
| 260 int rc = 0; |
|
| 261 |
|
| 262 rc = sqlite3_open(":memory:", &db); |
|
| 263 g_assert_nonnull(db); |
|
| 264 g_assert_cmpint(rc, ==, SQLITE_OK); |
|
| 265 |
|
| 266 res = purple_sqlite3_run_migrations_from_resources(db, path, NULL, |
|
| 267 &error); |
|
| 268 g_assert_no_error(error); |
|
| 269 g_assert_false(res); |
|
| 270 |
|
| 271 rc = sqlite3_close(db); |
|
| 272 g_assert_cmpint(rc, ==, SQLITE_OK); |
|
| 273 } |
|
| 274 |
|
| 275 g_test_trap_subprocess(NULL, 0, 0); |
|
| 276 g_test_trap_assert_failed(); |
|
| 277 g_test_trap_assert_stderr("*migrations != NULL*"); |
|
| 278 } |
|
| 279 |
|
| 280 static void |
|
| 281 test_sqlite3_resource_migrations_null_terminator(void) { |
|
| 282 GError *error = NULL; |
|
| 283 sqlite3 *db = NULL; |
|
| 284 gboolean res = FALSE; |
|
| 285 int rc = 0; |
|
| 286 int version = -1; |
|
| 287 const char *migrations[] = {NULL}; |
|
| 288 const char *path = "/im/pidgin/libpurple/tests/sqlite3/"; |
|
| 289 |
|
| 290 rc = sqlite3_open(":memory:", &db); |
|
| 291 g_assert_nonnull(db); |
|
| 292 g_assert_cmpint(rc, ==, SQLITE_OK); |
|
| 293 |
|
| 294 res = purple_sqlite3_run_migrations_from_resources(db, path, migrations, |
|
| 295 &error); |
|
| 296 g_assert_no_error(error); |
|
| 297 g_assert_true(res); |
|
| 298 |
|
| 299 version = purple_sqlite3_get_schema_version(db, &error); |
|
| 300 g_assert_no_error(error); |
|
| 301 g_assert_cmpint(version, ==, 0); |
|
| 302 |
|
| 303 rc = sqlite3_close(db); |
|
| 304 g_assert_cmpint(rc, ==, SQLITE_OK); |
|
| 305 } |
|
| 306 |
|
| 307 static void |
|
| 308 test_sqlite3_resource_migrations_multiple(void) { |
|
| 309 GError *error = NULL; |
|
| 310 sqlite3 *db = NULL; |
|
| 311 gboolean res = FALSE; |
|
| 312 int rc = 0; |
|
| 313 int version = -1; |
|
| 314 const char *migrations[] = {"initial.sql", "secondary.sql", NULL}; |
|
| 315 const char *path = "/im/pidgin/libpurple/tests/sqlite3/"; |
|
| 316 |
|
| 317 rc = sqlite3_open(":memory:", &db); |
|
| 318 g_assert_nonnull(db); |
|
| 319 g_assert_cmpint(rc, ==, SQLITE_OK); |
|
| 320 |
|
| 321 res = purple_sqlite3_run_migrations_from_resources(db, path, migrations, |
|
| 322 &error); |
|
| 323 g_assert_no_error(error); |
|
| 324 g_assert_true(res); |
|
| 325 |
|
| 326 version = purple_sqlite3_get_schema_version(db, &error); |
|
| 327 g_assert_no_error(error); |
|
| 328 g_assert_cmpint(version, ==, 2); |
|
| 329 |
|
| 330 /* Run the migrations again and make sure we remain at schema version 2. */ |
|
| 331 res = purple_sqlite3_run_migrations_from_strings(db, migrations, &error); |
|
| 332 g_assert_no_error(error); |
|
| 333 g_assert_true(res); |
|
| 334 |
|
| 335 version = purple_sqlite3_get_schema_version(db, &error); |
|
| 336 g_assert_no_error(error); |
|
| 337 g_assert_cmpint(version, ==, 2); |
|
| 338 |
|
| 339 rc = sqlite3_close(db); |
|
| 340 g_assert_cmpint(rc, ==, SQLITE_OK); |
|
| 341 } |
|
| 342 |
|
| 343 static void |
|
| 344 test_sqlite3_resource_migrations_missing(void) { |
|
| 345 GError *error = NULL; |
|
| 346 sqlite3 *db = NULL; |
|
| 347 gboolean res = FALSE; |
|
| 348 int rc = 0; |
|
| 349 int version = -1; |
|
| 350 const char *migrations[] = {"initial.sql", "imaginary.sql", NULL}; |
|
| 351 const char *path = "/im/pidgin/libpurple/tests/sqlite3/"; |
|
| 352 |
|
| 353 rc = sqlite3_open(":memory:", &db); |
|
| 354 g_assert_nonnull(db); |
|
| 355 g_assert_cmpint(rc, ==, SQLITE_OK); |
|
| 356 |
|
| 357 res = purple_sqlite3_run_migrations_from_resources(db, path, migrations, |
|
| 358 &error); |
|
| 359 g_assert_error(error, G_RESOURCE_ERROR, 0); |
|
| 360 g_clear_error(&error); |
|
| 361 g_assert_false(res); |
|
| 362 |
|
| 363 version = purple_sqlite3_get_schema_version(db, &error); |
|
| 364 g_assert_no_error(error); |
|
| 365 g_assert_cmpint(version, ==, 1); |
|
| 366 |
|
| 367 rc = sqlite3_close(db); |
|
| 368 g_assert_cmpint(rc, ==, SQLITE_OK); |
|
| 369 } |
|
| 370 |
|
| 371 static void |
|
| 372 test_sqlite3_resource_migrations_syntax_error(void) { |
|
| 373 GError *error = NULL; |
|
| 374 sqlite3 *db = NULL; |
|
| 375 gboolean res = FALSE; |
|
| 376 int rc = 0; |
|
| 377 int version = -1; |
|
| 378 const char *migrations[] = {"malformed.sql", NULL}; |
|
| 379 const char *path = "/im/pidgin/libpurple/tests/sqlite3/"; |
|
| 380 |
|
| 381 rc = sqlite3_open(":memory:", &db); |
|
| 382 g_assert_nonnull(db); |
|
| 383 g_assert_cmpint(rc, ==, SQLITE_OK); |
|
| 384 |
|
| 385 res = purple_sqlite3_run_migrations_from_resources(db, path, migrations, |
|
| 386 &error); |
|
| 387 g_assert_error(error, PURPLE_SQLITE3_DOMAIN, 0); |
|
| 388 g_clear_error(&error); |
|
| 389 g_assert_false(res); |
|
| 390 |
|
| 391 version = purple_sqlite3_get_schema_version(db, &error); |
|
| 392 g_assert_no_error(error); |
|
| 393 g_assert_cmpint(version, ==, 0); |
|
| 394 |
|
| 395 rc = sqlite3_close(db); |
|
| 396 g_assert_cmpint(rc, ==, SQLITE_OK); |
|
| 397 } |
|
| 398 |
|
| 399 static void |
|
| 400 test_sqlite3_resource_migrations_older(void) { |
|
| 401 GError *error = NULL; |
|
| 402 sqlite3 *db = NULL; |
|
| 403 gboolean res = FALSE; |
|
| 404 int rc = 0; |
|
| 405 int version = -1; |
|
| 406 const char *migrations1[] = {"initial.sql", "secondary.sql", NULL}; |
|
| 407 const char *migrations2[] = {"initial.sql", NULL}; |
|
| 408 const char *path = "/im/pidgin/libpurple/tests/sqlite3/"; |
|
| 409 |
|
| 410 rc = sqlite3_open(":memory:", &db); |
|
| 411 g_assert_nonnull(db); |
|
| 412 g_assert_cmpint(rc, ==, SQLITE_OK); |
|
| 413 |
|
| 414 res = purple_sqlite3_run_migrations_from_resources(db, path, migrations1, |
|
| 415 &error); |
|
| 416 g_assert_no_error(error); |
|
| 417 g_assert_true(res); |
|
| 418 |
|
| 419 version = purple_sqlite3_get_schema_version(db, &error); |
|
| 420 g_assert_no_error(error); |
|
| 421 g_assert_cmpint(version, ==, 2); |
|
| 422 |
|
| 423 /* Run the older migrations now and verify we get a failure. */ |
|
| 424 res = purple_sqlite3_run_migrations_from_resources(db, path, migrations2, |
|
| 425 &error); |
|
| 426 g_assert_error(error, PURPLE_SQLITE3_DOMAIN, 0); |
|
| 427 g_clear_error(&error); |
|
| 428 g_assert_false(res); |
|
| 429 |
|
| 430 version = purple_sqlite3_get_schema_version(db, &error); |
|
| 431 g_assert_no_error(error); |
|
| 432 g_assert_cmpint(version, ==, 2); |
|
| 433 |
|
| 434 rc = sqlite3_close(db); |
|
| 435 g_assert_cmpint(rc, ==, SQLITE_OK); |
|
| 436 } |
|
| 437 |
|
| 438 /****************************************************************************** |
|
| 439 * Main |
|
| 440 *****************************************************************************/ |
|
| 441 int |
|
| 442 main(int argc, char *argv[]) { |
|
| 443 g_test_init(&argc, &argv, G_TEST_OPTION_ISOLATE_DIRS, NULL); |
|
| 444 |
|
| 445 g_test_add_func("/sqlite3/schema_version/null", |
|
| 446 test_sqlite3_get_schema_version_null); |
|
| 447 g_test_add_func("/sqlite3/schema_version/new", |
|
| 448 test_sqlite3_get_schema_version_new); |
|
| 449 |
|
| 450 g_test_add_func("/sqlite3/string_migrations/null", |
|
| 451 test_sqlite3_string_migrations_null); |
|
| 452 g_test_add_func("/sqlite3/string_migrations/null-terminator", |
|
| 453 test_sqlite3_string_migrations_null_terminator); |
|
| 454 g_test_add_func("/sqlite3/string_migrations/multiple", |
|
| 455 test_sqlite3_string_migrations_multiple); |
|
| 456 g_test_add_func("/sqlite3/string_migrations/syntax-error", |
|
| 457 test_sqlite3_string_migrations_syntax_error); |
|
| 458 g_test_add_func("/sqlite3/string_migrations/older", |
|
| 459 test_sqlite3_string_migrations_older); |
|
| 460 |
|
| 461 g_test_add_func("/sqlite3/resource_migrations/null-path", |
|
| 462 test_sqlite3_resource_migrations_null_path); |
|
| 463 g_test_add_func("/sqlite3/resource_migrations/null-migrations", |
|
| 464 test_sqlite3_resource_migrations_null_migrations); |
|
| 465 g_test_add_func("/sqlite3/resource_migrations/null-terminator", |
|
| 466 test_sqlite3_resource_migrations_null_terminator); |
|
| 467 g_test_add_func("/sqlite3/resource_migrations/multiple", |
|
| 468 test_sqlite3_resource_migrations_multiple); |
|
| 469 g_test_add_func("/sqlite3/resource_migrations/missing", |
|
| 470 test_sqlite3_resource_migrations_missing); |
|
| 471 g_test_add_func("/sqlite3/resource_migrations/syntax-error", |
|
| 472 test_sqlite3_resource_migrations_syntax_error); |
|
| 473 g_test_add_func("/sqlite3/resource_migrations/older", |
|
| 474 test_sqlite3_resource_migrations_older); |
|
| 475 |
|
| 476 return g_test_run(); |
|
| 477 } |
|