2014-05-12 00:42:01 +04:00
|
|
|
/**
|
2014-05-22 23:56:47 +04:00
|
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
2014-05-12 00:42:01 +04:00
|
|
|
*
|
2014-05-22 23:56:47 +04:00
|
|
|
* Copyright 2014 Thincast Technologies GmbH
|
|
|
|
* Copyright 2014 Hardening <contact@hardening-consulting.com>
|
2014-05-12 00:42:01 +04:00
|
|
|
*
|
2014-05-22 23:56:47 +04:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2014-05-12 00:42:01 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <freerdp/crypto/crypto.h>
|
|
|
|
|
2019-05-08 13:20:43 +03:00
|
|
|
struct Encode64test
|
|
|
|
{
|
|
|
|
const char* input;
|
2014-05-12 00:42:01 +04:00
|
|
|
int len;
|
2019-05-08 13:20:43 +03:00
|
|
|
const char* output;
|
2014-05-12 00:42:01 +04:00
|
|
|
};
|
|
|
|
|
2019-10-29 12:18:09 +03:00
|
|
|
static const struct Encode64test encodeTests[] =
|
2019-05-08 13:20:43 +03:00
|
|
|
{
|
|
|
|
{"\x00", 1, "AA=="},
|
|
|
|
{"\x00\x00", 2, "AAA="},
|
|
|
|
{"\x00\x00\x00", 3, "AAAA"},
|
|
|
|
{"0123456", 7, "MDEyMzQ1Ng=="},
|
|
|
|
{"90123456", 8, "OTAxMjM0NTY="},
|
|
|
|
{"890123456", 9, "ODkwMTIzNDU2"},
|
|
|
|
{"7890123456", 10, "Nzg5MDEyMzQ1Ng=="},
|
|
|
|
|
|
|
|
{NULL, -1, NULL}, /* /!\ last one /!\ */
|
2014-05-12 00:42:01 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
int TestBase64(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
int i, testNb = 0;
|
|
|
|
int outLen;
|
2019-05-08 13:20:43 +03:00
|
|
|
BYTE* decoded;
|
|
|
|
WINPR_UNUSED(argc);
|
|
|
|
WINPR_UNUSED(argv);
|
2014-05-12 00:42:01 +04:00
|
|
|
testNb++;
|
|
|
|
fprintf(stderr, "%d:encode base64...", testNb);
|
|
|
|
|
|
|
|
for (i = 0; encodeTests[i].input; i++)
|
|
|
|
{
|
2019-05-08 13:20:43 +03:00
|
|
|
char* encoded = crypto_base64_encode((const BYTE*)encodeTests[i].input, encodeTests[i].len);
|
2014-05-12 00:42:01 +04:00
|
|
|
|
|
|
|
if (strcmp(encodeTests[i].output, encoded))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ko, error for string %d\n", i);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(encoded);
|
|
|
|
}
|
|
|
|
|
2019-05-08 13:20:43 +03:00
|
|
|
fprintf(stderr, "ok\n");
|
2014-05-12 00:42:01 +04:00
|
|
|
testNb++;
|
|
|
|
fprintf(stderr, "%d:decode base64...", testNb);
|
2019-05-08 13:20:43 +03:00
|
|
|
|
2014-05-12 00:42:01 +04:00
|
|
|
for (i = 0; encodeTests[i].input; i++)
|
|
|
|
{
|
|
|
|
crypto_base64_decode(encodeTests[i].output, strlen(encodeTests[i].output), &decoded, &outLen);
|
|
|
|
|
|
|
|
if (!decoded || (outLen != encodeTests[i].len) || memcmp(encodeTests[i].input, decoded, outLen))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ko, error for string %d\n", i);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(decoded);
|
|
|
|
}
|
|
|
|
|
2019-05-08 13:20:43 +03:00
|
|
|
fprintf(stderr, "ok\n");
|
2014-05-12 00:42:01 +04:00
|
|
|
testNb++;
|
|
|
|
fprintf(stderr, "%d:decode base64 errors...", testNb);
|
|
|
|
crypto_base64_decode("000", 3, &decoded, &outLen);
|
2019-05-08 13:20:43 +03:00
|
|
|
|
2014-05-12 00:42:01 +04:00
|
|
|
if (decoded)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ko, badly padded string\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
crypto_base64_decode("0=00", 4, &decoded, &outLen);
|
2019-05-08 13:20:43 +03:00
|
|
|
|
2014-05-12 00:42:01 +04:00
|
|
|
if (decoded)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ko, = in a wrong place\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
crypto_base64_decode("00=0", 4, &decoded, &outLen);
|
2019-05-08 13:20:43 +03:00
|
|
|
|
2014-05-12 00:42:01 +04:00
|
|
|
if (decoded)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ko, = in a wrong place\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-05-08 13:20:43 +03:00
|
|
|
fprintf(stderr, "ok\n");
|
2014-05-12 00:42:01 +04:00
|
|
|
return 0;
|
|
|
|
}
|