| 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 | |
| 30 | m4_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. |
| 37 | m4_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. |
| 51 | m4_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 |
| 60 | AT_SETUP([codec: base64]) |
| 61 | AT_KEYWORDS([codec base64]) |
| 62 | |
| 63 | ## Test vectors from RFC4648. |
| 64 | CODEC_TESTENCODE([base64], [], []) |
| 65 | CODEC_TESTENCODE([base64], [f], [Zg==]) |
| 66 | CODEC_TESTENCODE([base64], [fo], [Zm8=]) |
| 67 | CODEC_TESTENCODE([base64], [foo], [Zm9v]) |
| 68 | CODEC_TESTENCODE([base64], [foob], [Zm9vYg==]) |
| 69 | CODEC_TESTENCODE([base64], [fooba], [Zm9vYmE=]) |
| 70 | CODEC_TESTENCODE([base64], [foobar], [Zm9vYmFy]) |
| 71 | |
| 72 | ## Test vectors for Base64-URL. |
| 73 | CODEC_TESTENCODE([base64url], [fooba], [Zm9vYmE=]) |
| 74 | CODEC_TESTENCODE([base64], [fooba?], [Zm9vYmE/]) |
| 75 | CODEC_TESTENCODE([base64url], [fooba?], [Zm9vYmE_]) |
| 76 | CODEC_TESTENCODE([file64], [fooba?], [Zm9vYmE%]) |
| 77 | CODEC_TESTENCODE([base64], [fooba~], [Zm9vYmF+]) |
| 78 | CODEC_TESTENCODE([base64url], [fooba~], [Zm9vYmF-]) |
| 79 | CODEC_TESTENCODE([file64], [fooba~], [Zm9vYmF+]) |
| 80 | |
| 81 | ## Some noeqpad tests. |
| 82 | CODEC_TESTENCODE([base64], [f], [Zg], [-fnoeqpad]) |
| 83 | CODEC_TESTENCODE([base64], [foo], [Zm9v], [-fnoeqpad]) |
| 84 | CODEC_TESTENCODE([base64], [foob], [Zm9vYg], [-fnoeqpad]) |
| 85 | |
| 86 | ## Test for pad-character errors. |
| 87 | CODEC_TESTDECODE([base64], [Zg], [f], [], |
| 88 | [1], [Invalid padding character]) |
| 89 | CODEC_TESTDECODE([base64], [Zg], [f], [-figneqpad]) |
| 90 | CODEC_TESTDECODE([base64], [Zg=], [f], [-figneqpad]) |
| 91 | CODEC_TESTDECODE([base64], [Zg==], [f], [-figneqpad]) |
| 92 | CODEC_TESTDECODE([base64], [Zg], [f], [-fnoeqpad]) |
| 93 | CODEC_TESTDECODE([base64], [Zg=], [], [-fnoeqpad], |
| 94 | [1], [Invalid character]) |
| 95 | CODEC_TESTDECODE([base64], [Zg], [f], [-figneqmid]) |
| 96 | CODEC_TESTDECODE([base64], [Z==g=], [f], [-figneqmid]) |
| 97 | |
| 98 | ## Test for other crappy characters. |
| 99 | CODEC_TESTDECODE([base64], [Z:g=:=], [], [], |
| 100 | [1], [Invalid character]) |
| 101 | CODEC_TESTDECODE([base64], [Z:g=:=], [f], [-figninvch]) |
| 102 | CODEC_TESTDECODE([base64], [Z:g=:==], [], [-figninvch], |
| 103 | [1], [Invalid padding character]) |
| 104 | |
| 105 | ## Test for incorrect padding bits. |
| 106 | CODEC_TESTDECODE([base64], [Zh==], [], [], |
| 107 | [1], [Nonzero padding bits]) |
| 108 | CODEC_TESTDECODE([base64], [Zh==], [f], [-fignzpad]) |
| 109 | |
| 110 | ## Make sure the case flags are suppressed. |
| 111 | CODEC_TESTENCODE([base64], [fooba], [Zm9vYmE=], [-flowerc]) |
| 112 | |
| 113 | ## Multiline formatting. |
| 114 | AT_DATA([bigfile], |
| 115 | [There are three infallible ways of pleasing an author, and the three form a |
| 116 | rising scale of compliment: 1, to tell him you have read one of his books; 2, |
| 117 | to tell him you have read all of his books; 3, to ask him to let you read the |
| 118 | manuscript of his forthcoming book. No. 1 admits you to his respect; No. 2 |
| 119 | admits you to his admiration; No. 3 carries you clear into his heart. |
| 120 | -- Mark Twain, "Pudd'nhead Wilson's Calendar" |
| 121 | ]) |
| 122 | |
| 123 | AT_DATA([bigfile.b64], |
| 124 | [VGhlcmUgYXJlIHRocmVlIGluZmFsbGlibGUgd2F5cyBvZiBwbGVhc2luZyBhbiBh |
| 125 | dXRob3IsIGFuZCB0aGUgdGhyZWUgZm9ybSBhCnJpc2luZyBzY2FsZSBvZiBjb21w |
| 126 | bGltZW50OiAxLCB0byB0ZWxsIGhpbSB5b3UgaGF2ZSByZWFkIG9uZSBvZiBoaXMg |
| 127 | Ym9va3M7IDIsCnRvIHRlbGwgaGltIHlvdSBoYXZlIHJlYWQgYWxsIG9mIGhpcyBi |
| 128 | b29rczsgMywgdG8gYXNrIGhpbSB0byBsZXQgeW91IHJlYWQgdGhlCm1hbnVzY3Jp |
| 129 | cHQgb2YgaGlzIGZvcnRoY29taW5nIGJvb2suICBOby4gMSBhZG1pdHMgeW91IHRv |
| 130 | IGhpcyByZXNwZWN0OyBOby4gMgphZG1pdHMgeW91IHRvIGhpcyBhZG1pcmF0aW9u |
| 131 | OyBOby4gMyBjYXJyaWVzIHlvdSBjbGVhciBpbnRvIGhpcyBoZWFydC4KICAgICAg |
| 132 | ICAgICAgICAgIC0tIE1hcmsgVHdhaW4sICJQdWRkJ25oZWFkIFdpbHNvbidzIENh |
| 133 | bGVuZGFyIgo= |
| 134 | ]) |
| 135 | cp bigfile.b64 expout |
| 136 | AT_CHECK([BUILDDIR/bincode base64 bigfile], [0], [expout]) |
| 137 | AT_CHECK([BUILDDIR/bincode -f-ignnewl -d base64 bigfile.b64], [1], [ignore], |
| 138 | [bincode: decoding error: Invalid character[]NEWLINE]) |
| 139 | cp bigfile expout |
| 140 | AT_CHECK([BUILDDIR/bincode -d base64 bigfile.b64], [0], [expout]) |
| 141 | |
| 142 | AT_CLEANUP |
| 143 | |
| 144 | ## base32 |
| 145 | AT_SETUP([codec: base32]) |
| 146 | AT_KEYWORDS([codec base32]) |
| 147 | |
| 148 | CODEC_TESTENCODE([base32], [], []) |
| 149 | CODEC_TESTENCODE([base32], [f], [MY======]) |
| 150 | CODEC_TESTENCODE([base32], [fo], [MZXQ====]) |
| 151 | CODEC_TESTENCODE([base32], [foo], [MZXW6===]) |
| 152 | CODEC_TESTENCODE([base32], [foob], [MZXW6YQ=]) |
| 153 | CODEC_TESTENCODE([base32], [fooba], [MZXW6YTB]) |
| 154 | CODEC_TESTENCODE([base32], [foobar], [MZXW6YTBOI======]) |
| 155 | |
| 156 | CODEC_TESTENCODE([base32hex], [], []) |
| 157 | CODEC_TESTENCODE([base32hex], [f], [CO======]) |
| 158 | CODEC_TESTENCODE([base32hex], [fo], [CPNG====]) |
| 159 | CODEC_TESTENCODE([base32hex], [foo], [CPNMU===]) |
| 160 | CODEC_TESTENCODE([base32hex], [foob], [CPNMUOG=]) |
| 161 | CODEC_TESTENCODE([base32hex], [fooba], [CPNMUOJ1]) |
| 162 | CODEC_TESTENCODE([base32hex], [foobar], [CPNMUOJ1E8======]) |
| 163 | |
| 164 | AT_CLEANUP |
| 165 | |
| 166 | ## hex |
| 167 | AT_SETUP([codec: hex]) |
| 168 | AT_KEYWORDS([codec hex]) |
| 169 | |
| 170 | CODEC_TESTENCODE([hex], [], []) |
| 171 | CODEC_TESTENCODE([hex], [f], [66]) |
| 172 | CODEC_TESTENCODE([hex], [fo], [666F]) |
| 173 | CODEC_TESTENCODE([hex], [foo], [666F6F]) |
| 174 | CODEC_TESTENCODE([hex], [foob], [666F6F62]) |
| 175 | CODEC_TESTENCODE([hex], [fooba], [666F6F6261]) |
| 176 | CODEC_TESTENCODE([hex], [foobar], [666F6F626172]) |
| 177 | |
| 178 | AT_CLEANUP |
| 179 | |
| 180 | ###----- That's all, folks -------------------------------------------------- |