2022-12-03 22:38:14 +01:00
|
|
|
/* BEGIN_HEADER */
|
2023-02-23 21:54:59 +01:00
|
|
|
|
|
|
|
/* The printf test functions take a format argument from the test data
|
|
|
|
* for several reasons:
|
|
|
|
* - For some tests, it makes sense to vary the format.
|
|
|
|
* - For all tests, it means we're testing the actual printf function
|
|
|
|
* that parses the format at runtime, and not a compiler optimization.
|
|
|
|
* (It may be useful to add tests that allow compiler optimizations.
|
|
|
|
* There aren't any yet at the time of writing.)
|
|
|
|
*/
|
|
|
|
|
2022-12-03 22:38:14 +01:00
|
|
|
#include "mbedtls/platform.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2022-12-03 23:50:05 +01:00
|
|
|
|
|
|
|
#define NEWLINE_CHAR '\n'
|
|
|
|
#define SPACE_CHAR ' '
|
|
|
|
#define DOUBLE_QUOTE_CHAR '"'
|
|
|
|
#define COLON_CHAR ':'
|
2023-04-26 19:50:57 +02:00
|
|
|
#define QUESTION_CHAR '?'
|
2022-12-03 23:50:05 +01:00
|
|
|
#define BACKSLASH_CHAR '\\'
|
|
|
|
#define LOWERCASE_N_CHAR 'n'
|
2022-12-03 22:38:14 +01:00
|
|
|
/* END_HEADER */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2023-02-23 21:54:59 +01:00
|
|
|
void printf_int(char *format, /* any format expecting one int argument, e.g. "%d" */
|
|
|
|
int x, char *result)
|
2022-12-03 22:38:14 +01:00
|
|
|
{
|
|
|
|
char *output = NULL;
|
|
|
|
const size_t n = strlen(result);
|
|
|
|
|
|
|
|
/* Nominal case: buffer just large enough */
|
|
|
|
ASSERT_ALLOC(output, n + 1);
|
|
|
|
TEST_EQUAL(n, mbedtls_snprintf(output, n + 1, format, x));
|
|
|
|
ASSERT_COMPARE(result, n + 1, output, n + 1);
|
|
|
|
mbedtls_free(output);
|
|
|
|
output = NULL;
|
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_free(output);
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
2022-12-03 23:50:05 +01:00
|
|
|
|
2022-12-04 15:57:49 +01:00
|
|
|
/* BEGIN_CASE */
|
2023-02-23 21:54:59 +01:00
|
|
|
void printf_long_max(const char *format, /* "%lx" or longer type */
|
|
|
|
long value)
|
2022-12-04 15:57:49 +01:00
|
|
|
{
|
|
|
|
char *expected = NULL;
|
|
|
|
char *output = NULL;
|
2023-02-23 21:55:59 +01:00
|
|
|
/* 2 hex digits per byte */
|
2022-12-04 15:57:49 +01:00
|
|
|
const size_t n = sizeof(value) * 2;
|
|
|
|
|
|
|
|
/* We assume that long has no padding bits! */
|
|
|
|
ASSERT_ALLOC(expected, n + 1);
|
|
|
|
expected[0] = '7';
|
|
|
|
memset(expected + 1, 'f', sizeof(value) * 2 - 1);
|
|
|
|
|
|
|
|
ASSERT_ALLOC(output, n + 1);
|
|
|
|
TEST_EQUAL(n, mbedtls_snprintf(output, n + 1, format, value));
|
|
|
|
ASSERT_COMPARE(expected, n + 1, output, n + 1);
|
|
|
|
mbedtls_free(output);
|
|
|
|
output = NULL;
|
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_free(output);
|
|
|
|
mbedtls_free(expected);
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2022-12-03 23:50:05 +01:00
|
|
|
/* BEGIN_CASE */
|
2023-02-23 21:54:59 +01:00
|
|
|
void printf_char2(char *format, /* "%c%c" */
|
|
|
|
int arg1, int arg2, char *result)
|
2022-12-03 23:50:05 +01:00
|
|
|
{
|
|
|
|
char *output = NULL;
|
|
|
|
const size_t n = strlen(result);
|
|
|
|
|
|
|
|
/* Nominal case: buffer just large enough */
|
|
|
|
ASSERT_ALLOC(output, n + 1);
|
|
|
|
TEST_EQUAL(n, mbedtls_snprintf(output, n + 1, format, arg1, arg2));
|
|
|
|
ASSERT_COMPARE(result, n + 1, output, n + 1);
|
|
|
|
mbedtls_free(output);
|
|
|
|
output = NULL;
|
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_free(output);
|
|
|
|
}
|
|
|
|
/* END_CASE */
|