Skip to content
Snippets Groups Projects
Commit 54bcc732 authored by Kirill's avatar Kirill
Browse files

Fixed

parent 5b9acaab
Branches
No related merge requests found
No preview for this file type
......@@ -22,13 +22,13 @@ int main (int argc, char *argv [])
{
char buffer[SIZE]="hello world";
/* test arg number */
if(argc != 2){
fprintf(stderr, "usage: %s port_number\n", argv[0]);
if(argc != 3){
fprintf(stderr, "usage: %s ip_addr port_number\n", argv[0]);
exit(EXIT_FAILURE);
}
/* convert and check port number */
int port_number =atoi(argv[1]);
int port_number =atoi(argv[2]);
if(port_number<10000||port_number>65000){
fprintf(stderr, "usage: %d port_number is not in [10000;65000] \n", port_number);
exit(EXIT_FAILURE);
......@@ -43,7 +43,7 @@ int main (int argc, char *argv [])
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
int error = getaddrinfo(IP, argv[1], &hints, &res);
int error = getaddrinfo(argv[1], argv[2], &hints, &res);
if (error != 0) {
fprintf(stderr,"getaddrinfo %s\n",gai_strerror(error)); // Generic error message
......@@ -51,15 +51,9 @@ int main (int argc, char *argv [])
}
/* connect to the remote peer */
struct sockaddr_storage dest_addr;
memcpy(&dest_addr, res->ai_addr, res->ai_addrlen);
struct sockaddr_in *pointer=(struct sockaddr_in *)&dest_addr;
socklen_t size=sizeof(*pointer);
CHECK(connect(new_socket,(struct sockaddr*)pointer,size));
CHECK(connect(new_socket, res->ai_addr, res->ai_addrlen));
/* send the message */
CHECK(send(new_socket,buffer,11,0));
/* close socket */
CHECK(send(new_socket, buffer, 11, 0));
close(new_socket);
/* free memory */
freeaddrinfo(res);
......
#!/bin/bash
PROG="./sender-tcp"
FILE="$PROG.c"
PORT=`shuf -i 10000-65000 -n 1`
echo -n "test 01 - program without arg: "
$PROG > /dev/null 2>&1 && echo "KO -> exit status $? instead of 1" && exit 1
echo "....................OK"
OUT="/tmp/$$"
mkdir $OUT
IP="127.0.0.1"
SE=$(uname -s)
######################################
echo -n "test 01 - program usage: "
echo "usage: $PROG ip_addr port_number" > $OUT/usage
$PROG > $OUT/stdout 2> $OUT/stderr && echo "KO -> exit status $? instead of 1" && exit 1
! cmp -s $OUT/usage $OUT/stderr && echo "KO -> unexpected output on stderr => check file \"$OUT/usage\"" && exit 1
[ -s $OUT/stdout ] && echo "KO -> output detected on stdout" && exit 1
$PROG a > $OUT/stdout 2> $OUT/stderr && echo "KO -> exit status $? instead of 1" && exit 1
! cmp -s $OUT/usage $OUT/stderr && echo "KO -> unexpected output on stderr => check file \"$OUT/usage\"" && exit 1
[ -s $OUT/stdout ] && echo "KO -> output detected on stdout" && exit 1
$PROG a b c > $OUT/stdout 2> $OUT/stderr && echo "KO -> exit status $? instead of 1" && exit 1
! cmp -s $OUT/usage $OUT/stderr && echo "KO -> unexpected output on stderr => check file \"$OUT/usage\"" && exit 1
[ -s $OUT/stdout ] && echo "KO -> output detected on stdout" && exit 1
echo ".....................OK"
######################################
echo -n "test 02 - no dynamic memory allocation: "
grep -q "[cm]alloc" $FILE && echo "KO -> dynamic memory allocation is not allowed!" && exit 1
echo "......OK"
######################################
echo -n "test 03 - invalid port number: "
$PROG a 65001 > $OUT/stdout 2> $OUT/stderr && echo "KO -> exit status $? instead of 1" && exit 1
! [ -s $OUT/stderr ] && echo "KO -> no output on stderr" && exit 1
[ -s $OUT/stdout ] && echo "KO -> output detected on stdout" && exit 1
$PROG a 9999 > $OUT/stdout 2> $OUT/stderr && echo "KO -> exit status $? instead of 1" && exit 1
! [ -s $OUT/stderr ] && echo "KO -> no output on stderr" && exit 1
[ -s $OUT/stdout ] && echo "KO -> output detected on stdout" && exit 1
echo "...............OK"
######################################
echo -n "test 04 - getaddrinfo usage: "
ERROR="Name or service not known"
if [ "$SE" == "Darwin" ]; then
ERROR="nodename nor servname provided, or not known"
fi
echo -n "test 02 - invalid port number: "
$PROG 65001 > /dev/null 2>&1 && echo "KO -> exit status $? instead of 1" && exit 1
echo "....................OK"
LC_ALL=C $PROG a $PORT > $OUT/stdout 2> $OUT/stderr && echo "KO -> exit status $? instead of 1" && exit 1
! grep -q "$ERROR" $OUT/stderr && echo "KO -> unexpected output on stderr, you should use gai_strerror" && exit 1
grep -q "$ERROR" $FILE && echo "KO -> no static msg for getaddrinfo error" && exit 1
[ -s $OUT/stdout ] && echo "KO -> output detected on stdout" && exit 1
echo -n "test 03 - program exits without error: "
timeout 3 nc -4l 127.0.0.1 $PORT > output &
sleep 1
! $PROG $PORT 2> /tmp/null && echo "KO -> exit status $R" && exit 1
echo "............OK"
echo ".................OK"
echo -n "test 04 - program sends a message: "
MES=`cat output`
[ -z "$MES" ] && echo "KO -> no message" && exit 1
echo "................OK"
######################################
echo -n "test 05 - message is valid: "
[ "$MES" != "hello world" ] && echo "KO - received: $MESSAGE | expected: hello world" && exit 1
echo ".......................OK"
echo -n "test 05 - check connect error: "
echo -n "test 06 - connection to a non listening server: "
$PROG $PORT 2> /tmp/null && echo "KO -> exit status $?" && exit 1
echo "...OK"
LC_ALL=C $PROG $IP 23451 > $OUT/stdout 2> $OUT/stderr && echo "KO -> exit status $? instead of 1" && exit 1
ERROR="Connection refused"
! grep -q "$ERROR" $OUT/stderr && echo "KO -> unexpected output on stderr, do you use perror?" && exit 1
[ -s $OUT/stdout ] && echo "KO -> output detected on stdout" && exit 1
echo "...............OK"
######################################
echo -n "test 06 - use struct returned by getaddrinfo: "
grep -q "sockaddr_.*" $FILE && echo "KO -> you should not use struct sockaddrX variable" && exit 1
echo "OK"
######################################
echo -n "test 07 - program exits without error: "
timeout 10 nc -4l $IP $PORT > $OUT/msg_r &
sleep 4
! $PROG $IP $PORT > $OUT/stdin 2> $OUT/stderr && echo "KO -> exit status != 0" && exit 1
[ -s $OUT/stderr ] && echo "KO -> output detected on stderr" && exit 1
[ -s $OUT/stdout ] && echo "KO -> output detected on stdout" && exit 1
echo ".......OK"
######################################
echo -n "test 08 - program sends a message: "
! [ -s $OUT/msg_r ] && echo "KO -> no message transmitted" && exit 1
echo "...........OK"
######################################
echo -n "test 09 - message is valid: "
printf "hello world" > $OUT/msg_o
! cmp -s $OUT/msg_o $OUT/msg_r && echo "KO -> check files \"$OUT/msg_o\" (expected) and \"$OUT/msg_r\" (sent)" && exit 1
echo "..................OK"
######################################
echo -n "test 10 - memory error: "
echo -n "test 07 - memory error: "
P=`which valgrind`
[ -z "$P" ] && echo "KO -> please install valgrind" && exit 1
timeout 5 nc -4l 127.0.0.1 $PORT > output &
sleep 1
valgrind --leak-check=full --error-exitcode=100 --log-file=valgrind.log $PROG $PORT
timeout 10 nc -4l $IP $PORT > /dev/null &
sleep 4
valgrind --leak-check=full --error-exitcode=100 --log-file=$OUT/valgrind.log $PROG $IP $PORT
[ "$?" == "100" ] && echo "KO -> memory pb please check valgrind.log" && exit 1
echo "...........................OK"
rm output valgrind.log
echo "..................................OK"
rm -r $OUT
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment