Annotation of embedaddon/strongswan/src/libstrongswan/tests/suites/test_watcher.c, revision 1.1

1.1     ! misho       1: /*
        !             2:  * Copyright (C) 2013 Martin Willi
        !             3:  * Copyright (C) 2013 revosec AG
        !             4:  *
        !             5:  * This program is free software; you can redistribute it and/or modify it
        !             6:  * under the terms of the GNU General Public License as published by the
        !             7:  * Free Software Foundation; either version 2 of the License, or (at your
        !             8:  * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
        !             9:  *
        !            10:  * This program is distributed in the hope that it will be useful, but
        !            11:  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
        !            12:  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
        !            13:  * for more details.
        !            14:  */
        !            15: 
        !            16: #include "test_suite.h"
        !            17: 
        !            18: #include <library.h>
        !            19: 
        !            20: #include <unistd.h>
        !            21: #include <errno.h>
        !            22: 
        !            23: static char testbuf[1] = "";
        !            24: 
        !            25: static bool readcb(void *data, int fd, watcher_event_t event)
        !            26: {
        !            27:        ck_assert_int_eq(*(int*)data, fd);
        !            28:        ck_assert_int_eq(event, WATCHER_READ);
        !            29: 
        !            30:        if (recv(fd, testbuf, 1, MSG_DONTWAIT) != 1)
        !            31:        {
        !            32:                ck_assert(errno == EAGAIN || errno == EWOULDBLOCK);
        !            33:        }
        !            34:        return TRUE;
        !            35: }
        !            36: 
        !            37: START_TEST(test_read)
        !            38: {
        !            39:        int fd[2];
        !            40:        char c;
        !            41: 
        !            42:        lib->processor->set_threads(lib->processor, 8);
        !            43: 
        !            44:        ck_assert(socketpair(AF_UNIX, SOCK_STREAM, 0, fd) != -1);
        !            45: 
        !            46:        lib->watcher->add(lib->watcher, fd[0], WATCHER_READ, readcb, &fd[0]);
        !            47: 
        !            48:        for (c = 'a'; c <= 'z'; c++)
        !            49:        {
        !            50:                ck_assert_int_eq(send(fd[1], &c, 1, 0), 1);
        !            51:                while (testbuf[0] != c)
        !            52:                {
        !            53:                        sched_yield();
        !            54:                }
        !            55:        }
        !            56: 
        !            57:        lib->watcher->remove(lib->watcher, fd[0]);
        !            58:        close(fd[0]);
        !            59:        close(fd[1]);
        !            60: 
        !            61:        lib->processor->cancel(lib->processor);
        !            62: }
        !            63: END_TEST
        !            64: 
        !            65: static bool writecb(void *data, int fd, watcher_event_t event)
        !            66: {
        !            67:        ck_assert_int_eq(event, WATCHER_WRITE);
        !            68:        if (send(fd, data, 1, MSG_DONTWAIT) != 1)
        !            69:        {
        !            70:                ck_assert(errno == EAGAIN || errno == EWOULDBLOCK);
        !            71:        }
        !            72:        return TRUE;
        !            73: }
        !            74: 
        !            75: START_TEST(test_write)
        !            76: {
        !            77:        int fd[2];
        !            78:        char in = 'x', out;
        !            79: 
        !            80:        lib->processor->set_threads(lib->processor, 8);
        !            81: 
        !            82:        ck_assert(socketpair(AF_UNIX, SOCK_STREAM, 0, fd) != -1);
        !            83: 
        !            84:        lib->watcher->add(lib->watcher, fd[1], WATCHER_WRITE, writecb, &in);
        !            85: 
        !            86:        ck_assert_int_eq(recv(fd[0], &out, 1, 0), 1);
        !            87:        ck_assert_int_eq(out, in);
        !            88: 
        !            89:        lib->watcher->remove(lib->watcher, fd[1]);
        !            90:        close(fd[1]);
        !            91:        close(fd[0]);
        !            92: 
        !            93:        lib->processor->cancel(lib->processor);
        !            94: }
        !            95: END_TEST
        !            96: 
        !            97: static bool multiread(void *data, int fd, watcher_event_t event)
        !            98: {
        !            99:        ck_assert_int_eq(event, WATCHER_READ);
        !           100:        if (recv(fd, data, 1, MSG_DONTWAIT) != 1)
        !           101:        {
        !           102:                ck_assert(errno == EAGAIN || errno == EWOULDBLOCK);
        !           103:        }
        !           104:        return TRUE;
        !           105: }
        !           106: 
        !           107: START_TEST(test_multiread)
        !           108: {
        !           109:        int fd[10][2], i;
        !           110:        char in, out[countof(fd)];
        !           111: 
        !           112:        lib->processor->set_threads(lib->processor, 8);
        !           113: 
        !           114:        for (i = 0; i < countof(fd); i++)
        !           115:        {
        !           116:                ck_assert(socketpair(AF_UNIX, SOCK_STREAM, 0, fd[i]) != -1);
        !           117:                lib->watcher->add(lib->watcher, fd[i][0],
        !           118:                                                  WATCHER_READ, multiread, &out[i]);
        !           119:        }
        !           120: 
        !           121:        for (i = 0; i < countof(fd); i++)
        !           122:        {
        !           123:                for (in = 'a'; in <= 'z'; in++)
        !           124:                {
        !           125:                        ck_assert_int_eq(send(fd[i][1], &in, 1, 0), 1);
        !           126:                        while (out[i] != in)
        !           127:                        {
        !           128:                                sched_yield();
        !           129:                        }
        !           130:                }
        !           131:        }
        !           132: 
        !           133:        for (i = 0; i < countof(fd); i++)
        !           134:        {
        !           135:                lib->watcher->remove(lib->watcher, fd[i][0]);
        !           136:                close(fd[i][1]);
        !           137:                close(fd[i][0]);
        !           138:        }
        !           139: 
        !           140:        lib->processor->cancel(lib->processor);
        !           141: }
        !           142: END_TEST
        !           143: 
        !           144: static bool multiwrite(void *data, int fd, watcher_event_t event)
        !           145: {
        !           146:        ck_assert_int_eq(event, WATCHER_WRITE);
        !           147:        if (send(fd, data, 1, MSG_DONTWAIT) != 1)
        !           148:        {
        !           149:                ck_assert(errno == EAGAIN || errno == EWOULDBLOCK);
        !           150:        }
        !           151:        return TRUE;
        !           152: }
        !           153: 
        !           154: START_TEST(test_multiwrite)
        !           155: {
        !           156:        int fd[10][2], i, j;
        !           157:        u_char out, in[countof(fd)];
        !           158: 
        !           159:        lib->processor->set_threads(lib->processor, 8);
        !           160: 
        !           161:        for (i = 0; i < countof(fd); i++)
        !           162:        {
        !           163:                ck_assert(socketpair(AF_UNIX, SOCK_STREAM, 0, fd[i]) != -1);
        !           164:                in[i] = i;
        !           165:                lib->watcher->add(lib->watcher, fd[i][1],
        !           166:                                                  WATCHER_WRITE, multiwrite, &in[i]);
        !           167:        }
        !           168: 
        !           169:        for (j = 0; j < 10; j++)
        !           170:        {
        !           171:                for (i = 0; i < countof(fd); i++)
        !           172:                {
        !           173:                        ck_assert_int_eq(recv(fd[i][0], &out, 1, 0), 1);
        !           174:                        ck_assert_int_eq(out, i);
        !           175:                }
        !           176:        }
        !           177: 
        !           178:        for (i = 0; i < countof(fd); i++)
        !           179:        {
        !           180:                lib->watcher->remove(lib->watcher, fd[i][1]);
        !           181:                close(fd[i][1]);
        !           182:                close(fd[i][0]);
        !           183:        }
        !           184: 
        !           185:        lib->processor->cancel(lib->processor);
        !           186: }
        !           187: END_TEST
        !           188: 
        !           189: Suite *watcher_suite_create()
        !           190: {
        !           191:        Suite *s;
        !           192:        TCase *tc;
        !           193: 
        !           194:        s = suite_create("watcher");
        !           195: 
        !           196:        tc = tcase_create("read");
        !           197:        tcase_add_test(tc, test_read);
        !           198:        suite_add_tcase(s, tc);
        !           199: 
        !           200:        tc = tcase_create("write");
        !           201:        tcase_add_test(tc, test_write);
        !           202:        suite_add_tcase(s, tc);
        !           203: 
        !           204:        tc = tcase_create("multiread");
        !           205:        tcase_add_test(tc, test_multiread);
        !           206:        suite_add_tcase(s, tc);
        !           207: 
        !           208:        tc = tcase_create("multiwrite");
        !           209:        tcase_add_test(tc, test_multiwrite);
        !           210:        suite_add_tcase(s, tc);
        !           211: 
        !           212:        return s;
        !           213: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>