Files
dat-thang/tests/test_wire.c
T
divorce b735900f2e 0.0.002
ddat interop with dat js
cable interop with cabal js
ddat's frontend = dat-thang
cable fe = cable-thang
dillo support based on cel's dillo-dat
bootstrap node
xplatform support

Signed-off-by: divo <418@cryspace.space>
2026-10-19 00:17:02 -04:00

12 lines
2.9 KiB
C

#include "dat_wire.h"
#include <assert.h>
#include <string.h>
int main(void){unsigned char buf[256],payload[3]={1,2,3},peer[32]={0},golden[]={5,0x13,0x08,0,0x10,1},want_without_length[]={0x08,0x05};size_t n,used,i;dat_wire_message_t a={7,DAT_MSG_DATA,payload,3},b;dat_handshake_t h,decoded;dat_request_t q={12,4096,1,1},r;dat_range_t range={0,1};dat_info_t info={0,1},decoded_info;dat_extension_t ext={3,payload,3},decoded_ext;assert(dat_range_encode(&range,buf,sizeof(buf),&n)==0);a.channel=1;a.type=DAT_MSG_HAVE;a.payload=buf;a.payload_len=n;assert(dat_wire_encode(&a,buf+32,sizeof(buf)-32,&used)==0);assert(used==sizeof(golden)&&!memcmp(buf+32,golden,sizeof(golden)));assert(dat_wire_decode(&b,buf+32,used,&n)==0&&n==used);assert(b.channel==1&&b.type==DAT_MSG_HAVE);assert(dat_range_decode(&range,b.payload,b.payload_len)==0&&range.start==0&&range.length==1);assert(dat_want_decode(&range,want_without_length,sizeof(want_without_length),12)==0&&range.start==5&&range.length==7);a.channel=7;a.type=DAT_MSG_DATA;a.payload=payload;a.payload_len=3;assert(dat_wire_encode(&a,buf,sizeof(buf),&n)==0);assert(dat_wire_decode(&b,buf,n,&used)==0&&used==n);assert(b.channel==7&&b.type==DAT_MSG_DATA&&b.payload_len==3&&!memcmp(b.payload,payload,3));assert(dat_request_encode(&q,buf,sizeof(buf),&n)==0);assert(dat_request_decode(&r,buf,n)==0);assert(r.index==12&&r.bytes==4096&&r.want_hash&&r.want_nodes);assert(dat_info_encode(&info,buf,sizeof(buf),&n)==0);assert(dat_info_decode(&decoded_info,buf,n)==0&&!decoded_info.uploading&&decoded_info.downloading);assert(dat_extension_encode(&ext,buf,sizeof(buf),&n)==0);assert(dat_extension_decode(&decoded_ext,buf,n)==0&&decoded_ext.id==3&&decoded_ext.message_len==3&&!memcmp(decoded_ext.message,payload,3));assert(dat_handshake_init(&h,peer,7)==0);assert(!memcmp(h.protocol,DAT_NETWORK_PROTOCOL,strlen(DAT_NETWORK_PROTOCOL)));for(i=strlen(DAT_NETWORK_PROTOCOL);i<DAT_PROTOCOL_BYTES;i++)assert(h.protocol[i]==0);assert(dat_handshake_protocol_matches(&h)==1);assert(dat_handshake_encode(&h,buf,sizeof(buf),&n)==0);assert(dat_handshake_decode(&decoded,buf,n)==0);assert(decoded.has_peer_id&&dat_handshake_protocol_matches(&decoded)==1);
#ifndef DAT_LEGACY_HYPERCORE
assert(decoded.capabilities==7);assert(dat_handshake_init(&h,peer,(UINT64_C(1)<<63))==0);assert(dat_handshake_encode(&h,buf,sizeof(buf),&n)==0);assert(dat_handshake_decode(&decoded,buf,n)==0&&decoded.capabilities==(UINT64_C(1)<<63));
#endif
#ifdef DAT_LEGACY_HYPERCORE
assert(dat_handshake_init(&h,peer,0)==0);assert(dat_handshake_encode(&h,buf,sizeof(buf),&n)==0);assert(n==34&&buf[0]==0x0a&&buf[1]==0x20);for(i=2;i<n;i++)assert(buf[i]==0);
#endif
{uint8_t old_handshake[]={0x10,1};assert(dat_handshake_decode(&decoded,old_handshake,sizeof(old_handshake))==0);assert(!decoded.has_peer_id&&dat_handshake_protocol_matches(&decoded)==1);}decoded.protocol[0]^=1;assert(dat_handshake_protocol_matches(&decoded)==0);return 0;}