Skip to content

Commit

Permalink
Remove CFG_USERINPUTFD and use stdin replacement in affected tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
antonsviridenko committed Jun 14, 2023
1 parent 32149be commit b018914
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 15 deletions.
1 change: 0 additions & 1 deletion src/rnp/rnpcfg.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
#define CFG_PASSFD "pass-fd" /* password file descriptor */
#define CFG_PASSWD "password" /* password as command-line constant */
#define CFG_PASSWORDC "passwordc" /* number of passwords for symmetric encryption */
#define CFG_USERINPUTFD "user-input-fd" /* user input file descriptor */
#define CFG_NUMTRIES "numtries" /* number of password request tries, or 'unlimited' */
#define CFG_EXPIRATION "expiration" /* signature expiration time */
#define CFG_CREATION "creation" /* signature validity start */
Expand Down
12 changes: 0 additions & 12 deletions src/rnpkeys/tui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -365,18 +365,6 @@ cli_rnp_set_generate_params(rnp_cfg &cfg, bool subkey)
cfg.set_int(CFG_KG_SUBKEY_BITS, cfg.get_int(CFG_NUMBITS));
} else {
FILE *input = stdin;
if (cfg.has(CFG_USERINPUTFD)) {
int inputfd = dup(cfg.get_int(CFG_USERINPUTFD));
if (inputfd != -1) {
input = rnp_fdopen(inputfd, "r");
if (!input) {
close(inputfd);
}
}
}
if (!input) {
return false;
}
if (subkey) {
res = rnpkeys_ask_generate_params_subkey(cfg, input);
} else {
Expand Down
14 changes: 12 additions & 2 deletions src/tests/generatekey.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,7 @@ ask_expert_details(cli_rnp_t *ctx, rnp_cfg &ops, const char *rsp)
bool ret = false;
int pipefd[2] = {-1, -1};
int user_input_pipefd[2] = {-1, -1};
int saved_stdin = -1;
size_t rsp_len;

if (pipe(pipefd) == -1) {
Expand All @@ -605,20 +606,29 @@ ask_expert_details(cli_rnp_t *ctx, rnp_cfg &ops, const char *rsp)
}
close(user_input_pipefd[1]);

/* Mock user-input*/
ctx->cfg().set_int(CFG_USERINPUTFD, user_input_pipefd[0]);
/* Replace stdin with our pipe */
saved_stdin = dup(STDIN_FILENO);
if (dup2(user_input_pipefd[0], STDIN_FILENO) == -1) {
ret = false;
goto end;
}

if (!rnp_cmd(ctx, CMD_GENERATE_KEY, NULL)) {
ret = false;
goto end;
}
if (dup2(saved_stdin, STDIN_FILENO) == -1) {
ret = false;
goto end;
}
ops.copy(ctx->cfg());
ret = true;
end:
/* Close & clean fd*/
if (user_input_pipefd[0]) {
close(user_input_pipefd[0]);
}
close(saved_stdin);
return ret;
}

Expand Down
2 changes: 2 additions & 0 deletions src/tests/support.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@

#ifdef _WIN32
#define pipe(fds) _pipe(fds, 256, O_BINARY)
#define dup(fd) _dup(fd)
#define dup2(fd1, fd2) _dup2(fd1, fd2)
int setenv(const char *name, const char *value, int overwrite);
int unsetenv(const char *name);
#endif
Expand Down

0 comments on commit b018914

Please sign in to comment.