LCOV - code coverage report
Current view: top level - waltz/tls - test_tls_helper.h (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 61 68 89.7 %
Date: 2025-01-08 12:08:44 Functions: 14 171 8.2 %

          Line data    Source code
       1             : #ifndef HEADER_fd_src_waltz_tls_test_tls_helper_h
       2             : #define HEADER_fd_src_waltz_tls_test_tls_helper_h
       3             : 
       4             : #include "fd_tls.h"
       5             : #include "fd_tls_proto.h"
       6             : #include "../../ballet/sha512/fd_sha512.h"
       7             : #include "../../ballet/ed25519/fd_ed25519.h"
       8             : 
       9             : /* Common routines for fd_tls unit tests */
      10             : 
      11             : /* fd_tls_test_rand creates an fd_tls provider from an fd_rng_t.
      12             :    This is a deliberately insecure, deterministic RNG inteded for tests. */
      13             : 
      14             : static void *
      15             : fd_tls_test_rand_read( void * ctx,
      16             :                        void * buf,
      17           9 :                        ulong  bufsz ) {
      18             : 
      19           9 :   if( FD_UNLIKELY( !ctx ) ) return NULL;
      20             : 
      21           9 :   fd_rng_t * rng  = (fd_rng_t *)ctx;
      22           9 :   uchar *    buf_ = (uchar *)buf;
      23         297 :   for( ulong i=0UL; i<bufsz; i++ )
      24         288 :     buf_[i] = (uchar)fd_rng_uchar( rng );
      25           9 :   return buf_;
      26           9 : }
      27             : 
      28             : static FD_FN_UNUSED fd_tls_rand_t
      29          18 : fd_tls_test_rand( fd_rng_t * rng ) {
      30          18 :   return (fd_tls_rand_t) {
      31          18 :     .ctx     = rng,
      32          18 :     .rand_fn = fd_tls_test_rand_read
      33          18 :   };
      34          18 : }
      35             : 
      36             : struct fd_tls_test_sign_ctx {
      37             :   fd_sha512_t sha512[ 1 ];
      38             : 
      39             :   uchar public_key[ 32UL ];
      40             :   uchar private_key[ 32UL ];
      41             : };
      42             : typedef struct fd_tls_test_sign_ctx fd_tls_test_sign_ctx_t;
      43             : 
      44             : static void
      45             : fd_tls_test_sign_sign( void *        _ctx,
      46             :                        uchar *       signature,
      47        6021 :                        uchar const * payload ) {
      48        6021 :   fd_tls_test_sign_ctx_t * ctx = (fd_tls_test_sign_ctx_t *)_ctx;
      49        6021 :   fd_ed25519_sign( signature, payload, 130UL, ctx->public_key, ctx->private_key, ctx->sha512 );
      50        6021 : }
      51             : 
      52             : static FD_FN_UNUSED void
      53             : fd_tls_test_sign_ctx( fd_tls_test_sign_ctx_t * ctx,
      54        2148 :                       fd_rng_t *               rng ) {
      55        2148 :   FD_TEST( fd_sha512_join( fd_sha512_new( ctx->sha512 ) ) );
      56       70884 :   for( ulong b=0; b<32UL; b++ ) ctx->private_key[b] = fd_rng_uchar( rng );
      57        2148 :   fd_ed25519_public_from_private( ctx->public_key, ctx->private_key, ctx->sha512 );
      58        2148 : }
      59             : 
      60             : 
      61             : static FD_FN_UNUSED fd_tls_sign_t
      62          21 : fd_tls_test_sign( void * ctx ) {
      63          21 :   return (fd_tls_sign_t) {
      64          21 :     .ctx     = ctx,
      65          21 :     .sign_fn = fd_tls_test_sign_sign
      66          21 :   };
      67          21 : }
      68             : 
      69             : /* Test record transport */
      70             : 
      71             : #define TEST_RECORD_BUFSZ (4096UL)
      72             : struct test_record {
      73             :   uint  level;
      74             :   uchar buf[ TEST_RECORD_BUFSZ ];
      75             :   ulong cur;
      76             : };
      77             : 
      78             : typedef struct test_record test_record_t;
      79             : 
      80          24 : #define TEST_RECORD_BUF_CNT (8UL)
      81             : struct test_record_buf {
      82             :   test_record_t records[ TEST_RECORD_BUF_CNT ];
      83             :   ulong         recv;
      84             :   ulong         send;
      85             : };
      86             : 
      87             : typedef struct test_record_buf test_record_buf_t;
      88             : 
      89             : static FD_FN_UNUSED void
      90           0 : test_record_reset( test_record_buf_t * buf ) {
      91           0 :   buf->recv = buf->send = 0UL;
      92           0 : }
      93             : 
      94             : static FD_FN_UNUSED void
      95             : test_record_send( test_record_buf_t * buf,
      96             :                   uint                level,
      97             :                   uchar const *       record,
      98          24 :                   ulong               record_sz ) {
      99          24 :   test_record_t * r = &buf->records[ (buf->send++ % TEST_RECORD_BUF_CNT) ];
     100          24 :   r->level = level;
     101          24 :   r->cur   = record_sz;
     102          24 :   FD_TEST( record_sz<=TEST_RECORD_BUFSZ );
     103          24 :   fd_memcpy( r->buf, record, record_sz );
     104          24 : }
     105             : 
     106             : static FD_FN_UNUSED test_record_t *
     107          30 : test_record_recv( test_record_buf_t * buf ) {
     108          30 :   if( buf->recv==buf->send ) return NULL;
     109          21 :   return &buf->records[ buf->recv++ ];
     110          30 : }
     111             : 
     112             : static FD_FN_UNUSED void
     113             : test_record_log( uchar const * record,
     114             :                  ulong         record_sz,
     115          24 :                  int           from_server ) {
     116             : 
     117          24 :   FD_TEST( record_sz>=4UL );
     118             : 
     119          24 :   char buf[ 512UL ];
     120          24 :   char * str = fd_cstr_init( buf );
     121             : 
     122          24 :   char const * prefix = from_server ? "server" : "client";
     123          24 :          str = fd_cstr_append_cstr( str, prefix );
     124          24 :          str = fd_cstr_append_cstr( str, ": " );
     125             : 
     126          24 :   char const * type = NULL;
     127          24 :   switch( *(uchar const *)record ) {
     128           6 :   case FD_TLS_MSG_CLIENT_HELLO:       type = "ClientHello";         break;
     129           3 :   case FD_TLS_MSG_SERVER_HELLO:       type = "ServerHello";         break;
     130           3 :   case FD_TLS_MSG_ENCRYPTED_EXT:      type = "EncryptedExtensions"; break;
     131           3 :   case FD_TLS_MSG_CERT:               type = "Certificate";         break;
     132           3 :   case FD_TLS_MSG_CERT_VERIFY:        type = "CertificateVerify";   break;
     133           0 :   case FD_TLS_MSG_CERT_REQ:           type = "CertificateRequest";  break;
     134           6 :   case FD_TLS_MSG_FINISHED:           type = "Finished";            break;
     135           0 :   case FD_TLS_MSG_NEW_SESSION_TICKET: type = "NewSessionTicket";    break;
     136           0 :   default:
     137           0 :     FD_LOG_ERR(( "unknown TLS message type %u", *(uchar const *)record ));
     138          24 :   }
     139          24 :   str = fd_cstr_append_cstr( str, type );
     140          24 :   fd_cstr_fini( str );
     141             : 
     142          24 :   FD_LOG_HEXDUMP_INFO(( buf, record, record_sz ));
     143          24 : }
     144             : 
     145             : #endif /* HEADER_fd_src_waltz_tls_test_tls_helper_h */

Generated by: LCOV version 1.14