[Spice-devel] [PATCH v4 16/41] dissector: Add code to read input from a file (or stdin)

Frediano Ziglio fziglio at redhat.com
Thu Jul 23 08:54:33 PDT 2015


This allows to emulate reading from network

Signed-off-by: Frediano Ziglio <fziglio at redhat.com>
---
 codegen/check_dissector  |  4 ++--
 codegen/dissector_test.c | 32 +++++++++++++++++++++++++++++---
 2 files changed, 31 insertions(+), 5 deletions(-)

diff --git a/codegen/check_dissector b/codegen/check_dissector
index f1444a2..7d6d086 100755
--- a/codegen/check_dissector
+++ b/codegen/check_dissector
@@ -6,8 +6,8 @@ error() {
 	exit 1
 }
 
-./dissector_test 1 100
-if ./dissector_test 1 99; then
+./dissector_test 1 100 </dev/null
+if ./dissector_test 1 99 </dev/null; then
 	error "This test should fail"
 fi
 exit 0
diff --git a/codegen/dissector_test.c b/codegen/dissector_test.c
index 5a49f40..ba87367 100644
--- a/codegen/dissector_test.c
+++ b/codegen/dissector_test.c
@@ -15,6 +15,7 @@ enum {
 	first_hf_registered = 0x10000,
 	first_ei_registered = 0x20000,
 	first_tree_registered = 0x30000,
+	max_message_size = 0x100000,
 };
 static int last_hf_registered = first_hf_registered - 1;
 static int last_ei_registered = first_ei_registered - 1;
@@ -474,8 +475,9 @@ static const struct option long_options[] = {
 	{ "client", 0, NULL, 'c' },
 	{ "output-file", required_argument, NULL, 'o' },
 	{ "xml", 0, NULL, 'x' },
+	{ "input-file", required_argument, NULL, 'i' },
 };
-static const char options[] = "hscxo:";
+static const char options[] = "hscxo:i:";
 
 static void syntax(FILE *f, int exit_value)
 {
@@ -488,6 +490,7 @@ static void syntax(FILE *f, int exit_value)
 		"  -c, --client             Process client messages\n"
 		"  -x, --xml                Output in XML format\n"
 		"  -o, --output-file=FILE   Output to specified file\n"
+		"  -i, --input-file=FILE    Input from specified file\n"
 	);
 	exit(exit_value);
 }
@@ -495,8 +498,12 @@ static void syntax(FILE *f, int exit_value)
 int main(int argc, char **argv)
 {
 	int channel, message_type;
+	guint8 *buf, *p, *pend;
+	size_t size;
+	FILE *f = stdin;
 	GlobalInfo glb;
 	proto_tree tree;
+	struct tvbuff tvb;
 	spice_dissect_func_t (*msg_func)(guint8 channel);
 	spice_dissect_func_t channel_func = NULL;
 
@@ -526,6 +533,10 @@ int main(int argc, char **argv)
 			output_file = fopen(optarg, "w");
 			check(output_file != NULL, "Error opening output file");
 			break;
+		case 'i':
+			f = fopen(optarg, "rb");
+			check(f != NULL, "Error opening input file");
+			break;
 		default:
 			syntax(stderr, EXIT_FAILURE);
 			break;
@@ -540,10 +551,25 @@ int main(int argc, char **argv)
 	hfs = g_ptr_array_new_with_free_func(free);
 	spice_register_fields(1, NULL);
 
+	/* read message from standard input */
+	buf = malloc(max_message_size);
+	assert(buf);
+	p = buf;
+	pend = buf + max_message_size;
+	while (p < pend && (size = fread(p, 1, pend - p, f)) > 0)
+		p += size;
+	assert(p < pend);
+	assert(!ferror(f));
+	size = p - buf;
+
+	memset(&tvb, 0, sizeof(tvb));
+	tvb.data = buf;
+	tvb.len = size;
+
 	memset(&glb, 0, sizeof(glb));
-	glb.tvb = NULL;
+	glb.tvb = &tvb;
 	glb.message_offset = 0;
-	glb.message_end = 0;
+	glb.message_end = tvb.len;
 
 	channel_func = msg_func(channel);
 	assert(channel_func);
-- 
2.1.0



More information about the Spice-devel mailing list