File: test-json-filter.c

package info (click to toggle)
event-dance 0.2.0-8~exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 2,096 kB
  • sloc: ansic: 29,033; javascript: 2,709; makefile: 434; xml: 247; sh: 30; python: 27
file content (173 lines) | stat: -rw-r--r-- 4,184 bytes parent folder | download | duplicates (3)
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
/*
 * test-json-filter.c
 *
 * EventDance, Peer-to-peer IPC library <http://eventdance.org>
 *
 * Copyright (C) 2009-2014, Igalia S.L.
 *
 * Authors:
 *   Eduardo Lima Mitev <elima@igalia.com>
 */

#include <glib.h>
#include <gio/gio.h>
#include <string.h>

#include "evd-json-filter.h"

static const gchar *evd_json_filter_chunks[] =
{
  "   [\"hell",
  "o world!\", 1, 4, fal",
  "se,    456, 4,   ",
  "null]      {\"foo\":1234} "
};

static const gchar *evd_json_filter_packets[] =
{
  "[\"hello world!\", 1, 4, false,    456, 4,   null]",
  "{\"foo\":1234} "
};

typedef struct
{
  EvdJsonFilter *filter;
  gint packet_index;
} EvdJsonFilterFixture;

void
evd_json_filter_fixture_setup (EvdJsonFilterFixture *f,
                               gconstpointer         test_data)
{
  f->filter = evd_json_filter_new ();

  f->packet_index = 0;
}

void
evd_json_filter_fixture_teardown (EvdJsonFilterFixture *f,
                                  gconstpointer         test_data)
{
  g_object_unref (f->filter);
}

static void
evd_json_filter_test_basic (EvdJsonFilterFixture *f,
                            gconstpointer         test_data)
{
  gint i;
  GError *error = NULL;
  const gchar *wrong[] =
    {
      "null",
      "true",
      "false",
      "1",
      "\"hello world!\"",
      "{]",
      "[}",
      "}}",
      "]]",
      "{foo: 123}",
      "{\"foo\":]",
      "{:\"bar\"]",
      "[\"bar\",]"
    };

  const gchar *good[] =
    {
      "{}",
      "[]",
      "  {  }  [  ] ",
      "{\"foo\":123}",
      "[null,true,false]",
      "[1, 0.01, 3.12e5, -666.99E+12, -0.23e-5]",
      "[\"hello world!\", \"foo (\\\"bar') \"]",
      "{\"obj\":{\"null\": true},\"arr\":[false]}"
    };

  /* wrong */
  for (i=0; i<sizeof (wrong) / sizeof (gchar *); i++)
    {
      g_assert (! evd_json_filter_feed_len (f->filter,
                                            wrong[i],
                                            strlen (wrong[i]),
                                            &error));

      g_assert_error (error,
                      G_IO_ERROR,
                      G_IO_ERROR_INVALID_DATA);

      g_error_free (error);
      error = NULL;
    }

  /* good */
  for (i=0; i<sizeof (good) / sizeof (gchar *); i++)
    {
      g_assert (evd_json_filter_feed (f->filter, good[i], &error));
      g_assert_no_error (error);
    }
}

static void
evd_json_filter_test_chunked_on_packet (EvdJsonFilter *filter,
                                        const gchar   *buffer,
                                        gsize          size,
                                        gpointer       user_data)
{
  EvdJsonFilterFixture *f = (EvdJsonFilterFixture *) user_data;

  g_assert (EVD_IS_JSON_FILTER (filter));

  g_assert_cmpint (g_strcmp0 (buffer, evd_json_filter_packets[f->packet_index]),
                   ==,
                   0);

  f->packet_index++;
}

static void
evd_json_filter_test_chunked (EvdJsonFilterFixture *f,
                              gconstpointer         test_data)
{
  gint i;
  GError *error = NULL;

  evd_json_filter_set_packet_handler (f->filter,
          (EvdJsonFilterOnPacketHandler) evd_json_filter_test_chunked_on_packet,
          (gpointer) f,
          NULL);

  for (i=0; i<sizeof (evd_json_filter_chunks) / sizeof (gchar *); i++)
    {
      g_assert (evd_json_filter_feed_len (f->filter,
                                          evd_json_filter_chunks[i],
                                          strlen (evd_json_filter_chunks[i]),
                                          &error));
      g_assert_no_error (error);
    }
}

gint
main (gint argc, gchar *argv[])
{
  g_type_init ();
  g_test_init (&argc, &argv, NULL);

  g_test_add ("/evd/json/filter/basic",
              EvdJsonFilterFixture,
              NULL,
              evd_json_filter_fixture_setup,
              evd_json_filter_test_basic,
              evd_json_filter_fixture_teardown);

  g_test_add ("/evd/json/filter/chunked",
              EvdJsonFilterFixture,
              NULL,
              evd_json_filter_fixture_setup,
              evd_json_filter_test_chunked,
              evd_json_filter_fixture_teardown);

  return g_test_run ();
}