Skip to content

Commit

Permalink
CI test to confirm fd_frank_quic is working
Browse files Browse the repository at this point in the history
  • Loading branch information
llamb-jump committed Aug 14, 2023
1 parent 7ee1e31 commit 06b4917
Show file tree
Hide file tree
Showing 11 changed files with 518 additions and 0 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/make_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,21 @@ jobs:

- name: Run unit tests
run: make -k -j2 --output-sync=target run-unit-test

frank-single-transaction:
runs-on: [self-hosted, Linux, X64]
strategy:
matrix:
compiler: [gcc]
defaults:
run:
shell: bash
steps:
- uses: actions/checkout@v3
with:
submodules: recursive

- uses: dtolnay/[email protected]

- name: Run
run: ./src/test/frank-single-transaction.sh
3 changes: 3 additions & 0 deletions src/ballet/base64/Local.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
$(call add-hdrs,fd_base64.h)
$(call add-objs,fd_base64,fd_ballet)
$(call make-unit-test,test_base64,test_base64,fd_ballet fd_util)
84 changes: 84 additions & 0 deletions src/ballet/base64/fd_base64.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#include "fd_base64.h"

/* Function to get the index of a character in the Base64 alphabet */
static inline int
base64_decode_char( char c ) {
if( c >= 'A' && c <= 'Z' ) return c - 'A';
if( c >= 'a' && c <= 'z' ) return c - 'a' + 26;
if( c >= '0' && c <= '9' ) return c - '0' + 52;
if( c == '+' ) return 62;
if( c == '/' ) return 63;
return -1; // Invalid character
}

/* Function to decode a base64 encoded string into an unsigned char array
The function returns the length of the decoded array */
int
fd_base64_decode( const char * encoded,
uchar * decoded ) {
int len = 0;
int bits_collected = 0;
uint accumulator = 0;

while ( *encoded ) {
char c = *encoded++;
int value = base64_decode_char(c);

if( value >= 0 ) {
accumulator = ( accumulator << 6 ) | ( uint ) value;
bits_collected += 6;

if( bits_collected >= 8 ) {
bits_collected -= 8;
decoded[ len++ ] = ( uchar )( accumulator >> bits_collected );
accumulator &= ( 1U << bits_collected ) - 1;
}
} else if( c == '=' ) {
/* Padding character, ignore and break the loop */
break;
} else {
/* Fail with invalid characters (e.g., whitespace, padding) */
return -1;
}
}

return len;
}

ulong
fd_base64_encode( const uchar * data,
int data_len,
char * encoded ) {
static const char base64_alphabet[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

uint encoded_len = 0;
uint accumulator = 0;
int bits_collected = 0;

while( data_len-- ) {
accumulator = ( accumulator << 8 ) | *data++;
bits_collected += 8;

while( bits_collected >= 6 ) {
encoded[ encoded_len++ ] = base64_alphabet[ ( accumulator >> ( bits_collected - 6) ) & 0x3F ];
bits_collected -= 6;
}
}

if( bits_collected > 0 ) {
// If there are remaining bits, pad the last Base64 character with zeroes
accumulator <<= 6 - bits_collected;
encoded[ encoded_len++ ] = base64_alphabet[accumulator & 0x3F ];
}

// Add padding characters if necessary
while( encoded_len % 4 != 0 ) {
encoded[ encoded_len++ ] = '=';
}

// Null-terminate the encoded string
encoded[ encoded_len ] = '\0';

return encoded_len;
}
15 changes: 15 additions & 0 deletions src/ballet/base64/fd_base64.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#ifndef HEADER_fd_src_ballet_base64_fd_base64_h
#define HEADER_fd_src_ballet_base64_fd_base64_h

/* fd_base64.h provides methods for converting between binary and base64. */
#include "../fd_ballet_base.h"
int
fd_base64_decode( const char * encoded,
uchar * decoded );

ulong
fd_base64_encode( const uchar * data,
int data_len,
char * encoded );

#endif /* HEADER_fd_src_ballet_base64_fd_base64_h */
49 changes: 49 additions & 0 deletions src/ballet/base64/test_base64.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include "fd_base64.h"
#include <string.h>
#include <stdio.h>

void
decode_test( void ) {
const char * expected_string = "Hello World!";
const char * encoded_string = "SGVsbG8gV29ybGQh";
uchar decoded[ 100 ]; /* Assuming the decoded data won't exceed 100 bytes */

int decoded_length = fd_base64_decode( encoded_string, decoded );

FD_TEST( (uint)decoded_length == strlen( expected_string ) );
FD_TEST( memcmp( decoded, expected_string, strlen(expected_string) ) == 0 );
}

void
decode_test_equals( void ) {
const char * encoded_string = "AZCML352XGjOwgIwMGsRf8oa2IoWzSvgWlJwcAEtLtwk3/h2VIe7n+YbPrAwpbIiK3KOM/G4XiNAKyhHbn2VBQ0BAAEGUn3G2+sjJ+xarkiI77ZYW6CEGHzEjzovKWoUG3/TSKAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACdPJdmqIA5PfVdI4dCMAMKH7z7U0fpkodPhLfE54yrfhMa9sJylZdraDb38lv6aISwi7GkOXsRZ8PQnKkkbFarB08LBYexRVX/v3EVfNeQk9z+WMTKqR0tc/WtXBjQP+v5pDHP1tYUMPTA8WZARvn8XTCjSs+9iPlPPYBWQrEspMZwcluFwA/afVpAczCo7+IJMw5/a0W/kR2EsJRNuF3IBBQUCAwAEAYgEJgW58JTT/VUQEAERERERARABAREBEBERERERABEBEQAQABARAAAREQEREBAQEBERERABERABEBAREBEQEQARAAERABARAQEQABABABAQAQABABEBAREQAAARAAAREREREAERARAAAREQEBAAEAAAEAAQEBEAEREAAAEQEBERAQAAAQEAABABABAQAAEBAAAQEBEAERAQEAAQAREBEQAREREAARAAAREAEAAAAREBEQEQEAAAEQEBEREREBABAQEQAQEQAAEQAREAERAAEAEQARABEBAAAQAQEAABEQEBEAAAEAABEBAAEAEAAQAQEBEQAQABABAREREBAQEAABEBARAAEAABEBAQEREAEBAREQEAAAEBEAEREREQARERAQEQAAEBEQEQARABEBAQAAAAEBEBAQEREQEQEQEAEQAQEBERARAQAQEBABAAAAEAERABAAAAAAEBEAEAAREREBEQEQARAREBEBEREAABABEAEBEBERAQABAAEQEAEAAQABEBEAAAABABAAABEAAAEQAQAQEBAQARAAEREAAAAQAQAREAAQAQAAEQEAEQEAAAARABEAEREBEBAQAQAQEREQEAAAEBAAAQEQABEAABEAEBEBAQABEREAAAABEBAAAQEQEQAAARERAQABERERABEAARABABEBAQAQEAEAARARERABABAREQEBAAAA==";
uchar decoded[ 1500 ]; /* Assuming the decoded data won't exceed 1500 bytes */

int decoded_length = fd_base64_decode( encoded_string, decoded );

FD_TEST( decoded_length != -1 );
}

void encode_test( void ) {
uchar binary_data[] = {72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33}; /* "Hello World!" */
const char * expected_string = "SGVsbG8gV29ybGQh";

char encoded[100]; /* Assuming the encoded data won't exceed 100 characters */

ulong encoded_length = fd_base64_encode(binary_data, sizeof(binary_data), encoded);
printf("%lu %ld\n", sizeof(binary_data), strlen((char*)binary_data));

FD_TEST( (uint)encoded_length == strlen( expected_string ) );
FD_TEST( memcmp( encoded, expected_string, strlen(encoded) ) == 0 );
}

int
main( int argc,
char ** argv ) {
fd_boot( &argc, &argv );
decode_test();
decode_test_equals();
encode_test();
fd_halt();
return 0;
}
1 change: 1 addition & 0 deletions src/tango/quic/tests/Local.mk
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ $(call make-unit-test,test_quic_streams,test_quic_streams,fd_aio fd_ballet fd_ta
$(call make-unit-test,test_quic_conn,test_quic_conn,fd_aio fd_quic fd_ballet fd_tango fd_util)
$(call make-unit-test,test_quic_server,test_quic_server,fd_aio fd_ballet fd_quic fd_tango fd_util)
$(call make-unit-test,test_quic_client_flood,test_quic_client_flood,fd_aio fd_quic fd_ballet fd_tango fd_util)
$(call make-unit-test,test_quic_txn,test_quic_txn,fd_aio fd_quic fd_ballet fd_tango fd_util)
$(call make-unit-test,test_quic_bw,test_quic_bw,fd_aio fd_quic fd_ballet fd_tango fd_util)
$(call make-unit-test,test_quic_handshake,test_handshake,fd_aio fd_ballet fd_quic fd_util)
$(call make-unit-test,test_quic_crypto,test_crypto,fd_quic fd_ballet fd_util)
Expand Down
72 changes: 72 additions & 0 deletions src/tango/quic/tests/fd_quic_test_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,77 @@ fd_quic_test_keylog( fd_quic_virtual_pair_t const * pair,
fd_pcapng_fwrite_tls_key_log( (uchar const *)line, (uint)strlen( line ), pair->quic_a2b.pcapng );
}

fd_quic_udpsock_t *
fd_quic_client_create_udpsock(fd_quic_udpsock_t * udpsock,
fd_wksp_t * wksp,
fd_aio_t const * rx_aio,
uint listen_ip) {
ulong mtu = 2048UL;
ulong rx_depth = 1024UL;
ulong tx_depth = 1024UL;

int sock_fd = socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP );
if( FD_UNLIKELY( sock_fd<0 ) ) {
FD_LOG_WARNING(( "socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP) failed (%d-%s)",
errno, strerror( errno ) ));
return NULL;
}

struct sockaddr_in listen_addr = {
.sin_family = AF_INET,
.sin_addr = { .s_addr = listen_ip },
.sin_port = 0,
};
if( FD_UNLIKELY( 0!=bind( sock_fd, (struct sockaddr const *)fd_type_pun_const( &listen_addr ), sizeof(struct sockaddr_in) ) ) ) {
FD_LOG_WARNING(( "bind(sock_fd) failed (%d-%s)",
errno, strerror( errno ) ));
close( sock_fd );
return NULL;
}

void * sock_mem = fd_wksp_alloc_laddr( wksp, fd_udpsock_align(),
fd_udpsock_footprint( mtu, rx_depth, tx_depth ),
1UL );
if( FD_UNLIKELY( !sock_mem ) ) {
FD_LOG_WARNING(( "fd_wksp_alloc_laddr() failed" ));
close( sock_fd );
return NULL;
}

fd_udpsock_t * sock = fd_udpsock_join( fd_udpsock_new( sock_mem, mtu, rx_depth, tx_depth ), sock_fd );
if( FD_UNLIKELY( !sock ) ) {
FD_LOG_WARNING(( "fd_udpsock_join() failed" ));
close( sock_fd );
fd_wksp_free_laddr( sock_mem );
return NULL;
}

udpsock->type = FD_QUIC_UDPSOCK_TYPE_UDPSOCK;
udpsock->wksp = wksp;
udpsock->udpsock.sock = sock;
udpsock->udpsock.sock_fd = sock_fd;
udpsock->aio = fd_udpsock_get_tx( sock );
udpsock->listen_ip = fd_udpsock_get_ip4_address( sock );
udpsock->listen_port = (ushort)fd_udpsock_get_listen_port( sock );
fd_udpsock_set_rx( sock, rx_aio );

FD_LOG_NOTICE(( "UDP socket listening on " FD_IP4_ADDR_FMT ":%u",
FD_IP4_ADDR_FMT_ARGS( udpsock->listen_ip ), udpsock->listen_port ));
return udpsock;
}

// TODO: LML complete this thought?
fd_quic_udpsock_t *
create_udp_socket(fd_quic_udpsock_t * udpsock) {
if( FD_UNLIKELY( !fd_cstr_to_ip4_addr("0.0.0.0", &udpsock->listen_ip ) ) ) {
goto error_1;
}
udpsock->listen_port = 0; // TODO: check this where is it set in flood?
error_1:
FD_LOG_NOTICE(( "invalid --listen-ip" ));
return NULL;
}

fd_quic_udpsock_t *
fd_quic_udpsock_create( void * _sock,
int * pargc,
Expand Down Expand Up @@ -255,6 +326,7 @@ fd_quic_udpsock_create( void * _sock,
quic_sock->listen_port = listen_port;

int is_xsk = (!!xdp_app_name);
FD_LOG_NOTICE(( "is_xsk %d", is_xsk ));
if( is_xsk ) {
FD_TEST( _src_mac );
if( FD_UNLIKELY( !fd_cstr_to_mac_addr( _src_mac, quic_sock->self_mac ) ) ) FD_LOG_ERR(( "invalid --src-mac" ));
Expand Down
6 changes: 6 additions & 0 deletions src/tango/quic/tests/fd_quic_test_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ typedef struct fd_quic_udpsock fd_quic_udpsock_t;

FD_PROTOTYPES_BEGIN

fd_quic_udpsock_t *
fd_quic_client_create_udpsock(fd_quic_udpsock_t * udpsock,
fd_wksp_t * wksp,
fd_aio_t const * rx_aio,
uint listen_ip);

fd_quic_udpsock_t *
fd_quic_udpsock_create( void * _sock,
int * argc,
Expand Down
Binary file added src/tango/quic/tests/quic_txn.bin
Binary file not shown.
Loading

0 comments on commit 06b4917

Please sign in to comment.