...
authoralex <[email protected]>
Thu, 31 Mar 2022 04:16:36 +0000 (21:16 -0700)
committeralex <[email protected]>
Thu, 31 Mar 2022 04:16:36 +0000 (21:16 -0700)
src/net/expected.c
src/net/peek.c
test/unit/Makefile.am
test/unit/net.tests.c

index 35a13a8e5b4ace9731890e28ffecbf966aee791a..5916a5fde8f4ea1437cc76bf8aab483b8b18cf18 100644 (file)
@@ -8,6 +8,7 @@ ssize_t net_expected(struct peer *info, enum peer_message *type) {
 
        (*type) = PEER_MESSAGE_KEEPALIVE;
 
+       memset(buf,0,4);
        /* 
         * used to figure out whether 4 or 5 bytes
         * received; 0xff is an invalid message
@@ -16,7 +17,7 @@ ssize_t net_expected(struct peer *info, enum peer_message *type) {
        buf[4] = 0xff;
 
        if(net_peek(info,buf,5)<0) { return -1; }
-       if((info->in!=NULL)&&(info->in_size<4)) {
+       if((info->in==NULL)||(info->in_size<4)) {
                /*
                 * Message should at least have 4 bytes (length prefix of
                 * the following message).
@@ -38,5 +39,8 @@ ssize_t net_expected(struct peer *info, enum peer_message *type) {
                i += buf[j];
        }
 
+       // check whether invalid length/message given
+       if((i==0)&&(buf[4]==0xff)) { return -1; }
+
        return i+4;
 }
index b17db79207547c152321809674012711f60c42da..5617b99a493a854a198604984a025c8e4dd1bf9e 100644 (file)
@@ -2,15 +2,25 @@
 
 int net_peek(struct peer *info, void *p, size_t size) {
        void *next;
+
+       if(NULL==info) { return -1; }
+       if(NULL==p) { return -1; }
+       if(0==size) { return 0; }
+
        next = malloc(size);
        if(NULL==next) { return -1; }
 
        if(net_wait(info,next,size)<0) { return -1; }
        if(info->in!=NULL) {
+               /*
+                * incomplete net_wait; p wasn't filled
+                */
                free(next);
                return 0;
        }
 
+       if(0==info->in_size) { return 0; }
+       
        info->in = next;
        info->in_size = size;
 
index fe93e83baa01e92f51ff4e441cae193c4aebd723..e1ffe4800b55bf514b8933d8d46c577117d4f6f6 100644 (file)
@@ -122,6 +122,7 @@ net_tests_SOURCES = \
        $(top_srcdir)/src/net/cache.c \
        $(top_srcdir)/src/net/expected.c \
        $(top_srcdir)/src/net/handler.c \
+       $(top_srcdir)/src/net/peek.c \
        $(top_srcdir)/src/net/queue.c \
        $(top_srcdir)/src/net/send.c \
        $(top_srcdir)/src/net/wait.c \
index d91570fd252e1cb92c477e6d1847141872c0d883..36bc51b019cba7cced0449a72135316867370573 100644 (file)
@@ -15,21 +15,21 @@ static void net_wait_basic_test();
 
 /* dummy functions (and array) used to test net_handler */
 int message_count[25];
-int peer_bitfield_received(int sock,struct peer *info, ssize_t len) { message_count[PEER_MESSAGE_BITFIELD+1]++; return 0; }
-int peer_cancel_received(int sock,struct peer *info, ssize_t len) { message_count[PEER_MESSAGE_CANCEL+1]++; return 0; }
-int peer_choke_received(int sock,struct peer *info, ssize_t len) { message_count[PEER_MESSAGE_CHOKE+1]++; return 0; }
-int peer_handshake(int sock,struct peer *info) { return 0; }
-int peer_hash_reject_received(int sock,struct peer *info, ssize_t len) { message_count[PEER_MESSAGE_HASH_REJECT+1]++; return 0; }
-int peer_hash_request_received(int sock,struct peer *info, ssize_t len) { message_count[PEER_MESSAGE_HASH_REQUEST+1]++; return 0; }
-int peer_hashes_received(int sock,struct peer *info, ssize_t len) { message_count[PEER_MESSAGE_HASHES+1]++; return 0; }
-int peer_have_received(int sock,struct peer *info, ssize_t len) { message_count[PEER_MESSAGE_HAVE+1]++; return 0; }
-int peer_interested_received(int sock,struct peer *info, ssize_t len) { message_count[PEER_MESSAGE_INTERESTED+1]++; return 0; }
-int peer_keepalive_received(int sock,struct peer *info, ssize_t len) { message_count[PEER_MESSAGE_KEEPALIVE+1]++; return 0; }
-int peer_not_interested_received(int sock,struct peer *info, ssize_t len) { message_count[PEER_MESSAGE_NOT_INTERESTED+1]++; return 0; }
-int peer_piece_received(int sock,struct peer *info, ssize_t len) { message_count[PEER_MESSAGE_PIECE+1]++; return 0; }
-int peer_reject_received(int sock,struct peer *info, ssize_t len) { message_count[PEER_MESSAGE_REJECT+1]++; return 0; }
-int peer_request_received(int sock,struct peer *info, ssize_t len) { message_count[PEER_MESSAGE_REQUEST+1]++; return 0; }
-int peer_unchoke_received(int sock,struct peer *info, ssize_t len) { message_count[PEER_MESSAGE_UNCHOKE+1]++; return 0; }
+int peer_bitfield_received(struct peer *info, ssize_t len) { message_count[PEER_MESSAGE_BITFIELD+1]++; return 0; }
+int peer_cancel_received(struct peer *info, ssize_t len) { message_count[PEER_MESSAGE_CANCEL+1]++; return 0; }
+int peer_choke_received(struct peer *info, ssize_t len) { message_count[PEER_MESSAGE_CHOKE+1]++; return 0; }
+int peer_handshake(struct peer *info) { return 0; }
+int peer_hash_reject_received(struct peer *info, ssize_t len) { message_count[PEER_MESSAGE_HASH_REJECT+1]++; return 0; }
+int peer_hash_request_received(struct peer *info, ssize_t len) { message_count[PEER_MESSAGE_HASH_REQUEST+1]++; return 0; }
+int peer_hashes_received(struct peer *info, ssize_t len) { message_count[PEER_MESSAGE_HASHES+1]++; return 0; }
+int peer_have_received(struct peer *info, ssize_t len) { message_count[PEER_MESSAGE_HAVE+1]++; return 0; }
+int peer_interested_received(struct peer *info, ssize_t len) { message_count[PEER_MESSAGE_INTERESTED+1]++; return 0; }
+int peer_keepalive_received(struct peer *info, ssize_t len) { message_count[PEER_MESSAGE_KEEPALIVE+1]++; return 0; }
+int peer_not_interested_received(struct peer *info, ssize_t len) { message_count[PEER_MESSAGE_NOT_INTERESTED+1]++; return 0; }
+int peer_piece_received(struct peer *info, ssize_t len) { message_count[PEER_MESSAGE_PIECE+1]++; return 0; }
+int peer_reject_received(struct peer *info, ssize_t len) { message_count[PEER_MESSAGE_REJECT+1]++; return 0; }
+int peer_request_received(struct peer *info, ssize_t len) { message_count[PEER_MESSAGE_REQUEST+1]++; return 0; }
+int peer_unchoke_received(struct peer *info, ssize_t len) { message_count[PEER_MESSAGE_UNCHOKE+1]++; return 0; }
 /* end dummy functions */
 
 static int read_sock;
@@ -38,11 +38,12 @@ static int write_sock;
 int main() {
        setup_env();
 
+       net_peek_basic_test();
+
        net_cache_basic_test();
        net_expected_basic_test();
        net_handler_basic_test();
        net_handler_invalid_message_test();
-       net_peek_basic_test();
        net_queue_basic_test();
        net_send_basic_test();
        net_wait_basic_test();
@@ -98,7 +99,6 @@ static void net_cache_basic_test() {
 static void net_expected_basic_test() {
        struct peer *info;
        enum peer_message type;
-       unsigned char buf[10];
 
        setup_sockets();
 
@@ -111,58 +111,69 @@ static void net_expected_basic_test() {
 
        assert(0==net_expected(info,&type));
        assert(PEER_MESSAGE_KEEPALIVE==type);
-       assert(3==read(read_sock,buf,sizeof(buf)));
+       assert(info->in!=NULL);
+       assert(memcmp(info->in,expected1,3)==0);
+       assert(3==info->in_size);
+
+       free(info->in);
+       info->in = NULL;
 
        /* verify keepalive returns as expected */
        assert(4==write(write_sock,expected1,4));
 
        assert(4==net_expected(info,&type));
        assert(PEER_MESSAGE_KEEPALIVE==type);
-       assert(4==read(read_sock,buf,sizeof(buf)));
+       assert(info->in!=NULL);
+       assert(memcmp(info->in,expected1,4)==0);
+       assert(4==info->in_size);
+
+       free(info->in);
+       info->in = NULL;
 
        unsigned char expected2[5] = "\x00\x00\x01\x00";
        expected2[4] = PEER_MESSAGE_CHOKE;
        assert(5==write(write_sock,expected2,5));
 
-       assert(260==net_expected(read_sock,&type));
+       assert(260==net_expected(info,&type));
        assert(PEER_MESSAGE_CHOKE==type);
-       assert(5==read(read_sock,buf,sizeof(buf)));
+       assert(info->in!=NULL);
+       assert(memcmp(info->in,expected2,5)==0);
+       assert(5==info->in_size);
+
+       free(info->in);
+       info->in = NULL;
 
        unsigned char expected3[5] = "\x00\x10\x01\x00";
        expected3[4] = PEER_MESSAGE_HASHES;
        assert(5==write(write_sock,expected3,5));
 
-       assert(1048836==net_expected(read_sock,&type));
+       assert(1048836==net_expected(info,&type));
        assert(PEER_MESSAGE_HASHES==type);
-       assert(5==read(read_sock,buf,sizeof(buf)));
+       assert(info->in!=NULL);
+       assert(memcmp(info->in,expected3,5)==0);
+       assert(5==info->in_size);
+
+       free(info->in);
+       info->in = NULL;
 
        unsigned char expected4[5] = "\xf0\x10\x01\x00";
        expected4[4] = PEER_MESSAGE_UNCHOKE;
        assert(5==write(write_sock,expected4,5));
 
-       assert(4027580676==net_expected(read_sock,&type));
+       assert(4027580676==net_expected(info,&type));
        assert(PEER_MESSAGE_UNCHOKE==type);
-       assert(5==read(read_sock,buf,sizeof(buf)));
+       assert(info->in!=NULL);
+       assert(memcmp(info->in,expected4,5)==0);
+       assert(5==info->in_size);
+
+       free(info->in);
+       info->in = NULL;
 
        peer_free(info);
        close(read_sock);
        close(write_sock);
 }
 
-/* Parameters:
- * unsigned char *message
- * enum peer_message type
- */
-#define NET_HANDLER_HELPER(message,type) { \
-       assert(sizeof(message)==write(write_sock,message,sizeof(message))); \
-\
-       net_handler(epoll_fd,info); \
-       assert(0==epoll_ctl(epoll_fd,EPOLL_CTL_DEL,read_sock,NULL)); \
-       assert(sizeof(message)==read(read_sock,buf,sizeof(buf))); \
-\
-       assert(1==message_count[type+1]); \
-}
-
 static void net_handler_invalid_message_test() {
        struct peer *info;
        int epoll_fd;
@@ -178,7 +189,7 @@ static void net_handler_invalid_message_test() {
        
        memset(message_count, 0, sizeof(message_count));
 
-       unsigned char invalid_message_type[5] = "\x00\x00\x00\x01\xff";
+       unsigned char invalid_message_type[5] = "\x00\x00\x00\x01\xfe";
        assert(5==write(write_sock,invalid_message_type,5));
        net_handler(epoll_fd,info);
 
@@ -189,9 +200,26 @@ static void net_handler_invalid_message_test() {
        close(epoll_fd);
 }
 
+/* Parameters:
+ * unsigned char *message
+ * enum peer_message type
+ */
+#define NET_HANDLER_HELPER(message,type) { \
+       assert(sizeof(message)==write(write_sock,message,sizeof(message))); \
+\
+       net_handler(epoll_fd,info); \
+       assert(0==epoll_ctl(epoll_fd,EPOLL_CTL_DEL,read_sock,NULL)); \
+\
+       assert(info->in!=NULL); \
+       assert(memcmp(info->in,message,sizeof(message))==0); \
+       free(info->in); \
+       info->in = NULL; \
+\
+       assert(1==message_count[type+1]); \
+}
+
 static void net_handler_basic_test() {
        struct peer *info;
-       unsigned char buf[10];
        int epoll_fd;
 
        setup_sockets();
@@ -255,12 +283,46 @@ static void net_handler_basic_test() {
 
 static void net_peek_basic_test() {
        struct peer *info;
+       char buf[10];
+       char expected[10];
+
+       setup_sockets();
+
+       memset(buf,0,10);
+       memset(expected,1,10);
+
+       assert(1==peer_init(&info));
+       info->sock = read_sock;
 
+       assert(-1==net_peek(NULL,NULL,0));
+       assert(-1==net_peek(info,NULL,0));
+       assert(0==net_peek(info,buf,0));
+       
+       assert(0==net_peek(info,buf,10));
+
+       assert(5==write(write_sock,expected,5));
+       assert(0==net_peek(info,buf,10));
+       assert(info->in!=NULL);
+       assert(info->in_size==5);
+       assert(memcmp(info->in,expected,5)==0);
+       for(size_t i=0;i<5;i++) {
+               assert(buf[i]==0);
+       }
+       free(info->in);
+       info->in = NULL;
+
+       assert(5==write(write_sock,expected,5));
+       assert(1==net_peek(info,buf,5));
+       assert(info->in!=NULL);
+       assert(info->in_size==5);
+       assert(memcmp(info->in,expected,5)==0);
+       assert(memcmp(buf,expected,5)==0);
+       free(info->in);
+       info->in = NULL;
 }
 
 static void net_queue_basic_test() {
        int epoll_fd;
-       int sock;
        struct peer *info;
 
        assert(1==peer_init(&info));
@@ -291,7 +353,7 @@ static void net_send_basic_test() {
        assert(1==peer_init(&info));
        info->sock = write_sock;
 
-       assert(-1==net_send(-1,NULL,NULL,0));
+       assert(-1==net_send(NULL,NULL,0));
        assert(-1==net_send(info,NULL,0));
        
        assert(1==net_send(info,buf,0));
@@ -347,6 +409,7 @@ static void net_wait_basic_test() {
 
        assert(1==net_wait(info,buf,100));
        assert(info->in==NULL);
+       assert(info->in_size==0);
        assert(memcmp(buf,expected,100)==0);
 
        i = 0;
@@ -363,6 +426,7 @@ static void net_wait_basic_test() {
        assert(1==write(write_sock,expected,1));
        assert(1==net_wait(info,buf,100));
        assert(info->in==NULL);
+       assert(info->in_size==0);
        assert(memcmp(buf,expected,100)==0);
        
        close(read_sock);