summaryrefslogtreecommitdiff
path: root/test/test-entity.c
blob: 6197864b88d46c3caa4463b3a64a054febcfd79b (plain)
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#include <stdlib.h>
#include <osinfo/osinfo.h>
#include <check.h>

/* OsinfoEntity is abstract, so we need to trivially subclass it to test it */
typedef struct _OsinfoDummy        OsinfoDummy;
typedef struct _OsinfoDummyClass   OsinfoDummyClass;

struct _OsinfoDummy
{
    OsinfoEntity parent_instance;
};

struct _OsinfoDummyClass
{
    OsinfoEntityClass parent_class;
};

GType osinfo_dummy_get_type(void);

G_DEFINE_TYPE (OsinfoDummy, osinfo_dummy, OSINFO_TYPE_ENTITY);

static void osinfo_dummy_class_init(OsinfoDummyClass *klass G_GNUC_UNUSED){}
static void osinfo_dummy_init (OsinfoDummy *self G_GNUC_UNUSED) {}


START_TEST(test_id)
{
  OsinfoEntity *ent = g_object_new(osinfo_dummy_get_type(), "id", "myentity", NULL);
  gchar *id;

  fail_unless(g_strcmp0(osinfo_entity_get_id(ent), "myentity") == 0,
	      "Entity id was not 'myentity'");

  g_object_get(ent, "id", &id, NULL);
  fail_unless(g_strcmp0(id, "myentity") == 0,
	      "Entity id was not 'myentity'");
  g_free(id);

  g_object_unref(ent);
}
END_TEST

START_TEST(test_empty_props)
{
  OsinfoEntity *ent = g_object_new(osinfo_dummy_get_type(), "id", "myentity", NULL);

  GList *keys = osinfo_entity_get_param_keys(ent);
  fail_unless(keys == NULL, "Entity param key list was not empty");

  const gchar *value = osinfo_entity_get_param_value(ent, "wibble");
  fail_unless(value == NULL, "Entity param value was not NULL");

  GList *values = osinfo_entity_get_param_value_list(ent, "wibble");
  fail_unless(values == NULL, "Entity param value list was not NULL");

  g_object_unref(ent);
}
END_TEST

START_TEST(test_single_prop_value)
{
  OsinfoEntity *ent = g_object_new(osinfo_dummy_get_type(), "id", "myentity", NULL);

  osinfo_entity_add_param(ent, "hello", "world");

  GList *keys = osinfo_entity_get_param_keys(ent);
  fail_unless(keys != NULL, "Entity param key list was empty");
  fail_unless(keys->next == NULL, "Entity param key list has too many values");
  fail_unless(g_strcmp0(keys->data, "hello") == 0, "Entity param key was not 'hello'");
  g_list_free(keys);

  const gchar *value = osinfo_entity_get_param_value(ent, "hello");
  fail_unless(g_strcmp0(value, "world") == 0, "Entity param value was not 'world'");
  value = osinfo_entity_get_param_value(ent, "world");
  fail_unless(value == NULL, "Entity param bogus value was not NULL");

  GList *values = osinfo_entity_get_param_value_list(ent, "hello");
  fail_unless(values != NULL, "Entity param value list was empty");
  fail_unless(values->next == NULL, "Entity param value list has too many values");
  fail_unless(g_strcmp0(values->data, "world") == 0, "Entity param list value was not 'world'");

  g_object_unref(ent);
}
END_TEST

START_TEST(test_multi_prop_value)
{
  OsinfoEntity *ent = g_object_new(osinfo_dummy_get_type(), "id", "myentity", NULL);

  osinfo_entity_add_param(ent, "hello", "world");
  osinfo_entity_add_param(ent, "hello", "fred");
  osinfo_entity_add_param(ent, "hello", "elephant");

  GList *keys = osinfo_entity_get_param_keys(ent);
  fail_unless(keys != NULL, "Entity param key list was empty");
  fail_unless(keys->next == NULL, "Entity param key list has too many values");
  fail_unless(g_strcmp0(keys->data, "hello") == 0, "Entity param key was not 'hello'");
  g_list_free(keys);

  const gchar *value = osinfo_entity_get_param_value(ent, "hello");
  fail_unless(g_strcmp0(value, "world") == 0, "Entity param value was not 'world'");
  value = osinfo_entity_get_param_value(ent, "world");
  fail_unless(value == NULL, "Entity param bogus value was not NULL");

  GList *values = osinfo_entity_get_param_value_list(ent, "hello");
  fail_unless(values != NULL, "Entity param value list was empty");
  fail_unless(values->next != NULL, "Entity param value list doesn't have enough values");
  fail_unless(values->next->next != NULL, "Entity param value list doesn't have enough values");
  fail_unless(values->next->next->next == NULL, "Entity param value list has too many values");
  fail_unless(g_strcmp0(values->data, "world") == 0, "Entity param list first value was not 'world'");
  fail_unless(g_strcmp0(values->next->data, "fred") == 0, "Entity param list second value was not 'fred'");
  fail_unless(g_strcmp0(values->next->next->data, "elephant") == 0, "Entity param list third was not 'elephant'");

  g_object_unref(ent);
}
END_TEST

START_TEST(test_multi_props)
{
  OsinfoEntity *ent = g_object_new(osinfo_dummy_get_type(), "id", "myentity", NULL);

  osinfo_entity_add_param(ent, "hello", "world");
  osinfo_entity_add_param(ent, "fish", "food");
  osinfo_entity_add_param(ent, "kevin", "bacon");

  GList *keys = osinfo_entity_get_param_keys(ent);
  GList *tmp = keys;
  gboolean foundHello = FALSE;
  gboolean foundFish = FALSE;
  gboolean foundKevin = FALSE;
  gboolean foundBad = FALSE;
  while (tmp) {
    if (g_strcmp0(tmp->data, "hello") == 0)
      foundHello = TRUE;
    else if (g_strcmp0(tmp->data, "fish") == 0)
      foundFish = TRUE;
    else if (g_strcmp0(tmp->data, "kevin") == 0)
      foundKevin = TRUE;
    else
      foundBad = TRUE;
    tmp = tmp->next;
  }
  fail_unless(foundHello, "Entity param key list was missing 'hello'");
  fail_unless(foundFish, "Entity param key list was missing 'fish'");
  fail_unless(foundKevin, "Entity param key list was missing 'kevin'");
  fail_unless(!foundBad, "Entity param key list has unexpected key");
  g_list_free(keys);

  const gchar *value = osinfo_entity_get_param_value(ent, "hello");
  fail_unless(g_strcmp0(value, "world") == 0, "Entity param value was not 'world'");
  value = osinfo_entity_get_param_value(ent, "fish");
  fail_unless(g_strcmp0(value, "food") == 0, "Entity param value was not 'food'");
  value = osinfo_entity_get_param_value(ent, "kevin");
  fail_unless(g_strcmp0(value, "bacon") == 0, "Entity param value was not 'bacon'");

  GList *values = osinfo_entity_get_param_value_list(ent, "hello");
  fail_unless(values != NULL, "Entity param value list was empty");
  fail_unless(values->next == NULL, "Entity param value list has too many values");
  fail_unless(g_strcmp0(values->data, "world") == 0, "Entity param list value was not 'world'");

  values = osinfo_entity_get_param_value_list(ent, "fish");
  fail_unless(values != NULL, "Entity param value list was empty");
  fail_unless(values->next == NULL, "Entity param value list has too many values");
  fail_unless(g_strcmp0(values->data, "food") == 0, "Entity param list value was not 'food'");

  values = osinfo_entity_get_param_value_list(ent, "kevin");
  fail_unless(values != NULL, "Entity param value list was empty");
  fail_unless(values->next == NULL, "Entity param value list has too many values");
  fail_unless(g_strcmp0(values->data, "bacon") == 0, "Entity param list value was not 'bacon'");

  g_object_unref(ent);
}
END_TEST


START_TEST(test_multi_props_clear)
{
  OsinfoEntity *ent = g_object_new(osinfo_dummy_get_type(), "id", "myentity", NULL);

  osinfo_entity_add_param(ent, "hello", "world");
  osinfo_entity_add_param(ent, "fish", "food");

  GList *keys = osinfo_entity_get_param_keys(ent);
  GList *tmp = keys;
  gboolean foundHello = FALSE;
  gboolean foundFish = FALSE;
  gboolean foundBad = FALSE;
  while (tmp) {
    if (g_strcmp0(tmp->data, "hello") == 0)
      foundHello = TRUE;
    else if (g_strcmp0(tmp->data, "fish") == 0)
      foundFish = TRUE;
    else
      foundBad = TRUE;
    tmp = tmp->next;
  }
  fail_unless(foundHello, "Entity param key list was missing 'hello'");
  fail_unless(foundFish, "Entity param key list was missing 'fish'");
  fail_unless(!foundBad, "Entity param key list has unexpected key");
  g_list_free(keys);

  const gchar *value = osinfo_entity_get_param_value(ent, "hello");
  fail_unless(g_strcmp0(value, "world") == 0, "Entity param value was not 'world'");
  value = osinfo_entity_get_param_value(ent, "fish");
  fail_unless(g_strcmp0(value, "food") == 0, "Entity param value was not 'food'");

  osinfo_entity_clear_param(ent, "hello");

  keys = osinfo_entity_get_param_keys(ent);
  tmp = keys;
  foundHello = FALSE;
  foundFish = FALSE;
  foundBad = FALSE;
  while (tmp) {
    if (g_strcmp0(tmp->data, "hello") == 0)
      foundHello = TRUE;
    else if (g_strcmp0(tmp->data, "fish") == 0)
      foundFish = TRUE;
    else
      foundBad = TRUE;
    tmp = tmp->next;
  }
  fail_unless(!foundHello, "Entity param key list has unexpected 'hello'");
  fail_unless(foundFish, "Entity param key list was missing 'fish'");
  fail_unless(!foundBad, "Entity param key list has unexpected key");
  g_list_free(keys);

  value = osinfo_entity_get_param_value(ent, "hello");
  fail_unless(g_strcmp0(value, NULL) == 0, "Entity param value was not removed");
  value = osinfo_entity_get_param_value(ent, "fish");
  fail_unless(g_strcmp0(value, "food") == 0, "Entity param value was not 'food'");

  osinfo_entity_add_param(ent, "hello", "world");
  osinfo_entity_clear_param(ent, "fish");

  keys = osinfo_entity_get_param_keys(ent);
  tmp = keys;
  foundHello = FALSE;
  foundFish = FALSE;
  foundBad = FALSE;
  while (tmp) {
    if (g_strcmp0(tmp->data, "hello") == 0)
      foundHello = TRUE;
    else if (g_strcmp0(tmp->data, "fish") == 0)
      foundFish = TRUE;
    else
      foundBad = TRUE;
    tmp = tmp->next;
  }
  fail_unless(foundHello, "Entity param key list has unexpected 'hello'");
  fail_unless(!foundFish, "Entity param key list was missing 'fish'");
  fail_unless(!foundBad, "Entity param key list has unexpected key");
  g_list_free(keys);

  value = osinfo_entity_get_param_value(ent, "hello");
  fail_unless(g_strcmp0(value, "world") == 0, "Entity param value was not readded");
  value = osinfo_entity_get_param_value(ent, "fish");
  fail_unless(g_strcmp0(value, NULL) == 0, "Entity param value was not removed");

  g_object_unref(ent);
}
END_TEST


static Suite *
entity_suite(void)
{
  Suite *s = suite_create("Entity");
  TCase *tc = tcase_create("Core");
  tcase_add_test(tc, test_id);
  tcase_add_test(tc, test_empty_props);
  tcase_add_test(tc, test_single_prop_value);
  tcase_add_test(tc, test_multi_prop_value);
  tcase_add_test(tc, test_multi_props);
  tcase_add_test(tc, test_multi_props_clear);
  suite_add_tcase(s, tc);
  return s;
}

int main(void)
{
  int number_failed;
  Suite *s = entity_suite ();
  SRunner *sr = srunner_create (s);

  g_type_init();

  /* Upfront so we don't confuse valgrind */
  osinfo_dummy_get_type();

  srunner_run_all (sr, CK_ENV);
  number_failed = srunner_ntests_failed (sr);
  srunner_free (sr);

  return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}