[Pkg-wmaker-commits] [wmbiff] 07/17: test for proper handling of buffering small bits of expected data

Doug Torrance dtorrance-guest at moszumanska.debian.org
Thu Aug 20 03:04:20 UTC 2015


This is an automated email from the git hooks/post-receive script.

dtorrance-guest pushed a commit to tag wmbiff_0_4_18
in repository wmbiff.

commit 250ca7d1828fe396130b3d305b4a69d982f1ed72
Author: bluehal <bluehal>
Date:   Tue Oct 28 06:56:34 2003 +0000

    test for proper handling of buffering small bits of expected data
---
 wmbiff/test_tlscomm.c | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++
 wmbiff/tlsComm.c      | 35 ++++++++++++++++++---------
 2 files changed, 91 insertions(+), 11 deletions(-)

diff --git a/wmbiff/test_tlscomm.c b/wmbiff/test_tlscomm.c
new file mode 100644
index 0000000..b3ea83a
--- /dev/null
+++ b/wmbiff/test_tlscomm.c
@@ -0,0 +1,67 @@
+#include <stdio.h>
+#include <sys/types.h>
+#include <sys/time.h>
+
+int debug_default = 2;
+
+int indices[12];
+char *sequence[][4] = { 
+  { NULL, NULL, NULL, NULL },
+  { "prefix", " hello", NULL },
+  { "pre", "fix", " hello", NULL },
+};
+
+/* trick tlscomm into believing it can read. */
+int read(int s, char *buf, int buflen) {
+  int val = indices[s]++;
+
+  if(sequence[s][val] == NULL) {
+    indices[s]--; /* make it stay */
+    return 0;
+  } else {
+    strncpy(buf, sequence[s][val], buflen);
+    printf("read: %s\n", sequence[s][val]);
+    return(strlen(sequence[s][val]));
+  }
+}
+
+int select(int nfds, fd_set *r, fd_set *w, fd_set *x, struct timeval *tv) {
+  int i;
+  int ready;
+  for(i=0;i<nfds;i++) {
+    if(FD_ISSET(i,r) && sequence[i][indices[i]] != NULL) {
+      ready++;
+    } else {
+      FD_CLR(i,r);
+    }
+  }
+  if(ready == 0) {
+    printf("botched.\n");
+  }
+}
+
+#define BUF_SIZE 1024
+
+struct connection_state {
+	int sd;
+	char *name;
+	/*@null@ */ void *tls_state;
+	/*@null@ */ void *xcred;
+	char unprocessed[BUF_SIZE];
+	void * pc;					/* mailbox handle for debugging messages */
+};
+
+int main(int argc, char **argv) {
+  char buf[255];
+  struct connection_state scs;
+  scs.name = "test";
+  scs.unprocessed[0] = '\0';
+  scs.pc = NULL;
+  // alarm(10);
+  
+  for(scs.sd = 1; scs.sd < 3; scs.sd++) {
+    memset(scs.unprocessed, 0, BUF_SIZE);
+    printf("%d\n", tlscomm_expect(&scs, "prefix", buf, 255));
+  }
+  
+}
diff --git a/wmbiff/tlsComm.c b/wmbiff/tlsComm.c
index 2f37909..52717a0 100644
--- a/wmbiff/tlsComm.c
+++ b/wmbiff/tlsComm.c
@@ -124,7 +124,9 @@ static int wait_for_it(int sd, int timeoutseconds)
 	return (FD_ISSET(sd, &readfds));
 }
 
-int getline_from_buffer(char *readbuffer, char *linebuffer, int linebuflen)
+/* exported for testing */
+extern int
+getline_from_buffer(char *readbuffer, char *linebuffer, int linebuflen)
 {
 	char *p, *q;
 	int i;
@@ -138,6 +140,11 @@ int getline_from_buffer(char *readbuffer, char *linebuffer, int linebuflen)
 		   the newline */
 		i++;
 		p++;
+	} else {
+		/* TODO -- perhaps we should return no line at all
+		   here, as it might be incomplete.  don't want to
+		   break anything though. */
+		DMA(DEBUG_INFO, "expected line doesn't end on its own.\n");
 	}
 
 	if (i != 0) {
@@ -173,7 +180,7 @@ int tlscomm_expect(struct connection_state *scs,
 				   const char *prefix, char *linebuf, int buflen)
 {
 	int prefixlen = (int) strlen(prefix);
-	int readbytes = -1;
+	int readbytes = 0;
 	memset(linebuf, 0, buflen);
 	TDM(DEBUG_INFO, "%s: expecting: %s\n", scs->name, prefix);
 	/*     if(scs->unprocessed[0]) {
@@ -181,27 +188,32 @@ int tlscomm_expect(struct connection_state *scs,
 	   } */
 	while (scs->unprocessed[0] != '\0'
 		   || wait_for_it(scs->sd, EXPECT_TIMEOUT)) {
-		if (scs->unprocessed[0] == '\0') {
+		if (scs->unprocessed[readbytes] == '\0') {
+			int thisreadbytes;
 #ifdef USE_GNUTLS
 			if (scs->tls_state) {
 				/* BUF_SIZE - 1 leaves room for trailing \0 */
-				readbytes =
-					gnutls_read(scs->tls_state, scs->unprocessed,
-								BUF_SIZE - 1);
-				if (readbytes < 0) {
-					handle_gnutls_read_error(readbytes, scs);
+				thisreadbytes =
+					gnutls_read(scs->tls_state,
+								&scs->unprocessed[readbytes],
+								BUF_SIZE - 1 - readbytes);
+				if (thisreadbytes < 0) {
+					handle_gnutls_read_error(thisreadbytes, scs);
 					return 0;
 				}
 			} else
 #endif
 			{
-				readbytes = read(scs->sd, scs->unprocessed, BUF_SIZE - 1);
-				if (readbytes < 0) {
+				thisreadbytes =
+					read(scs->sd, &scs->unprocessed[readbytes],
+						 BUF_SIZE - 1 - readbytes);
+				if (thisreadbytes < 0) {
 					TDM(DEBUG_ERROR, "%s: error reading: %s\n", scs->name,
 						strerror(errno));
 					return 0;
 				}
 			}
+			readbytes += thisreadbytes;
 			/* force null termination */
 			scs->unprocessed[readbytes] = '\0';
 			if (readbytes == 0) {
@@ -432,7 +444,8 @@ struct connection_state *initialize_gnutls(int sd, char *name, Pop3 pc,
 
 		gnutls_cred_set(scs->tls_state, GNUTLS_CRD_CERTIFICATE,
 						scs->xcred);
-		gnutls_transport_set_ptr(scs->tls_state, sd);
+		gnutls_transport_set_ptr(scs->tls_state,
+								 (gnutls_transport_ptr) sd);
 		do {
 			zok = gnutls_handshake(scs->tls_state);
 		} while (zok == GNUTLS_E_INTERRUPTED || zok == GNUTLS_E_AGAIN);

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-wmaker/wmbiff.git



More information about the Pkg-wmaker-commits mailing list