File:  [ELWIX - Embedded LightWeight unIX -] / libaitcfg / example / test_pwd.c
Revision 1.1.2.2: download - view: text, annotated - select for diffs - revision graph
Wed Sep 19 11:50:02 2012 UTC (11 years, 8 months ago) by misho
Branches: cfg5_5
added passwd UT

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

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