/*
 * Copyright (c) 2004-2006 Sendmail, Inc. and its suppliers.
 *	All rights reserved.
 *
 * By using this file, you agree to the terms and conditions set
 * forth in the LICENSE file which can be found at the top level of
 * the sendmail distribution.
 *
 */

#include "sm/generic.h"
SM_RCSID("@(#)$Id: t-sockmap-0.c,v 1.8 2006/04/16 05:37:10 ca Exp $")

#include "sm/error.h"
#include "sm/heap.h"
#include "sm/memops.h"
#include "sm/sysexits.h"
#include "sm/test.h"
#include "sm/maps.h"
#include "sm/mapc.h"
#include "sm/map.h"
#include "sm/mapclasses.h"
#include "sm/net.h"

#include "sm/io.h"

int Verbose = 0;

#define MAPC_TYPE	"socket"
#define MAPC_NAME	"name"

static void
testh(const char *name, const char *path, ipv4_T ipv4, short port)
{
	sm_ret_T ret;
	sm_maps_P maps;
	sm_map_P map;
	sm_cstr_P mtype, mname;
	sm_str_P lhs, rhs;

	maps = NULL;
	mtype = mname = NULL;
	lhs = rhs = NULL;

	ret = sm_maps_init(&maps);
	SM_TEST(maps != NULL);
	if (maps == NULL)
		return;
	SM_TEST(sm_is_success(ret));

	mtype = sm_cstr_scpyn0((const uchar *)MAPC_TYPE, strlen(MAPC_TYPE));
	SM_TEST(mtype != NULL);
	if (mtype == NULL)
		goto error;

	mname = sm_cstr_scpyn0((const uchar *)MAPC_NAME, strlen(MAPC_NAME));
	SM_TEST(mname != NULL);
	if (mname == NULL)
		goto error;

	lhs = sm_str_new(NULL, 256, 1024);
	SM_TEST(lhs != NULL);
	if (lhs == NULL)
		goto error;

	rhs = sm_str_new(NULL, 256, 1024);
	SM_TEST(rhs != NULL);
	if (rhs == NULL)
		goto error;

	ret = sm_sockmap_class_create(maps);
	SM_TEST(sm_is_success(ret));

	map = NULL;
	ret = sm_map_open(maps, mname, mtype, 0, name, SMAP_MODE_RDWR, &map,
		SMPO_SOCKPATH, path, SMPO_PORT, port, SMPO_IPV4, ipv4,
		SMPO_END);
	SM_TEST(sm_is_success(ret));
	if (!sm_is_success(ret))
		goto error;

	sm_str_clr(rhs);
	sm_str_clr(lhs);
	sm_str_scat(lhs, (const char *)"bastiaan.bakker@example.com");
	ret = sm_map_lookup(map, SMMAP_FL_NONE, lhs, rhs);
	SM_TEST(sm_is_success(ret));

	sm_str_clr(rhs);
	sm_str_clr(lhs);
	sm_str_scat(lhs, (const char *)"wolter.eldering@example.com");
	ret = sm_map_lookup(map, SMMAP_FL_NONE, lhs, rhs);
	SM_TEST(sm_is_success(ret));

	sm_str_clr(rhs);
	sm_str_clr(lhs);
	sm_str_scat(lhs, (const char *)"no@example.com");
	ret = sm_map_lookup(map, SMMAP_FL_NONE, lhs, rhs);
	SM_TEST(sm_is_err(ret));

	sm_str_clr(rhs);
	sm_str_clr(lhs);
	sm_str_scat(lhs, (const char *)"TIMEOUT");
	ret = sm_map_lookup(map, SMMAP_FL_NONE, lhs, rhs);
	SM_TEST(sm_is_err(ret));

	sm_str_clr(rhs);
	sm_str_clr(lhs);
	sm_str_scat(lhs, (const char *)"wolter.eldering@example.com");
	ret = sm_map_lookup(map, SMMAP_FL_NONE, lhs, rhs);
	SM_TEST(sm_is_success(ret));

	sm_str_clr(rhs);
	sm_str_clr(lhs);
	sm_str_scat(lhs, (const char *)"bastiaan.bakker@example.com");
	ret = sm_map_lookup(map, SMMAP_FL_NONE, lhs, rhs);
	SM_TEST(sm_is_success(ret));

	ret = sm_map_close(map, 0);
	SM_TEST(sm_is_success(ret));

	ret = sm_maps_term(maps);
	SM_TEST(sm_is_success(ret));
	SM_CSTR_FREE(mtype);
	SM_CSTR_FREE(mname);
	SM_STR_FREE(lhs);
	SM_STR_FREE(rhs);
	return;

  error:
	sm_maps_term(maps);
}

#define T_SM_NAME "virtuser"
#define T_SM_PATH NULL
#define T_SM_PORT 1289
#define T_SM_IP 0x7F000001

static sm_ret_T
usage(const char *prg)
{
	sm_io_fprintf(smioerr, "usage: %s [options]\n"
		"-a ip     IPv4 address [%#x]\n"
		"-N name   name of map [%s]\n"
		"-P path   path [%s]\n"
		"-p port   port [%d]\n"
		, prg
		, T_SM_IP
		, T_SM_NAME
		, T_SM_PATH
		, T_SM_PORT
		);
	return EX_USAGE;
}

int
main(int argc, char *argv[])
{
	char *path, *name, *prg;
	ipv4_T ipv4;
	short port;
	int c;

	prg = argv[0];
	path = NULL;
	name = "virtuser";
	port = 1289;
	ipv4 = ntohl(0x7F000001);
	while ((c = getopt(argc, argv, "a:N:P:p:H:s:V")) != -1)
	{
		switch (c)
		{
		  case 'a':
			ipv4 = (ipv4_T) atol(optarg);
			break;
		  case 'N':
			name = strdup(optarg);
			if (name == NULL)
				return 1;
			break;
		  case 'P':
			path = strdup(optarg);
			if (path == NULL)
				return 1;
			break;
		  case 'p':
			port = atoi(optarg);
			break;
		  default:
			usage(prg);
			return EX_USAGE;
		}
	}
	sm_test_begin(argc, argv, "test socket map 0");
	testh(name, path, ipv4, port);
	return sm_test_end();
}


syntax highlighted by Code2HTML, v. 0.9.1