3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
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.
10 #***********************************************************************
13 set testdir [file dirname $argv0]
14 source $testdir/tester.tcl
17 if {[catch {load_static_extension db basexx} error]} {
18 puts "Skipping basexx tests, hit load error: $error"
22 # Empty blobs encode to empty strings.
24 SELECT base64(x'')||base85(x'');
27 # Empty strings decode to empty blobs.
29 SELECT hex(x'01'||base64('')||base85('')||x'02');
32 # Basic base64 encoding
34 SELECT base64(x'000102030405');
35 SELECT base64(x'0001020304');
36 SELECT base64(x'00010203');
42 # Basic base64 decoding with pad chars
44 SELECT hex(base64('AAECAwQF'));
45 SELECT hex(base64('AAECAwQ='));
46 SELECT hex(base64('AAECAw=='));
47 } {000102030405 0001020304 00010203}
49 # Basic base64 decoding without pad chars and with whitespace
51 SELECT hex(base64(' AAECAwQF '));
52 SELECT hex(base64(' AAECAwQ'));
53 SELECT hex(base64('AAECAw '));
54 } {000102030405 0001020304 00010203}
56 # Basic base85 encoding
58 SELECT base85(x'000102030405');
59 SELECT base85(x'0001020304');
60 SELECT base85(x'00010203');
66 # Basic base85 decoding with and without whitespace
68 SELECT hex(base85('##/2,#2/'));
69 SELECT hex(base85('##/2,#*'));
70 SELECT hex(base85('##/2,'));
71 SELECT hex(base85(' ##/2,#2/ '));
72 SELECT hex(base85(' ##/2,#*'));
73 SELECT hex(base85('##/2, '));
74 } {000102030405 0001020304 00010203 000102030405 0001020304 00010203}
76 # Round-trip some random blobs.
78 CREATE TEMP TABLE rb( len int, b blob ) STRICT;
79 INSERT INTO rb(len) VALUES (1),(2),(3),(4),(5),(150),(151),(152),(153),(1054);
80 UPDATE rb SET b = randomblob(len);
81 SELECT len, base64(base64(b))=b, base85(base85(b))=b
83 } {1 1 1 2 1 1 3 1 1 4 1 1 5 1 1 150 1 1 151 1 1 152 1 1 153 1 1 1054 1 1}
85 # Same round-trip but with space or junk prepended and/or appended or not.
87 CREATE TEMP TABLE junk(j text, rank int);
88 INSERT INTO junk VALUES ('',0),(' ',1),('~',2);
89 SELECT len, base64(j.j||base64(b)||j.j)=b, base85(j.j||base85(b)||j.j)=b
90 FROM rb r, junk j WHERE j.rank=(r.len+r.len/25)%3 ORDER BY len;
91 } {1 1 1 2 1 1 3 1 1 4 1 1 5 1 1 150 1 1 151 1 1 152 1 1 153 1 1 1054 1 1}
93 # Exercise the fail-on-too-large result feature.
95 set inLimit [sqlite3_limit db SQLITE_LIMIT_LENGTH -1]
96 sqlite3_limit db SQLITE_LIMIT_LENGTH 1300
98 do_catchsql_test 109 {
99 SELECT len, base64(b) FROM rb WHERE len>200;
100 } {1 {blob expanded to base64 too big}}
102 do_catchsql_test 110 {
103 SELECT len, base85(b) FROM rb WHERE len>200;
104 } {1 {blob expanded to base85 too big}}
106 do_catchsql_test 111 {
107 SELECT length(base85(b))=1335 FROM rb WHERE len=1054;
108 } {1 {blob expanded to base85 too big}}
110 sqlite3_limit db SQLITE_LIMIT_LENGTH $inLimit
112 # Exercise is_base85(t)
114 do_execsql_test 112 {
115 SELECT is_base85(' '||base85(x'123456')||char(10)),
116 is_base85('#$%&*+,-./0123456789:;<=>?@'
117 ||'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
119 ||'abcdefghijklmnopqrstuvwxyz'),
120 is_base85('!'), is_base85('"'), is_base85(''''), is_base85('('),
121 is_base85(')'), is_base85(char(123)), is_base85('|'), is_base85(char(125)),
122 is_base85('~'), is_base85(char(127));
123 } {1 1 0 0 0 0 0 0 0 0 0 0}
125 do_execsql_test 113 {
126 SELECT is_base85(NULL) IS NULL;
129 do_catchsql_test 114 {
131 } {1 {is_base85 accepts only text or NULL}}
133 do_catchsql_test 115 {
134 SELECT is_base85(1.1);
135 } {1 {is_base85 accepts only text or NULL}}
137 do_catchsql_test 116 {
138 SELECT is_base85(x'00');
139 } {1 {is_base85 accepts only text or NULL}}
141 # Round-trip many bigger random blobs.
143 do_execsql_test 117 {
144 CREATE TABLE bs(b blob, num);
145 INSERT INTO bs SELECT randomblob(4000 + n%3), n
147 WITH RECURSIVE seq(n) AS (
148 VALUES(1) UNION ALL SELECT n+1
150 ) SELECT n FROM seq);
151 SELECT num FROM bs WHERE base64(base64(b))!=b;
152 SELECT num FROM bs WHERE base85(base85(b))!=b;