File:  [ELWIX - Embedded LightWeight unIX -] / libaitcfg / example / test_pwd.c
Revision 1.1.2.3: download - view: text, annotated - select for diffs - revision graph
Wed Sep 19 11:53:52 2012 UTC (11 years, 9 months ago) by misho
Branches: cfg5_5
added hdrs

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

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