LCOV - code coverage report
Current view: top level - waltz/tls - test_tls_helper.h (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 63 70 90.0 %
Date: 2024-11-13 11:58:15 Functions: 14 189 7.4 %

          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        6018 :                        uchar const * payload ) {
      48        6018 :   fd_tls_test_sign_ctx_t * ctx = (fd_tls_test_sign_ctx_t *)_ctx;
      49        6018 :   fd_ed25519_sign( signature, payload, 130UL, ctx->public_key, ctx->private_key, ctx->sha512 );
      50        6018 : }
      51             : 
      52             : static FD_FN_UNUSED fd_tls_test_sign_ctx_t
      53        2142 : fd_tls_test_sign_ctx( fd_rng_t * rng ) {
      54        2142 :   fd_tls_test_sign_ctx_t ctx[1];
      55        2142 :   FD_TEST( fd_sha512_join( fd_sha512_new( ctx->sha512 ) ) );
      56       70686 :   for( ulong b=0; b<32UL; b++ ) ctx->private_key[b] = fd_rng_uchar( rng );
      57        2142 :   fd_ed25519_public_from_private( ctx->public_key, ctx->private_key, ctx->sha512 );
      58             : 
      59        2142 :   return *ctx;
      60        2142 : }
      61             : 
      62             : 
      63             : static FD_FN_UNUSED fd_tls_sign_t
      64          21 : fd_tls_test_sign( void * ctx ) {
      65          21 :   return (fd_tls_sign_t) {
      66          21 :     .ctx     = ctx,
      67          21 :     .sign_fn = fd_tls_test_sign_sign
      68          21 :   };
      69          21 : }
      70             : 
      71             : /* Test record transport */
      72             : 
      73             : #define TEST_RECORD_BUFSZ (1024UL)
      74             : struct test_record {
      75             :   uint  level;
      76             :   uchar buf[ TEST_RECORD_BUFSZ ];
      77             :   ulong cur;
      78             : };
      79             : 
      80             : typedef struct test_record test_record_t;
      81             : 
      82          24 : #define TEST_RECORD_BUF_CNT (8UL)
      83             : struct test_record_buf {
      84             :   test_record_t records[ TEST_RECORD_BUF_CNT ];
      85             :   ulong         recv;
      86             :   ulong         send;
      87             : };
      88             : 
      89             : typedef struct test_record_buf test_record_buf_t;
      90             : 
      91             : static FD_FN_UNUSED void
      92           0 : test_record_reset( test_record_buf_t * buf ) {
      93           0 :   buf->recv = buf->send = 0UL;
      94           0 : }
      95             : 
      96             : static FD_FN_UNUSED void
      97             : test_record_send( test_record_buf_t * buf,
      98             :                   uint                level,
      99             :                   uchar const *       record,
     100          24 :                   ulong               record_sz ) {
     101          24 :   test_record_t * r = &buf->records[ (buf->send++ % TEST_RECORD_BUF_CNT) ];
     102          24 :   r->level = level;
     103          24 :   r->cur   = record_sz;
     104          24 :   FD_TEST( record_sz<=TEST_RECORD_BUFSZ );
     105          24 :   fd_memcpy( r->buf, record, record_sz );
     106          24 : }
     107             : 
     108             : static FD_FN_UNUSED test_record_t *
     109          30 : test_record_recv( test_record_buf_t * buf ) {
     110          30 :   if( buf->recv==buf->send ) return NULL;
     111          21 :   return &buf->records[ buf->recv++ ];
     112          30 : }
     113             : 
     114             : static FD_FN_UNUSED void
     115             : test_record_log( uchar const * record,
     116             :                  ulong         record_sz,
     117          24 :                  int           from_server ) {
     118             : 
     119          24 :   FD_TEST( record_sz>=4UL );
     120             : 
     121          24 :   char buf[ 512UL ];
     122          24 :   char * str = fd_cstr_init( buf );
     123             : 
     124          24 :   char const * prefix = from_server ? "server" : "client";
     125          24 :          str = fd_cstr_append_cstr( str, prefix );
     126          24 :          str = fd_cstr_append_cstr( str, ": " );
     127             : 
     128          24 :   char const * type = NULL;
     129          24 :   switch( *(uchar const *)record ) {
     130           6 :   case FD_TLS_MSG_CLIENT_HELLO:       type = "ClientHello";         break;
     131           3 :   case FD_TLS_MSG_SERVER_HELLO:       type = "ServerHello";         break;
     132           3 :   case FD_TLS_MSG_ENCRYPTED_EXT:      type = "EncryptedExtensions"; break;
     133           3 :   case FD_TLS_MSG_CERT:               type = "Certificate";         break;
     134           3 :   case FD_TLS_MSG_CERT_VERIFY:        type = "CertificateVerify";   break;
     135           0 :   case FD_TLS_MSG_CERT_REQ:           type = "CertificateRequest";  break;
     136           6 :   case FD_TLS_MSG_FINISHED:           type = "Finished";            break;
     137           0 :   case FD_TLS_MSG_NEW_SESSION_TICKET: type = "NewSessionTicket";    break;
     138           0 :   default:
     139           0 :     FD_LOG_ERR(( "unknown TLS message type %u", *(uchar const *)record ));
     140          24 :   }
     141          24 :   str = fd_cstr_append_cstr( str, type );
     142          24 :   fd_cstr_fini( str );
     143             : 
     144          24 :   FD_LOG_HEXDUMP_INFO(( buf, record, record_sz ));
     145          24 : }
     146             : 
     147             : #endif /* HEADER_fd_src_waltz_tls_test_tls_helper_h */

Generated by: LCOV version 1.14