blob.test 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. # 2001 September 15
  2. #
  3. # The author disclaims copyright to this source code. In place of
  4. # 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. # This file implements regression tests for SQLite library.
  12. #
  13. # $Id: blob.test,v 1.8 2009/04/28 18:00:27 drh Exp $
  14. set testdir [file dirname $argv0]
  15. source $testdir/tester.tcl
  16. ifcapable {!bloblit} {
  17. finish_test
  18. return
  19. }
  20. proc bin_to_hex {blob} {
  21. set bytes {}
  22. binary scan $blob \c* bytes
  23. set bytes2 [list]
  24. foreach b $bytes {lappend bytes2 [format %02X [expr $b & 0xFF]]}
  25. join $bytes2 {}
  26. }
  27. # Simplest possible case. Specify a blob literal
  28. do_test blob-1.0 {
  29. set blob [execsql {SELECT X'01020304';}]
  30. bin_to_hex [lindex $blob 0]
  31. } {01020304}
  32. do_test blob-1.1 {
  33. set blob [execsql {SELECT x'ABCDEF';}]
  34. bin_to_hex [lindex $blob 0]
  35. } {ABCDEF}
  36. do_test blob-1.2 {
  37. set blob [execsql {SELECT x'';}]
  38. bin_to_hex [lindex $blob 0]
  39. } {}
  40. do_test blob-1.3 {
  41. set blob [execsql {SELECT x'abcdEF12';}]
  42. bin_to_hex [lindex $blob 0]
  43. } {ABCDEF12}
  44. do_test blob-1.3.2 {
  45. set blob [execsql {SELECT x'0123456789abcdefABCDEF';}]
  46. bin_to_hex [lindex $blob 0]
  47. } {0123456789ABCDEFABCDEF}
  48. # Try some syntax errors in blob literals.
  49. do_test blob-1.4 {
  50. catchsql {SELECT X'01020k304', 100}
  51. } {1 {unrecognized token: "X'01020k304'"}}
  52. do_test blob-1.5 {
  53. catchsql {SELECT X'01020, 100}
  54. } {1 {unrecognized token: "X'01020, 100"}}
  55. do_test blob-1.6 {
  56. catchsql {SELECT X'01020 100'}
  57. } {1 {unrecognized token: "X'01020 100'"}}
  58. do_test blob-1.7 {
  59. catchsql {SELECT X'01001'}
  60. } {1 {unrecognized token: "X'01001'"}}
  61. do_test blob-1.8 {
  62. catchsql {SELECT x'012/45'}
  63. } {1 {unrecognized token: "x'012/45'"}}
  64. do_test blob-1.9 {
  65. catchsql {SELECT x'012:45'}
  66. } {1 {unrecognized token: "x'012:45'"}}
  67. do_test blob-1.10 {
  68. catchsql {SELECT x'012@45'}
  69. } {1 {unrecognized token: "x'012@45'"}}
  70. do_test blob-1.11 {
  71. catchsql {SELECT x'012G45'}
  72. } {1 {unrecognized token: "x'012G45'"}}
  73. do_test blob-1.12 {
  74. catchsql {SELECT x'012`45'}
  75. } {1 {unrecognized token: "x'012`45'"}}
  76. do_test blob-1.13 {
  77. catchsql {SELECT x'012g45'}
  78. } {1 {unrecognized token: "x'012g45'"}}
  79. # Insert a blob into a table and retrieve it.
  80. do_test blob-2.0 {
  81. execsql {
  82. CREATE TABLE t1(a BLOB, b BLOB);
  83. INSERT INTO t1 VALUES(X'123456', x'7890ab');
  84. INSERT INTO t1 VALUES(X'CDEF12', x'345678');
  85. }
  86. set blobs [execsql {SELECT * FROM t1}]
  87. set blobs2 [list]
  88. foreach b $blobs {lappend blobs2 [bin_to_hex $b]}
  89. set blobs2
  90. } {123456 7890AB CDEF12 345678}
  91. # An index on a blob column
  92. do_test blob-2.1 {
  93. execsql {
  94. CREATE INDEX i1 ON t1(a);
  95. }
  96. set blobs [execsql {SELECT * FROM t1}]
  97. set blobs2 [list]
  98. foreach b $blobs {lappend blobs2 [bin_to_hex $b]}
  99. set blobs2
  100. } {123456 7890AB CDEF12 345678}
  101. do_test blob-2.2 {
  102. set blobs [execsql {SELECT * FROM t1 where a = X'123456'}]
  103. set blobs2 [list]
  104. foreach b $blobs {lappend blobs2 [bin_to_hex $b]}
  105. set blobs2
  106. } {123456 7890AB}
  107. do_test blob-2.3 {
  108. set blobs [execsql {SELECT * FROM t1 where a = X'CDEF12'}]
  109. set blobs2 [list]
  110. foreach b $blobs {lappend blobs2 [bin_to_hex $b]}
  111. set blobs2
  112. } {CDEF12 345678}
  113. do_test blob-2.4 {
  114. set blobs [execsql {SELECT * FROM t1 where a = X'CD12'}]
  115. set blobs2 [list]
  116. foreach b $blobs {lappend blobs2 [bin_to_hex $b]}
  117. set blobs2
  118. } {}
  119. # Try to bind a blob value to a prepared statement.
  120. do_test blob-3.0 {
  121. sqlite3 db2 test.db
  122. set DB [sqlite3_connection_pointer db2]
  123. set STMT [sqlite3_prepare $DB "DELETE FROM t1 WHERE a = ?" -1 DUMMY]
  124. sqlite3_bind_blob $STMT 1 "\x12\x34\x56" 3
  125. sqlite3_step $STMT
  126. } {SQLITE_DONE}
  127. do_test blob-3.1 {
  128. sqlite3_finalize $STMT
  129. db2 close
  130. } {}
  131. do_test blob-3.2 {
  132. set blobs [execsql {SELECT * FROM t1}]
  133. set blobs2 [list]
  134. foreach b $blobs {lappend blobs2 [bin_to_hex $b]}
  135. set blobs2
  136. } {CDEF12 345678}
  137. finish_test