#include<net.h>
int main();
-static int setup_socket();
+static void setup_sockets();
+static void net_cache_basic_test();
static void net_send_basic_test();
static void net_wait_basic_test();
-static int read_socket;
-static int write_socket;
+static int read_sock;
+static int write_sock;
int main() {
setup_env();
+ net_cache_basic_test();
net_send_basic_test();
net_wait_basic_test();
return EXIT_SUCCESS;
}
+static void net_cache_basic_test() {
+ unsigned char buf1[100], buf2[100];
+ void *p1, *p2;
+ size_t i;
+
+ memset(buf1,1,100);
+ memset(buf2,2,100);
+
+ p1 = NULL;
+ p2 = NULL;
+
+ assert(-1==net_cache(NULL,NULL,NULL,0));
+ assert(-1==net_cache(&p1,NULL,NULL,0));
+
+ i = 0;
+ assert(-1==net_cache(&p1,&i,NULL,0));
+ assert(-1==net_cache(&p1,&i,p2,0));
+
+ p2 = buf2;
+ assert(1==net_cache(&p1,&i,p2,0));
+
+ assert(NULL==p1);
+
+ assert(1==net_cache(&p1,&i,p2,50));
+ assert(memcmp(buf2,p1,50)==0);
+ assert(50==i);
+
+ p2 = buf1;
+ assert(1==net_cache(&p1,&i,p2,50));
+
+ assert(100==i);
+ assert(memcmp(buf2,p1,50)==0);
+ assert(memcmp(buf1,&(((unsigned char*)p1)[50]),50)==0);
+
+ assert(1==net_cache(&p1,&i,p2,100));
+ assert(200==i);
+ assert(memcmp(buf2,p1,50)==0);
+ assert(memcmp(buf1,&(((unsigned char*)p1)[50]),100)==0);
+ assert(memcmp(buf1,&(((unsigned char*)p1)[150]),50)==0);
+
+ free(p1);
+}
+
static void net_send_basic_test() {
struct peer *info;
- int sock;
char buf[100];
char expected[100];
- sock = setup_socket();
+ setup_sockets();
memset(buf,1,100);
memset(expected,0,100);
assert(1==peer_init(&info));
assert(-1==net_send(-1,NULL,NULL,0));
- assert(-1==net_send(sock,info,NULL,0));
+ assert(-1==net_send(write_sock,info,NULL,0));
- assert(1==net_send(sock,info,buf,0));
- assert(0==read(sock,expected,100));
+ assert(1==net_send(write_sock,info,buf,0));
+ assert(-1==read(read_sock,expected,100));
- assert(1==net_send(sock,info,buf,50));
- assert(50==read(sock,expected,100));
+ assert(1==net_send(write_sock,info,buf,50));
+ assert(50==read(read_sock,expected,100));
- assert(1==net_send(sock,info,buf,100));
- assert(100==read(sock,expected,100));
+ assert(1==net_send(write_sock,info,buf,100));
+ assert(100==read(read_sock,expected,100));
+
+ close(read_sock);
+ close(write_sock);
- close(sock);
+ peer_free(info);
}
static void net_wait_basic_test() {
struct peer *info;
- int sock;
char buf[100];
char expected[100];
- sock = setup_socket();
+ setup_sockets();
memset(buf,1,100);
memset(expected,0,100);
assert(1==peer_init(&info));
assert(-1==net_wait(-1,NULL,NULL,0));
- assert(-1==net_wait(sock,NULL,NULL,0));
- assert(-1==net_wait(sock,info,NULL,0));
- assert(-1==net_wait(sock,info,expected,0));
+ assert(-1==net_wait(read_sock,NULL,NULL,0));
+ assert(-1==net_wait(read_sock,info,NULL,0));
+ assert(1==net_wait(read_sock,info,expected,0));
- assert(0==net_wait(sock,info,buf,100));
+ assert(0==net_wait(read_sock,info,buf,100));
- assert(50==write(sock,expected,50));
+ assert(50==write(write_sock,expected,50));
- assert(0==net_wait(sock,info,buf,100));
+ assert(0==net_wait(read_sock,info,buf,100));
assert(info->in!=NULL);
assert(info->in_size==50);
assert(memcmp(buf,expected,100)!=0);
- assert(25==write(sock,expected,25));
- assert(0==net_wait(sock,info,buf,100));
+ assert(25==write(write_sock,expected,25));
+ assert(0==net_wait(read_sock,info,buf,100));
assert(info->in!=NULL);
assert(info->in_size==75);
assert(memcmp(buf,expected,100)!=0);
- assert(25==write(sock,expected,25));
+ assert(25==write(write_sock,expected,25));
- assert(1==net_wait(sock,info,buf,100));
+ assert(1==net_wait(read_sock,info,buf,100));
assert(info->in==NULL);
assert(info->in_size==0);
assert(memcmp(buf,expected,100)==0);
- close(sock);
+ close(read_sock);
+ close(write_sock);
+
+ peer_free(info);
}
-static int setup_sockets() {
- int sock = socket(AF_UNIX,SOCK_STREAM,0);
- assert(sock!=-1);
+static void setup_sockets() {
+ int fds[2];
- assert(0==bind(sock,
+ assert(0==socketpair(AF_UNIX,SOCK_STREAM | SOCK_NONBLOCK, 0 ,fds));
- return sock;
+ read_sock = fds[0];
+ write_sock = fds[1];
}