summaryrefslogtreecommitdiff
path: root/tests/test-filter.c
blob: 071f72e1db96735707539c8d5fb15b370040ed32 (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
/*
 * Copyright (C) 2009-2012, 2014 Red Hat, Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program. If not, see <http://www.gnu.org/licenses/>
 */

#include <osinfo/osinfo.h>



static void
test_basic(void)
{
    OsinfoFilter *filter = osinfo_filter_new();
    OsinfoDevice *dev = osinfo_device_new("e1000");
    GList *keys;
    GList *values;

    g_assert_true(OSINFO_IS_FILTER(filter));
    g_assert_true(osinfo_filter_matches(filter, OSINFO_ENTITY(dev)));

    osinfo_filter_add_constraint(filter, "class", "network");
    keys = osinfo_filter_get_constraint_keys(filter);
    g_assert_nonnull(keys);
    g_assert_cmpstr(keys->data, ==, "class");
    g_assert_null(keys->next);
    g_list_free(keys);

    osinfo_filter_add_constraint(filter, "class", "audio");
    keys = osinfo_filter_get_constraint_keys(filter);
    g_assert_nonnull(keys);
    g_assert_cmpstr(keys->data, ==, "class");
    g_assert_null(keys->next);
    g_list_free(keys);

    osinfo_filter_add_constraint(filter, "bus", "pci");
    keys = osinfo_filter_get_constraint_keys(filter);
    g_assert_nonnull(keys);
    g_assert_nonnull(keys->next);
    g_assert_cmpstr(keys->data, ==, "bus");
    g_assert_cmpstr(keys->next->data, ==, "class");
    g_assert_null(keys->next->next);
    g_list_free(keys);

    values = osinfo_filter_get_constraint_values(filter, "bus");
    g_assert_nonnull(values);
    g_assert_cmpstr(values->data, ==, "pci");
    g_assert_null(values->next);
    g_list_free(values);

    values = osinfo_filter_get_constraint_values(filter, "class");
    g_assert_nonnull(values);
    g_assert_nonnull(values->next);
    g_assert_cmpstr(values->data, ==, "audio");
    g_assert_cmpstr(values->next->data, ==, "network");
    g_assert_null(values->next->next);
    g_list_free(values);

    g_object_unref(dev);
    g_object_unref(filter);
}

static void
test_filter_single(void)
{
    OsinfoFilter *filter = osinfo_filter_new();
    OsinfoDevice *dev = osinfo_device_new("e1000");

    osinfo_entity_add_param(OSINFO_ENTITY(dev), "bus", "pci");

    osinfo_filter_add_constraint(filter, "class", "network");
    g_assert_false(osinfo_filter_matches(filter, OSINFO_ENTITY(dev)));

    osinfo_entity_add_param(OSINFO_ENTITY(dev), "class", "network");
    g_assert_true(osinfo_filter_matches(filter, OSINFO_ENTITY(dev)));

    osinfo_filter_clear_constraint(filter, "class");
    osinfo_filter_add_constraint(filter, "class", "audio");
    g_assert_false(osinfo_filter_matches(filter, OSINFO_ENTITY(dev)));

    g_object_unref(dev);
    g_object_unref(filter);
}

static void
test_filter_multi(void)
{
    OsinfoFilter *filter = osinfo_filter_new();
    OsinfoDevice *dev = osinfo_device_new("e1000");

    osinfo_entity_add_param(OSINFO_ENTITY(dev), "bus", "pci");

    osinfo_filter_add_constraint(filter, "bus", "isa");
    g_assert_false(osinfo_filter_matches(filter, OSINFO_ENTITY(dev)));

    osinfo_filter_add_constraint(filter, "bus", "pci");
    /* XXX is this right ?  Multiple values for a filter constraint
     * is treated as requiring all constraint values to match, not
     * required any to match */
    //g_assert_true(osinfo_filter_matches(filter, OSINFO_ENTITY(dev)));
    g_assert_false(osinfo_filter_matches(filter, OSINFO_ENTITY(dev)));

    osinfo_filter_clear_constraints(filter);
    osinfo_filter_add_constraint(filter, "bus", "pci");
    g_assert_true(osinfo_filter_matches(filter, OSINFO_ENTITY(dev)));

    g_object_unref(dev);
    g_object_unref(filter);
}


static void
test_filter_combine(void)
{
    OsinfoFilter *filter = osinfo_filter_new();
    OsinfoDevice *dev1 = osinfo_device_new("e1000");
    OsinfoDevice *dev2 = osinfo_device_new("ne2k");

    osinfo_entity_add_param(OSINFO_ENTITY(dev1), "bus", "pci");
    osinfo_entity_add_param(OSINFO_ENTITY(dev1), "class", "network");

    osinfo_entity_add_param(OSINFO_ENTITY(dev2), "bus", "isa");
    osinfo_entity_add_param(OSINFO_ENTITY(dev2), "class", "network");

    osinfo_filter_add_constraint(filter, "class", "network");
    g_assert_true(osinfo_filter_matches(filter, OSINFO_ENTITY(dev1)));
    g_assert_true(osinfo_filter_matches(filter, OSINFO_ENTITY(dev2)));

    osinfo_filter_add_constraint(filter, "bus", "isa");
    g_assert_false(osinfo_filter_matches(filter, OSINFO_ENTITY(dev1)));
    g_assert_true(osinfo_filter_matches(filter, OSINFO_ENTITY(dev2)));

    g_object_unref(dev1);
    g_object_unref(dev2);
    g_object_unref(filter);
}


int
main(int argc, char *argv[])
{
    g_test_init(&argc, &argv, NULL);

    g_test_add_func("/filter/basic", test_basic);
    g_test_add_func("/filter/filter_single", test_filter_single);
    g_test_add_func("/filter/filter_multi", test_filter_multi);
    g_test_add_func("/filter/filter_combine", test_filter_combine);

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

    return g_test_run();
}