gsubprocess: Add a missing test for invalid UTF-8 output

There were tests for invalid UTF-8 output when asynchronously
communicating with a subprocess, but nothing for synchronous
communication. Add such a test, and refine the code as a result.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
This commit is contained in:
Philip Withnall 2018-10-11 11:29:17 +13:00
parent 4795dadde4
commit 19c7a7bb23

View File

@ -1190,7 +1190,7 @@ test_communicate_nothing (void)
}
static void
test_communicate_utf8_invalid (void)
test_communicate_utf8_async_invalid (void)
{
GSubprocessFlags flags = G_SUBPROCESS_FLAGS_STDOUT_PIPE;
GError *error = NULL;
@ -1222,6 +1222,37 @@ test_communicate_utf8_invalid (void)
g_object_unref (proc);
}
/* Test that invalid UTF-8 received using g_subprocess_communicate_utf8()
* results in an error. */
static void
test_communicate_utf8_invalid (void)
{
GSubprocessFlags flags = G_SUBPROCESS_FLAGS_STDOUT_PIPE;
GError *local_error = NULL;
gboolean ret;
GPtrArray *args;
gchar *stdout_str = NULL, *stderr_str = NULL;
GSubprocess *proc;
args = get_test_subprocess_args ("cat", NULL);
proc = g_subprocess_newv ((const gchar* const*)args->pdata,
G_SUBPROCESS_FLAGS_STDIN_PIPE | flags,
&local_error);
g_assert_no_error (local_error);
g_ptr_array_free (args, TRUE);
ret = g_subprocess_communicate_utf8 (proc, "\xFF\xFF", NULL,
&stdout_str, &stderr_str, &local_error);
g_assert_error (local_error, G_IO_ERROR, G_IO_ERROR_FAILED);
g_error_free (local_error);
g_assert_false (ret);
g_assert_null (stdout_str);
g_assert_null (stderr_str);
g_object_unref (proc);
}
static gboolean
send_terminate (gpointer user_data)
{
@ -1783,6 +1814,7 @@ main (int argc, char **argv)
g_free (test_path);
}
g_test_add_func ("/gsubprocess/communicate/utf8/async/invalid", test_communicate_utf8_async_invalid);
g_test_add_func ("/gsubprocess/communicate/utf8/invalid", test_communicate_utf8_invalid);
g_test_add_func ("/gsubprocess/communicate/nothing", test_communicate_nothing);
g_test_add_func ("/gsubprocess/terminate", test_terminate);