cosmopolitan/third_party/mbedtls/test/lib.h
Justine Tunney cc1920749e Add SSL to redbean
Your redbean can now interoperate with clients that require TLS crypto.
This is accomplished using a protocol polyglot that lets us distinguish
between HTTP and HTTPS regardless of the port number. Certificates will
be generated automatically, if none are supplied by the user. Footprint
increases by only a few hundred kb so redbean in MODY=tiny is now 1.0mb

- Add lseek() polyfills for ZIP executable
- Automatically polyfill /tmp/FOO paths on NT
- Fix readdir() / ftw() / nftw() bugs on Windows
- Introduce -B flag for slower SSL that's stronger
- Remove mbedtls features Cosmopolitan doesn't need
- Have base64 decoder support the uri-safe alternative
- Remove Truncated HMAC because it's forbidden by the IETF
- Add all the mbedtls test suites and make them go 3x faster
- Support opendir() / readdir() / closedir() on ZIP executable
- Use Everest for ECDHE-ECDSA because it's so good it's so good
- Add tinier implementation of sha1 since it's not worth the rom
- Add chi-square monte-carlo mean correlation tests for getrandom()
- Source entropy on Windows from the proper interface everyone uses

We're continuing to outperform NGINX and other servers on raw message
throughput. Using SSL means that instead of 1,000,000 qps you can get
around 300,000 qps. However redbean isn't as fast as NGINX yet at SSL
handshakes, since redbean can do 2,627 per second and NGINX does 4.3k

Right now, the SSL UX story works best if you give your redbean a key
signing key since that can be easily generated by openssl using a one
liner then redbean will do all the things that are impossibly hard to
do like signing ecdsa and rsa certificates that'll work in chrome. We
should integrate the let's encrypt acme protocol in the future.

Live Demo: https://redbean.justine.lol/
Root Cert: https://redbean.justine.lol/redbean1.crt
2021-06-24 13:20:50 -07:00

230 lines
8.7 KiB
C

#ifndef COSMOPOLITAN_THIRD_PARTY_MBEDTLS_TEST_LIB_H_
#define COSMOPOLITAN_THIRD_PARTY_MBEDTLS_TEST_LIB_H_
#include "third_party/mbedtls/config.h"
#include "third_party/mbedtls/platform.h"
#define DEPENDENCY_SUPPORTED 0
#define KEY_VALUE_MAPPING_FOUND 0
#define DISPATCH_TEST_SUCCESS 0
#define KEY_VALUE_MAPPING_NOT_FOUND -1
#define DEPENDENCY_NOT_SUPPORTED -2
#define DISPATCH_TEST_FN_NOT_FOUND -3
#define DISPATCH_INVALID_TEST_DATA -4
#define DISPATCH_UNSUPPORTED_SUITE -5
#define PSA_ECC_FAMILY_SECP_R1 0x12
#define PSA_ECC_FAMILY_SECP_K1 0x17
#define PSA_ECC_FAMILY_BRAINPOOL_P_R1 0x30
#if defined(MBEDTLS_TEST_CONSTANT_FLOW_MEMSAN)
#include <sanitizer/msan_interface.h>
#define TEST_CF_SECRET __msan_allocated_memory
#define TEST_CF_PUBLIC __msan_unpoison
#elif defined(MBEDTLS_TEST_CONSTANT_FLOW_VALGRIND)
#include <valgrind/memcheck.h>
#define TEST_CF_SECRET VALGRIND_MAKE_MEM_UNDEFINED
#define TEST_CF_PUBLIC VALGRIND_MAKE_MEM_DEFINED
#else
#define TEST_CF_SECRET(ptr, size)
#define TEST_CF_PUBLIC(ptr, size)
#endif
#if defined(MBEDTLS_TEST_NULL_ENTROPY) || \
(!defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) && \
(!defined(MBEDTLS_NO_PLATFORM_ENTROPY) || defined(MBEDTLS_HAVEGE_C) || \
defined(MBEDTLS_ENTROPY_HARDWARE_ALT) || defined(ENTROPY_NV_SEED)))
#define ENTROPY_HAVE_STRONG
#else
#error oh no entropy
#endif
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
#define WRITE(...) AppendFmt(&output, __VA_ARGS__)
#define TEST_ASSERT(TEST) \
do { \
if (!(TEST)) { \
mbedtls_test_fail(#TEST, __LINE__, __FILE__); \
goto exit; \
} \
} while (0)
#define TEST_EQUAL(expr1, expr2) TEST_ASSERT((expr1) == (expr2))
#define ASSERT_ALLOC(pointer, length) \
do { \
TEST_ASSERT((pointer) == NULL); \
if ((length) != 0) { \
(pointer) = mbedtls_calloc(sizeof(*(pointer)), (length)); \
TEST_ASSERT((pointer) != NULL); \
} \
} while (0)
#define ASSERT_ALLOC_WEAK(pointer, length) \
do { \
TEST_ASSERT((pointer) == NULL); \
if ((length) != 0) { \
(pointer) = mbedtls_calloc(sizeof(*(pointer)), (length)); \
TEST_ASSUME((pointer) != NULL); \
} \
} while (0)
#define ASSERT_COMPARE(p1, size1, p2, size2) \
do { \
TEST_ASSERT((size1) == (size2)); \
if ((size1) != 0) TEST_ASSERT(memcmp((p1), (p2), (size1)) == 0); \
} while (0)
#define TEST_ASSUME(TEST) \
do { \
if (!(TEST)) { \
mbedtls_test_skip(#TEST, __LINE__, __FILE__); \
goto exit; \
} \
} while (0)
#define TEST_INVALID_PARAM_RET(PARAM_ERR_VALUE, TEST) \
do { \
mbedtls_test_param_failed_expect_call(); \
if (((TEST) != (PARAM_ERR_VALUE)) || \
(mbedtls_test_param_failed_check_expected_call() != 0)) { \
mbedtls_test_fail(#TEST, __LINE__, __FILE__); \
goto exit; \
} \
mbedtls_test_param_failed_check_expected_call(); \
} while (0)
#define TEST_INVALID_PARAM(TEST) \
do { \
memcpy(jmp_tmp, mbedtls_test_param_failed_get_state_buf(), \
sizeof(jmp_tmp)); \
if (setjmp(mbedtls_test_param_failed_get_state_buf()) == 0) { \
TEST; \
mbedtls_test_fail(#TEST, __LINE__, __FILE__); \
goto exit; \
} \
mbedtls_test_param_failed_reset_state(); \
} while (0)
#define TEST_VALID_PARAM(TEST) TEST_ASSERT((TEST, 1));
#define ASSERT_ALLOC(pointer, length) \
do { \
TEST_ASSERT((pointer) == NULL); \
if ((length) != 0) { \
(pointer) = mbedtls_calloc(sizeof(*(pointer)), (length)); \
TEST_ASSERT((pointer) != NULL); \
} \
} while (0)
#define TEST_HELPER_ASSERT(a) \
if (!(a)) { \
WRITE("%s:%d: assertion failed: %s\n", __FILE__, __LINE__, #a); \
exit(1); \
}
#define ARRAY_LENGTH_UNSAFE(array) (sizeof(array) / sizeof(*(array)))
#ifdef __GNUC__
#define IS_ARRAY_NOT_POINTER(arg) \
(!__builtin_types_compatible_p(__typeof__(arg), __typeof__(&(arg)[0])))
#define STATIC_ASSERT_EXPR(const_expr) \
(0 && sizeof(struct { unsigned int STATIC_ASSERT : 1 - 2 * !(const_expr); }))
#define STATIC_ASSERT_THEN_RETURN(condition, value) \
(STATIC_ASSERT_EXPR(condition) ? 0 : (value))
#define ARRAY_LENGTH(array) \
(STATIC_ASSERT_THEN_RETURN(IS_ARRAY_NOT_POINTER(array), \
ARRAY_LENGTH_UNSAFE(array)))
#else
#define ARRAY_LENGTH(array) ARRAY_LENGTH_UNSAFE(array)
#endif
#define USE_PSA_INIT() ((void)0)
#define USE_PSA_DONE() ((void)0)
typedef enum {
MBEDTLS_TEST_RESULT_SUCCESS = 0,
MBEDTLS_TEST_RESULT_FAILED,
MBEDTLS_TEST_RESULT_SKIPPED
} mbedtls_test_result_t;
typedef struct {
mbedtls_test_result_t result;
const char *test;
const char *filename;
int line_no;
unsigned long step;
} mbedtls_test_info_t;
extern mbedtls_test_info_t mbedtls_test_info;
typedef struct {
const char *failure_condition;
const char *file;
int line;
} mbedtls_test_param_failed_location_record_t;
typedef struct {
unsigned char *buf;
size_t length;
} mbedtls_test_rnd_buf_info;
typedef struct data_tag {
uint8_t *x;
uint32_t len;
} data_t;
/**
* Info structure for the pseudo random function
*
* Key should be set at the start to a test-unique value.
* Do not forget endianness!
* State( v0, v1 ) should be set to zero.
*/
typedef struct {
uint32_t key[16];
uint32_t v0, v1;
} mbedtls_test_rnd_pseudo_info;
struct Buffer {
size_t i, n;
char *p;
};
extern jmp_buf jmp_tmp;
extern struct Buffer output;
int mbedtls_test_platform_setup(void);
void mbedtls_test_platform_teardown(void);
int mbedtls_test_hexcmp(uint8_t *, uint8_t *, uint32_t, uint32_t);
void mbedtls_test_fail(const char *, int, const char *);
void mbedtls_test_skip(const char *, int, const char *);
void mbedtls_test_set_step(unsigned long);
void mbedtls_test_info_reset(void);
int mbedtls_test_unhexify(unsigned char *, size_t, const char *, size_t *);
void mbedtls_test_hexify(unsigned char *, const unsigned char *, int);
unsigned char *mbedtls_test_zero_alloc(size_t);
unsigned char *mbedtls_test_unhexify_alloc(const char *, size_t *);
void mbedtls_test_param_failed_get_location_record(
mbedtls_test_param_failed_location_record_t *);
void mbedtls_test_param_failed_expect_call(void);
int mbedtls_test_param_failed_check_expected_call(void);
void *mbedtls_test_param_failed_get_state_buf(void);
void mbedtls_test_param_failed_reset_state(void);
int mbedtls_test_rnd_std_rand(void *, unsigned char *, size_t);
int mbedtls_test_rnd_zero_rand(void *, unsigned char *, size_t);
int mbedtls_test_rnd_buffer_rand(void *, unsigned char *, size_t);
int mbedtls_test_rnd_pseudo_rand(void *, unsigned char *, size_t);
int execute_tests(int, const char **, const char *);
int get_expression(int32_t, int32_t *);
int dispatch_test(size_t, void **);
int dep_check(int);
int check_test(size_t);
int AppendFmt(struct Buffer *, const char *, ...);
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_THIRD_PARTY_MBEDTLS_TEST_LIB_H_ */