| 1 |
/* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|---|
| 2 |
|
|---|
| 3 |
/* test-mux-deserializer.c - Tests for the multiplexer deserializer. |
|---|
| 4 |
* |
|---|
| 5 |
* Copyright (C) 2007 Andreas Rottmann |
|---|
| 6 |
* |
|---|
| 7 |
* This library is free software; you can redistribute it and/or |
|---|
| 8 |
* modify it under the terms of the GNU Lesser General Public |
|---|
| 9 |
* License as published by the Free Software Foundation; either |
|---|
| 10 |
* version 2 of the License, or (at your option) any later version. |
|---|
| 11 |
* |
|---|
| 12 |
* This library is distributed in the hope that it will be useful, |
|---|
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|---|
| 15 |
* Lesser General Public License for more details. |
|---|
| 16 |
* |
|---|
| 17 |
* You should have received a copy of the GNU Lesser General Public |
|---|
| 18 |
* License along with this program. If not, see |
|---|
| 19 |
* <http://www.gnu.org/licenses/>. |
|---|
| 20 |
* |
|---|
| 21 |
* Authors: Andreas Rottmann <a.rottmann@gmx.at> |
|---|
| 22 |
*/ |
|---|
| 23 |
|
|---|
| 24 |
#define TEST_UNIT_NAME "FlowMuxDeserializer" |
|---|
| 25 |
#define TEST_TIMEOUT_S 20 |
|---|
| 26 |
|
|---|
| 27 |
#include "test-common.c" |
|---|
| 28 |
|
|---|
| 29 |
#define N_CHANNELS 5 |
|---|
| 30 |
#define BUFFER_SIZE 4096 |
|---|
| 31 |
#define MAX_CHUNK_SIZE (1 << 16) |
|---|
| 32 |
#define ITERATIONS 500 |
|---|
| 33 |
#define BUFFER_PROPABILITY 5 |
|---|
| 34 |
|
|---|
| 35 |
typedef struct |
|---|
| 36 |
{ |
|---|
| 37 |
guint channel_id; |
|---|
| 38 |
guint32 size; |
|---|
| 39 |
} Chunk; |
|---|
| 40 |
|
|---|
| 41 |
static void |
|---|
| 42 |
add_chunk (GQueue *expected_chunks, guint channel_id, guint32 size) |
|---|
| 43 |
{ |
|---|
| 44 |
Chunk *chunk = g_new (Chunk, 1); |
|---|
| 45 |
chunk->channel_id = channel_id; |
|---|
| 46 |
chunk->size = size; |
|---|
| 47 |
g_queue_push_tail (expected_chunks, chunk); |
|---|
| 48 |
} |
|---|
| 49 |
|
|---|
| 50 |
static void |
|---|
| 51 |
test_run (void) |
|---|
| 52 |
{ |
|---|
| 53 |
FlowMuxDeserializer *deserializer; |
|---|
| 54 |
FlowUserAdapter *adapter; |
|---|
| 55 |
FlowPad *input_pad; |
|---|
| 56 |
FlowPacket *packet; |
|---|
| 57 |
GQueue *expected_chunks; |
|---|
| 58 |
guint32 chunk_size; |
|---|
| 59 |
guchar *buffer; |
|---|
| 60 |
int i, len; |
|---|
| 61 |
Chunk *chunk; |
|---|
| 62 |
FlowPacketQueue *input_packet_queue; |
|---|
| 63 |
guint current_channel_id; |
|---|
| 64 |
|
|---|
| 65 |
deserializer = flow_mux_deserializer_new (); |
|---|
| 66 |
|
|---|
| 67 |
adapter = flow_user_adapter_new (); |
|---|
| 68 |
flow_pad_connect (FLOW_PAD (flow_simplex_element_get_output_pad ( |
|---|
| 69 |
FLOW_SIMPLEX_ELEMENT (deserializer))), |
|---|
| 70 |
FLOW_PAD (flow_simplex_element_get_input_pad ( |
|---|
| 71 |
FLOW_SIMPLEX_ELEMENT (adapter)))); |
|---|
| 72 |
input_pad = FLOW_PAD (flow_simplex_element_get_input_pad (FLOW_SIMPLEX_ELEMENT(deserializer))); |
|---|
| 73 |
|
|---|
| 74 |
buffer = g_malloc (BUFFER_SIZE); |
|---|
| 75 |
memset (buffer, 0xaa, BUFFER_SIZE); |
|---|
| 76 |
|
|---|
| 77 |
chunk_size = 0; |
|---|
| 78 |
expected_chunks = g_queue_new (); |
|---|
| 79 |
for (i = 0; i < ITERATIONS; i++) |
|---|
| 80 |
{ |
|---|
| 81 |
FlowPacket *header; |
|---|
| 82 |
guint hdr_size; |
|---|
| 83 |
|
|---|
| 84 |
current_channel_id = g_random_int_range (0, N_CHANNELS); |
|---|
| 85 |
len = g_random_int_range (1, MAX_CHUNK_SIZE); |
|---|
| 86 |
hdr_size = flow_mux_deserializer_get_header_size (deserializer); |
|---|
| 87 |
flow_mux_deserializer_unparse_header (deserializer, buffer, current_channel_id, len); |
|---|
| 88 |
header = flow_packet_new (FLOW_PACKET_FORMAT_BUFFER, buffer, hdr_size); |
|---|
| 89 |
flow_pad_push (input_pad, header); |
|---|
| 90 |
add_chunk (expected_chunks, current_channel_id, len); |
|---|
| 91 |
|
|---|
| 92 |
while (len > 0) |
|---|
| 93 |
{ |
|---|
| 94 |
guint n = g_random_int_range (1, BUFFER_SIZE); |
|---|
| 95 |
if (n > len) |
|---|
| 96 |
n = len; |
|---|
| 97 |
flow_pad_push (input_pad, flow_packet_new (FLOW_PACKET_FORMAT_BUFFER, buffer, n)); |
|---|
| 98 |
len -= n; |
|---|
| 99 |
} |
|---|
| 100 |
} |
|---|
| 101 |
|
|---|
| 102 |
flow_pad_push (input_pad, flow_create_simple_event_packet (FLOW_STREAM_DOMAIN, FLOW_STREAM_END)); |
|---|
| 103 |
|
|---|
| 104 |
input_packet_queue = flow_user_adapter_get_input_queue (adapter); |
|---|
| 105 |
|
|---|
| 106 |
while ((chunk = g_queue_pop_head (expected_chunks)) != NULL) |
|---|
| 107 |
{ |
|---|
| 108 |
guint32 size; |
|---|
| 109 |
gpointer data; |
|---|
| 110 |
|
|---|
| 111 |
while ((packet = flow_packet_queue_pop_packet (input_packet_queue)) == NULL) |
|---|
| 112 |
flow_user_adapter_wait_for_input (adapter); |
|---|
| 113 |
|
|---|
| 114 |
data = flow_packet_get_data (packet); |
|---|
| 115 |
if (packet->format != FLOW_PACKET_FORMAT_OBJECT |
|---|
| 116 |
|| !FLOW_IS_MUX_EVENT (data)) |
|---|
| 117 |
test_end (TEST_RESULT_FAILED, "expected mux event packet"); |
|---|
| 118 |
|
|---|
| 119 |
if (flow_mux_event_get_channel_id (FLOW_MUX_EVENT (data)) != chunk->channel_id) |
|---|
| 120 |
test_end (TEST_RESULT_FAILED, "unexpected channel id in mux event packet"); |
|---|
| 121 |
|
|---|
| 122 |
flow_packet_free (packet); |
|---|
| 123 |
|
|---|
| 124 |
size = 0; |
|---|
| 125 |
while (TRUE) |
|---|
| 126 |
{ |
|---|
| 127 |
while (!flow_packet_queue_peek_packet (input_packet_queue, &packet, NULL)) |
|---|
| 128 |
flow_user_adapter_wait_for_input (adapter); |
|---|
| 129 |
if (packet->format != FLOW_PACKET_FORMAT_BUFFER) |
|---|
| 130 |
break; |
|---|
| 131 |
size += packet->size; |
|---|
| 132 |
flow_packet_queue_drop_packet (input_packet_queue); |
|---|
| 133 |
} |
|---|
| 134 |
|
|---|
| 135 |
if (size != chunk->size) |
|---|
| 136 |
test_end (TEST_RESULT_FAILED, "chunk differs in size"); |
|---|
| 137 |
|
|---|
| 138 |
g_free (chunk); |
|---|
| 139 |
} |
|---|
| 140 |
g_queue_free (expected_chunks); |
|---|
| 141 |
g_free (buffer); |
|---|
| 142 |
} |
|---|