eport function test done;

master
Alexander Vdolainen 4 years ago
parent c0f4b10721
commit 51e43425ce

@ -59,14 +59,27 @@ static void print_help(const char *cname)
return; return;
} }
const char *tfmt[] = {
"auth:%s:%s:%s",
"isuser:%s:%s",
"setpass:%s:%s:%s",
"tryregister:%s:%s:%s",
"removeuser:%s:%s",
"removeuser3:%s:%s:%s",
"weird-stuff",
};
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
char *domain, *user, *password; char *domain, *user, *password;
char *utbuf = NULL, *msgbuf = NULL; char *utbuf = NULL, *msgbuf = NULL;
int verbose = 0, opt, msgbuf_len = 0; int verbose = 0, opt, msgbuf_len = 0, items = 0;
FILE *inp, *outp;
int pfd[2];
struct ejabber_msg m; struct ejabber_msg m;
domain = user = password = NULL; domain = user = password = NULL;
inp = outp = NULL;
/* parse options */ /* parse options */
while(1) { while(1) {
@ -129,8 +142,136 @@ int main(int argc, char **argv)
return ENOMEM; return ENOMEM;
} else memset(msgbuf, 0, msgbuf_len); } else memset(msgbuf, 0, msgbuf_len);
if(pipe(pfd) == -1) {
fprintf(stderr, "Error: cannot create pipe\n");
opt = -1;
goto finit0;
} else {
opt = -1;
inp = fdopen(pfd[0], "r"); /* port to read from */
outp = fdopen(pfd[1], "w"); /* port to write to */
if(!inp || !outp) {
fprintf(stderr, "Cannot create file stream.\n");
goto finit1;
} else opt = 0;
}
do {
/* do the message write */
switch(opt) {
case 0:
case 2:
case 3:
case 5:
snprintf(msgbuf, msgbuf_len, tfmt[opt], user, domain, password);
break;
case 1:
case 4:
snprintf(msgbuf, msgbuf_len, tfmt[opt], user, domain);
break;
case 6:
snprintf(msgbuf, msgbuf_len, tfmt[opt]);
break;
default:
opt = -1;
break;
};
if(verbose) {
fprintf(stdout, "Push (%s) ... ", msgbuf);
fflush(stdout);
}
if(eport_write(outp, msgbuf, strlen(msgbuf)) < 0) {
opt = -1;
if(verbose) fprintf(stdout, "fail\n");
else fprintf(stderr, "Error write to out port.\n");
} else {
if(verbose) fprintf(stdout, "ok\n");
/* try to read */
if(verbose) {
fprintf(stdout, "Pull ... ");
fflush(stdout);
}
if(eport_read(inp, msgbuf, msgbuf_len) < 0) {
opt = -1;
if(verbose) fprintf(stdout, "fail\n");
else fprintf(stderr, "Error on reading in port.\n");
} else if((items = eport_ejabberd_msgread(msgbuf, msgbuf_len, &m)) == -1) {
if(verbose) fprintf(stdout, "fail on parsing\n");
else fprintf(stderr, "Error on parsing data from in port.\n");
opt = -1;
}
if(opt != -1) {
if(verbose) {
fprintf(stdout, "ok, has been parsed\n");
fprintf(stdout, "Checking data ... ");
fflush(stdout);
}
/* check the data first */
switch(items) {
case 3:
if(strcmp(m.password, password)) {
opt = -1;
if(verbose) fprintf(stdout, "password data is incorrect\n");
else fprintf(stderr, "Password data is invalid.\n");
}
case 2:
if(strcmp(m.user, user)) {
opt = -1;
if(verbose) fprintf(stdout, "user data is incorrect\n");
else fprintf(stderr, "User data is invalid.\n");
}
if(strcmp(m.domain, domain)) {
opt = -1;
if(verbose) fprintf(stdout, "domain data is incorrect\n");
else fprintf(stderr, "Domain data is invalid.\n");
}
break;
case 0:
default:
if(opt != 6) opt = -1;
break;
}
if(opt != -1) { /* check command */
if(verbose) {
fprintf(stdout, "ok\n");
fprintf(stdout, "Check event ... ");
fflush(stdout);
}
switch(opt) {
case 0: if(m.aev != EJA_AUTH) opt = -1; break;
case 1: if(m.aev != EJA_ISUSER) opt = -1; break;
case 2: if(m.aev != EJA_SETPASS) opt = -1; break;
case 3: if(m.aev != EJA_TRYREGISTER) opt = -1; break;
case 4: if(m.aev != EJA_REMOVEUSER) opt = -1; break;
case 5: if(m.aev != EJA_REMOVEUSER3) opt = -1; break;
default:
opt = -1;
}
if(opt == -1) {
if(verbose) fprintf(stdout, "fail\n");
else fprintf(stderr, "Event cmd invalid.\n");
} else {
if(verbose) fprintf(stdout, "ok\n");
}
}
}
}
memset(msgbuf, 0, msgbuf_len);
if(opt != -1) opt++;
} while((opt < 6) && (opt != -1));
if(opt > 0) opt = 0;
finit1:
if(!inp) close(pfd[0]);
else fclose(inp);
if(!outp) close(pfd[1]);
else fclose(outp);
finit0:
free(utbuf); free(utbuf);
free(msgbuf); free(msgbuf);
return 0; return opt;
} }

Loading…
Cancel
Save