File:  [ELWIX - Embedded LightWeight unIX -] / libaitcfg / example / test_pwd.c
Revision 1.1.2.4: download - view: text, annotated - select for diffs - revision graph
Wed Sep 19 12:04:44 2012 UTC (11 years, 8 months ago) by misho
Branches: cfg5_5
normalize return type

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "aitpwd.h"


int main()
{
	pwd_root_t ddd, db;
	struct tagUser *u;
	char str[256];
	int ret = 0;

	if (cfgLoadPasswd("test.pwd", &db)) {
		printf("Error #%d - %s\n", cfg_GetErrno(), cfg_GetError());
		return 1;
	}

	/* unit test find */
	// count elements
	u = (const struct tagUser *) cfg_findPasswdBy(&db, PWD_CRIT_UID, 0);
	printf("cfg_findPasswdBy %p UID 0 User=%s %u:%u\n", u, AIT_GET_STR(&u->usr_name), 
			AIT_GET_U32(&u->usr_uid), AIT_GET_U32(&u->usr_gid));
	u = (const struct tagUser *) cfg_findPasswdBy(&db, PWD_CRIT_UID, 444);
	printf("cfg_findPasswdBy %p UID 444 User=%s %u:%u\n", u, AIT_GET_STR(&u->usr_name), 
			AIT_GET_U32(&u->usr_uid), AIT_GET_U32(&u->usr_gid));
	/*
	// find by attributes
	printf("Count of array %d w/ NULL \n", Username, pwd_FindAttribute(db, Username, NULL));
	printf("Count of array %d w/o NULL \n", Username, pwd_FindAttribute(db, Username, ""));
	printf("Count of array %d 'user_l' \n", Username, pwd_FindAttribute(db, Username, "user_l"));
	printf("Count of array %d 'a' \n", Username, pwd_FindAttribute(db, Username, "a"));
	printf("Count of array %d 'class4e' \n", Class, pwd_FindAttribute(db, Class, "class4e"));
	printf("Count of array %d 'clas' \n", Class, pwd_FindAttribute(db, Class, "clas"));
	printf("Count of array %d 'ii' \n", Home, pwd_FindAttribute(db, Home, "ii"));
	printf("Count of array %d 'i' \n", Home, pwd_FindAttribute(db, Home, "i"));
	printf("Count of array %d '' \n", Home, pwd_FindAttribute(db, Home, ""));
	printf("Count of array %d NULL \n", Home, pwd_FindAttribute(db, Home, NULL));
	/* unit end find */

#if 0
	/* unit test set */
	printf("attr=%d set %d NULL\n", ALL, pwd_SetAttribute(&db, ALL, NULL));
	printf("attr=%d set %d w/ empty\n", ALL, pwd_SetAttribute(&db, ALL, ddd));
	pwdSetValue(&ddd, ALL, "use");
	pwdSetValue(&ddd, Password, "alabala");
	pwdSetValue(&ddd, UID, "101");
	pwdGetValue(ddd, Password, str, 256);
	printf("attr=%d set %d use getvalue=%s\n", ALL, pwd_SetAttribute(&db, ALL, ddd), str);
	pwdFreeValue(&ddd, ALL);

	pwdSetValue(&ddd, ALL, "user_l");
	pwdSetValue(&ddd, UID, "999");
	pwdSetValue(&ddd, Password, "pliok");
	pwdGetValue(ddd, Username, str, 256);
	printf("attr=%d %s db=%p ddd=%p set %d\n", ALL, str, db, ddd, pwd_SetAttribute(&db, ALL, ddd));
	pwdSetValue(&ddd, Username, "use");
	pwdSetValue(&ddd, Password, "WC_00");
	printf("2.attr=%d %s db=%p ddd=%p set %d\n", Password, str, db, ddd, pwd_SetAttribute(&db, Password, ddd));
	printf("get fail.attr=%d %s db=%p ddd=%p set %d\n", Password, str, db, ddd, pwd_GetAttribute(db, Password, ddd));
	pwdFreeValue(&ddd, ALL);
	/* unit end set */
	/* unit test get */
	pwdSetValue(&ddd, ALL, "user_l");
	printf("GET attr=%d %s db=%p ddd=%p set %d\n", Password, ddd->psPass, db, ddd, pwd_GetAttribute(db, Password, ddd));
	pwdFreeValue(&ddd, ALL);
	/* unit end set */

	/* unit test unset */
	pwdSetValue(&ddd, ALL, "ttt");
	printf("attr=%d Count of array %d w/ NULL \n", Username, pwd_FindAttribute(db, Username, ddd->psUser));
	printf("UNset attr=%d %s db=%p ddd=%p set %d\n", Class, ddd->psClass, db, ddd, pwd_UnsetAttribute(&db, Class, ddd));
	printf("UNset attr=%d %s db=%p ddd=%p set %d\n", Password, ddd->psPass, db, ddd, pwd_UnsetAttribute(&db, Password, ddd));
	pwdFreeValue(&ddd, ALL);
	pwdSetValue(&ddd, ALL, "aa");
	printf("UNset attr=%d %s db=%p ddd=%p set %d\n", ALL, ddd->psUser, db, ddd, pwd_UnsetAttribute(&db, ALL, ddd));
	pwdFreeValue(&ddd, ALL);
	/* unit end unset */
	printf("ALL el. Count of array %d w/ NULL \n", pwd_FindAttribute(db, ALL, NULL));

	WritePasswd(stdout, &db);

	pwdSetValue(&ddd, ALL, "user_l");
	pwdSetValue(&ddd, Password, "000");
	switch ((ret = pwd_Authenticate(db, ddd))) {
		case -1:
			printf("Error:: #%d - %s\n", pwd_GetErrno(), pwd_GetError());
			break;
		case 0:
			printf("Access denied!\n");
			break;
		default:
			printf("Access granted - user found at position %d\n", ret);
	}
	pwdFreeValue(&ddd, ALL);

	if (CreatePasswd("test4e.pwd", &db)) {
		printf("Error #%d - %s\n", pwd_GetErrno(), pwd_GetError());
		return 2;
	}
#endif

	cfgUnloadPasswd(&db);
	return 0;
}

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