add test for simple socket syscalls

This commit is contained in:
WangRunji 2019-04-03 01:04:10 +08:00
parent e5bc58d3f0
commit 0dda84d7f2
5 changed files with 122 additions and 1 deletions

@ -4,7 +4,7 @@ PROJECT_DIR := $(realpath $(CUR_DIR)/../)
# Dependencies: need to be compiled but not to run by any Makefile target # Dependencies: need to be compiled but not to run by any Makefile target
TEST_DEPS := dev_null TEST_DEPS := dev_null
# Tests: need to be compiled and run by test-% target # Tests: need to be compiled and run by test-% target
TESTS := empty argv hello_world malloc file getpid spawn pipe time truncate readdir mkdir link tls pthread TESTS := empty argv hello_world malloc file getpid spawn pipe time truncate readdir mkdir link tls pthread clone server
# Benchmarks: need to be compiled and run by bench-% target # Benchmarks: need to be compiled and run by bench-% target
BENCHES := spawn_and_exit_latency pipe_throughput BENCHES := spawn_and_exit_latency pipe_throughput

5
test/client/Makefile Normal file

@ -0,0 +1,5 @@
include ../test_common.mk
EXTRA_C_FLAGS :=
EXTRA_LINK_FLAGS :=
BIN_ARGS :=

52
test/client/main.c Normal file

@ -0,0 +1,52 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <spawn.h>
int main(int argc, const char *argv[]) {
const int BUF_SIZE = 0x1000;
const char* message = "Hello world!";
int ret;
if (argc != 2) {
printf("usage: ./client <ipaddress>\n");
return -1;
}
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
printf("create socket error: %s(errno: %d)\n", strerror(errno), errno);
return -1;
}
struct sockaddr_in servaddr;
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(6666);
ret = inet_pton(AF_INET, argv[1], &servaddr.sin_addr);
if (ret <= 0) {
printf("inet_pton error for %s\n", argv[1]);
return -1;
}
ret = connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr));
if (ret < 0) {
printf("connect error: %s(errno: %d)\n", strerror(errno), errno);
return -1;
}
printf("send msg to server: %s\n", message);
ret = send(sockfd, message, strlen(message), 0);
if (ret < 0) {
printf("send msg error: %s(errno: %d)\n", strerror(errno), errno);
return -1;
}
close(sockfd);
return 0;
}

5
test/server/Makefile Normal file

@ -0,0 +1,5 @@
include ../test_common.mk
EXTRA_C_FLAGS :=
EXTRA_LINK_FLAGS :=
BIN_ARGS :=

59
test/server/main.c Normal file

@ -0,0 +1,59 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <spawn.h>
int main(int argc, const char *argv[]) {
const int BUF_SIZE = 0x1000;
int ret;
int listenfd = socket(AF_INET, SOCK_STREAM, 0);
if (listenfd < 0) {
printf("create socket error: %s(errno: %d)\n", strerror(errno), errno);
return -1;
}
struct sockaddr_in servaddr;
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(6666);
ret = bind(listenfd, (struct sockaddr *) &servaddr, sizeof(servaddr));
if (ret < 0) {
printf("bind socket error: %s(errno: %d)\n", strerror(errno), errno);
return -1;
}
ret = listen(listenfd, 10);
if (ret < 0) {
printf("listen socket error: %s(errno: %d)\n", strerror(errno), errno);
return -1;
}
int client_pid;
char* client_argv[] = {"client", "127.0.0.1"};
ret = posix_spawn(&client_pid, "client", NULL, NULL, client_argv, NULL);
if (ret < 0) {
printf("spawn client process error: %s(errno: %d)\n", strerror(errno), errno);
return -1;
}
printf("====== waiting for client's request ======\n");
int connect_fd = accept(listenfd, (struct sockaddr *) NULL, NULL);
if (connect_fd < 0) {
printf("accept socket error: %s(errno: %d)", strerror(errno), errno);
return -1;
}
char buff[BUF_SIZE];
int n = recv(connect_fd, buff, BUF_SIZE, 0);
buff[n] = '\0';
printf("recv msg from client: %s\n", buff);
close(connect_fd);
close(listenfd);
}