CoolFace
Modelpublic

AryaWu/sqlite

sourceHugging Faceupdated 9mo agoView on Hugging Face
0likes
e_blobopen.test554 linesDownload Raw Back to test
1# 2014 October 302#3# The author disclaims copyright to this source code.  In place of4# a legal notice, here is a blessing:5#6#    May you do good and not evil.7#    May you find forgiveness for yourself and forgive others.8#    May you share freely, never taking more than you give.9#10#***********************************************************************11#12 13set testdir [file dirname $argv0]14source $testdir/tester.tcl15set testprefix e_blobopen16 17ifcapable !incrblob {18  finish_test19  return20}21 22forcedelete test.db223 24do_execsql_test 1.0 {25  ATTACH 'test.db2' AS aux;26 27  CREATE TABLE main.t1(a INTEGER PRIMARY KEY, b TEXT, c BLOB);28  CREATE TEMP TABLE t1(a INTEGER PRIMARY KEY, b TEXT, c BLOB);29  CREATE TABLE aux.t1(a INTEGER PRIMARY KEY, b TEXT, c BLOB);30 31  CREATE TABLE main.x1(a INTEGER PRIMARY KEY, b TEXT, c BLOB);32  CREATE TEMP TABLE x2(a INTEGER PRIMARY KEY, b TEXT, c BLOB);33  CREATE TABLE aux.x3(a INTEGER PRIMARY KEY, b TEXT, c BLOB);34 35  INSERT INTO main.t1 VALUES(1, 'main one', X'0101');36  INSERT INTO main.t1 VALUES(2, 'main two', X'0102');37  INSERT INTO main.t1 VALUES(3, 'main three', X'0103');38  INSERT INTO main.t1 VALUES(4, 'main four', X'0104');39  INSERT INTO main.t1 VALUES(5, 'main five', X'0105');40 41  INSERT INTO main.x1 VALUES(1, 'x main one', X'000101');42  INSERT INTO main.x1 VALUES(2, 'x main two', X'000102');43  INSERT INTO main.x1 VALUES(3, 'x main three', X'000103');44  INSERT INTO main.x1 VALUES(4, 'x main four', X'000104');45  INSERT INTO main.x1 VALUES(5, 'x main five', X'000105');46 47  INSERT INTO temp.t1 VALUES(1, 'temp one', X'0201');48  INSERT INTO temp.t1 VALUES(2, 'temp two', X'0202');49  INSERT INTO temp.t1 VALUES(3, 'temp three', X'0203');50  INSERT INTO temp.t1 VALUES(4, 'temp four', X'0204');51  INSERT INTO temp.t1 VALUES(5, 'temp five', X'0205');52 53  INSERT INTO temp.x2 VALUES(1, 'x temp one', X'000201');54  INSERT INTO temp.x2 VALUES(2, 'x temp two', X'000202');55  INSERT INTO temp.x2 VALUES(3, 'x temp three', X'000203');56  INSERT INTO temp.x2 VALUES(4, 'x temp four', X'000204');57  INSERT INTO temp.x2 VALUES(5, 'x temp five', X'000205');58 59  INSERT INTO aux.t1 VALUES(1, 'aux one', X'0301');60  INSERT INTO aux.t1 VALUES(2, 'aux two', X'0302');61  INSERT INTO aux.t1 VALUES(3, 'aux three', X'0303');62  INSERT INTO aux.t1 VALUES(4, 'aux four', X'0304');63  INSERT INTO aux.t1 VALUES(5, 'aux five', X'0305');64 65  INSERT INTO aux.x3 VALUES(1, 'x aux one', X'000301');66  INSERT INTO aux.x3 VALUES(2, 'x aux two', X'000302');67  INSERT INTO aux.x3 VALUES(3, 'x aux three', X'000303');68  INSERT INTO aux.x3 VALUES(4, 'x aux four', X'000304');69  INSERT INTO aux.x3 VALUES(5, 'x aux five', X'000305');70}71 72#-------------------------------------------------------------------------73# EVIDENCE-OF: R-37639-55938 This interfaces opens a handle to the BLOB74# located in row iRow, column zColumn, table zTable in database zDb; in75# other words, the same BLOB that would be selected by: SELECT zColumn76# FROM zDb.zTable WHERE rowid = iRow;77#78proc read_blob {zDb zTab zCol iRow} {79  sqlite3_blob_open db $zDb $zTab $zCol $iRow 0 B80  set nByte [sqlite3_blob_bytes $B]81  set data [sqlite3_blob_read $B 0 $nByte]82  sqlite3_blob_close $B83  return $data84}85 86do_test 1.1.1 { read_blob main t1 b 1 } "main one"87do_test 1.1.2 { read_blob main t1 c 1 } "\01\01"88do_test 1.1.3 { read_blob temp t1 b 1 } "temp one"89do_test 1.1.4 { read_blob temp t1 c 1 } "\02\01"90do_test 1.1.6 { read_blob aux  t1 b 1 } "aux one"91do_test 1.1.7 { read_blob aux  t1 c 1 } "\03\01"92 93do_test 1.2.1 { read_blob main t1 b 4 } "main four"94do_test 1.2.2 { read_blob main t1 c 4 } "\01\04"95do_test 1.2.3 { read_blob temp t1 b 4 } "temp four"96do_test 1.2.4 { read_blob temp t1 c 4 } "\02\04"97do_test 1.2.6 { read_blob aux  t1 b 4 } "aux four"98do_test 1.2.7 { read_blob aux  t1 c 4 } "\03\04"99 100do_test 1.3.1 { read_blob main x1 b 2 } "x main two"101do_test 1.3.2 { read_blob main x1 c 2 } "\00\01\02"102do_test 1.3.3 { read_blob temp x2 b 2 } "x temp two"103do_test 1.3.4 { read_blob temp x2 c 2 } "\00\02\02"104do_test 1.3.6 { read_blob aux  x3 b 2 } "x aux two"105do_test 1.3.7 { read_blob aux  x3 c 2 } "\00\03\02"106 107#-------------------------------------------------------------------------108# EVIDENCE-OF: R-27234-05761 Parameter zDb is not the filename that109# contains the database, but rather the symbolic name of the database.110# For attached databases, this is the name that appears after the AS111# keyword in the ATTACH statement. For the main database file, the112# database name is "main". For TEMP tables, the database name is "temp".113#114#   The test cases immediately above demonstrate that the database name115#   for the main db, for TEMP tables and for those in attached databases116#   is correct. The following tests check that filenames cannot be117#   used as well.118#119do_test 2.1 {120  list [catch { sqlite3_blob_open db "test.db" t1 b 1 0 B } msg] $msg121} {1 SQLITE_ERROR}122do_test 2.2 {123  list [catch { sqlite3_blob_open db "test.db2" t1 b 1 0 B } msg] $msg124} {1 SQLITE_ERROR}125 126#-------------------------------------------------------------------------127# EVIDENCE-OF: R-50854-53979 If the flags parameter is non-zero, then128# the BLOB is opened for read and write access.129#130# EVIDENCE-OF: R-03922-41160 If the flags parameter is zero, the BLOB is131# opened for read-only access.132#133foreach {tn iRow flags} {134  1 1   0135  2 2   1136  3 3  -1137  4 4   2147483647138  5 5  -2147483648139} {140  do_test 3.$tn.1 {141    sqlite3_blob_open db main x1 c $iRow $flags B142    set n [sqlite3_blob_bytes $B]143    sqlite3_blob_read $B 0 $n144  } [binary format ccc 0 1 $iRow]145 146  if {$flags==0} {147    # Blob was opened for read-only access - writing returns an error.148    do_test 3.$tn.2 {149      list [catch { sqlite3_blob_write $B 0 xxx 3 } msg] $msg150    } {1 SQLITE_READONLY}151 152    do_execsql_test 3.$tn.3 {153      SELECT c FROM x1 WHERE a=$iRow;154    } [binary format ccc 0 1 $iRow]155  } else {156    # Blob was opened for read/write access - writing succeeds157    do_test 3.$tn.4 {158      list [catch { sqlite3_blob_write $B 0 xxx 3 } msg] $msg159    } {0 {}}160 161    do_execsql_test 3.$tn.5 {162      SELECT c FROM x1 WHERE a=$iRow;163    } {xxx}164  }165 166  sqlite3_blob_close $B167}168 169#-------------------------------------------------------------------------170#171reset_db172do_execsql_test 4.0 {173  CREATE TABLE t1(x, y);174  INSERT INTO t1 VALUES('abcd', 152);175  INSERT INTO t1 VALUES(NULL, X'00010203');176  INSERT INTO t1 VALUES('', 154.2);177 178  CREATE TABLE t2(x PRIMARY KEY, y) WITHOUT ROWID;179  INSERT INTO t2 VALUES(1, 'blob');180 181  CREATE TABLE t3(a PRIMARY KEY, b, c, d, e, f, UNIQUE(e, f));182  INSERT INTO t3 VALUES('aaaa', 'bbbb', 'cccc', 'dddd', 'eeee', 'ffff');183  CREATE INDEX t3b ON t3(b);184 185  CREATE TABLE p1(x PRIMARY KEY);186  INSERT INTO p1 VALUES('abc');187 188  CREATE TABLE c1(a INTEGER PRIMARY KEY, b REFERENCES p1);189  INSERT INTO c1 VALUES(45, 'abc');190}191 192proc test_blob_open {tn zDb zTab zCol iRow flags    errcode errmsg} {193  global B194  set B "0x1234"195 196  if {$errcode=="SQLITE_OK"} {197    set expected "0 {}"198  } else {199    set expected "1 $errcode"200  }201 202  set ::res [list [203    catch { sqlite3_blob_open db $zDb $zTab $zCol $iRow $flags B } msg204  ] $msg]205  do_test 4.$tn.1 { set ::res } $expected206 207  # EVIDENCE-OF: R-08940-21305 Unless it returns SQLITE_MISUSE, this208  # function sets the database connection error code and message209  # accessible via sqlite3_errcode() and sqlite3_errmsg() and related210  # functions.211  #212  #   This proc (test_blob_open) is used below to test various error and213  #   non-error conditions. But never SQLITE_MISUSE conditions. So these214  #   test cases are considered as partly verifying the requirement above.215  #   See below for a test of the SQLITE_MISUSE case.216  #217  do_test 4.$tn.2 {218    sqlite3_errcode db219  } $errcode220  do_test 4.$tn.3 {221    sqlite3_errmsg db222  } $errmsg223 224  # EVIDENCE-OF: R-31086-35521 On success, SQLITE_OK is returned and the225  # new BLOB handle is stored in *ppBlob. Otherwise an error code is226  # returned and, unless the error code is SQLITE_MISUSE, *ppBlob is set227  # to NULL.228  #229  do_test 4.$tn.4 {230    expr {$B == "0"}231  } [expr {$errcode != "SQLITE_OK"}]232 233  # EVIDENCE-OF: R-63421-15521 This means that, provided the API is not234  # misused, it is always safe to call sqlite3_blob_close() on *ppBlob235  # after this function it returns.236  do_test 4.$tn.5 {237    sqlite3_blob_close $B238  } {}239}240 241# EVIDENCE-OF: R-31204-44780 Database zDb does not exist242test_blob_open 1 nosuchdb t1 x 1 0 SQLITE_ERROR "no such table: nosuchdb.t1"243 244# EVIDENCE-OF: R-28676-08005 Table zTable does not exist within database zDb245test_blob_open 2 main tt1 x 1 0    SQLITE_ERROR "no such table: main.tt1"246 247# EVIDENCE-OF: R-40134-30296 Table zTable is a WITHOUT ROWID table248test_blob_open 3 main t2 y 1 0     SQLITE_ERROR \249    "cannot open table without rowid: t2"250 251# EVIDENCE-OF: R-56376-21261 Column zColumn does not exist252test_blob_open 4 main t1 z 2 0     SQLITE_ERROR "no such column: \"z\""253 254# EVIDENCE-OF: R-28258-23166 Row iRow is not present in the table255test_blob_open 5 main t1 y 6 0     SQLITE_ERROR "no such rowid: 6"256 257# EVIDENCE-OF: R-11683-62380 The specified column of row iRow contains a258# value that is not a TEXT or BLOB value259test_blob_open 6 main t1 x 2 0 SQLITE_ERROR "cannot open value of type null"260test_blob_open 7 main t1 y 1 0 SQLITE_ERROR "cannot open value of type integer"261test_blob_open 8 main t1 y 3 0 SQLITE_ERROR "cannot open value of type real"262 263# EVIDENCE-OF: R-34146-30782 Column zColumn is part of an index, PRIMARY264# KEY or UNIQUE constraint and the blob is being opened for read/write265# access266#267# Test cases 8.1.* show that such columns can be opened for read-access. 268# Tests 8.2.* show that read-write access is different. Columns "c" and "c"269# are not part of an index, PK or UNIQUE constraint, so they work in both270# cases.271#272test_blob_open 8.1.1 main t3 a 1 0 SQLITE_OK "not an error"273test_blob_open 8.1.2 main t3 b 1 0 SQLITE_OK "not an error"274test_blob_open 8.1.3 main t3 c 1 0 SQLITE_OK "not an error"275test_blob_open 8.1.4 main t3 d 1 0 SQLITE_OK "not an error"276test_blob_open 8.1.5 main t3 e 1 0 SQLITE_OK "not an error"277test_blob_open 8.1.6 main t3 f 1 0 SQLITE_OK "not an error"278 279set cannot "cannot open indexed column for writing"280test_blob_open 8.2.1 main t3 a 1 8 SQLITE_ERROR $cannot281test_blob_open 8.2.2 main t3 b 1 8 SQLITE_ERROR $cannot282test_blob_open 8.2.3 main t3 c 1 8 SQLITE_OK "not an error"283test_blob_open 8.2.4 main t3 d 1 8 SQLITE_OK "not an error"284test_blob_open 8.2.5 main t3 e 1 8 SQLITE_ERROR $cannot285test_blob_open 8.2.6 main t3 f 1 8 SQLITE_ERROR $cannot286 287# EVIDENCE-OF: R-50117-55204 Foreign key constraints are enabled, column288# zColumn is part of a child key definition and the blob is being opened289# for read/write access290#291#   9.1: FK disabled, read-only access.292#   9.2: FK disabled, read-only access.293#   9.3: FK enabled, read/write access.294#   9.4: FK enabled, read/write access.295#296test_blob_open 9.1 main c1 b 45 0 SQLITE_OK "not an error"297test_blob_open 9.2 main c1 b 45 1 SQLITE_OK "not an error"298execsql { PRAGMA foreign_keys = ON }299test_blob_open 9.3 main c1 b 45 0 SQLITE_OK "not an error"300test_blob_open 9.4 main c1 b 45 1 SQLITE_ERROR \301        "cannot open foreign key column for writing"302 303#-------------------------------------------------------------------------304# EVIDENCE-OF: R-08940-21305 Unless it returns SQLITE_MISUSE, this305# function sets the database connection error code and message306# accessible via sqlite3_errcode() and sqlite3_errmsg() and related307# functions.308#309#   This requirement is partially verified by the many uses of test310#   command [test_blob_open] above. All that is left is to verify the311#   SQLITE_MISUSE case.312#313#   SQLITE_MISUSE is only returned if SQLITE_ENABLE_API_ARMOR is defined314#   during compilation.315#316ifcapable api_armor {317  sqlite3_blob_open db main t1 x 1 0 B318 319  do_test 10.1.1 {320    list [catch {sqlite3_blob_open $B main t1 x 1 0 B2} msg] $msg321  } {1 SQLITE_MISUSE}322  do_test 10.1.2 {323    list [sqlite3_errcode db] [sqlite3_errmsg db]324  } {SQLITE_OK {not an error}}325  sqlite3_blob_close $B326 327  do_test 10.2.1 {328    list [catch {sqlite3_blob_open db main {} x 1 0 B} msg] $msg329  } {1 SQLITE_MISUSE}330  do_test 10.2.2 {331    list [sqlite3_errcode db] [sqlite3_errmsg db]332  } {SQLITE_OK {not an error}}333}334 335#-------------------------------------------------------------------------336# EVIDENCE-OF: R-50542-62589 If the row that a BLOB handle points to is337# modified by an UPDATE, DELETE, or by ON CONFLICT side-effects then the338# BLOB handle is marked as "expired". This is true if any column of the339# row is changed, even a column other than the one the BLOB handle is340# open on.341#342# EVIDENCE-OF: R-48367-20048 Calls to sqlite3_blob_read() and343# sqlite3_blob_write() for an expired BLOB handle fail with a return344# code of SQLITE_ABORT.345#346#   11.2: read-only handle, DELETE.347#   11.3: read-only handle, UPDATE.348#   11.4: read-only handle, REPLACE.349#   11.5: read/write handle, DELETE.350#   11.6: read/write handle, UPDATE.351#   11.7: read/write handle, REPLACE.352#353do_execsql_test 11.1 {354  CREATE TABLE b1(a INTEGER PRIMARY KEY, b, c UNIQUE);355  INSERT INTO b1 VALUES(1, '1234567890', 1);356  INSERT INTO b1 VALUES(2, '1234567890', 2);357  INSERT INTO b1 VALUES(3, '1234567890', 3);358  INSERT INTO b1 VALUES(4, '1234567890', 4);359  INSERT INTO b1 VALUES(5, '1234567890', 5);360  INSERT INTO b1 VALUES(6, '1234567890', 6);361 362  CREATE TABLE b2(a INTEGER PRIMARY KEY, b, c UNIQUE);363  INSERT INTO b2 VALUES(1, '1234567890', 1);364  INSERT INTO b2 VALUES(2, '1234567890', 2);365  INSERT INTO b2 VALUES(3, '1234567890', 3);366  INSERT INTO b2 VALUES(4, '1234567890', 4);367  INSERT INTO b2 VALUES(5, '1234567890', 5);368  INSERT INTO b2 VALUES(6, '1234567890', 6);369}370 371do_test 11.2.1 {372  sqlite3_blob_open db main b1 b 2 0 B373  sqlite3_blob_read $B 0 10374} {1234567890}375do_test 11.2.2 {376  # Deleting a different row does not invalidate the blob handle.377  execsql { DELETE FROM b1 WHERE a = 1 }378  sqlite3_blob_read $B 0 10379} {1234567890}380do_test 11.2.3 {381  execsql { DELETE FROM b1 WHERE a = 2 }382  list [catch { sqlite3_blob_read $B 0 10 } msg] $msg383} {1 SQLITE_ABORT}384do_test 11.2.4 {385  sqlite3_blob_close $B386} {}387 388do_test 11.3.1 {389  sqlite3_blob_open db main b1 b 3 0 B390  sqlite3_blob_read $B 0 10391} {1234567890}392do_test 11.3.2 {393  # Updating a different row394  execsql { UPDATE b1 SET c = 42 WHERE a=4 }395  sqlite3_blob_read $B 0 10396} {1234567890}397do_test 11.3.3 {398  execsql { UPDATE b1 SET c = 43 WHERE a=3 }399  list [catch { sqlite3_blob_read $B 0 10 } msg] $msg400} {1 SQLITE_ABORT}401do_test 11.3.4 {402  sqlite3_blob_close $B403} {}404 405do_test 11.4.1 {406  sqlite3_blob_open db main b1 b 6 0 B407  sqlite3_blob_read $B 0 10408} {1234567890}409do_test 11.4.2 {410  # Replace a different row411  execsql { INSERT OR REPLACE INTO b1 VALUES(10, 'abcdefghij', 5) }412  sqlite3_blob_read $B 0 10413} {1234567890}414do_test 11.4.3 {415  execsql { INSERT OR REPLACE INTO b1 VALUES(11, 'abcdefghij', 6) }416  list [catch { sqlite3_blob_read $B 0 10 } msg] $msg417} {1 SQLITE_ABORT}418do_test 11.4.4 {419  sqlite3_blob_close $B420} {}421 422do_test 11.4.1 {423  sqlite3_blob_open db main b2 b 2 1 B424  sqlite3_blob_write $B 0 "abcdefghij"425} {}426do_test 11.4.2 {427  # Deleting a different row does not invalidate the blob handle.428  execsql { DELETE FROM b2 WHERE a = 1 }429  sqlite3_blob_write $B 0 "ABCDEFGHIJ"430} {}431do_test 11.4.3 {432  execsql { DELETE FROM b2 WHERE a = 2 }433  list [catch { sqlite3_blob_write $B 0 "0987654321" } msg] $msg434} {1 SQLITE_ABORT}435do_test 11.4.4 {436  sqlite3_blob_close $B437} {}438 439do_test 11.5.1 {440  sqlite3_blob_open db main b2 b 3 1 B441  sqlite3_blob_write $B 0 "abcdefghij"442} {}443do_test 11.5.2 {444  # Updating a different row445  execsql { UPDATE b2 SET c = 42 WHERE a=4 }446  sqlite3_blob_write $B 0 "ABCDEFGHIJ"447} {}448do_test 11.5.3 {449  execsql { UPDATE b2 SET c = 43 WHERE a=3 }450  list [catch { sqlite3_blob_write $B 0 "0987654321" } msg] $msg451} {1 SQLITE_ABORT}452do_test 11.5.4 {453  sqlite3_blob_close $B454} {}455 456do_test 11.6.1 {457  sqlite3_blob_open db main b2 b 6 1 B458  sqlite3_blob_write $B 0 "abcdefghij"459} {}460do_test 11.6.2 {461  # Replace a different row462  execsql { INSERT OR REPLACE INTO b2 VALUES(10, 'abcdefghij', 5) }463  sqlite3_blob_write $B 0 "ABCDEFGHIJ"464} {}465do_test 11.6.3 {466  execsql { INSERT OR REPLACE INTO b2 VALUES(11, 'abcdefghij', 6) }467  list [catch { sqlite3_blob_write $B 0 "0987654321" } msg] $msg468} {1 SQLITE_ABORT}469do_test 11.6.4 {470  sqlite3_blob_close $B471} {}472 473#-------------------------------------------------------------------------474# EVIDENCE-OF: R-45408-40694 Changes written into a BLOB prior to the475# BLOB expiring are not rolled back by the expiration of the BLOB. Such476# changes will eventually commit if the transaction continues to477# completion.478#479do_execsql_test 12.1 {480  CREATE TABLE b3(x INTEGER PRIMARY KEY, y TEXT, z INTEGER);481  INSERT INTO b3 VALUES(22, '..........', NULL);482}483do_test 12.2 {484  sqlite3_blob_open db main b3 y 22 1 B485  sqlite3_blob_write $B 0 "xxxxx" 5486} {}487do_execsql_test 12.3 {488  UPDATE b3 SET z = 'not null';489}490do_test 12.4 {491  list [catch {sqlite3_blob_write $B 5 "xxxxx" 5} msg] $msg492} {1 SQLITE_ABORT}493do_execsql_test 12.5 {494  SELECT * FROM b3;495} {22 xxxxx..... {not null}}496do_test 12.5 {497  sqlite3_blob_close $B498} {}499do_execsql_test 12.6 {500  SELECT * FROM b3;501} {22 xxxxx..... {not null}}502 503#-------------------------------------------------------------------------504# EVIDENCE-OF: R-58813-55036 The sqlite3_bind_zeroblob() and505# sqlite3_result_zeroblob() interfaces and the built-in zeroblob SQL506# function may be used to create a zero-filled blob to read or write507# using the incremental-blob interface.508#509do_execsql_test 13.1 {510  CREATE TABLE c2(i INTEGER PRIMARY KEY, j);511  INSERT INTO c2 VALUES(10, zeroblob(24));512}513 514do_test 13.2 {515  set stmt [sqlite3_prepare_v2 db "INSERT INTO c2 VALUES(11, ?)" -1]516  sqlite3_bind_zeroblob $stmt 1 45517  sqlite3_step $stmt518  sqlite3_finalize $stmt519} {SQLITE_OK}520 521# The blobs can be read:522#523do_test 13.3.1 {524  sqlite3_blob_open db main c2 j 10 1 B525  sqlite3_blob_open db main c2 j 11 1 B2526  list [sqlite3_blob_bytes $B] [sqlite3_blob_bytes $B2]527} {24 45}528do_test 13.3.2 {529  sqlite3_blob_read $B 0 24530} [string repeat [binary format c 0] 24]531do_test 13.3.3 {532  sqlite3_blob_read $B2 0 45533} [string repeat [binary format c 0] 45]534 535# And also written:536#537do_test 13.4.1 {538  sqlite3_blob_write $B 0 [string repeat [binary format c 1] 24]539} {}540do_test 13.4.2 {541  sqlite3_blob_write $B2 0 [string repeat [binary format c 1] 45]542} {}543do_test 13.5 {544  sqlite3_blob_close $B545  sqlite3_blob_close $B2546  execsql { SELECT j FROM c2 }547} [list \548    [string repeat [binary format c 1] 24] \549    [string repeat [binary format c 1] 45] \550]551 552 553finish_test554