| 1 |
/* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|---|
| 2 |
|
|---|
| 3 |
/* test-mux-serializer.c - Tests for the multiplexer serializer. |
|---|
| 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 "FlowMuxSerializer" |
|---|
| 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 ITERATIONS 500 |
|---|
| 32 |
#define BUFFER_PROPABILITY 5 |
|---|
| 33 |
|
|---|
| 34 |
typedef struct |
|---|
| 35 |
{ |
|---|
| 36 |
guint channel_id; |
|---|
| 37 |
guint32 size; |
|---|
| 38 |
} Chunk; |
|---|
| 39 |
|
|---|
| 40 |
static void |
|---|
| 41 |
add_chunk (GQueue *expected_chunks, guint channel_id, guint32 size) |
|---|
| 42 |
{ |
|---|
| 43 |
Chunk *chunk = g_new (Chunk, 1); |
|---|
| 44 |
chunk->channel_id = channel_id; |
|---|
| 45 |
chunk->size = size; |
|---|
| 46 |
g_queue_push_tail (expected_chunks, chunk); |
|---|
| 47 |
} |
|---|
| 48 |
|
|---|
| 49 |
static void |
|---|
| 50 |
test_run (void) |
|---|
| 51 |
{ |
|---|
| 52 |
FlowMuxSerializer *serializer; |
|---|
| 53 |
FlowUserAdapter *adapter; |
|---|
| 54 |
FlowPad *input_pad; |
|---|
| 55 |
FlowPacket *packet; |
|---|
| 56 |
GQueue *expected_chunks; |
|---|
| 57 |
guint32 chunk_size; |
|---|
| 58 |
guchar *buffer; |
|---|
| 59 |
int i, len; |
|---|
| 60 |
Chunk *chunk; |
|---|
| 61 |
FlowPacketQueue *input_packet_queue; |
|---|
| 62 |
guchar *hdr_buffer; |
|---|
| 63 |
guint current_channel_id; |
|---|
| 64 |
guint hdr_size; |
|---|
| 65 |
|
|---|
| 66 |
serializer = flow_mux_serializer_new (); |
|---|
| 67 |
|
|---|
| 68 |
adapter = flow_user_adapter_new (); |
|---|
| 69 |
flow_pad_connect (FLOW_PAD (flow_simplex_element_get_output_pad ( |
|---|
| 70 |
FLOW_SIMPLEX_ELEMENT (serializer))), |
|---|
| 71 |
FLOW_PAD (flow_simplex_element_get_input_pad ( |
|---|
| 72 |
FLOW_SIMPLEX_ELEMENT (adapter)))); |
|---|
| 73 |
input_pad = FLOW_PAD (flow_simplex_element_get_input_pad (FLOW_SIMPLEX_ELEMENT(serializer))); |
|---|
| 74 |
|
|---|
| 75 |
buffer = g_malloc (BUFFER_SIZE); |
|---|
| 76 |
|
|---|
| 77 |
current_channel_id = g_random_int_range (0, N_CHANNELS); |
|---|
| 78 |
flow_pad_push (input_pad, flow_packet_new_take_object (flow_mux_event_new (current_channel_id), 0)); |
|---|
| 79 |
chunk_size = 0; |
|---|
| 80 |
expected_chunks = g_queue_new (); |
|---|
| 81 |
for (i = 0; i < ITERATIONS; i++) |
|---|
| 82 |
{ |
|---|
| 83 |
len = g_random_int_range (1, BUFFER_SIZE); |
|---|
| 84 |
memset (buffer, 0xaa, len); |
|---|
| 85 |
|
|---|
| 86 |
if (g_random_int () % BUFFER_PROPABILITY != 0) |
|---|
| 87 |
{ |
|---|
| 88 |
packet = flow_packet_new (FLOW_PACKET_FORMAT_BUFFER, buffer, len); |
|---|
| 89 |
chunk_size += len; |
|---|
| 90 |
} |
|---|
| 91 |
else |
|---|
| 92 |
{ |
|---|
| 93 |
guint channel_id = g_random_int_range (0, N_CHANNELS); |
|---|
| 94 |
|
|---|
| 95 |
add_chunk (expected_chunks, current_channel_id, chunk_size); |
|---|
| 96 |
|
|---|
| 97 |
packet = flow_packet_new_take_object (flow_mux_event_new (channel_id), 0); |
|---|
| 98 |
current_channel_id = channel_id; |
|---|
| 99 |
chunk_size = 0; |
|---|
| 100 |
} |
|---|
| 101 |
flow_pad_push (input_pad, packet); |
|---|
| 102 |
} |
|---|
| 103 |
if (chunk_size > 0) |
|---|
| 104 |
add_chunk (expected_chunks, current_channel_id, chunk_size); |
|---|
| 105 |
|
|---|
| 106 |
flow_pad_push (input_pad, flow_create_simple_event_packet (FLOW_STREAM_DOMAIN, FLOW_STREAM_END)); |
|---|
| 107 |
|
|---|
| 108 |
hdr_size = flow_mux_serializer_get_header_size (serializer); |
|---|
| 109 |
hdr_buffer = g_alloca (hdr_size); |
|---|
| 110 |
input_packet_queue = flow_user_adapter_get_input_queue (adapter); |
|---|
| 111 |
|
|---|
| 112 |
while ((chunk = g_queue_pop_head (expected_chunks)) != NULL) |
|---|
| 113 |
{ |
|---|
| 114 |
guint channel_id; |
|---|
| 115 |
guint32 size; |
|---|
| 116 |
|
|---|
| 117 |
while (!flow_packet_queue_peek_packet (input_packet_queue, NULL, NULL)) |
|---|
| 118 |
flow_user_adapter_wait_for_input (adapter); |
|---|
| 119 |
|
|---|
| 120 |
if (!flow_packet_queue_pop_bytes_exact (input_packet_queue, hdr_buffer, hdr_size)) |
|---|
| 121 |
test_end (TEST_RESULT_FAILED, "header corrupted"); |
|---|
| 122 |
|
|---|
| 123 |
flow_mux_serializer_parse_header (serializer, hdr_buffer, &channel_id, &size); |
|---|
| 124 |
if (chunk->channel_id != channel_id || chunk->size != size) |
|---|
| 125 |
test_end (TEST_RESULT_FAILED, "chunk corrupt"); |
|---|
| 126 |
|
|---|
| 127 |
while (size > 0) |
|---|
| 128 |
{ |
|---|
| 129 |
guint to_read = size; |
|---|
| 130 |
gint read; |
|---|
| 131 |
|
|---|
| 132 |
if (to_read > BUFFER_SIZE) |
|---|
| 133 |
to_read = BUFFER_SIZE; |
|---|
| 134 |
read = flow_packet_queue_pop_bytes (input_packet_queue, buffer, to_read); |
|---|
| 135 |
g_assert (read > 0); |
|---|
| 136 |
size -= read; |
|---|
| 137 |
} |
|---|
| 138 |
g_free (chunk); |
|---|
| 139 |
} |
|---|
| 140 |
g_queue_free (expected_chunks); |
|---|
| 141 |
g_free (buffer); |
|---|
| 142 |
} |
|---|