/* 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;
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();
static void net_expected_basic_test() {
struct peer *info;
enum peer_message type;
- unsigned char buf[10];
setup_sockets();
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;
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);
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();
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));
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));
assert(1==net_wait(info,buf,100));
assert(info->in==NULL);
+ assert(info->in_size==0);
assert(memcmp(buf,expected,100)==0);
i = 0;
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);