OSSP CVS Repository

ossp - ossp-pkg/xds/regression-tests/xds-register.c
Not logged in
[Honeypot]  [Browse]  [Directory]  [Home]  [Login
[Reports]  [Search]  [Ticket]  [Timeline
  [Raw

ossp-pkg/xds/regression-tests/xds-register.c
/*
   XDS - OSSP Extensible Data Serialization Library
   Copyright (c) 2001 The OSSP Project (http://www.ossp.org/)
   Copyright (c) 2001 Cable & Wireless Deutschland (http://www.cw.com/de/)

   This file is part of OSSP XDS, an extensible data serialization
   library which can be found at http://www.ossp.com/pkg/xds/.

   Permission to use, copy, modify, and distribute this software for
   any purpose with or without fee is hereby granted, provided that
   the above copyright notice and this permission notice appear in all
   copies.

   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
   MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   SUCH DAMAGE.
*/

#include <stdio.h>
#include <string.h>
#include "../internal.h"

static int dummy_engine(xds_t* xds, void* engine_context,
			void* buffer, size_t buffer_size, size_t* used_buffer_size,
			va_list* args)
    {
    return 0;
    }

int main()
    {
    const char* test_names[] =
	{
	"foo",
	"bar",
	"zarah",
	"caesar",
	"claus",
	"heinz",
	};
    size_t test_names_len = sizeof(test_names) / sizeof(char*);
    xds_t* xds;
    size_t i;

    /* Create context. */

    xds = xds_init(XDS_ENCODE);
    if (xds == NULL)
	{
	printf("Failed to initialize XDS context.\n");
	return 1;
	}

    /* Register the dummy callback under an invalid name to see
       whether the routine fails correctly. */

    if (xds_register(xds, "abcdefh1230#", &dummy_engine, NULL) != XDS_ERR_INVALID_ARG)
	{
	printf("xds_register() illegally accepted an invalid name for the engine.\n");
	return 1;
	}

    /* Register the dummy callback under multiple names. */

    for(i = 0; i < test_names_len; ++i)
	{
	if (xds_register(xds, test_names[i], &dummy_engine, NULL) != XDS_OK)
	    {
	    printf("Failed to register engine for '%s'.\n", test_names[i]);
	    return 1;
	    }
	}

    /* Register the callback again, overwriting an existing entry. */

    if (xds_register(xds, "claus", &dummy_engine, NULL) != XDS_OK)
	{
	printf("Failed to re-register engine for 'claus'.\n");
	return 1;
	}

    /* Ensure that everything is in alphabetical order. */

    for (i = 1; i < xds->engines_len; ++i)
	{
	assert(xds->engines[i-1].name != NULL);
	assert(xds->engines[i].name != NULL);
	if (strcmp(xds->engines[i-1].name, xds->engines[i].name) >= 0)
	    {
	    printf("xds->engines is not in alphabetical order!\n");
	    exit(1);
	    }
	}

    /* Try to remove an unknown entry. */

    if (xds_unregister(xds, "abacadabra") != XDS_ERR_UNKNOWN_ENGINE)
	{
	printf("xds_unregister() succeeded at removing 'abacadabra' even though it is not there.\n");
	exit(1);
	}

    /* Remove an entry from the middle. */

    if (xds_unregister(xds, test_names[test_names_len/2]) != XDS_OK)
	{
	printf("xds_unregister() failed to remove '%s'.\n", test_names[test_names_len/2]);
	exit(1);
	}

    /* Remove the last entry. */

    assert(test_names_len > 0);
    if (xds_unregister(xds, test_names[test_names_len-1]) != XDS_OK)
	{
	printf("xds_unregister() failed to remove '%s'.\n", test_names[test_names_len-1]);
	exit(1);
	}

    /* Remove the first entry. */

    if (xds_unregister(xds, test_names[0]) != XDS_OK)
	{
	printf("xds_unregister() failed to remove '%s'.\n", test_names[0]);
	exit(1);
	}

    /* Clean up. */

    xds_destroy(xds);

    /* Everything went fine. */

    return 0;
    }

CVSTrac 2.0.1