summaryrefslogtreecommitdiff
path: root/test/direct/marshalling-test.vala
blob: a611fe181f868e524d2849f0bb764402b5a76338 (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
/* marshalling-test.vala
 *
 * Copyright © 2011 Michal Hruby <michal.mhr@gmail.com>
 * Copyright © 2011 Canonical Ltd.
 *             By Siegfried-A. Gevatter <siegfried.gevatter@collabora.co.uk>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 2.1 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 Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

using Zeitgeist;
using Assertions;

int main (string[] argv)
{
    Test.init (ref argv);

    Test.add_func ("/Marshalling/subjects", subject_test);
    Test.add_func ("/Marshalling/event", event_test);
    Test.add_func ("/Marshalling/events", events_test);
    Test.add_func ("/Marshalling/timerange", timerange_test);
    Test.add_func ("/Marshalling/corrupt_events", corrupt_events_test);
    Test.add_func ("/Marshalling/corrupt_subjects", corrupt_subjects_test);
    Test.add_func ("/Marshalling/corrupt_timerange", corrupt_timerange_test);

    return Test.run ();
}

Subject create_subject ()
{
    var s = new Subject ();
    s.uri = "scheme:///uri";
    s.interpretation = "subject_interpretation_uri";
    s.manifestation = "subject_manifestation_uri";
    s.mimetype = "text/plain";
    s.origin = "scheme:///";
    s.text = "Human readable text";
    s.storage = "";
    s.current_uri = "scheme:///uri";

    return s;
}

Event create_event ()
{
    var e = new Event ();
    e.id = 1234;
    e.timestamp = 1234567890L;
    e.interpretation = "interpretation_uri";
    e.manifestation = "manifestation_uri";
    e.actor = "test.desktop";
    e.origin = "source";

    return e;
}

void subject_test ()
{
    for (int i = 0; i < 1000; i++)
    {
        Variant vsubject = create_subject ().to_variant ();
        var subject = new Subject.from_variant (vsubject);
        warn_if_fail (subject != null);
    }
}

void event_test ()
{
    for (int i = 0; i < 1000; i++)
    {
        Variant vevent = create_event ().to_variant ();
        var event = new Event.from_variant (vevent);
        warn_if_fail (event != null);
    }
}

void events_test ()
{
    GenericArray<Event> events = new GenericArray<Event> ();
    for (int i = 0; i < 1000; i++)
    {
        var e = create_event ();
        e.add_subject (create_subject ());
        events.add (e);
    }

    Variant vevents = Events.to_variant (events);

    var demarshalled = Events.from_variant (vevents);
    assert_cmpint (demarshalled.length, CompareOperator.EQ, 1000);
}

void timerange_test ()
{
    for (int64 i = 0; i < 1000; i++)
    {
        Variant v = new Variant("(xx)", i, i+42);
        TimeRange timerange = new TimeRange.from_variant (v);
        assert_cmpint ((int) timerange.start, CompareOperator.EQ, (int)i);
        assert_cmpint ((int) timerange.end, CompareOperator.EQ, (int)i+42);
    }
}

void corrupt_events_test ()
{
    // Let's just try to parse some crap and see that it does not crash :)
    Variant v = new Variant ("(s)", "Zeitgeist is so awesome");
    bool error_thrown = false;
    try
    {
        new Event.from_variant (v);
    }
    catch (DataModelError.INVALID_SIGNATURE err) {
        error_thrown = true;
    }
    assert (error_thrown);
}

void corrupt_subjects_test ()
{
    Variant v;
    string[] arr;
    bool error_thrown;

    // Parse a valid subject variant
    arr = { "uri", "interpretation", "manifestation", "origin",
        "mimetype", "text", "storage", "current_uri" };
    v = new Variant.strv (arr);
    new Subject.from_variant (v);

    // Another valid variant, but this time without current_uri
    arr = { "uri", "interpretation", "manifestation", "origin",
        "mimetype", "text", "storage" };
    v = new Variant.strv (arr);
    new Subject.from_variant (v);

    // And this one is not valid :(
    arr = { "uri", "interpretation", "manifestation", "origin",
        "mimetype", "text" };
    v = new Variant.strv (arr);
    error_thrown = false;
    try
    {
        new Subject.from_variant (v);
    }
    catch (DataModelError err)
    {
        assert (err is DataModelError.INVALID_SIGNATURE);
        error_thrown = true;
    }
    assert (error_thrown);

    // Those one is just insane :)
    v = new Variant ("(x)", 42);
    error_thrown = false;
    try
    {
        new Subject.from_variant (v);
    }
    catch (DataModelError.INVALID_SIGNATURE err)
    {
        error_thrown = true;
    }
    assert (error_thrown);
}

void corrupt_timerange_test ()
{
    Variant v = new Variant ("(s)", "oh noes, what is this?");
    bool error_thrown = false;
    try
    {
        new TimeRange.from_variant (v);
    }
    catch (DataModelError.INVALID_SIGNATURE err)
    {
        error_thrown = true;
    }
}

// vim:expandtab:ts=4:sw=4