[Spice-devel] [RFC] server: add very basic tests including a working do nothing server, for channel cleanup tests

Hans de Goede hdegoede at redhat.com
Wed Nov 3 00:48:15 PDT 2010


Hi,

On 11/03/2010 12:54 AM, Alon Levy wrote:
> Not really an RFC, just some tests I have that I wanted to know
>   - commit as is?
>   - or with fitting Makefile.am?

I think committing them with a fitting Makefile.am, and a README on
how to use them would be best.

>   - or get some "make test" into spice (requires running a client process, or making a mock client, or some interface to it in process, all doable with various amounts of work)

Having a make test would of course be even better, but I can understand
that that is going to be quite a lot of work.

Regards,

Hans

>
> Anyway they helped me debug splitting the main channel (patches to follow) as is.
>
> ---
>   server/tests/Makefile                              |   11 ++
>   server/tests/template_basic_event_loop_no_timers.c |  154 ++++++++++++++++++++
>   server/tests/test_empty_success.c                  |   57 +++++++
>   server/tests/test_fail_on_null_core_interface.c    |   13 ++
>   server/tests/test_startup.c                        |  154 ++++++++++++++++++++
>   5 files changed, 389 insertions(+), 0 deletions(-)
>   create mode 100644 server/tests/Makefile
>   create mode 100644 server/tests/template_basic_event_loop_no_timers.c
>   create mode 100644 server/tests/test_empty_success.c
>   create mode 100644 server/tests/test_fail_on_null_core_interface.c
>   create mode 100644 server/tests/test_startup.c
>
> diff --git a/server/tests/Makefile b/server/tests/Makefile
> new file mode 100644
> index 0000000..fc3eacb
> --- /dev/null
> +++ b/server/tests/Makefile
> @@ -0,0 +1,11 @@
> +CFLAGS=`pkg-config spice-server --cflags` -O0 -ggdb -std=c99
> +LDFLAGS=`pkg-config spice-server --libs`
> +
> +TESTS=test_fail_on_null_core_interface test_empty_success test_startup
> +
> +all: $(TESTS)
> +
> +.PHONY: clean
> +
> +clean:
> +	rm -f $(TESTS)
> diff --git a/server/tests/template_basic_event_loop_no_timers.c b/server/tests/template_basic_event_loop_no_timers.c
> new file mode 100644
> index 0000000..4b15e94
> --- /dev/null
> +++ b/server/tests/template_basic_event_loop_no_timers.c
> @@ -0,0 +1,154 @@
> +#include<strings.h>
> +#include<stdio.h>
> +#include<stdlib.h>
> +//#include<unistd.h>
> +//#include<sys/types.h>
> +#include<sys/select.h>
> +#include<spice.h>
> +
> +#define NOT_IMPLEMENTED printf("%s not implemented\n", __func__);
> +
> +SpiceTimer* timer_add(SpiceTimerFunc func, void *opaque)
> +{
> +    NOT_IMPLEMENTED
> +}
> +
> +void timer_start(SpiceTimer *timer, uint32_t ms)
> +{
> +    NOT_IMPLEMENTED
> +}
> +
> +void timer_cancel(SpiceTimer *timer)
> +{
> +    NOT_IMPLEMENTED
> +}
> +
> +void timer_remove(SpiceTimer *timer)
> +{
> +    NOT_IMPLEMENTED
> +}
> +
> +struct SpiceWatch {
> +    int id;
> +};
> +
> +typedef struct Watch {
> +    SpiceWatch id;
> +    int fd;
> +    int event_mask;
> +    SpiceWatchFunc func;
> +    void *opaque;
> +} Watch;
> +
> +Watch watches[100];
> +int watch_count = 0;
> +int next_id = 1;
> +
> +SpiceWatch *watch_add(int fd, int event_mask, SpiceWatchFunc func, void *opaque)
> +{
> +    watches[watch_count].fd = fd;
> +    watches[watch_count].event_mask = event_mask;
> +    watches[watch_count].func = func;
> +    watches[watch_count].opaque = opaque;
> +    watches[watch_count].id.id = next_id++;
> +    return&watches[watch_count++].id;
> +}
> +
> +void watch_update_mask(SpiceWatch *watch, int event_mask)
> +{
> +    int i;
> +    Watch *my_watch;
> +
> +    for (i = 0 ; i<  watch_count; ++i) {
> +        if (watches[i].id.id == watch->id) {
> +            my_watch =&watches[i];
> +            if (my_watch->event_mask != event_mask) {
> +                my_watch->event_mask = event_mask;
> +            }
> +            return;
> +        }
> +    }
> +}
> +
> +void watch_remove(SpiceWatch *watch)
> +{
> +    int i;
> +
> +    for (i = 0 ; i<  watch_count ; ++i) {
> +        if (watches[i].id.id == watch->id) {
> +            watches[i] = watches[--watch_count];
> +            return;
> +        }
> +    }
> +}
> +
> +void channel_event(int event, SpiceChannelEventInfo *info)
> +{
> +    NOT_IMPLEMENTED
> +}
> +
> +void mainloop()
> +{
> +    fd_set rfds, wfds;
> +    int max_fd = -1;
> +    int i;
> +    int retval;
> +
> +    while (1) {
> +        FD_ZERO(&rfds);
> +        FD_ZERO(&wfds);
> +        for (i = 0 ; i<  watch_count; ++i) {
> +            Watch* watch =&watches[i];
> +            if (watch->event_mask&  SPICE_WATCH_EVENT_READ) {
> +                FD_SET(watch->fd,&rfds);
> +                max_fd = watch->fd>  max_fd ? watch->fd : max_fd;
> +            }
> +            if (watch->event_mask&  SPICE_WATCH_EVENT_WRITE) {
> +                FD_SET(watch->fd,&wfds);
> +                max_fd = watch->fd>  max_fd ? watch->fd : max_fd;
> +            }
> +        }
> +        retval = select(max_fd + 1,&rfds,&wfds, NULL, NULL);
> +        if (retval == -1) {
> +            printf("error in select - exiting\n");
> +            exit(-1);
> +        }
> +        if (retval) {
> +            for (int i = 0 ; i<  watch_count; ++i) {
> +                Watch* watch =&watches[i];
> +                if ((watch->event_mask&  SPICE_WATCH_EVENT_READ)&&  FD_ISSET(watch->fd,&rfds)) {
> +                    watch->func(watch->fd, SPICE_WATCH_EVENT_READ, watch->opaque);
> +                }
> +                if ((watch->event_mask&  SPICE_WATCH_EVENT_WRITE)&&  FD_ISSET(watch->fd,&wfds)) {
> +                    watch->func(watch->fd, SPICE_WATCH_EVENT_WRITE, watch->opaque);
> +                }
> +            }
> +        }
> +    }
> +}
> +
> +int main()
> +{
> +    SpiceServer *server = spice_server_new();
> +    SpiceCoreInterface core;
> +
> +    bzero(&core, sizeof(core));
> +    core.base.major_version = SPICE_INTERFACE_CORE_MAJOR;
> +    core.timer_add = timer_add;
> +    core.timer_start = timer_start;
> +    core.timer_cancel = timer_cancel;
> +    core.timer_remove = timer_remove;
> +    core.watch_add = watch_add;
> +    core.watch_update_mask = watch_update_mask;
> +    core.watch_remove = watch_remove;
> +    core.channel_event = channel_event;
> +
> +    spice_server_set_port(server, 5911);
> +    spice_server_set_noauth(server);
> +    spice_server_init(server,&core);
> +
> +    mainloop();
> +
> +    return 0;
> +}
> +
> diff --git a/server/tests/test_empty_success.c b/server/tests/test_empty_success.c
> new file mode 100644
> index 0000000..e747e40
> --- /dev/null
> +++ b/server/tests/test_empty_success.c
> @@ -0,0 +1,57 @@
> +#include<strings.h>
> +#include<spice.h>
> +
> +SpiceTimer* timer_add(SpiceTimerFunc func, void *opaque)
> +{
> +}
> +
> +void timer_start(SpiceTimer *timer, uint32_t ms)
> +{
> +}
> +
> +void timer_cancel(SpiceTimer *timer)
> +{
> +}
> +
> +void timer_remove(SpiceTimer *timer)
> +{
> +}
> +
> +SpiceWatch *watch_add(int fd, int event_mask, SpiceWatchFunc func, void *opaque)
> +{
> +}
> +
> +void watch_update_mask(SpiceWatch *watch, int event_mask)
> +{
> +}
> +
> +void watch_remove(SpiceWatch *watch)
> +{
> +}
> +
> +void channel_event(int event, SpiceChannelEventInfo *info)
> +{
> +}
> +
> +int main()
> +{
> +    SpiceServer *server = spice_server_new();
> +    SpiceCoreInterface core;
> +
> +    bzero(&core, sizeof(core));
> +    core.base.major_version = SPICE_INTERFACE_CORE_MAJOR;
> +    core.timer_add = timer_add;
> +    core.timer_start = timer_start;
> +    core.timer_cancel = timer_cancel;
> +    core.timer_remove = timer_remove;
> +    core.watch_add = watch_add;
> +    core.watch_update_mask = watch_update_mask;
> +    core.watch_remove = watch_remove;
> +    core.channel_event = channel_event;
> +
> +    spice_server_set_port(server, 5911);
> +    spice_server_init(server,&core);
> +
> +    return 0;
> +}
> +
> diff --git a/server/tests/test_fail_on_null_core_interface.c b/server/tests/test_fail_on_null_core_interface.c
> new file mode 100644
> index 0000000..699b2a2
> --- /dev/null
> +++ b/server/tests/test_fail_on_null_core_interface.c
> @@ -0,0 +1,13 @@
> +#include<spice.h>
> +
> +int main()
> +{
> +    SpiceServer *server = spice_server_new();
> +    SpiceCoreInterface core;
> +
> +    spice_server_init(server,&core);
> +    spice_server_set_port(server, 5911);
> +
> +    return 0;
> +}
> +
> diff --git a/server/tests/test_startup.c b/server/tests/test_startup.c
> new file mode 100644
> index 0000000..4b15e94
> --- /dev/null
> +++ b/server/tests/test_startup.c
> @@ -0,0 +1,154 @@
> +#include<strings.h>
> +#include<stdio.h>
> +#include<stdlib.h>
> +//#include<unistd.h>
> +//#include<sys/types.h>
> +#include<sys/select.h>
> +#include<spice.h>
> +
> +#define NOT_IMPLEMENTED printf("%s not implemented\n", __func__);
> +
> +SpiceTimer* timer_add(SpiceTimerFunc func, void *opaque)
> +{
> +    NOT_IMPLEMENTED
> +}
> +
> +void timer_start(SpiceTimer *timer, uint32_t ms)
> +{
> +    NOT_IMPLEMENTED
> +}
> +
> +void timer_cancel(SpiceTimer *timer)
> +{
> +    NOT_IMPLEMENTED
> +}
> +
> +void timer_remove(SpiceTimer *timer)
> +{
> +    NOT_IMPLEMENTED
> +}
> +
> +struct SpiceWatch {
> +    int id;
> +};
> +
> +typedef struct Watch {
> +    SpiceWatch id;
> +    int fd;
> +    int event_mask;
> +    SpiceWatchFunc func;
> +    void *opaque;
> +} Watch;
> +
> +Watch watches[100];
> +int watch_count = 0;
> +int next_id = 1;
> +
> +SpiceWatch *watch_add(int fd, int event_mask, SpiceWatchFunc func, void *opaque)
> +{
> +    watches[watch_count].fd = fd;
> +    watches[watch_count].event_mask = event_mask;
> +    watches[watch_count].func = func;
> +    watches[watch_count].opaque = opaque;
> +    watches[watch_count].id.id = next_id++;
> +    return&watches[watch_count++].id;
> +}
> +
> +void watch_update_mask(SpiceWatch *watch, int event_mask)
> +{
> +    int i;
> +    Watch *my_watch;
> +
> +    for (i = 0 ; i<  watch_count; ++i) {
> +        if (watches[i].id.id == watch->id) {
> +            my_watch =&watches[i];
> +            if (my_watch->event_mask != event_mask) {
> +                my_watch->event_mask = event_mask;
> +            }
> +            return;
> +        }
> +    }
> +}
> +
> +void watch_remove(SpiceWatch *watch)
> +{
> +    int i;
> +
> +    for (i = 0 ; i<  watch_count ; ++i) {
> +        if (watches[i].id.id == watch->id) {
> +            watches[i] = watches[--watch_count];
> +            return;
> +        }
> +    }
> +}
> +
> +void channel_event(int event, SpiceChannelEventInfo *info)
> +{
> +    NOT_IMPLEMENTED
> +}
> +
> +void mainloop()
> +{
> +    fd_set rfds, wfds;
> +    int max_fd = -1;
> +    int i;
> +    int retval;
> +
> +    while (1) {
> +        FD_ZERO(&rfds);
> +        FD_ZERO(&wfds);
> +        for (i = 0 ; i<  watch_count; ++i) {
> +            Watch* watch =&watches[i];
> +            if (watch->event_mask&  SPICE_WATCH_EVENT_READ) {
> +                FD_SET(watch->fd,&rfds);
> +                max_fd = watch->fd>  max_fd ? watch->fd : max_fd;
> +            }
> +            if (watch->event_mask&  SPICE_WATCH_EVENT_WRITE) {
> +                FD_SET(watch->fd,&wfds);
> +                max_fd = watch->fd>  max_fd ? watch->fd : max_fd;
> +            }
> +        }
> +        retval = select(max_fd + 1,&rfds,&wfds, NULL, NULL);
> +        if (retval == -1) {
> +            printf("error in select - exiting\n");
> +            exit(-1);
> +        }
> +        if (retval) {
> +            for (int i = 0 ; i<  watch_count; ++i) {
> +                Watch* watch =&watches[i];
> +                if ((watch->event_mask&  SPICE_WATCH_EVENT_READ)&&  FD_ISSET(watch->fd,&rfds)) {
> +                    watch->func(watch->fd, SPICE_WATCH_EVENT_READ, watch->opaque);
> +                }
> +                if ((watch->event_mask&  SPICE_WATCH_EVENT_WRITE)&&  FD_ISSET(watch->fd,&wfds)) {
> +                    watch->func(watch->fd, SPICE_WATCH_EVENT_WRITE, watch->opaque);
> +                }
> +            }
> +        }
> +    }
> +}
> +
> +int main()
> +{
> +    SpiceServer *server = spice_server_new();
> +    SpiceCoreInterface core;
> +
> +    bzero(&core, sizeof(core));
> +    core.base.major_version = SPICE_INTERFACE_CORE_MAJOR;
> +    core.timer_add = timer_add;
> +    core.timer_start = timer_start;
> +    core.timer_cancel = timer_cancel;
> +    core.timer_remove = timer_remove;
> +    core.watch_add = watch_add;
> +    core.watch_update_mask = watch_update_mask;
> +    core.watch_remove = watch_remove;
> +    core.channel_event = channel_event;
> +
> +    spice_server_set_port(server, 5911);
> +    spice_server_set_noauth(server);
> +    spice_server_init(server,&core);
> +
> +    mainloop();
> +
> +    return 0;
> +}
> +


More information about the Spice-devel mailing list