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

    1: #include <stdio.h>
    2: #include <stdlib.h>
    3: #include <string.h>
    4: #include <time.h>
    5: #include "aitpwd.h"
    6: 
    7: 
    8: int main()
    9: {
   10: 	pwd_root_t ddd, db;
   11: 	struct tagUser *u;
   12: 	char str[256];
   13: 	int ret = 0;
   14: 
   15: 	if (cfgLoadPasswd("test.pwd", &db)) {
   16: 		printf("Error #%d - %s\n", cfg_GetErrno(), cfg_GetError());
   17: 		return 1;
   18: 	}
   19: 
   20: 	/* unit test find */
   21: 	// count elements
   22: 	u = (const struct tagUser *) cfg_findPasswdBy(&db, PWD_CRIT_UID, 0);
   23: 	printf("cfg_findPasswdBy %p UID 0 User=%s %u:%u\n", u, AIT_GET_STR(&u->usr_name), 
   24: 			AIT_GET_U32(&u->usr_uid), AIT_GET_U32(&u->usr_gid));
   25: 	u = (const struct tagUser *) cfg_findPasswdBy(&db, PWD_CRIT_UID, 444);
   26: 	printf("cfg_findPasswdBy %p UID 444 User=%s %u:%u\n", u, AIT_GET_STR(&u->usr_name), 
   27: 			AIT_GET_U32(&u->usr_uid), AIT_GET_U32(&u->usr_gid));
   28: 	/*
   29: 	// find by attributes
   30: 	printf("Count of array %d w/ NULL \n", Username, pwd_FindAttribute(db, Username, NULL));
   31: 	printf("Count of array %d w/o NULL \n", Username, pwd_FindAttribute(db, Username, ""));
   32: 	printf("Count of array %d 'user_l' \n", Username, pwd_FindAttribute(db, Username, "user_l"));
   33: 	printf("Count of array %d 'a' \n", Username, pwd_FindAttribute(db, Username, "a"));
   34: 	printf("Count of array %d 'class4e' \n", Class, pwd_FindAttribute(db, Class, "class4e"));
   35: 	printf("Count of array %d 'clas' \n", Class, pwd_FindAttribute(db, Class, "clas"));
   36: 	printf("Count of array %d 'ii' \n", Home, pwd_FindAttribute(db, Home, "ii"));
   37: 	printf("Count of array %d 'i' \n", Home, pwd_FindAttribute(db, Home, "i"));
   38: 	printf("Count of array %d '' \n", Home, pwd_FindAttribute(db, Home, ""));
   39: 	printf("Count of array %d NULL \n", Home, pwd_FindAttribute(db, Home, NULL));
   40: 	/* unit end find */
   41: 
   42: #if 0
   43: 	/* unit test set */
   44: 	printf("attr=%d set %d NULL\n", ALL, pwd_SetAttribute(&db, ALL, NULL));
   45: 	printf("attr=%d set %d w/ empty\n", ALL, pwd_SetAttribute(&db, ALL, ddd));
   46: 	pwdSetValue(&ddd, ALL, "use");
   47: 	pwdSetValue(&ddd, Password, "alabala");
   48: 	pwdSetValue(&ddd, UID, "101");
   49: 	pwdGetValue(ddd, Password, str, 256);
   50: 	printf("attr=%d set %d use getvalue=%s\n", ALL, pwd_SetAttribute(&db, ALL, ddd), str);
   51: 	pwdFreeValue(&ddd, ALL);
   52: 
   53: 	pwdSetValue(&ddd, ALL, "user_l");
   54: 	pwdSetValue(&ddd, UID, "999");
   55: 	pwdSetValue(&ddd, Password, "pliok");
   56: 	pwdGetValue(ddd, Username, str, 256);
   57: 	printf("attr=%d %s db=%p ddd=%p set %d\n", ALL, str, db, ddd, pwd_SetAttribute(&db, ALL, ddd));
   58: 	pwdSetValue(&ddd, Username, "use");
   59: 	pwdSetValue(&ddd, Password, "WC_00");
   60: 	printf("2.attr=%d %s db=%p ddd=%p set %d\n", Password, str, db, ddd, pwd_SetAttribute(&db, Password, ddd));
   61: 	printf("get fail.attr=%d %s db=%p ddd=%p set %d\n", Password, str, db, ddd, pwd_GetAttribute(db, Password, ddd));
   62: 	pwdFreeValue(&ddd, ALL);
   63: 	/* unit end set */
   64: 	/* unit test get */
   65: 	pwdSetValue(&ddd, ALL, "user_l");
   66: 	printf("GET attr=%d %s db=%p ddd=%p set %d\n", Password, ddd->psPass, db, ddd, pwd_GetAttribute(db, Password, ddd));
   67: 	pwdFreeValue(&ddd, ALL);
   68: 	/* unit end set */
   69: 
   70: 	/* unit test unset */
   71: 	pwdSetValue(&ddd, ALL, "ttt");
   72: 	printf("attr=%d Count of array %d w/ NULL \n", Username, pwd_FindAttribute(db, Username, ddd->psUser));
   73: 	printf("UNset attr=%d %s db=%p ddd=%p set %d\n", Class, ddd->psClass, db, ddd, pwd_UnsetAttribute(&db, Class, ddd));
   74: 	printf("UNset attr=%d %s db=%p ddd=%p set %d\n", Password, ddd->psPass, db, ddd, pwd_UnsetAttribute(&db, Password, ddd));
   75: 	pwdFreeValue(&ddd, ALL);
   76: 	pwdSetValue(&ddd, ALL, "aa");
   77: 	printf("UNset attr=%d %s db=%p ddd=%p set %d\n", ALL, ddd->psUser, db, ddd, pwd_UnsetAttribute(&db, ALL, ddd));
   78: 	pwdFreeValue(&ddd, ALL);
   79: 	/* unit end unset */
   80: 	printf("ALL el. Count of array %d w/ NULL \n", pwd_FindAttribute(db, ALL, NULL));
   81: 
   82: 	WritePasswd(stdout, &db);
   83: 
   84: 	pwdSetValue(&ddd, ALL, "user_l");
   85: 	pwdSetValue(&ddd, Password, "000");
   86: 	switch ((ret = pwd_Authenticate(db, ddd))) {
   87: 		case -1:
   88: 			printf("Error:: #%d - %s\n", pwd_GetErrno(), pwd_GetError());
   89: 			break;
   90: 		case 0:
   91: 			printf("Access denied!\n");
   92: 			break;
   93: 		default:
   94: 			printf("Access granted - user found at position %d\n", ret);
   95: 	}
   96: 	pwdFreeValue(&ddd, ALL);
   97: 
   98: 	if (CreatePasswd("test4e.pwd", &db)) {
   99: 		printf("Error #%d - %s\n", pwd_GetErrno(), pwd_GetError());
  100: 		return 2;
  101: 	}
  102: #endif
  103: 
  104: 	cfgUnloadPasswd(&db);
  105: 	return 0;
  106: }

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