Main Page | Data Structures | File List | Data Fields | Globals | Related Pages

ntest.c

Go to the documentation of this file.
00001 #include <neuro/neuro.h>
00002 #include <unistd.h>
00003 #include <neuro/ns2net.h>
00004 #include <string.h>
00005 #include <search.h>
00006 
00007 struct NSCounter { int success, timedOut, refused, unknownHost, bindSuccess, bindError; };
00008 
00009 struct NSNetConnectionController *bindNSCC, *connectNSCC;
00010 
00011 #define MAXCHAR 1024
00012 
00013 char readBuffer[MAXCHAR];
00014 int readLen;
00015 static void clearReadBuffer(void)
00016 {
00017   readLen = 0;
00018 }
00019 
00020 static void readBufferMustMatch(const char *str)
00021 {
00022   rassert(readLen == strlen(str));
00023   rassert(strncmp(str, readBuffer, strlen(str)) == 0);
00024 }
00025 
00026 
00027 static void NSRHBytesRead(void *udata, const char *buf, int len)
00028 {
00029     int i;
00030     for (i = 0; i < len; ++i) {
00031       rassert(readLen < MAXCHAR);
00032       readBuffer[readLen] = buf[i];
00033       readLen += 1;
00034       putchar(buf[i]);
00035     }
00036 }
00037 
00038 static void NSRHClosed(void *udata)
00039 {
00040   printf("NSRH Stream closed.\n");
00041 }
00042 
00043 static void NSBHBindError(void *udata) {
00044   struct NSCounter *count = (struct NSCounter *) udata;
00045   count->bindError += 1;
00046 }
00047 
00048 static void NSCHUnknownHost(void *udata) {
00049   struct NSCounter *count = (struct NSCounter *) udata;
00050   count->unknownHost += 1;
00051 }
00052 
00053 static void NSCHTimedOut(void *udata) {
00054   struct NSCounter *count = (struct NSCounter *) udata;
00055   count->timedOut += 1;
00056 }
00057 
00058 static void NSCHRefused(void *udata) {
00059   struct NSCounter *count = (struct NSCounter *) udata;
00060   count->refused += 1;
00061 }
00062 
00063 static struct NSNetConnectionReadHandler nsrh;
00064 
00065 static void NSBHBindSuccess(void *udata, struct NSNetConnectionController *nscc) {
00066   struct NSCounter *count = (struct NSCounter *) udata;
00067   int retval;
00068   count->bindSuccess += 1;
00069   retval = attachConnectionReadHandler(nscc, &nsrh, "bindsuccess");
00070   rassert(retval == 0);
00071   bindNSCC = nscc;
00072 }
00073 
00074 static void NSCHSuccess(void *udata, struct NSNetConnectionController *nscc) {
00075   int retval;
00076   struct NSCounter *count = (struct NSCounter *) udata;
00077   count->success += 1;
00078   retval = attachConnectionReadHandler(nscc, &nsrh, "connectsuccess");
00079   connectNSCC = nscc;
00080 }
00081 
00082 static void testNSConnect(void) {
00083   int portNum = 7211;
00084   struct NSCounter basic = { 0, 0, 0 };
00085   struct NSCounter cur;
00086   struct NSNetConnectionHandler nsch;
00087   struct NSNetBindHandler nsbh;
00088   struct NSNet *ns;
00089 
00090   nsrh.bytesRead = NSRHBytesRead;
00091   nsrh.closed = NSRHClosed;
00092 
00093   nsch.success = NSCHSuccess;
00094   nsch.timedOut = NSCHTimedOut;
00095   nsch.refused = NSCHRefused;
00096   nsch.unknownHost = NSCHUnknownHost;
00097 
00098   nsbh.success = NSBHBindSuccess;
00099   nsbh.error = NSBHBindError;
00100 
00101   cur = basic;
00102   ns = newNSNet();
00103   attemptConnect(ns, &nsch, "notAValidHostName", 5555, &cur);
00104   rassert(cur.success == 0 && cur.unknownHost == 1 && cur.refused == 0 && cur.timedOut == 0);
00105   cur = basic;
00106   rassert(0 == attemptConnect(ns, &nsch, "localhost", 5555, &cur));
00107   waitForNetEvent(ns, 2000);
00108   rassert(cur.success == 0 && cur.unknownHost == 0 && cur.refused == 1 && cur.timedOut == 0);
00109   cur = basic;
00110   rassert(0 == attemptBind(ns, &nsbh, 1, portNum, &cur));
00111   rassert(cur.bindSuccess == 0 && cur.bindError == 0);
00112   rassert(0 == attemptConnect(ns, &nsch, "localhost", portNum, &cur));
00113   waitForNetEvent(ns, 1000);
00114   rassert(cur.success == 1 && cur.unknownHost == 0 && cur.refused == 0 && cur.timedOut == 0);
00115   rassert(cur.bindSuccess == 1 && cur.bindError == 0);
00116   cur = basic;
00117   rassert(0 == attemptBind(ns, &nsbh, 0, portNum+1, &cur));
00118   rassert(cur.bindSuccess == 0 && cur.bindError == 0);
00119   rassert(0 == attemptConnect(ns, &nsch, "localhost", portNum+1, &cur));
00120   waitForNetEvent(ns, 1000);
00121   rassert(cur.bindSuccess == 1 && cur.bindError == 0);
00122   rassert(connectNSCC != NULL);
00123   rassert(bindNSCC != NULL);
00124   clearReadBuffer();
00125   { char *msg = "msgtoserver"; writeNSBytes(connectNSCC, msg, strlen(msg)); }
00126   waitForNetEvent(ns, 1000);
00127   readBufferMustMatch("msgtoserver");
00128   clearReadBuffer();
00129   { char *msg = "msgtoclient!"; 
00130     while (*msg) {
00131       writeNSBytes(bindNSCC, msg, 1);
00132       waitForNetEvent(ns, 1000);
00133       msg += 1;
00134     }
00135   }
00136   readBufferMustMatch("msgtoclient!");
00137 }
00138 
00139 static void testNSNet(void)
00140 {
00141   testNSConnect();
00142   if (fork()) {
00143   }
00144   else {
00145   }
00146 }
00147 
00148 static int allSum = 0;
00149 static void allStringTester
00150   (struct StringTable *st, const char *key, void *val, void *udata)
00151 {
00152   allSum += key[0] - '0';
00153 }
00154 
00155 static void testStringTable(void)
00156 {
00157   int val;
00158   struct StringTable *st;
00159   int i;
00160   for (i = 0; i < 100; i += 1) {
00161     st = newStringTable();
00162     rassert(st);
00163     rassert(findString(st, "dog") == NULL);
00164     rassert(putString(st, "cat", &val) == 0);
00165     rassert(findString(st, "cat") == &val);
00166     rassert(delString(st, "dog") == ERR_NOSTRING);
00167     rassert(delString(st, "cat") == 0);
00168     rassert(findString(st, "cat") == NULL);
00169     rassert(putString(st, "1", &val) == 0);
00170     rassert(putString(st, "2", &val) == 0);
00171     rassert(putString(st, "3", &val) == 0);
00172     allSum = 0;
00173     allStrings(st, allStringTester, NULL);
00174     rassert(allSum == 6);
00175     rassert(delString(st, "1") == 0);
00176     rassert(delString(st, "3") == 0);
00177     rassert(delString(st, "2") == 0);
00178     rassert(putString(st, "memcheck", &val) == 0);
00179     freeStringTable(st);
00180     }
00181 }
00182 
00183 static const char *gotParm;
00184 static int gotInt;
00185 static int gotUnknown;
00186 
00187 static void handlePrint(struct CommandHandler *ch, int cliIndex) {
00188   gotParm = fetchStringParameter(ch, 0);
00189   fetchIntParameters(ch, &gotInt, 1);
00190 }
00191 
00192 static void handleUnknown(struct CommandHandler *ch, int cliIndex) {
00193   gotUnknown = 1;
00194 }
00195 
00196 static void testCommandHandler(void)
00197 {
00198   struct CommandHandler *ch;
00199   gotUnknown = 0;
00200   gotParm = NULL;
00201   ch = newCommandHandler();
00202   newClientStarted(ch, 3);
00203   handleLine(ch, "what", 3);
00204   enregisterCommand(ch, "unknown", handleUnknown);
00205   enregisterCommand(ch, "print", handlePrint);
00206   rassert(gotUnknown == 0);
00207   handleLine(ch, "what", 3);
00208   rassert(gotUnknown == 1);
00209   handleLine(ch, "print 0", 3);
00210   rassert(gotParm != NULL);
00211   rassert(strcmp(gotParm, "0") == 0);
00212   rassert(gotInt == 0);
00213   handleLine(ch, "print \"\"", 3);
00214   rassert(strcmp(gotParm, "") == 0);
00215   rassert(gotInt == 0);
00216   handleLine(ch, "print \\\"", 3);
00217   rassert(strcmp(gotParm, "\"") == 0);
00218   rassert(gotInt == 0);
00219   handleLine(ch, "print 87", 3);
00220   rassert(gotInt == 87);
00221   rassert(strcmp(gotParm, "87") == 0);
00222   rassert(strcmp(gotParm, "87") == 0);
00223   handleLine(ch, "print \"45\"", 3);
00224   handleLine(ch, "print \"48\" ", 3);
00225   handleLine(ch, "print    \"14\" ", 3);
00226   handleLine(ch, "print   11  ", 3);
00227   handleLine(ch, "print \"hi\"", 3);
00228   handleLine(ch, "print \" hi\"", 3);
00229   handleLine(ch, "print \"h i\"", 3);
00230   handleLine(ch, "print \"hi \"", 3);
00231   handleLine(ch, "print \"h\\\\i\"", 3);
00232   handleLine(ch, "print \"h\\\"i\"", 3);
00233   freeCommandHandler(ch);
00234 }
00235 
00236 int main(int argc, char **argv)
00237 {
00238   testStringTable();
00239   testCommandHandler();
00240   testNSNet();
00241   return 0;
00242 }

Generated on Tue Feb 8 00:05:16 2005 for Neuroserver by doxygen 1.3.3