1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
# 2022 November 22
#
# The author disclaims copyright to this source code. In place of
# a legal notice, here is a blessing:
#
# May you do good and not evil.
# May you find forgiveness for yourself and forgive others.
# May you share freely, never taking more than you give.
#
#***********************************************************************
#
set testdir [file dirname $argv0]
source $testdir/tester.tcl
set testprefix basexx
if {[catch {load_static_extension db basexx} error]} {
puts "Skipping basexx tests, hit load error: $error"
finish_test; return
}
# Empty blobs encode to empty strings.
do_execsql_test 100 {
SELECT base64(x'')||base85(x'');
} {{}}
# Empty strings decode to empty blobs.
do_execsql_test 101 {
SELECT hex(x'01'||base64('')||base85('')||x'02');
} {0102}
# Basic base64 encoding
do_execsql_test 102 {
SELECT base64(x'000102030405');
SELECT base64(x'0001020304');
SELECT base64(x'00010203');
} {{AAECAwQF
} {AAECAwQ=
} {AAECAw==
}}
# Basic base64 decoding with pad chars
do_execsql_test 103 {
SELECT hex(base64('AAECAwQF'));
SELECT hex(base64('AAECAwQ='));
SELECT hex(base64('AAECAw=='));
} {000102030405 0001020304 00010203}
# Basic base64 decoding without pad chars and with whitespace
do_execsql_test 104 {
SELECT hex(base64(' AAECAwQF '));
SELECT hex(base64(' AAECAwQ'));
SELECT hex(base64('AAECAw '));
} {000102030405 0001020304 00010203}
# Basic base85 encoding
do_execsql_test 105 {
SELECT base85(x'000102030405');
SELECT base85(x'0001020304');
SELECT base85(x'00010203');
} {{##/2,#2/
} {##/2,#*
} {##/2,
}}
# Basic base85 decoding with and without whitespace
do_execsql_test 106 {
SELECT hex(base85('##/2,#2/'));
SELECT hex(base85('##/2,#*'));
SELECT hex(base85('##/2,'));
SELECT hex(base85(' ##/2,#2/ '));
SELECT hex(base85(' ##/2,#*'));
SELECT hex(base85('##/2, '));
} {000102030405 0001020304 00010203 000102030405 0001020304 00010203}
# Round-trip some random blobs.
do_execsql_test 107 {
CREATE TEMP TABLE rb( len int, b blob ) STRICT;
INSERT INTO rb(len) VALUES (1),(2),(3),(4),(5),(150),(151),(152),(153),(1054);
UPDATE rb SET b = randomblob(len);
SELECT len, base64(base64(b))=b, base85(base85(b))=b
FROM rb ORDER BY len;
} {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}
# Same round-trip but with space or junk prepended and/or appended or not.
do_execsql_test 108 {
CREATE TEMP TABLE junk(j text, rank int);
INSERT INTO junk VALUES ('',0),(' ',1),('~',2);
SELECT len, base64(j.j||base64(b)||j.j)=b, base85(j.j||base85(b)||j.j)=b
FROM rb r, junk j WHERE j.rank=(r.len+r.len/25)%3 ORDER BY len;
} {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}
# Exercise the fail-on-too-large result feature.
set inLimit [sqlite3_limit db SQLITE_LIMIT_LENGTH -1]
sqlite3_limit db SQLITE_LIMIT_LENGTH 1300
do_catchsql_test 109 {
SELECT len, base64(b) FROM rb WHERE len>200;
} {1 {blob expanded to base64 too big}}
do_catchsql_test 110 {
SELECT len, base85(b) FROM rb WHERE len>200;
} {1 {blob expanded to base85 too big}}
do_catchsql_test 111 {
SELECT length(base85(b))=1335 FROM rb WHERE len=1054;
} {1 {blob expanded to base85 too big}}
sqlite3_limit db SQLITE_LIMIT_LENGTH $inLimit
# Exercise is_base85(t)
do_execsql_test 112 {
SELECT is_base85(' '||base85(x'123456')||char(10)),
is_base85('#$%&*+,-./0123456789:;<=>?@'
||'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
||'[\]^_`'
||'abcdefghijklmnopqrstuvwxyz'),
is_base85('!'), is_base85('"'), is_base85(''''), is_base85('('),
is_base85(')'), is_base85(char(123)), is_base85('|'), is_base85(char(125)),
is_base85('~'), is_base85(char(127));
} {1 1 0 0 0 0 0 0 0 0 0 0}
do_execsql_test 113 {
SELECT is_base85(NULL) IS NULL;
} {1}
do_catchsql_test 114 {
SELECT is_base85(1);
} {1 {is_base85 accepts only text or NULL}}
do_catchsql_test 115 {
SELECT is_base85(1.1);
} {1 {is_base85 accepts only text or NULL}}
do_catchsql_test 116 {
SELECT is_base85(x'00');
} {1 {is_base85 accepts only text or NULL}}
# Round-trip many bigger random blobs.
do_execsql_test 117 {
CREATE TABLE bs(b blob, num);
INSERT INTO bs SELECT randomblob(4000 + n%3), n
FROM (
WITH RECURSIVE seq(n) AS (
VALUES(1) UNION ALL SELECT n+1
FROM seq WHERE n<100
) SELECT n FROM seq);
SELECT num FROM bs WHERE base64(base64(b))!=b;
SELECT num FROM bs WHERE base85(base85(b))!=b;
} {}
finish_test
|