chiark / gitweb /
codec/{base32,hex}.h: Include `codec.h'.
[mLib] / codec / tests.at
CommitLineData
7cf5c72a
MW
1### -*-autotest-*-
2###
3### Test script for utilities
4###
5### (c) 2009 Straylight/Edgeware
6###
7
8###----- Licensing notice ---------------------------------------------------
9###
10### This file is part of the mLib utilities library.
11###
12### mLib is free software; you can redistribute it and/or modify
13### it under the terms of the GNU Library General Public License as
14### published by the Free Software Foundation; either version 2 of the
15### License, or (at your option) any later version.
16###
17### mLib is distributed in the hope that it will be useful,
18### but WITHOUT ANY WARRANTY; without even the implied warranty of
19### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20### GNU Library General Public License for more details.
21###
22### You should have received a copy of the GNU Library General Public
23### License along with mLib; if not, write to the Free
24### Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25### MA 02111-1307, USA.
26
27###--------------------------------------------------------------------------
28### Useful macros.
29
30m4_define([NEWLINE], [
31])
32
33## CODEC_TESTENCODE(codec, raw-data, encoding, [options])
34##
35## Check that CODEC encodes RAW-DATA as ENCODING, and that it can decode
36## ENCODING as RAW-DATA, using OPTIONS.
37m4_define([CODEC_TESTENCODE], [
38 printf '%s' '$2' >in
39 printf '%s\n' '$3' >expout
40 AT_CHECK([BUILDDIR/bincode $4 $1 in], [0], [expout])
41 mv in expout
42 printf '%s' '$3' >in
43 AT_CHECK([BUILDDIR/bincode -d $4 $1 in], [0], [expout])
44])
45
46## CODEC_TESTDECODE(codec, encoding, [raw-data],
47## [options], [exp-status], [exp-stderr])
48##
49## Check that CODEC decodes ENCODING as RAW-DATA, given the OPTIONS, or that
50## it reports EXP-STATUS and produces EXP-STDERR.
51m4_define([CODEC_TESTDECODE], [
52 printf '%s' '$2' >in
53 printf '%s' '$3' >expout
54 AT_CHECK([BUILDDIR/bincode -d $4 $1 in], [$5], [expout],
55 m4_if([$6], [], [], [bincode: decoding error: $6[]NEWLINE]))
56])
57
58###--------------------------------------------------------------------------
59### base64
60AT_SETUP([codec: base64])
61AT_KEYWORDS([codec base64])
62
63## Test vectors from RFC4648.
64CODEC_TESTENCODE([base64], [], [])
65CODEC_TESTENCODE([base64], [f], [Zg==])
66CODEC_TESTENCODE([base64], [fo], [Zm8=])
67CODEC_TESTENCODE([base64], [foo], [Zm9v])
68CODEC_TESTENCODE([base64], [foob], [Zm9vYg==])
69CODEC_TESTENCODE([base64], [fooba], [Zm9vYmE=])
70CODEC_TESTENCODE([base64], [foobar], [Zm9vYmFy])
71
72## Test vectors for Base64-URL.
73CODEC_TESTENCODE([base64url], [fooba], [Zm9vYmE=])
74CODEC_TESTENCODE([base64], [fooba?], [Zm9vYmE/])
75CODEC_TESTENCODE([base64url], [fooba?], [Zm9vYmE_])
76CODEC_TESTENCODE([file64], [fooba?], [Zm9vYmE%])
77CODEC_TESTENCODE([base64], [fooba~], [Zm9vYmF+])
78CODEC_TESTENCODE([base64url], [fooba~], [Zm9vYmF-])
79CODEC_TESTENCODE([file64], [fooba~], [Zm9vYmF+])
80
81## Some noeqpad tests.
82CODEC_TESTENCODE([base64], [f], [Zg], [-fnoeqpad])
83CODEC_TESTENCODE([base64], [foo], [Zm9v], [-fnoeqpad])
84CODEC_TESTENCODE([base64], [foob], [Zm9vYg], [-fnoeqpad])
85
86## Test for pad-character errors.
87CODEC_TESTDECODE([base64], [Zg], [f], [],
88 [1], [Invalid padding character])
89CODEC_TESTDECODE([base64], [Zg], [f], [-figneqpad])
90CODEC_TESTDECODE([base64], [Zg=], [f], [-figneqpad])
91CODEC_TESTDECODE([base64], [Zg==], [f], [-figneqpad])
92CODEC_TESTDECODE([base64], [Zg], [f], [-fnoeqpad])
93CODEC_TESTDECODE([base64], [Zg=], [], [-fnoeqpad],
94 [1], [Invalid character])
95CODEC_TESTDECODE([base64], [Zg], [f], [-figneqmid])
96CODEC_TESTDECODE([base64], [Z==g=], [f], [-figneqmid])
97
98## Test for other crappy characters.
99CODEC_TESTDECODE([base64], [Z:g=:=], [], [],
100 [1], [Invalid character])
101CODEC_TESTDECODE([base64], [Z:g=:=], [f], [-figninvch])
102CODEC_TESTDECODE([base64], [Z:g=:==], [], [-figninvch],
103 [1], [Invalid padding character])
104
105## Test for incorrect padding bits.
106CODEC_TESTDECODE([base64], [Zh==], [], [],
107 [1], [Nonzero padding bits])
108CODEC_TESTDECODE([base64], [Zh==], [f], [-fignzpad])
109
110## Make sure the case flags are suppressed.
111CODEC_TESTENCODE([base64], [fooba], [Zm9vYmE=], [-flowerc])
112
113## Multiline formatting.
114AT_DATA([bigfile],
115[There are three infallible ways of pleasing an author, and the three form a
116rising scale of compliment: 1, to tell him you have read one of his books; 2,
117to tell him you have read all of his books; 3, to ask him to let you read the
118manuscript of his forthcoming book. No. 1 admits you to his respect; No. 2
119admits you to his admiration; No. 3 carries you clear into his heart.
120 -- Mark Twain, "Pudd'nhead Wilson's Calendar"
121])
122
123AT_DATA([bigfile.b64],
124[VGhlcmUgYXJlIHRocmVlIGluZmFsbGlibGUgd2F5cyBvZiBwbGVhc2luZyBhbiBh
125dXRob3IsIGFuZCB0aGUgdGhyZWUgZm9ybSBhCnJpc2luZyBzY2FsZSBvZiBjb21w
126bGltZW50OiAxLCB0byB0ZWxsIGhpbSB5b3UgaGF2ZSByZWFkIG9uZSBvZiBoaXMg
127Ym9va3M7IDIsCnRvIHRlbGwgaGltIHlvdSBoYXZlIHJlYWQgYWxsIG9mIGhpcyBi
128b29rczsgMywgdG8gYXNrIGhpbSB0byBsZXQgeW91IHJlYWQgdGhlCm1hbnVzY3Jp
129cHQgb2YgaGlzIGZvcnRoY29taW5nIGJvb2suICBOby4gMSBhZG1pdHMgeW91IHRv
130IGhpcyByZXNwZWN0OyBOby4gMgphZG1pdHMgeW91IHRvIGhpcyBhZG1pcmF0aW9u
131OyBOby4gMyBjYXJyaWVzIHlvdSBjbGVhciBpbnRvIGhpcyBoZWFydC4KICAgICAg
132ICAgICAgICAgIC0tIE1hcmsgVHdhaW4sICJQdWRkJ25oZWFkIFdpbHNvbidzIENh
133bGVuZGFyIgo=
134])
135cp bigfile.b64 expout
136AT_CHECK([BUILDDIR/bincode base64 bigfile], [0], [expout])
137AT_CHECK([BUILDDIR/bincode -f-ignnewl -d base64 bigfile.b64], [1], [ignore],
138 [bincode: decoding error: Invalid character[]NEWLINE])
139cp bigfile expout
140AT_CHECK([BUILDDIR/bincode -d base64 bigfile.b64], [0], [expout])
141
142AT_CLEANUP
143
144## base32
145AT_SETUP([codec: base32])
146AT_KEYWORDS([codec base32])
147
148CODEC_TESTENCODE([base32], [], [])
149CODEC_TESTENCODE([base32], [f], [MY======])
150CODEC_TESTENCODE([base32], [fo], [MZXQ====])
151CODEC_TESTENCODE([base32], [foo], [MZXW6===])
152CODEC_TESTENCODE([base32], [foob], [MZXW6YQ=])
153CODEC_TESTENCODE([base32], [fooba], [MZXW6YTB])
154CODEC_TESTENCODE([base32], [foobar], [MZXW6YTBOI======])
155
156CODEC_TESTENCODE([base32hex], [], [])
157CODEC_TESTENCODE([base32hex], [f], [CO======])
158CODEC_TESTENCODE([base32hex], [fo], [CPNG====])
159CODEC_TESTENCODE([base32hex], [foo], [CPNMU===])
160CODEC_TESTENCODE([base32hex], [foob], [CPNMUOG=])
161CODEC_TESTENCODE([base32hex], [fooba], [CPNMUOJ1])
162CODEC_TESTENCODE([base32hex], [foobar], [CPNMUOJ1E8======])
163
164AT_CLEANUP
165
166## hex
167AT_SETUP([codec: hex])
168AT_KEYWORDS([codec hex])
169
170CODEC_TESTENCODE([hex], [], [])
171CODEC_TESTENCODE([hex], [f], [66])
172CODEC_TESTENCODE([hex], [fo], [666F])
173CODEC_TESTENCODE([hex], [foo], [666F6F])
174CODEC_TESTENCODE([hex], [foob], [666F6F62])
175CODEC_TESTENCODE([hex], [fooba], [666F6F6261])
176CODEC_TESTENCODE([hex], [foobar], [666F6F626172])
177
178AT_CLEANUP
179
180###----- That's all, folks --------------------------------------------------