Browse code

fix use of uninitialized value. add test for STREAM. fix missing closing pragmas.

git-svn: trunk@4810

Török Edvin authored on 2009/02/18 02:35:31
Showing 6 changed files
... ...
@@ -1,3 +1,10 @@
1
+Tue Feb 17 20:06:02 EET 2009 (edwin)
2
+------------------------------------
3
+ * clamd/others.c, libclamunrar_iface/unrar_iface.h, shared/misc.c,
4
+ unit_tests/check_clamd.c, unit_tests/check_clamd.sh: fix use of
5
+ uninitialized value.  add test for STREAM.  fix missing closing
6
+ pragmas.
7
+
1 8
 Tue Feb 17 18:38:30 EET 2009 (edwin)
2 9
 ------------------------------------
3 10
  * unit_tests/check_clamd.c: another warning
... ...
@@ -436,11 +436,11 @@ int fds_poll_recv(struct fd_data *data, int timeout, int check_signals)
436 436
 	data->buf[i].got_newdata = 0;
437 437
     }
438 438
 
439
+    time(&now);
439 440
     if (timeout > 0)
440 441
 	closest_timeout = now + timeout;
441 442
     else
442 443
 	closest_timeout = 0;
443
-    time(&now);
444 444
     for (i=0;i < data->nfds; i++) {
445 445
 	time_t timeout_at = data->buf[i].timeout_at;
446 446
 	if (timeout_at && timeout_at < now) {
... ...
@@ -114,6 +114,14 @@ typedef struct unrar_state_tag {
114 114
     char filename[1024];
115 115
 } unrar_state_t;
116 116
 
117
+#ifdef HAVE_PRAGMA_PACK
118
+#pragma pack()
119
+#endif
120
+
121
+#ifdef HAVE_PRAGMA_PATCH_HPPA
122
+#pragma pack
123
+#endif
124
+
117 125
 int unrar_open(int fd, const char *dirname, unrar_state_t *state);
118 126
 int unrar_extract_next_prepare(unrar_state_t *state, const char *dirname);
119 127
 int unrar_extract_next(unrar_state_t *state, const char *dirname);
... ...
@@ -31,6 +31,7 @@
31 31
 #include <time.h>
32 32
 #include <sys/types.h>
33 33
 #include <sys/stat.h>
34
+#include <sys/socket.h>
34 35
 #ifndef	C_WINDOWS
35 36
 #include <dirent.h>
36 37
 #endif
... ...
@@ -50,6 +50,23 @@ static void conn_setup(void)
50 50
     signal(SIGPIPE, SIG_IGN);
51 51
 }
52 52
 
53
+static int conn_tcp(int port)
54
+{
55
+    struct sockaddr_in server;
56
+    int rc;
57
+    int sd = socket(AF_INET, SOCK_STREAM, 0);
58
+    fail_unless_fmt(sd != -1, "Unable to create socket: %s\n", strerror(errno));
59
+
60
+    memset(&server, 0, sizeof(server));
61
+    server.sin_family = AF_INET;
62
+    server.sin_port = htons(port);
63
+    server.sin_addr.s_addr = inet_addr("127.0.0.1");
64
+
65
+    rc = connect(sd, (struct sockaddr *)&server, sizeof(server));
66
+    fail_unless_fmt(rc != -1, "Unable to connect(): %s\n", strerror(errno));
67
+    return sd;
68
+}
69
+
53 70
 static void conn_teardown(void)
54 71
 {
55 72
     if (sockd != -1)
... ...
@@ -579,7 +596,6 @@ START_TEST (test_connections)
579 579
 		}
580 580
 	    }
581 581
 	}
582
-	printf("exited\n");
583 582
 	free(sock);
584 583
 	exit(0);
585 584
     } else {
... ...
@@ -597,6 +613,47 @@ START_TEST (test_connections)
597 597
 }
598 598
 END_TEST
599 599
 
600
+START_TEST (test_stream)
601
+{
602
+    char buf[BUFSIZ];
603
+    char *recvdata;
604
+    size_t len;
605
+    unsigned port;
606
+    int streamsd, infd, nread;
607
+
608
+    infd = open(SCANFILE, O_RDONLY);
609
+
610
+    fail_unless_fmt(infd != -1, "open failed: %s\n", strerror(errno));
611
+    conn_setup();
612
+    fail_unless_fmt(
613
+	send(sockd, "zSTREAM", sizeof("zSTREAM"), 0) == sizeof("zSTREAM"),
614
+	"send failed: %s\n", strerror(errno));
615
+    recvdata = recvpartial(sockd, &len, 1);
616
+    fail_unless_fmt (sscanf(recvdata, "PORT %u\n", &port) == 1,
617
+		     "Wrong stream reply: %s\n", recvdata);
618
+
619
+    free(recvdata);
620
+    streamsd = conn_tcp(port);
621
+
622
+    do {
623
+	nread = read(infd, buf, sizeof(buf));
624
+	if (nread > 0)
625
+	    fail_unless_fmt(send(streamsd, buf, nread, 0) == nread,
626
+			    "send failed: %s\n", strerror(errno));
627
+    } while (nread > 0 || (nread == -1 && errno == EINTR));
628
+    fail_unless_fmt(nread != -1, "read failed: %s\n", strerror(errno));
629
+    close(infd);
630
+    close(streamsd);
631
+
632
+    recvdata = recvfull(sockd, &len);
633
+    fail_unless_fmt(!strcmp(recvdata,"stream: ClamAV-Test-File.UNOFFICIAL FOUND"),
634
+		    "Wrong reply: %s\n", recvdata);
635
+    free(recvdata);
636
+
637
+    conn_teardown();
638
+}
639
+END_TEST
640
+
600 641
 static Suite *test_clamd_suite(void)
601 642
 {
602 643
     Suite *s = suite_create("clamd");
... ...
@@ -609,13 +666,13 @@ static Suite *test_clamd_suite(void)
609 609
     tcase_add_loop_test(tc_commands, test_fildes, 0, 4*sizeof(fildes_cmds)/sizeof(fildes_cmds[0]));
610 610
     tcase_add_test(tc_commands, test_stats);
611 611
     tcase_add_test(tc_commands, test_instream);
612
+    tcase_add_test(tc_commands, test_stream);
612 613
     tc_stress = tcase_create("clamd stress test");
613 614
     suite_add_tcase(s, tc_stress);
614 615
     tcase_add_test(tc_stress, test_fildes_many);
615 616
     tcase_add_test(tc_stress, test_idsession_stress);
616 617
     tcase_add_test(tc_stress, test_connections);
617 618
     tcase_add_test(tc_stress, test_fildes_unwanted);
618
-
619 619
     return s;
620 620
 }
621 621
 
... ...
@@ -84,13 +84,25 @@ run_clamdscan() {
84 84
 	$TOP/clamdscan/clamdscan --quiet --config-file=test-clamd.conf $* --fdpass --log=clamdscan-fdpass.log
85 85
 	if test $? = 2; then 
86 86
 		error "Failed to run clamdscan (fdpass)!"
87
-		cat clamdscan-multiscan.log
87
+		cat clamdscan-fdpass.log
88 88
 		die 1
89 89
 	fi
90 90
 	$TOP/clamdscan/clamdscan --quiet --config-file=test-clamd.conf $* -m --fdpass --log=clamdscan-multiscan-fdpass.log
91 91
 	if test $? = 2; then 
92 92
 		error "Failed to run clamdscan (fdpass + multiscan)!"
93
-		cat clamdscan-multiscan.log
93
+		cat clamdscan-multiscan-fdpass.log
94
+		die 1
95
+	fi
96
+	$TOP/clamdscan/clamdscan --quiet --config-file=test-clamd.conf $* --stream --log=clamdscan-stream.log
97
+	if test $? = 2; then 
98
+		error "Failed to run clamdscan (instream)!"
99
+		cat clamdscan-stream.log
100
+		die 1
101
+	fi
102
+	$TOP/clamdscan/clamdscan --quiet --config-file=test-clamd.conf $* -m --stream --log=clamdscan-multiscan-stream.log
103
+	if test $? = 2; then 
104
+		error "Failed to run clamdscan (instream + multiscan)!"
105
+		cat clamdscan-multiscan-stream.log
94 106
 		die 1
95 107
 	fi
96 108
 }
... ...
@@ -185,6 +197,8 @@ NINFECTED=`grep "Infected files" clamdscan.log | cut -f2 -d:|sed -e 's/ //g'`
185 185
 NINFECTED_MULTI=`grep "Infected files" clamdscan-multiscan.log | cut -f2 -d:|sed -e 's/ //g'`
186 186
 NINFECTED_FDPASS=`grep "Infected files" clamdscan-fdpass.log | cut -f2 -d:|sed -e 's/ //g'`
187 187
 NINFECTED_MULTI_FDPASS=`grep "Infected files" clamdscan-multiscan-fdpass.log | cut -f2 -d:|sed -e 's/ //g'`
188
+NINFECTED_STREAM=`grep "Infected files" clamdscan-stream.log | cut -f2 -d:|sed -e 's/ //g'`
189
+NINFECTED_MULTI_STREAM=`grep "Infected files" clamdscan-multiscan-stream.log | cut -f2 -d:|sed -e 's/ //g'`
188 190
 if test "$NFILES" -ne "0$NINFECTED"; then
189 191
 	grep OK clamdscan.log
190 192
 	scan_failed clamdscan.log "clamd did not detect all testfiles correctly!"