virt-viewer/tests/test-monitor-alignment.c
Pavel Grunt cb4ec1fc80 monitor-alignment: Do not crash on NULL display
Since commit 01b66ef88b virt-viewer
does not crash on connection to a guest using an invalid display
configuration (eg. Cirrus & QXL vga). That commit allowed existence
of NULL display, however the code handling monitors alignment does
not expect this and crashes when virt-viewer is shifting/aligning
its windows.

Avoid crashing by returning early on NULL display.

 #0  0x0000000000411d0a in displays_cmp (p1=p1@entry=0xbad940, p2=p2@entry=0xbad944, user_data=user_data@entry=0x8eb180) at virt-viewer-util.c:544
 #1  0x00007ffff3f16ac5 in msort_with_tmp (p=0x7fffffffd670, b=0xbad940, n=2) at gqsort.c:93
 #2  0x00007ffff3f16ded in msort_r (b=b@entry=0xbad940, n=n@entry=2, s=s@entry=4, cmp=cmp@entry=0x411ce0 <displays_cmp>, arg=arg@entry=0x8eb180) at gqsort.c:278
 #3  0x00007ffff3f16e78 in g_qsort_with_data (pbase=pbase@entry=0xbad940, total_elems=total_elems@entry=2, size=size@entry=4, compare_func=compare_func@entry=0x411ce0 <displays_cmp>, user_data=user_data@entry=0x8eb180) at gqsort.c:303
 #4  0x000000000041277c in virt_viewer_align_monitors_linear (displays=displays@entry=0x8eb180 = {...}) at virt-viewer-util.c:586
 #5  0x000000000041a92d in virt_viewer_session_on_monitor_geometry_changed (self=0x8f38a0 [VirtViewerSessionSpice], display=<optimized out>) at virt-viewer-session.c:373
 #6  0x00007ffff4415908 in g_closure_invoke (closure=0x9306c0, return_value=return_value@entry=0x0, n_param_values=2, param_values=param_values@entry=0x7fffffffd960, invocation_hint=invocation_hint@entry=0x7fffffffd900)  at gclosure.c:801
 #7  0x00007ffff4427a1d in signal_emit_unlocked_R (node=node@entry=0x668f80, detail=detail@entry=1551, instance=instance@entry=0x930440, emission_return=emission_return@entry=0x0, instance_and_params=instance_and_params@entry=0x7fffffffd960) at gsignal.c:3627
 #8  0x00007ffff442fab1 in g_signal_emit_valist (instance=<optimized out>, signal_id=<optimized out>, detail=<optimized out>, var_args=var_args@entry=0x7fffffffdaf0) at gsignal.c:3383
 #9  0x00007ffff442fd9f in <emit signal notify:agent-connected on instance 0x930440 [SpiceMainChannel]> (instance=instance@entry=0x930440, signal_id=<optimized out>, detail=<optimized out>) at gsignal.c:3439
 #10 0x00007ffff4419fd4 in g_object_dispatch_properties_changed (object=0x930440 [SpiceMainChannel], n_pspecs=<optimized out>, pspecs=<optimized out> at gobject.c:1061
 #11 0x00007ffff441c4f9 in g_object_notify (pspec=<optimized out>, object=0x930440 [SpiceMainChannel]) at gobject.c:1155
 #12 0x00007ffff441c4f9 in g_object_notify (object=0x930440 [SpiceMainChannel], property_name=<optimized out>) at gobject.c:1202
 #13 0x00007ffff5a63dd0 in notify_main_context (opaque=0x7fffd6fde990) at gio-coroutine.c:240
 #14 0x00007ffff3f07d7a in g_main_context_dispatch (context=0x68da40) at gmain.c:3152
 #15 0x00007ffff3f07d7a in g_main_context_dispatch (context=context@entry=0x68da40) at gmain.c:3767
 #16 0x00007ffff3f080b8 in g_main_context_iterate (context=0x68da40, block=block@entry=1, dispatch=dispatch@entry=1, self=<optimized out>) at gmain.c:3838
 #17 0x00007ffff3f0838a in g_main_loop_run (loop=0x710de0) at gmain.c:4032
 #18 0x00007ffff5f53045 in gtk_main () at gtkmain.c:1207
 #19 0x0000000000411a22 in main (argc=1, argv=0x7fffffffdfa8)

Resolves: rhbz#1250820

Acked-by: Fabiano Fidêncio <fidencio@redhat.com>
2016-06-20 17:08:28 +02:00

219 lines
7.0 KiB
C

/* -*- Mode: C; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
* Virt Viewer: A virtual machine console viewer
*
* Copyright (C) 2016 Red Hat, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111NULL307 USA
*/
#include <config.h>
#include <glib.h>
#include <gdk/gdk.h>
#include <virt-viewer-util.h>
/* GLIB_CHECK_VERSION(2, 40, 0) */
#ifndef g_assert_nonnull
#define g_assert_nonnull g_assert
#endif
gboolean doDebug = FALSE;
#define MAX_DISPLAYS 4
typedef struct test_case {
const guint display_cnt;
const GdkRectangle *displays_in[MAX_DISPLAYS];
const GdkRectangle *displays_out[MAX_DISPLAYS];
const GLogLevelFlags log_level;
const gchar *messages[3];
} TestCase;
typedef void (*MonitorAlignFunc) (GHashTable *);
static void
test_monitor_align(MonitorAlignFunc monitor_align, const TestCase *test_cases, const guint cases)
{
guint i;
for (i = 0; i < cases; i++) {
GdkRectangle *displays_in[MAX_DISPLAYS];
guint j;
GHashTable *displays = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, g_free);
g_assert_nonnull(displays);
for (j = 0; j < test_cases[i].display_cnt; j++) {
if (test_cases[i].displays_in[j] != NULL) {
GdkRectangle *monitor = g_new(GdkRectangle, 1);
*monitor = *test_cases[i].displays_in[j];
displays_in[j] = monitor;
} else {
displays_in[j] = NULL;
}
g_hash_table_insert(displays, GUINT_TO_POINTER(j), displays_in[j]);
}
for (j = 0; j < G_N_ELEMENTS(test_cases[i].messages) && test_cases[i].messages[j]; j++) {
g_test_expect_message(NULL, test_cases[i].log_level, test_cases[i].messages[j]);
}
monitor_align(displays);
g_test_assert_expected_messages();
for (j = 0; j < test_cases[i].display_cnt; j++) {
if (displays_in[j] == NULL) {
g_assert_null(test_cases[i].displays_out[j]);
continue;
}
g_assert_cmpint(displays_in[j]->x, ==, test_cases[i].displays_out[j]->x);
g_assert_cmpint(displays_in[j]->y, ==, test_cases[i].displays_out[j]->y);
g_assert_cmpint(displays_in[j]->width, ==, test_cases[i].displays_out[j]->width);
g_assert_cmpint(displays_in[j]->height, ==, test_cases[i].displays_out[j]->height);
}
g_hash_table_unref(displays);
}
}
static void
test_monitor_shift(void)
{
const GdkRectangle rects[] = {
{4240, 0, 1280, 1024},
{0, 0, 1280, 1024},
{100, 1000, 1024, 768},
{420, 680, 1024, 768},
{320, 320, 1024, 768},
{0, 1000, 1024, 768},
{4140, 0, 1280, 1024},
{220, 320, 1024, 768},
{320, 680, 1024, 768},
};
const TestCase test_cases[] = {
{
0, {NULL}, {NULL}, 0, {NULL}
},{
1,
{NULL},
{NULL},
G_LOG_LEVEL_CRITICAL,
{"*assertion 'display != NULL' failed"}
},{
2,
{NULL, &rects[0]},
{NULL, &rects[0]},
G_LOG_LEVEL_CRITICAL,
{"*assertion 'display != NULL' failed"}
},{
2,
{&rects[2], NULL},
{&rects[2], NULL},
G_LOG_LEVEL_CRITICAL,
{"*assertion 'display != NULL' failed"}
},{
2,
{&rects[0], &rects[0]},
{&rects[1], &rects[1]},
0,
{NULL}
},{
2,
{&rects[0], &rects[1]},
{&rects[0], &rects[1]},
0,
{NULL}
},{
4,
{&rects[0], &rects[2], &rects[4], &rects[3]},
{&rects[6], &rects[5], &rects[7], &rects[8]},
0,
{NULL}
},
};
test_monitor_align(virt_viewer_shift_monitors_to_origin, test_cases, G_N_ELEMENTS(test_cases));
}
static void
test_monitor_align_linear(void)
{
const GdkRectangle rects[] = {
{0, 0, 1280, 1024},
{100, 1000, 1024, 768},
{1280, 0, 1024, 768},
{0, 0, 1024, 768},
{3328, 0, 1024, 768},
{1024, 0, 1280, 1024},
{2304, 0, 1024, 768},
};
const TestCase test_cases[] = {
{
0, {NULL}, {NULL}, 0, {NULL}
},{
1,
{NULL},
{NULL},
G_LOG_LEVEL_CRITICAL,
{"*assertion 'rect != NULL' failed"}
},{
2,
{NULL, &rects[1]},
{NULL, &rects[1]},
G_LOG_LEVEL_CRITICAL,
{
"*displays_cmp: assertion 'm1 != NULL && m2 != NULL' failed",
"*assertion 'rect != NULL' failed"
}
},{
3,
{&rects[1], NULL, &rects[0]},
{&rects[3], NULL, &rects[0]},
G_LOG_LEVEL_CRITICAL,
{
"*displays_cmp: assertion 'm1 != NULL && m2 != NULL' failed",
"*displays_cmp: assertion 'm1 != NULL && m2 != NULL' failed",
"*assertion 'rect != NULL' failed"
}
},{
2,
{&rects[0], &rects[1]},
{&rects[0], &rects[2]},
0,
{NULL}
},{
2,
{&rects[1], &rects[0]},
{&rects[2], &rects[0]},
0,
{NULL}
},{
4,
{&rects[2], &rects[3], &rects[0], &rects[1]},
{&rects[4], &rects[3], &rects[5], &rects[6]},
0,
{NULL}
},
};
test_monitor_align(virt_viewer_align_monitors_linear, test_cases, G_N_ELEMENTS(test_cases));
}
int main(int argc, char* argv[])
{
gtk_init_check(&argc, &argv);
g_test_init(&argc, &argv, NULL);
g_test_add_func("/virt-viewer-util/monitor-shift", test_monitor_shift);
g_test_add_func("/virt-viewer-util/monitor-align-linear", test_monitor_align_linear);
return g_test_run();
}