int proxy_ftp_ctrl_send_cmd(pool *p, conn_t *ctrl_conn, cmd_rec *cmd) { int res; if (cmd->argc > 1) { char *display_str; size_t display_len = 0; display_str = pr_cmd_get_displayable_str(cmd, &display_len); pr_trace_msg(trace_channel, 9, "proxied command '%s' from frontend to backend", display_str); res = proxy_netio_printf(ctrl_conn->outstrm, "%s %s\r\n", cmd->argv[0], cmd->arg); } else { pr_trace_msg(trace_channel, 9, "proxied %s command from frontend to backend", (char *) cmd->argv[0]); res = proxy_netio_printf(ctrl_conn->outstrm, "%s\r\n", (char *) cmd->argv[0]); } return res; }
END_TEST START_TEST (cmd_get_displayable_str_test) { char *ok, *res = NULL; cmd_rec *cmd = NULL; size_t len = 0; res = pr_cmd_get_displayable_str(NULL, NULL); fail_unless(res == NULL, "Failed to handle null cmd_rec"); fail_unless(errno == EINVAL, "Failed to set errno to EINVAL"); cmd = pr_cmd_alloc(p, 1, "foo"); res = pr_cmd_get_displayable_str(cmd, NULL); ok = "foo"; fail_if(res == NULL, "Expected string, got null"); fail_unless(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res); mark_point(); cmd->argc = 0; res = pr_cmd_get_displayable_str(cmd, NULL); fail_if(res == NULL, "Expected string, got null"); /* Note: We still expect the PREVIOUS ok value, since * pr_cmd_get_displayable_str() should cache the constructed string, * rather than creating it anew. */ fail_unless(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res); mark_point(); pr_cmd_clear_cache(cmd); res = pr_cmd_get_displayable_str(cmd, NULL); ok = ""; fail_if(res == NULL, "Expected string, got null"); fail_unless(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res); mark_point(); cmd = pr_cmd_alloc(p, 1, "bar"); cmd->arg = NULL; res = pr_cmd_get_displayable_str(cmd, NULL); ok = "bar"; fail_if(res == NULL, "Expected string, got null"); fail_unless(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res); mark_point(); cmd = pr_cmd_alloc(p, 1, "baz"); cmd->argv[0] = NULL; cmd->arg = pstrdup(p, "baz"); res = pr_cmd_get_displayable_str(cmd, NULL); /* cmd->argv[0] is the command name; without that, it does not matter * what cmd->arg is. Hence why if cmd->argv[0] is null, we expect the * empty string. */ ok = ""; fail_if(res == NULL, "Expected string, got null"); fail_unless(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res); mark_point(); cmd = pr_cmd_alloc(p, 3, "foo", "bar", "baz"); cmd->arg = NULL; res = pr_cmd_get_displayable_str(cmd, NULL); /* cmd->argv[0] is the command name; without that, it does not matter * what cmd->arg is. Hence why if cmd->argv[0] is null, we expect the * empty string. */ ok = "foo bar baz"; fail_if(res == NULL, "Expected string, got null"); fail_unless(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res); /* Make sure we can handle cases where cmd_rec->argv has been tampered * with. */ mark_point(); cmd = pr_cmd_alloc(p, 3, "foo", "bar", "baz"); cmd->argv[0] = NULL; res = pr_cmd_get_displayable_str(cmd, NULL); ok = " bar baz"; fail_if(res == NULL, "Expected string, got null"); fail_unless(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res); }