Re: [Spice-devel] [PATCH v3 26/51] test: Add code to read input from a file (or stdin)

2015-07-23 Thread Christophe Fergeau
On Tue, Jul 21, 2015 at 05:45:56PM +0100, Frediano Ziglio wrote:
 This allow to emulate reading from network

'allows'

 @@ -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;

This could probably use glib stream functions as well, which might make
the code a bit simpler (or more complicated given that this is very
short ;) I'd tend to move this bit to a small separate helper though for
readability.

Christophe


pgpDLoVwcK5Tf.pgp
Description: PGP signature
___
Spice-devel mailing list
Spice-devel@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/spice-devel


[Spice-devel] [PATCH v3 26/51] test: Add code to read input from a file (or stdin)

2015-07-21 Thread Frediano Ziglio
This allow to emulate reading from network

Signed-off-by: Frediano Ziglio fzig...@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 = 0x1,
first_ei_registered = 0x2,
first_tree_registered = 0x3,
+   max_message_size = 0x10,
 };
 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, --xmlOutput in XML format\n
  -o, --output-file=FILE   Output to specified file\n
+ -i, --input-file=FILEInput 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

___
Spice-devel mailing list
Spice-devel@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/spice-devel