1 /**
2 * FreeRDP: A Remote Desktop Protocol Implementation
3 *
4 * Copyright 2014 Thincast Technologies GmbH
5 * Copyright 2014 Hardening <contact@hardening-consulting.com>
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20 #include <freerdp/crypto/crypto.h>
21
22 struct Encode64test
23 {
24 const char* input;
25 int len;
26 const char* output;
27 };
28
29 static const struct Encode64test encodeTests[] = {
30 { "\x00", 1, "AA==" },
31 { "\x00\x00", 2, "AAA=" },
32 { "\x00\x00\x00", 3, "AAAA" },
33 { "0123456", 7, "MDEyMzQ1Ng==" },
34 { "90123456", 8, "OTAxMjM0NTY=" },
35 { "890123456", 9, "ODkwMTIzNDU2" },
36 { "7890123456", 10, "Nzg5MDEyMzQ1Ng==" },
37
38 { NULL, -1, NULL }, /* /!\ last one /!\ */
39 };
40
TestBase64(int argc,char * argv[])41 int TestBase64(int argc, char* argv[])
42 {
43 int i, testNb = 0;
44 int outLen;
45 BYTE* decoded;
46 WINPR_UNUSED(argc);
47 WINPR_UNUSED(argv);
48 testNb++;
49 fprintf(stderr, "%d:encode base64...", testNb);
50
51 for (i = 0; encodeTests[i].input; i++)
52 {
53 char* encoded = crypto_base64_encode((const BYTE*)encodeTests[i].input, encodeTests[i].len);
54
55 if (strcmp(encodeTests[i].output, encoded))
56 {
57 fprintf(stderr, "ko, error for string %d\n", i);
58 return -1;
59 }
60
61 free(encoded);
62 }
63
64 fprintf(stderr, "ok\n");
65 testNb++;
66 fprintf(stderr, "%d:decode base64...", testNb);
67
68 for (i = 0; encodeTests[i].input; i++)
69 {
70 crypto_base64_decode(encodeTests[i].output, strlen(encodeTests[i].output), &decoded,
71 &outLen);
72
73 if (!decoded || (outLen != encodeTests[i].len) ||
74 memcmp(encodeTests[i].input, decoded, outLen))
75 {
76 fprintf(stderr, "ko, error for string %d\n", i);
77 return -1;
78 }
79
80 free(decoded);
81 }
82
83 fprintf(stderr, "ok\n");
84 testNb++;
85 fprintf(stderr, "%d:decode base64 errors...", testNb);
86 crypto_base64_decode("000", 3, &decoded, &outLen);
87
88 if (decoded)
89 {
90 fprintf(stderr, "ko, badly padded string\n");
91 return -1;
92 }
93
94 crypto_base64_decode("0=00", 4, &decoded, &outLen);
95
96 if (decoded)
97 {
98 fprintf(stderr, "ko, = in a wrong place\n");
99 return -1;
100 }
101
102 crypto_base64_decode("00=0", 4, &decoded, &outLen);
103
104 if (decoded)
105 {
106 fprintf(stderr, "ko, = in a wrong place\n");
107 return -1;
108 }
109
110 fprintf(stderr, "ok\n");
111 return 0;
112 }
113