1 /* Copyright (c) 2014, Google Inc.
3 * Permission to use, copy, modify, and/or distribute this software for any
4 * purpose with or without fee is hereby granted, provided that the above
5 * copyright notice and this permission notice appear in all copies.
7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
18 #include <openssl/base64.h>
19 #include <openssl/crypto.h>
20 #include <openssl/err.h>
28 /* Test vectors from RFC 4648. */
29 static const TEST_VECTOR test_vectors[] = {
34 { "foob", "Zm9vYg==" },
35 { "fooba", "Zm9vYmE=" },
36 { "foobar", "Zm9vYmFy" },
39 static const size_t kNumTests = sizeof(test_vectors) / sizeof(test_vectors[0]);
41 static int test_encode(void) {
46 for (i = 0; i < kNumTests; i++) {
47 const TEST_VECTOR *t = &test_vectors[i];
48 len = EVP_EncodeBlock(out, (const uint8_t*)t->decoded, strlen(t->decoded));
49 if (len != strlen(t->encoded) ||
50 memcmp(out, t->encoded, len) != 0) {
51 fprintf(stderr, "encode(\"%s\") = \"%.*s\", want \"%s\"\n",
52 t->decoded, (int)len, (const char*)out, t->encoded);
59 static int test_decode(void) {
64 for (i = 0; i < kNumTests; i++) {
65 /* Test the normal API. */
66 const TEST_VECTOR *t = &test_vectors[i];
67 size_t expected_len = strlen(t->decoded);
68 if (!EVP_DecodeBase64(out, &len, sizeof(out),
69 (const uint8_t*)t->encoded, strlen(t->encoded))) {
70 fprintf(stderr, "decode(\"%s\") failed\n", t->encoded);
73 if (len != strlen(t->decoded) ||
74 memcmp(out, t->decoded, len) != 0) {
75 fprintf(stderr, "decode(\"%s\") = \"%.*s\", want \"%s\"\n",
76 t->encoded, (int)len, (const char*)out, t->decoded);
80 /* Test that the padding behavior of the deprecated API is
82 ret = EVP_DecodeBlock(out, (const uint8_t*)t->encoded, strlen(t->encoded));
84 fprintf(stderr, "decode(\"%s\") failed\n", t->encoded);
88 fprintf(stderr, "EVP_DecodeBlock did not ignore padding\n");
91 if (expected_len % 3 != 0) {
92 ret -= 3 - (expected_len % 3);
94 if (ret != strlen(t->decoded) ||
95 memcmp(out, t->decoded, ret) != 0) {
96 fprintf(stderr, "decode(\"%s\") = \"%.*s\", want \"%s\"\n",
97 t->encoded, ret, (const char*)out, t->decoded);
102 if (EVP_DecodeBase64(out, &len, sizeof(out), (const uint8_t*)"a!bc", 4)) {
103 fprintf(stderr, "Failed to reject invalid characters in the middle.\n");
107 if (EVP_DecodeBase64(out, &len, sizeof(out), (const uint8_t*)"a=bc", 4)) {
108 fprintf(stderr, "Failed to reject invalid characters in the middle.\n");
112 if (EVP_DecodeBase64(out, &len, sizeof(out), (const uint8_t*)"abc", 4)) {
113 fprintf(stderr, "Failed to reject invalid input length.\n");
121 CRYPTO_library_init();
122 ERR_load_crypto_strings();
124 if (!test_encode()) {
128 if (!test_decode()) {