--- sa_test.c 2001/10/10 15:01:56 1.8
+++ sa_test.c 2001/10/10 15:50:31 1.9
@@ -38,35 +38,96 @@
#include "ts.h"
#include "sa.h"
-TST_FUNC(test_sa)
-{
-}
-
-TST_FUNC(test_saa)
+TST_FUNC(test_saa_impexp)
{
sa_addr_t *saa;
sa_rc_t rv;
- char *cp1;
- char *cp2;
+ char *cp;
+ int i;
+ struct {
+ char *in;
+ sa_rc_t rv;
+ char *out;
+ } table[] = {
+ /* positive tests */
+ { "inet://0.0.0.0:0", SA_OK, "inet://0.0.0.0:0" },
+ { "inet://127.0.0.1:514", SA_OK, "inet://127.0.0.1:514" },
+ { "inet://localhost:syslog#udp", SA_OK, "inet://127.0.0.1:514" },
+ { "inet://localhost:smtp", SA_OK, "inet://127.0.0.1:25" },
+ { "unix:/tmp/socket", SA_OK, "unix:/tmp/socket" },
+ /* negative tests */
+ { "inet:", SA_ERR_ARG, NULL },
+ { "inet://1.2.3.4.5:0", SA_ERR_ARG, NULL },
+ { "inet://just-hostname", SA_ERR_ARG, NULL },
+ { "unix:", SA_ERR_ARG, NULL }
+ };
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_fail(TST, "sa_addr_create -> %d[%s] (expected %d[%s])",
+ rv, sa_error(rv), SA_OK, sa_error(SA_OK));
+ for (i = 0; i < sizeof(table)/sizeof(table[0]); i++) {
+ tst_check(TST, "sa_addr_u2a(\"%s\")", table[i].in);
+ if ((rv = sa_addr_u2a(saa, table[i].in)) != table[i].rv)
+ tst_fail(TST, "sa_addr_a2u -> %d[%s] (expected %d[%s])",
+ rv, sa_error(rv), table[i].rv, sa_error(table[i].rv));
+ tst_check(TST, "sa_addr_a2u");
+ if ((rv = sa_addr_a2u(saa, &cp)) != SA_OK)
+ tst_fail(TST, "sa_addr_u2a -> %d[%s] (expected %d[%s])",
+ rv, sa_error(rv), SA_OK, sa_error(SA_OK));
+ if (table[i].rv == SA_OK)
+ if (strcmp(cp, table[i].out) != 0)
+ tst_fail(TST, "sa_addr_a2u -> \"%s\" (expected \"%s\")", cp, table[i].out);
+ }
tst_check(tst, "sa_addr_destroy");
if ((rv = sa_addr_destroy(saa)) != SA_OK)
- tst_fail(TST, "rv=%d (%s)", rv, sa_error(rv));
+ tst_fail(TST, "sa_addr_destroy -> %d[%s] (expected %d[%s])",
+ rv, sa_error(rv), SA_OK, sa_error(SA_OK));
+}
+
+TST_FUNC(test_saa_match)
+{
+ sa_addr_t *saa1;
+ sa_addr_t *saa2;
+ sa_rc_t rv;
+ int i;
+ struct {
+ char *in;
+ char *acl;
+ int prefixlen;
+ sa_rc_t rv;
+ } table[] = {
+ { "unix:/foo/bar", "unix:/foo/bar", -1, SA_OK },
+ { "unix:/foo/bar", "unix:/foo/bar", 0, SA_OK },
+ { "unix:/foo/bar", "unix:/foo", 4, SA_OK },
+ { "unix:/foo/bar", "unix:/foo/quux", 4, SA_OK },
+ { "unix:/foo/bar", "unix:/baz/quux", -1, SA_ERR_MTC },
+ { "inet://0.0.0.0:0", "inet://0.0.0.0:0", 0, SA_OK },
+ { "inet://127.0.0.1:514", "inet://127.0.0.1:514", -1, SA_OK },
+ { "inet://127.0.0.1:514", "inet://0.0.0.0:0", 0, SA_OK },
+ { "inet://127.0.0.1:514", "inet://12.34.56.78:9", 0, SA_OK },
+ { "inet://127.0.0.1:514", "inet://12.34.56.78:9", -1, SA_ERR_MTC },
+ { "inet://127.0.0.1:514", "inet://127.0.0.0:0", 24, SA_OK },
+ { "inet://127.0.0.1:514", "inet://127.0.0.0:0", 32, SA_ERR_MTC },
+ { "inet://141.1.23.20:25", "inet://141.1.23.40:25", 32, SA_ERR_MTC },
+ { "inet://141.1.23.20:25", "inet://141.1.23.40:25", 24, SA_OK }
+ };
+
+ sa_addr_create(&saa1);
+ sa_addr_create(&saa2);
+ for (i = 0; i < sizeof(table)/sizeof(table[0]); i++) {
+ if ((rv = sa_addr_u2a(saa1, table[i].in)) != SA_OK)
+ continue;
+ if ((rv = sa_addr_u2a(saa2, table[i].acl)) != SA_OK)
+ continue;
+ tst_check(TST, "sa_addr_match(\"%s\", \"%s\", %d)",
+ table[i].in, table[i].acl, table[i].prefixlen);
+ if ((rv = sa_addr_match(saa1, saa2, table[i].prefixlen)) != table[i].rv)
+ tst_fail(TST, "sa_addr_match -> %d[%s] (expected %d[%s])",
+ rv, sa_error(rv), table[i].rv, sa_error(table[i].rv));
+ }
+ sa_addr_destroy(saa1);
+ sa_addr_destroy(saa2);
}
int main(int argc, char *argv[])
@@ -75,8 +136,8 @@
int n;
ts = ts_new("OSSP SA (Socket Abstraction Library)");
- ts_test(ts, test_saa, "socket address abstraction");
- ts_test(ts, test_sa, "socket abstraction");
+ ts_test(ts, test_saa_impexp, "socket address abstraction (import/export)");
+ ts_test(ts, test_saa_match, "socket address abstraction (matching)");
n = ts_run(ts);
ts_free(ts);
return n;
|