Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove CFG_USERINPUTFD and use stdin replacement in affected tests. #2091

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 21 additions & 22 deletions src/rnp/rnpcfg.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,28 +52,27 @@
#define CFG_RECIPIENTS "recipients" /* list of encrypted data recipients */
#define CFG_SIGNERS "signers" /* list of signers */
#define CFG_HOMEDIR "homedir" /* home directory - folder with keyrings and so on */
#define CFG_KEYFILE "keyfile" /* path to the file with key(s), used instead of keyring */
#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 */
#define CFG_CIPHER "cipher" /* symmetric encryption algorithm as string */
#define CFG_HASH "hash" /* hash algorithm used, string like 'SHA1'*/
#define CFG_WEAK_HASH "weak-hash" /* allow weak algorithms */
#define CFG_S2K_ITER "s2k-iter" /* number of S2K hash iterations to perform */
#define CFG_S2K_MSEC "s2k-msec" /* number of milliseconds S2K should target */
#define CFG_ENCRYPT_PK "encrypt_pk" /* public key should be used during encryption */
#define CFG_ENCRYPT_SK "encrypt_sk" /* password encryption should be used */
#define CFG_IO_RESS "ress" /* results stream */
#define CFG_NUMBITS "numbits" /* number of bits in generated key */
#define CFG_EXPERT "expert" /* expert key generation mode */
#define CFG_ZLEVEL "zlevel" /* compression level: 0..9 (0 for no compression) */
#define CFG_ZALG "zalg" /* compression algorithm: zip, zlib or bzip2 */
#define CFG_AEAD "aead" /* if nonzero then AEAD enryption mode, int */
#define CFG_AEAD_CHUNK "aead_chunk" /* AEAD chunk size bits, int from 0 to 56 */
#define CFG_KEYFILE "keyfile" /* path to the file with key(s), used instead of keyring */
#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_NUMTRIES "numtries" /* number of password request tries, or 'unlimited' */
#define CFG_EXPIRATION "expiration" /* signature expiration time */
#define CFG_CREATION "creation" /* signature validity start */
#define CFG_CIPHER "cipher" /* symmetric encryption algorithm as string */
#define CFG_HASH "hash" /* hash algorithm used, string like 'SHA1'*/
#define CFG_WEAK_HASH "weak-hash" /* allow weak algorithms */
#define CFG_S2K_ITER "s2k-iter" /* number of S2K hash iterations to perform */
#define CFG_S2K_MSEC "s2k-msec" /* number of milliseconds S2K should target */
#define CFG_ENCRYPT_PK "encrypt_pk" /* public key should be used during encryption */
#define CFG_ENCRYPT_SK "encrypt_sk" /* password encryption should be used */
#define CFG_IO_RESS "ress" /* results stream */
#define CFG_NUMBITS "numbits" /* number of bits in generated key */
#define CFG_EXPERT "expert" /* expert key generation mode */
#define CFG_ZLEVEL "zlevel" /* compression level: 0..9 (0 for no compression) */
#define CFG_ZALG "zalg" /* compression algorithm: zip, zlib or bzip2 */
#define CFG_AEAD "aead" /* if nonzero then AEAD enryption mode, int */
#define CFG_AEAD_CHUNK "aead_chunk" /* AEAD chunk size bits, int from 0 to 56 */
#define CFG_KEYSTORE_DISABLED \
"disable_keystore" /* indicates whether keystore must be initialized */
#define CFG_FORCE "force" /* force command to succeed operation */
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
20 changes: 18 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,35 @@ 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;
}

fprintf(stderr, "saved_stdin: %i\n", saved_stdin);

if (!rnp_cmd(ctx, CMD_GENERATE_KEY, NULL)) {
ret = false;
goto end;
}
if (dup2(saved_stdin, STDIN_FILENO) == -1) {
ret = false;
goto end;
}
if (fcntl(STDIN_FILENO, F_SETFD, FD_CLOEXEC) == -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