summaryrefslogtreecommitdiff
path: root/test/basexx1.test
blob: 8b5b5c7c209d214a2151f1bf5e87b2554f19196d (plain) (blame)
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# 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==
}}

# Buffer size testing
do_execsql_test 102-b {
  WITH RECURSIVE c(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c wHERE n<5000)
  SELECT sum(length(base64(randomblob(n)))) FROM c;
} {16910656}

# 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;
} {}

do_catchsql_test 118 {
  SELECT base64(zeroblob(2000_000_000))
} {/1.*too big.*/}
do_catchsql_test 119 {
  SELECT base85(zeroblob(2000_000_000))
} {/1.*too big.*/}

# Forum post 6cb8eb520bc3: whitespace inside a group of four base64
# digits is ignored, and a group yields only the bytes its digits encode.
do_execsql_test 120 {
  SELECT hex(base64('Q UJD')), hex(base64('QU JD')), hex(base64('QUJ D'));
} {414243 414243 414243}
do_execsql_test 121 {
  SELECT hex(base64('QU'||char(13,10)||'JD')),
         hex(base64('QUJDQU'||char(10)||'JD')),
         hex(base64('A A E C A w Q F B g c I')),
         hex(base64('AAE CAw QFB gcI')),
         hex(base64('AAECA'||char(10)||'wQFBg'||char(10)||'cI'));
} {414243 414243414243 000102030405060708 000102030405060708 000102030405060708}

# Trailing whitespace is ignored, however much of it there is.
do_execsql_test 122 {
  SELECT hex(base64('AAECAw  ')), hex(base64('AAECAw   ')),
         hex(base64('AAECAw'||char(10,10,10))), hex(base64('AAECAwQ  '));
} {00010203 00010203 00010203 0001020304}

# A non-base64 character inside a group ends the decode, keeping only
# the whole bytes encoded by the digits before it.
do_execsql_test 123 {
  SELECT hex(base64('Q!UJD')), hex(base64('QU!JD')), hex(base64('QUJ!D')),
         hex(base64('QUJD!QUJD'));
} {{} 41 4142 414243414243}

# Pad characters are unchanged: they end a group and decoding may resume
# after a complete padded group.
do_execsql_test 124 {
  SELECT hex(base64('QQ==')), hex(base64('QUI=')), hex(base64('QQ=')),
         hex(base64('QQ==QUJD')), hex(base64('QQ= =')),
         hex(base64(base64(x'41')||base64(x'4243')));
} {41 4142 41 41414243 41 414243}

finish_test