1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
|
/* -*- Mode: C; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
Copyright (C) 2009-2017 Red Hat, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
/**
* Test streaming device
*/
#include <config.h>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <unistd.h>
#include <spice/protocol.h>
#include <spice/stream-device.h>
#include "test-display-base.h"
#include "test-glib-compat.h"
#include "stream-channel.h"
#include "reds.h"
#include "win-alarm.h"
#include "vmc-emu.h"
static VmcEmu *vmc;
static uint8_t *add_stream_hdr(uint8_t *p, StreamMsgType type, uint32_t size)
{
StreamDevHeader hdr = {
.protocol_version = STREAM_DEVICE_PROTOCOL,
.padding = 0,
.type = GUINT16_TO_LE(type),
.size = GUINT32_TO_LE(size),
};
memcpy(p, &hdr, sizeof(hdr));
return p + sizeof(hdr);
}
static uint8_t *add_format(uint8_t *p, uint32_t w, uint32_t h, SpiceVideoCodecType codec)
{
StreamMsgFormat fmt = {
.width = GUINT32_TO_LE(w),
.height = GUINT32_TO_LE(h),
.codec = codec,
};
p = add_stream_hdr(p, STREAM_TYPE_FORMAT, sizeof(fmt));
memcpy(p, &fmt, sizeof(fmt));
return p + sizeof(fmt);
}
/* currently we don't care about possible capabilities sent so discard them
* from server reply */
static void
discard_server_capabilities()
{
StreamDevHeader hdr;
if (vmc->write_pos == 0) {
return;
}
g_assert(vmc->write_pos >= sizeof(hdr));
memcpy(&hdr, vmc->write_buf, sizeof(hdr));
hdr.type = GUINT16_FROM_LE(hdr.type);
hdr.size = GUINT32_FROM_LE(hdr.size);
if (hdr.type == STREAM_TYPE_CAPABILITIES) {
g_assert_cmpint(hdr.size, <=, vmc->write_pos - sizeof(hdr));
vmc->write_pos -= hdr.size + sizeof(hdr);
memmove(vmc->write_buf, vmc->write_buf + hdr.size + sizeof(hdr), vmc->write_pos);
}
}
// check we have an error message on the write buffer
static void
check_vmc_error_message()
{
StreamDevHeader hdr;
discard_server_capabilities();
g_assert_cmpint(vmc->write_pos, >= ,sizeof(hdr));
memcpy(&hdr, vmc->write_buf, sizeof(hdr));
g_assert_cmpint(hdr.protocol_version, ==, STREAM_DEVICE_PROTOCOL);
g_assert_cmpint(GUINT16_FROM_LE(hdr.type), ==, STREAM_TYPE_NOTIFY_ERROR);
g_assert_cmpint(GUINT32_FROM_LE(hdr.size), <=, vmc->write_pos - sizeof(hdr));
}
static int num_send_data_calls = 0;
static size_t send_data_bytes = 0;
StreamChannel::StreamChannel(RedsState *reds, uint32_t id):
RedChannel(reds, SPICE_CHANNEL_DISPLAY, id, RedChannel::HandleAcks)
{
reds_register_channel(reds, this);
}
void
StreamChannel::change_format(const StreamMsgFormat *fmt)
{
}
void
StreamChannel::send_data(const void *data, size_t size, uint32_t mm_time)
{
++num_send_data_calls;
send_data_bytes += size;
}
void
StreamChannel::register_start_cb(stream_channel_start_proc cb, void *opaque)
{
}
void
StreamChannel::register_queue_stat_cb(stream_channel_queue_stat_proc cb, void *opaque)
{
}
red::shared_ptr<StreamChannel> stream_channel_new(RedsState *server, uint32_t id)
{
return red::make_shared<StreamChannel>(server, id);
}
void
StreamChannel::reset()
{
}
void StreamChannel::on_connect(RedClient *red_client, RedStream *stream,
int migration, RedChannelCapabilities *caps)
{
}
static SpiceCoreInterface *core;
static Test *test;
using TestFixture = int;
static void test_stream_device_setup(TestFixture *fixture, gconstpointer user_data)
{
g_assert_null(core);
g_assert_null(test);
g_assert_null(vmc);
core = basic_event_loop_init();
g_assert_nonnull(core);
test = test_new(core);
g_assert_nonnull(test);
vmc = vmc_emu_new("port", "org.spice-space.stream.0");
g_assert_nonnull(vmc);
num_send_data_calls = 0;
send_data_bytes = 0;
}
static void test_stream_device_teardown(TestFixture *fixture, gconstpointer user_data)
{
g_assert_nonnull(core);
g_assert_nonnull(test);
vmc_emu_destroy(vmc);
vmc = nullptr;
test_destroy(test);
test = nullptr;
basic_event_loop_destroy();
core = nullptr;
}
static void test_kick()
{
spice_server_add_interface(test->server, &vmc->instance.base);
// we need to open the device and kick the start
// the alarm is to prevent the program from getting stuck
alarm(5);
spice_server_port_event(&vmc->instance, SPICE_PORT_EVENT_OPENED);
spice_server_char_device_wakeup(&vmc->instance);
alarm(0);
}
static void test_stream_device(TestFixture *fixture, gconstpointer user_data)
{
for (int test_num=0; test_num < 2; ++test_num) {
vmc_emu_reset(vmc);
uint8_t *p = vmc->message;
// add some messages into device buffer
// here we are testing the device is reading at least two
// consecutive format messages
// first message part has 2 extra bytes to check for header split
p = add_format(p, 640, 480, SPICE_VIDEO_CODEC_TYPE_MJPEG);
vmc_emu_add_read_till(vmc, p + 2);
p = add_format(p, 640, 480, SPICE_VIDEO_CODEC_TYPE_VP9);
// this split the second format in half
vmc_emu_add_read_till(vmc, p - 4);
vmc_emu_add_read_till(vmc, p);
// add a message to stop data to be read
p = add_stream_hdr(p, STREAM_TYPE_INVALID, 0);
vmc_emu_add_read_till(vmc, p);
// this message should not be read
p = add_stream_hdr(p, STREAM_TYPE_INVALID, 0);
vmc_emu_add_read_till(vmc, p);
spice_server_add_interface(test->server, &vmc->instance.base);
// device should not have read data before we open it
spice_server_char_device_wakeup(&vmc->instance);
g_assert_cmpint(vmc->pos, ==, 0);
g_test_expect_message(G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "Stream device received invalid message: Invalid message type");
// we need to open the device and kick the start
spice_server_port_event(&vmc->instance, SPICE_PORT_EVENT_OPENED);
spice_server_char_device_wakeup(&vmc->instance);
spice_server_port_event(&vmc->instance, SPICE_PORT_EVENT_CLOSED);
// make sure first 3 parts are read completely
g_assert(vmc->message_sizes_curr - vmc->message_sizes >= 3);
// make sure the device readed all or that device was
// disabled, we need this to make sure that device will be in
// sync when opened again
g_assert(vmc->message_sizes_curr - vmc->message_sizes == 5 || !vmc->device_enabled);
check_vmc_error_message();
spice_server_remove_interface(&vmc->instance.base);
}
}
// check if sending a partial message causes issues
static void test_stream_device_unfinished(TestFixture *fixture, gconstpointer user_data)
{
uint8_t *p = vmc->message;
// this long and not finished message should not cause an infinite loop
p = add_stream_hdr(p, STREAM_TYPE_DATA, 100000);
vmc_emu_add_read_till(vmc, p);
test_kick();
// we should have read all data
g_assert(vmc->message_sizes_curr - vmc->message_sizes == 1);
// we should have no data from the device
discard_server_capabilities();
g_assert_cmpint(vmc->write_pos, ==, 0);
}
// check if sending multiple messages cause stall
static void test_stream_device_multiple(TestFixture *fixture, gconstpointer user_data)
{
uint8_t *p = vmc->message;
// add some messages into device buffer
p = add_format(p, 640, 480, SPICE_VIDEO_CODEC_TYPE_MJPEG);
p = add_format(p, 640, 480, SPICE_VIDEO_CODEC_TYPE_MJPEG);
p = add_format(p, 640, 480, SPICE_VIDEO_CODEC_TYPE_MJPEG);
vmc_emu_add_read_till(vmc, p);
test_kick();
// we should have read all data
g_assert(vmc->message_sizes_curr - vmc->message_sizes == 1);
}
// check if data message consume even following message
static void test_stream_device_format_after_data(TestFixture *fixture, gconstpointer user_data)
{
uint8_t *p = vmc->message;
// add some messages into device buffer
p = add_format(p, 640, 480, SPICE_VIDEO_CODEC_TYPE_MJPEG);
p = add_stream_hdr(p, STREAM_TYPE_DATA, 5);
memcpy(p, "hello", 5);
p += 5;
p = add_stream_hdr(p, STREAM_TYPE_INVALID, 0);
vmc_emu_add_read_till(vmc, p);
g_test_expect_message(G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "Stream device received invalid message: Invalid message type");
test_kick();
// we should read all data
g_assert(vmc->message_sizes_curr - vmc->message_sizes == 1);
// we should have an error back
check_vmc_error_message();
}
// check empty message
static void test_stream_device_empty(TestFixture *fixture, gconstpointer user_data)
{
const auto msg_type = static_cast<StreamMsgType> GPOINTER_TO_INT(user_data);
uint8_t *p = vmc->message;
// add some messages into device buffer
p = add_stream_hdr(p, msg_type, 0);
vmc_emu_add_read_till(vmc, p);
p = add_format(p, 640, 480, SPICE_VIDEO_CODEC_TYPE_MJPEG);
vmc_emu_add_read_till(vmc, p);
p = add_format(p, 640, 480, SPICE_VIDEO_CODEC_TYPE_MJPEG);
vmc_emu_add_read_till(vmc, p);
test_kick();
// we should read all data
g_assert(vmc->message_sizes_curr - vmc->message_sizes == 3);
// we should have no data from the device
discard_server_capabilities();
g_assert_cmpint(vmc->write_pos, ==, 0);
}
// check that server refuse huge data messages
static void test_stream_device_huge_data(TestFixture *fixture, gconstpointer user_data)
{
uint8_t *p = vmc->message;
// add some messages into device buffer
p = add_stream_hdr(p, STREAM_TYPE_DATA, 33 * 1024 * 1024);
p = add_format(p, 640, 480, SPICE_VIDEO_CODEC_TYPE_MJPEG);
vmc_emu_add_read_till(vmc, p);
g_test_expect_message(G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "Stream device received invalid message: STREAM_DATA too large");
test_kick();
// we should read all data
g_assert(vmc->message_sizes_curr - vmc->message_sizes == 1);
// we should have an error back
check_vmc_error_message();
}
// check that server send all message
static void test_stream_device_data_message(TestFixture *fixture, gconstpointer user_data)
{
uint8_t *p = vmc->message;
// add some messages into device buffer
p = add_format(p, 640, 480, SPICE_VIDEO_CODEC_TYPE_MJPEG);
p = add_stream_hdr(p, STREAM_TYPE_DATA, 1017);
for (int i = 0; i < 1017; ++i, ++p) {
*p = static_cast<uint8_t>(i * 123 + 57);
}
vmc_emu_add_read_till(vmc, vmc->message + 51);
vmc_emu_add_read_till(vmc, vmc->message + 123);
vmc_emu_add_read_till(vmc, vmc->message + 534);
vmc_emu_add_read_till(vmc, p);
test_kick();
// we should read all data
g_assert(vmc->message_sizes_curr - vmc->message_sizes == 4);
// we should have no data from the device
discard_server_capabilities();
g_assert_cmpint(vmc->write_pos, ==, 0);
// make sure data were collapsed in a single message
g_assert_cmpint(num_send_data_calls, ==, 1);
g_assert_cmpint(send_data_bytes, ==, 1017);
}
static void test_display_info(TestFixture *fixture, gconstpointer user_data)
{
// initialize a QXL interface. This must be done before receiving the display info message from
// the stream
test_add_display_interface(test);
// qxl device supports 2 monitors
spice_qxl_set_device_info(&test->qxl_instance, "pci/0/1.2", 0, 2);
// craft a message from the mock stream device that provides display info to the server for the
// given stream
static const char address[] = "pci/a/b.cde";
StreamMsgDeviceDisplayInfo info = {
.stream_id = GUINT32_TO_LE(0x01020304),
.device_display_id = GUINT32_TO_LE(0x0a0b0c0d),
.device_address_len = GUINT32_TO_LE(sizeof(address)),
};
uint8_t *p = vmc->message;
p = add_stream_hdr(p, STREAM_TYPE_DEVICE_DISPLAY_INFO, sizeof(info) + sizeof(address));
memcpy(p, &info, sizeof(info));
p += sizeof(info);
strcpy(reinterpret_cast<char *>(p), address);
p += sizeof(address);
vmc_emu_add_read_till(vmc, p);
// parse the simulated display info message from the stream device so the server now has display
// info for the mock stream device
test_kick();
// build the buffer to send to the agent for display information
SpiceMarshaller *m = spice_marshaller_new();
reds_marshall_device_display_info(test->server, m);
int to_free;
size_t buf_len;
uint8_t *buf = spice_marshaller_linearize(m, 0, &buf_len, &to_free);
// check output buffer. The message that we send to the vdagent should combine display info for
// the stream device that we crafted above and the qxl device.
static const uint8_t expected_buffer[] = {
/* device count */ 3, 0, 0, 0,
/* channel_id */ 0, 0, 0, 0,
/* monitor_id */ 0, 0, 0, 0,
/* device_display_id */ 0, 0, 0, 0,
/* device_address_len */ 10, 0, 0, 0,
/* device_address */ 'p','c','i','/','0','/','1','.','2', 0,
/* channel_id */ 0, 0, 0, 0,
/* monitor_id */ 1, 0, 0, 0,
/* device_display_id */ 1, 0, 0, 0,
/* device_address_len */ 10, 0, 0, 0,
/* device_address */ 'p','c', 'i','/','0','/','1','.','2', 0,
/* channel_id */ 1, 0, 0, 0,
/* monitor_id */ 4, 3, 2, 1,
/* device_display_id */ 13, 12, 11, 10,
/* device_address_len */ 12, 0, 0, 0,
/* device_address */ 'p','c','i','/','a','/','b','.','c','d','e', 0
};
g_assert_cmpint(buf_len, ==, sizeof(expected_buffer));
g_assert_true(memcmp(buf, expected_buffer, buf_len) == 0);
if (to_free) {
free(buf);
}
spice_marshaller_destroy(m);
}
static void test_add(const char *name, void (*func)(TestFixture *, gconstpointer), gconstpointer arg)
{
g_test_add(name, TestFixture, arg, test_stream_device_setup, func, test_stream_device_teardown);
}
int main(int argc, char *argv[])
{
g_test_init(&argc, &argv, NULL);
test_add("/server/stream-device",
test_stream_device, nullptr);
test_add("/server/stream-device-unfinished",
test_stream_device_unfinished, nullptr);
test_add("/server/stream-device-multiple",
test_stream_device_multiple, nullptr);
test_add("/server/stream-device-format-after-data",
test_stream_device_format_after_data, nullptr);
test_add("/server/stream-device-empty-capabilities",
test_stream_device_empty, GINT_TO_POINTER(STREAM_TYPE_CAPABILITIES));
test_add("/server/stream-device-empty-data",
test_stream_device_empty, GINT_TO_POINTER(STREAM_TYPE_DATA));
test_add("/server/stream-device-huge-data",
test_stream_device_huge_data, nullptr);
test_add("/server/stream-device-data-message",
test_stream_device_data_message, nullptr);
test_add("/server/display-info", test_display_info, nullptr);
return g_test_run();
}
|