OLD | NEW |
1 # 2003 January 29 | 1 # 2003 January 29 |
2 # | 2 # |
3 # The author disclaims copyright to this source code. In place of | 3 # The author disclaims copyright to this source code. In place of |
4 # a legal notice, here is a blessing: | 4 # a legal notice, here is a blessing: |
5 # | 5 # |
6 # May you do good and not evil. | 6 # May you do good and not evil. |
7 # May you find forgiveness for yourself and forgive others. | 7 # May you find forgiveness for yourself and forgive others. |
8 # May you share freely, never taking more than you give. | 8 # May you share freely, never taking more than you give. |
9 # | 9 # |
10 #*********************************************************************** | 10 #*********************************************************************** |
11 # This file implements regression tests for SQLite library. The | 11 # This file implements regression tests for SQLite library. The |
12 # focus of this script testing the callback-free C/C++ API. | 12 # focus of this script testing the callback-free C/C++ API. |
13 # | 13 # |
14 # $Id: capi3.test,v 1.70 2009/01/09 02:49:32 drh Exp $ | 14 # $Id: capi3.test,v 1.70 2009/01/09 02:49:32 drh Exp $ |
15 # | 15 # |
16 | 16 |
17 set testdir [file dirname $argv0] | 17 set testdir [file dirname $argv0] |
18 source $testdir/tester.tcl | 18 source $testdir/tester.tcl |
| 19 set ::testprefix capi3 |
19 | 20 |
20 # Do not use a codec for tests in this file, as the database file is | 21 # Do not use a codec for tests in this file, as the database file is |
21 # manipulated directly using tcl scripts (using the [hexio_write] command). | 22 # manipulated directly using tcl scripts (using the [hexio_write] command). |
22 # | 23 # |
23 do_not_use_codec | 24 do_not_use_codec |
24 | 25 |
25 # Return the UTF-16 representation of the supplied UTF-8 string $str. | 26 # Return the UTF-16 representation of the supplied UTF-8 string $str. |
26 # If $nt is true, append two 0x00 bytes as a nul terminator. | 27 # If $nt is true, append two 0x00 bytes as a nul terminator. |
27 proc utf16 {str {nt 1}} { | 28 proc utf16 {str {nt 1}} { |
28 set r [encoding convertto unicode $str] | 29 set r [encoding convertto unicode $str] |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
172 set db2 [sqlite3_open /bogus/path/test.db {}] | 173 set db2 [sqlite3_open /bogus/path/test.db {}] |
173 } | 174 } |
174 sqlite3_extended_errcode $db2 | 175 sqlite3_extended_errcode $db2 |
175 } {SQLITE_CANTOPEN} | 176 } {SQLITE_CANTOPEN} |
176 do_test capi3-3.4 { | 177 do_test capi3-3.4 { |
177 sqlite3_errmsg $db2 | 178 sqlite3_errmsg $db2 |
178 } {unable to open database file} | 179 } {unable to open database file} |
179 do_test capi3-3.5 { | 180 do_test capi3-3.5 { |
180 sqlite3_close $db2 | 181 sqlite3_close $db2 |
181 } {SQLITE_OK} | 182 } {SQLITE_OK} |
182 do_test capi3-3.6.1-misuse { | 183 if {[clang_sanitize_address]==0} { |
183 sqlite3_close $db2 | 184 do_test capi3-3.6.1-misuse { |
184 } {SQLITE_MISUSE} | 185 sqlite3_close $db2 |
185 do_test capi3-3.6.2-misuse { | 186 } {SQLITE_MISUSE} |
186 sqlite3_errmsg $db2 | 187 do_test capi3-3.6.2-misuse { |
187 } {library routine called out of sequence} | 188 sqlite3_errmsg $db2 |
188 ifcapable {utf16} { | |
189 do_test capi3-3.6.3-misuse { | |
190 utf8 [sqlite3_errmsg16 $db2] | |
191 } {library routine called out of sequence} | 189 } {library routine called out of sequence} |
| 190 ifcapable {utf16} { |
| 191 do_test capi3-3.6.3-misuse { |
| 192 utf8 [sqlite3_errmsg16 $db2] |
| 193 } {library routine called out of sequence} |
| 194 } |
192 } | 195 } |
193 | 196 |
194 do_test capi3-3.7 { | 197 do_test capi3-3.7 { |
195 set db2 [sqlite3_open] | 198 set db2 [sqlite3_open] |
196 sqlite3_errcode $db2 | 199 sqlite3_errcode $db2 |
197 } {SQLITE_OK} | 200 } {SQLITE_OK} |
198 do_test capi3-3.8 { | 201 do_test capi3-3.8 { |
199 sqlite3_close $db2 | 202 sqlite3_close $db2 |
200 } {SQLITE_OK} | 203 } {SQLITE_OK} |
201 | 204 |
(...skipping 440 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
642 set DB [sqlite3_connection_pointer db] | 645 set DB [sqlite3_connection_pointer db] |
643 if {[sqlite3 -has-codec]==0} { sqlite3_key $DB xyzzy } | 646 if {[sqlite3 -has-codec]==0} { sqlite3_key $DB xyzzy } |
644 set sql {SELECT a FROM t1 order by rowid} | 647 set sql {SELECT a FROM t1 order by rowid} |
645 set STMT [sqlite3_prepare $DB $sql -1 TAIL] | 648 set STMT [sqlite3_prepare $DB $sql -1 TAIL] |
646 expr 0 | 649 expr 0 |
647 } {0} | 650 } {0} |
648 do_test capi3-6.1 { | 651 do_test capi3-6.1 { |
649 db cache flush | 652 db cache flush |
650 sqlite3_close $DB | 653 sqlite3_close $DB |
651 } {SQLITE_BUSY} | 654 } {SQLITE_BUSY} |
| 655 |
| 656 # 6.2 and 6.3 used to return SQLITE_ERROR and SQLITE_SCHEMA, respectively. |
| 657 # But since attempting to close a connection no longer resets the internal |
| 658 # schema and expires all statements, this is no longer the case. |
652 do_test capi3-6.2 { | 659 do_test capi3-6.2 { |
653 sqlite3_step $STMT | 660 sqlite3_step $STMT |
654 } {SQLITE_ERROR} | 661 } {SQLITE_ROW} |
655 #check_data $STMT capi3-6.3 {INTEGER} {1} {1.0} {1} | 662 #check_data $STMT capi3-6.3 {INTEGER} {1} {1.0} {1} |
656 do_test capi3-6.3 { | 663 do_test capi3-6.3 { |
657 sqlite3_finalize $STMT | 664 sqlite3_finalize $STMT |
658 } {SQLITE_SCHEMA} | |
659 do_test capi3-6.4-misuse { | |
660 db cache flush | |
661 sqlite3_close $DB | |
662 } {SQLITE_OK} | 665 } {SQLITE_OK} |
| 666 |
| 667 if {[clang_sanitize_address]==0} { |
| 668 do_test capi3-6.4-misuse { |
| 669 db cache flush |
| 670 sqlite3_close $DB |
| 671 } {SQLITE_OK} |
| 672 } |
663 db close | 673 db close |
664 | 674 |
665 # This procedure sets the value of the file-format in file 'test.db' | 675 # This procedure sets the value of the file-format in file 'test.db' |
666 # to $newval. Also, the schema cookie is incremented. | 676 # to $newval. Also, the schema cookie is incremented. |
667 # | 677 # |
668 proc set_file_format {newval} { | 678 proc set_file_format {newval} { |
669 hexio_write test.db 44 [hexio_render_int32 $newval] | 679 hexio_write test.db 44 [hexio_render_int32 $newval] |
670 set schemacookie [hexio_get_int [hexio_read test.db 40 4]] | 680 set schemacookie [hexio_get_int [hexio_read test.db 40 4]] |
671 incr schemacookie | 681 incr schemacookie |
672 hexio_write test.db 40 [hexio_render_int32 $schemacookie] | 682 hexio_write test.db 40 [hexio_render_int32 $schemacookie] |
(...skipping 17 matching lines...) Expand all Loading... |
690 SELECT * FROM sqlite_master; | 700 SELECT * FROM sqlite_master; |
691 } | 701 } |
692 } {1 {unsupported file format}} | 702 } {1 {unsupported file format}} |
693 db close | 703 db close |
694 } | 704 } |
695 | 705 |
696 if {![sqlite3 -has-codec]} { | 706 if {![sqlite3 -has-codec]} { |
697 # Now test that the library correctly handles bogus entries in the | 707 # Now test that the library correctly handles bogus entries in the |
698 # sqlite_master table (schema corruption). | 708 # sqlite_master table (schema corruption). |
699 do_test capi3-8.1 { | 709 do_test capi3-8.1 { |
700 file delete -force test.db test.db-journal | 710 forcedelete test.db test.db-journal |
701 sqlite3 db test.db | 711 sqlite3 db test.db |
702 execsql { | 712 execsql { |
703 CREATE TABLE t1(a); | 713 CREATE TABLE t1(a); |
704 } | 714 } |
705 db close | 715 db close |
706 } {} | 716 } {} |
707 do_test capi3-8.2 { | 717 do_test capi3-8.2 { |
708 sqlite3 db test.db | 718 sqlite3 db test.db |
709 execsql { | 719 execsql { |
710 PRAGMA writable_schema=ON; | 720 PRAGMA writable_schema=ON; |
711 INSERT INTO sqlite_master VALUES(NULL,NULL,NULL,NULL,NULL); | 721 INSERT INTO sqlite_master VALUES(NULL,NULL,NULL,NULL,NULL); |
712 } | 722 } |
713 db close | 723 db close |
714 } {} | 724 } {} |
715 do_test capi3-8.3 { | 725 do_test capi3-8.3 { |
716 catch { sqlite3 db test.db } | 726 catch { sqlite3 db test.db } |
717 catchsql { | 727 catchsql { |
718 SELECT * FROM sqlite_master; | 728 SELECT * FROM sqlite_master; |
719 } | 729 } |
720 } {1 {malformed database schema (?)}} | 730 } {1 {malformed database schema (?)}} |
721 do_test capi3-8.4 { | 731 do_test capi3-8.4 { |
722 # Build a 5-field row record. The first field is a string 'table', and | 732 # Build a 5-field row record. The first field is a string 'table', and |
723 # subsequent fields are all NULL. | 733 # subsequent fields are all NULL. |
724 db close | 734 db close |
725 file delete -force test.db test.db-journal | 735 forcedelete test.db test.db-journal |
726 sqlite3 db test.db | 736 sqlite3 db test.db |
727 execsql { | 737 execsql { |
728 CREATE TABLE t1(a); | 738 CREATE TABLE t1(a); |
729 PRAGMA writable_schema=ON; | 739 PRAGMA writable_schema=ON; |
730 INSERT INTO sqlite_master VALUES('table',NULL,NULL,NULL,NULL); | 740 INSERT INTO sqlite_master VALUES('table',NULL,NULL,NULL,NULL); |
731 } | 741 } |
732 db close | 742 db close |
733 } {}; | 743 } {}; |
734 do_test capi3-8.5 { | 744 do_test capi3-8.5 { |
735 catch { sqlite3 db test.db } | 745 catch { sqlite3 db test.db } |
736 catchsql { | 746 catchsql { |
737 SELECT * FROM sqlite_master; | 747 SELECT * FROM sqlite_master; |
738 } | 748 } |
739 } {1 {malformed database schema (?)}} | 749 } {1 {malformed database schema (?)}} |
740 db close | 750 db close |
741 } | 751 } |
742 file delete -force test.db | 752 forcedelete test.db |
743 file delete -force test.db-journal | 753 forcedelete test.db-journal |
744 | 754 |
745 | 755 |
746 # Test the english language string equivalents for sqlite error codes | 756 # Test the english language string equivalents for sqlite error codes |
747 set code2english [list \ | 757 set code2english [list \ |
748 SQLITE_OK {not an error} \ | 758 SQLITE_OK {not an error} \ |
749 SQLITE_ERROR {SQL logic error or missing database} \ | 759 SQLITE_ERROR {SQL logic error or missing database} \ |
750 SQLITE_PERM {access permission denied} \ | 760 SQLITE_PERM {access permission denied} \ |
751 SQLITE_ABORT {callback requested query abort} \ | 761 SQLITE_ABORT {callback requested query abort} \ |
752 SQLITE_BUSY {database is locked} \ | 762 SQLITE_BUSY {database is locked} \ |
753 SQLITE_LOCKED {database table is locked} \ | 763 SQLITE_LOCKED {database table is locked} \ |
(...skipping 17 matching lines...) Expand all Loading... |
771 unknownerror {unknown error} \ | 781 unknownerror {unknown error} \ |
772 ] | 782 ] |
773 | 783 |
774 set test_number 1 | 784 set test_number 1 |
775 foreach {code english} $code2english { | 785 foreach {code english} $code2english { |
776 do_test capi3-9.$test_number "sqlite3_test_errstr $code" $english | 786 do_test capi3-9.$test_number "sqlite3_test_errstr $code" $english |
777 incr test_number | 787 incr test_number |
778 } | 788 } |
779 | 789 |
780 # Test the error message when a "real" out of memory occurs. | 790 # Test the error message when a "real" out of memory occurs. |
| 791 if { [permutation] != "nofaultsim" } { |
781 ifcapable memdebug { | 792 ifcapable memdebug { |
782 do_test capi3-10-1 { | 793 do_test capi3-10-1 { |
783 sqlite3 db test.db | 794 sqlite3 db test.db |
784 set DB [sqlite3_connection_pointer db] | 795 set DB [sqlite3_connection_pointer db] |
785 sqlite3_memdebug_fail 1 | 796 sqlite3_memdebug_fail 1 |
786 catchsql { | 797 catchsql { |
787 select * from sqlite_master; | 798 select * from sqlite_master; |
788 } | 799 } |
789 } {1 {out of memory}} | 800 } {1 {out of memory}} |
790 do_test capi3-10-2 { | 801 do_test capi3-10-2 { |
(...skipping 18 matching lines...) Expand all Loading... |
809 sqlite3_errmsg $::DB | 820 sqlite3_errmsg $::DB |
810 } {out of memory} | 821 } {out of memory} |
811 ifcapable {utf16} { | 822 ifcapable {utf16} { |
812 do_test capi3-10-6 { | 823 do_test capi3-10-6 { |
813 utf8 [sqlite3_errmsg16 $::DB] | 824 utf8 [sqlite3_errmsg16 $::DB] |
814 } {out of memory} | 825 } {out of memory} |
815 } | 826 } |
816 db close | 827 db close |
817 sqlite3_memdebug_fail -1 | 828 sqlite3_memdebug_fail -1 |
818 } | 829 } |
| 830 } |
819 | 831 |
820 # The following tests - capi3-11.* - test that a COMMIT or ROLLBACK | 832 # The following tests - capi3-11.* - test that a COMMIT or ROLLBACK |
821 # statement issued while there are still outstanding VMs that are part of | 833 # statement issued while there are still outstanding VMs that are part of |
822 # the transaction fails. | 834 # the transaction fails. |
823 sqlite3 db test.db | 835 sqlite3 db test.db |
824 set DB [sqlite3_connection_pointer db] | 836 set DB [sqlite3_connection_pointer db] |
825 sqlite_register_test_function $DB func | 837 sqlite_register_test_function $DB func |
826 do_test capi3-11.1 { | 838 do_test capi3-11.1 { |
827 execsql { | 839 execsql { |
828 BEGIN; | 840 BEGIN; |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
887 set STMT [sqlite3_prepare $DB "SELECT a FROM t2" -1 TAIL] | 899 set STMT [sqlite3_prepare $DB "SELECT a FROM t2" -1 TAIL] |
888 sqlite3_step $STMT | 900 sqlite3_step $STMT |
889 } {SQLITE_ROW} | 901 } {SQLITE_ROW} |
890 do_test capi3-11.9.1 { | 902 do_test capi3-11.9.1 { |
891 sqlite3_get_autocommit $DB | 903 sqlite3_get_autocommit $DB |
892 } 0 | 904 } 0 |
893 do_test capi3-11.9.2 { | 905 do_test capi3-11.9.2 { |
894 catchsql { | 906 catchsql { |
895 ROLLBACK; | 907 ROLLBACK; |
896 } | 908 } |
897 } {1 {cannot rollback transaction - SQL statements in progress}} | 909 } {0 {}} |
898 do_test capi3-11.9.3 { | 910 do_test capi3-11.9.3 { |
899 sqlite3_get_autocommit $DB | 911 sqlite3_get_autocommit $DB |
900 } 0 | 912 } 1 |
901 do_test capi3-11.10 { | 913 do_test capi3-11.10 { |
902 sqlite3_step $STMT | 914 sqlite3_step $STMT |
903 } {SQLITE_ROW} | 915 } {SQLITE_ROW} |
| 916 ifcapable !autoreset { |
| 917 # If SQLITE_OMIT_AUTORESET is defined, then the statement must be |
| 918 # reset() before it can be passed to step() again. |
| 919 do_test capi3-11.11a { sqlite3_step $STMT } {SQLITE_MISUSE} |
| 920 do_test capi3-11.11b { sqlite3_reset $STMT } {SQLITE_ABORT} |
| 921 } |
904 do_test capi3-11.11 { | 922 do_test capi3-11.11 { |
905 sqlite3_step $STMT | 923 sqlite3_step $STMT |
906 } {SQLITE_ROW} | 924 } {SQLITE_DONE} |
907 do_test capi3-11.12 { | 925 do_test capi3-11.12 { |
908 sqlite3_step $STMT | 926 sqlite3_step $STMT |
909 } {SQLITE_DONE} | 927 sqlite3_step $STMT |
| 928 } {SQLITE_ROW} |
910 do_test capi3-11.13 { | 929 do_test capi3-11.13 { |
911 sqlite3_finalize $STMT | 930 sqlite3_finalize $STMT |
912 } {SQLITE_OK} | 931 } {SQLITE_OK} |
913 do_test capi3-11.14 { | 932 do_test capi3-11.14 { |
914 execsql { | 933 execsql { |
915 SELECT a FROM t2; | 934 SELECT a FROM t2; |
916 } | 935 } |
917 } {1 2 3} | 936 } {1 2} |
918 do_test capi3-11.14.1 { | 937 do_test capi3-11.14.1 { |
919 sqlite3_get_autocommit $DB | 938 sqlite3_get_autocommit $DB |
920 } 0 | 939 } 1 |
921 do_test capi3-11.15 { | 940 do_test capi3-11.15 { |
922 catchsql { | 941 catchsql { |
923 ROLLBACK; | 942 ROLLBACK; |
924 } | 943 } |
925 } {0 {}} | 944 } {1 {cannot rollback - no transaction is active}} |
926 do_test capi3-11.15.1 { | 945 do_test capi3-11.15.1 { |
927 sqlite3_get_autocommit $DB | 946 sqlite3_get_autocommit $DB |
928 } 1 | 947 } 1 |
929 do_test capi3-11.16 { | 948 do_test capi3-11.16 { |
930 execsql { | 949 execsql { |
931 SELECT a FROM t2; | 950 SELECT a FROM t2; |
932 } | 951 } |
933 } {1 2} | 952 } {1 2} |
934 | 953 |
935 # Sanity check on the definition of 'outstanding VM'. This means any VM | 954 # Sanity check on the definition of 'outstanding VM'. This means any VM |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1039 } {{} {} hello world {} {}} | 1058 } {{} {} hello world {} {}} |
1040 } | 1059 } |
1041 if {[llength [info commands sqlite3_sleep]]>0} { | 1060 if {[llength [info commands sqlite3_sleep]]>0} { |
1042 do_test capi3-13-5 { | 1061 do_test capi3-13-5 { |
1043 set ms [sqlite3_sleep 80] | 1062 set ms [sqlite3_sleep 80] |
1044 expr {$ms==80 || $ms==1000} | 1063 expr {$ms==80 || $ms==1000} |
1045 } {1} | 1064 } {1} |
1046 } | 1065 } |
1047 | 1066 |
1048 # Ticket #1219: Make sure binding APIs can handle a NULL pointer. | 1067 # Ticket #1219: Make sure binding APIs can handle a NULL pointer. |
1049 # | 1068 # |
1050 do_test capi3-14.1-misuse { | 1069 if {[clang_sanitize_address]==0} { |
1051 set rc [catch {sqlite3_bind_text 0 1 hello 5} msg] | 1070 do_test capi3-14.1-misuse { |
1052 lappend rc $msg | 1071 set rc [catch {sqlite3_bind_text 0 1 hello 5} msg] |
1053 } {1 SQLITE_MISUSE} | 1072 lappend rc $msg |
| 1073 } {1 SQLITE_MISUSE} |
| 1074 } |
1054 | 1075 |
1055 # Ticket #1650: Honor the nBytes parameter to sqlite3_prepare. | 1076 # Ticket #1650: Honor the nBytes parameter to sqlite3_prepare. |
1056 # | 1077 # |
1057 do_test capi3-15.1 { | 1078 do_test capi3-15.1 { |
1058 set sql {SELECT * FROM t2} | 1079 set sql {SELECT * FROM t2} |
1059 set nbytes [string length $sql] | 1080 set nbytes [string length $sql] |
1060 append sql { WHERE a==1} | 1081 append sql { WHERE a==1} |
1061 set STMT [sqlite3_prepare $DB $sql $nbytes TAIL] | 1082 set STMT [sqlite3_prepare $DB $sql $nbytes TAIL] |
1062 sqlite3_step $STMT | 1083 sqlite3_step $STMT |
1063 sqlite3_column_int $STMT 0 | 1084 sqlite3_column_int $STMT 0 |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1194 sqlite3_finalize $STMT | 1215 sqlite3_finalize $STMT |
1195 | 1216 |
1196 # Ticket #3134. Prepare a statement with an nBytes parameter of 0. | 1217 # Ticket #3134. Prepare a statement with an nBytes parameter of 0. |
1197 # Make sure this works correctly and does not reference memory out of | 1218 # Make sure this works correctly and does not reference memory out of |
1198 # range. | 1219 # range. |
1199 # | 1220 # |
1200 do_test capi3-19.1 { | 1221 do_test capi3-19.1 { |
1201 sqlite3_prepare_tkt3134 db | 1222 sqlite3_prepare_tkt3134 db |
1202 } {} | 1223 } {} |
1203 | 1224 |
| 1225 # Test that calling sqlite3_column_blob() on a TEXT value does not change |
| 1226 # the return type of subsequent calls to sqlite3_column_type(). |
| 1227 # |
| 1228 do_execsql_test 20.1 { |
| 1229 CREATE TABLE t4(x); |
| 1230 INSERT INTO t4 VALUES('abcdefghij'); |
| 1231 } |
| 1232 do_test 20.2 { |
| 1233 set stmt [sqlite3_prepare db "SELECT * FROM t4" -1 dummy] |
| 1234 sqlite3_step $stmt |
| 1235 } {SQLITE_ROW} |
| 1236 do_test 20.3 { sqlite3_column_type $stmt 0 } {TEXT} |
| 1237 do_test 20.4 { sqlite3_column_blob $stmt 0 } {abcdefghij} |
| 1238 do_test 20.5 { sqlite3_column_type $stmt 0 } {TEXT} |
| 1239 do_test 20.6 { sqlite3_finalize $stmt } SQLITE_OK |
| 1240 |
| 1241 |
1204 # Tests of the interface when no VFS is registered. | 1242 # Tests of the interface when no VFS is registered. |
1205 # | 1243 # |
1206 if {![info exists tester_do_binarylog]} { | 1244 if {![info exists tester_do_binarylog]} { |
1207 db close | 1245 db close |
1208 vfs_unregister_all | 1246 vfs_unregister_all |
1209 do_test capi3-20.1 { | 1247 do_test capi3-20.1 { |
1210 sqlite3_sleep 100 | 1248 sqlite3_sleep 100 |
1211 } {0} | 1249 } {0} |
1212 vfs_reregister_all | 1250 vfs_reregister_all |
1213 } | 1251 } |
1214 | 1252 |
1215 finish_test | 1253 finish_test |
OLD | NEW |