1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|
/*
* testvalidate.c - A program for testing validate.
*
* To use this program, set the TEST_USER and TEST_PASSWORD
* constants below. After making (make test) and running
* (./testvalidate) this program, you should see a message
* indicating whether or not the combination represents a real user.
*/
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#define VALIDATE_PROGRAM "./validate"
#define TEST_USER "dummy"
#define TEST_PASSWORD "dumbpassword"
main()
{
int filedes[2]; /* fd's for pipe. Read from 0, write to 1*/
int ret, status;
FILE* fp;
if (pipe(filedes)!=0) {
fprintf(stderr,"Unable to open pipe. Error: %d\n",errno);
exit(1);
}
ret = fork();
if (ret==-1) {
fprintf(stderr,"Unable to fork. Error: %d\n",errno);
exit(1);
}
if (ret==0) { /* Child */
/* Set stdin to filedes[0] */
dup2(filedes[0],STDIN_FILENO);
execl( VALIDATE_PROGRAM,
VALIDATE_PROGRAM,
NULL);
/* We shouldn't reach this point */
fprintf(stderr,"Unable to exec. Error: %d\n",errno);
exit(1);
}
/* Parent */
/* We write to the pipe, then wait for the child to finish. */
fp = fdopen(filedes[1],"w");
if (!fp) {
fprintf(stderr,"Unable to open pipe for writing: %d\n",errno);
exit(1);
}
fprintf(fp, "%s\n",TEST_USER);
fprintf(fp, "%s\n",TEST_PASSWORD);
fclose(fp);
ret = wait(&status);
if (ret==0 || ret==-1) {
fprintf(stderr,"Error while waiting for child: %d.\n",errno);
exit(1);
}
if (status!=0)
fprintf(stderr,"Incorrect authentication\n");
else
fprintf(stderr,"Correctly authenticated.\n");
}
|