--- sa_test.c 2001/10/08 15:08:46 1.7
+++ sa_test.c 2001/10/10 15:01:56 1.8
@@ -35,90 +35,50 @@
#include <sys/utsname.h>
#include <unistd.h>
+#include "ts.h"
#include "sa.h"
-#define DIE_MARK \
- __FILE__, __LINE__
-
-static void die(const char *file, int line, char *fmt, ...)
+TST_FUNC(test_sa)
{
- va_list ap;
+}
- va_start(ap, fmt);
- fprintf(stderr, "%s:%d:ERROR: ", file, line);
- vfprintf(stderr, fmt, ap);
- fprintf(stderr, "\n");
- va_end(ap);
- exit(1);
+TST_FUNC(test_saa)
+{
+ sa_addr_t *saa;
+ sa_rc_t rv;
+ char *cp1;
+ char *cp2;
+
+ tst_check(TST, "sa_addr_create");
+ if ((rv = sa_addr_create(&saa)) != SA_OK)
+ tst_fail(tst, "rv=%d (%s)", rv, sa_error(rv));
+
+ tst_check(TST, "sa_addr_u2a");
+ cp1 = "inet://127.0.0.1:12345";
+ if ((rv = sa_addr_u2a(saa, cp1)) != SA_OK)
+ tst_fail(TST, "rv=%d (%s)", rv, sa_error(rv));
+
+ tst_check(TST, "sa_addr_a2u");
+ if ((rv = sa_addr_a2u(saa, &cp2)) != SA_OK)
+ tst_fail(TST, "rv=%d (%s)", rv, sa_error(rv));
+ if (strcmp(cp1, cp2) != 0)
+ tst_fail(TST, "import \"%s\" <-> export \"%s\"", cp1, cp2);
+
+ tst_check(tst, "sa_addr_destroy");
+ if ((rv = sa_addr_destroy(saa)) != SA_OK)
+ tst_fail(TST, "rv=%d (%s)", rv, sa_error(rv));
}
-int main(int argc, char *argv[])
+int main(int argc, char *argv[])
{
- sa_addr_t *ra;
- sa_addr_t *la;
- sa_t *sa;
- char caBuf[1024];
- int nBuf;
- char caTime[15+1];
- time_t now;
- struct tm *tm;
- struct utsname uts;
- char *cp;
- char caTag[32+1];
- char *cpHost;
+ ts_t *ts;
int n;
- /* create remote address */
- if (sa_addr_create(&ra) != SA_OK)
- die(DIE_MARK, "sa_addr_create (ra)");
- if (sa_addr_u2a(ra, "inet://141.1.129.1:514") != SA_OK)
- die(DIE_MARK, "sa_addr_u2a (ra)");
-
- /* create local address */
- if (sa_addr_create(&la) != SA_OK)
- die(DIE_MARK, "sa_addr_create (la)");
- if (sa_addr_u2a(la, "inet://0.0.0.0:0") != SA_OK)
- die(DIE_MARK, "sa_addr_u2a (la)");
-
- /* create datagram socket */
- if (sa_create(&sa) != SA_OK)
- die(DIE_MARK, "sa_create");
- if (sa_type(sa, SA_TYPE_DATAGRAM) != SA_OK)
- die(DIE_MARK, "sa_type");
-
- /* bind socket to local address */
- if (sa_bind(sa, la) != SA_OK)
- die(DIE_MARK, "sa_bind");
-
- /* RFC3164: The BSD syslog Protocol; C. Lonvick; August 2001. */
- now = time(NULL);
- tm = localtime(&now);
- strftime(caTime, sizeof(caTime), "%b %e %H:%M:%S", tm);
- if (uname(&uts) == -1)
- die(DIE_MARK, "uname");
- cpHost = strdup(uts.nodename);
- if ((cp = strchr(cpHost, '.')) != NULL)
- *cp = '\0';
- strcpy(caTag, "progname[12]: ");
- if (strlen(caTag) > 32)
- caTag[32] = '\0';
- sprintf(caBuf, "<16>%s %s %s[%ld]: %s", caTime, cpHost,
- argv[0], (long)getpid(), "test for Internet Datagram socket");
- fprintf(stderr, "Send to syslog: \"%s\"\n", caBuf);
-
- /* send message to syslogd(8) */
- nBuf = strlen(caBuf);
- if (sa_send(sa, caBuf, nBuf, (size_t *)&n, ra) != SA_OK)
- die(DIE_MARK, "sa_writeto");
-
- /* destroy socket and address objects */
- if (sa_destroy(sa) != SA_OK)
- die(DIE_MARK, "sa_destroy");
- if (sa_addr_destroy(la) != SA_OK)
- die(DIE_MARK, "sa_addr_destroy (la)");
- if (sa_addr_destroy(ra) != SA_OK)
- die(DIE_MARK, "sa_addr_destroy (ra)");
-
- return 0;
+ ts = ts_new("OSSP SA (Socket Abstraction Library)");
+ ts_test(ts, test_saa, "socket address abstraction");
+ ts_test(ts, test_sa, "socket abstraction");
+ n = ts_run(ts);
+ ts_free(ts);
+ return n;
}
|