tests: fix Search calls in tests
[profile/ivi/rygel.git] / tests / rygel-item-creator-test.vala
1 /*
2  * Copyright (C) 2012 Nokia Corporation.
3  *
4  * Author: Jens Georg <jensg@openismus.com>
5  *
6  * This file is part of Rygel.
7  *
8  * Rygel is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * Rygel is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  */
22
23 [CCode (cname = "uuid_generate", cheader_filename = "uuid/uuid.h")]
24 internal extern static void uuid_generate ([CCode (array_length = false)]
25                                            uchar[] uuid);
26 [CCode (cname = "uuid_unparse", cheader_filename = "uuid/uuid.h")]
27 internal extern static void uuid_unparse ([CCode (array_length = false)]
28                                           uchar[] uuid,
29                                           [CCode (array_length = false)]
30                                           uchar[] output);
31
32 public const string DIDL_ITEM = """<?xml version="1.0" encoding="UTF-8"?>
33 <DIDL-Lite
34     xmlns:dc="http://purl.org/dc/elements/1.1/"
35     xmlns="urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/"
36     xmlns:upnp="urn:schemas-upnp-org:metadata-1-0/upnp/"
37     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
38     xsi:schemaLocation="urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/
39         http://www.upnp.org/schemas/av/didl-lite-v2-20060531.xsd
40       urn:schemas-upnp-org:metadata-1-0/upnp/
41         http://www.upnp.org/schemas/av/upnp-v2-20060531.xsd">
42     <item id="" parentID="0" restricted="0">
43         <dc:title>New Song</dc:title>
44         <upnp:class>object.item.audioItem</upnp:class>
45         <res protocolInfo="*:*:*:*" />
46     </item>
47 </DIDL-Lite>""";
48
49 public class Rygel.ServiceAction : GLib.Object {
50     public int error_code;
51     public string error_message;
52     public string id;
53     public string elements;
54
55     public ServiceAction (string? container_id,
56                           string? elements) {
57         this.id = container_id;
58         this.elements = elements;
59     }
60
61     public void @return() {}
62     public void return_error (int code, string message) {
63         this.error_code = code;
64         this.error_message = message;
65     }
66
67     public new void @get (string arg1_name,
68                           Type arg1_type,
69                           out string arg1_val,
70                           string arg2_name,
71                           Type arg2_type,
72                           out string arg2_val) {
73         assert (arg1_name == "ContainerID");
74         assert (arg1_type == typeof (string));
75         arg1_val = id;
76
77         assert (arg2_name == "Elements");
78         assert (arg2_type == typeof (string));
79         arg2_val = elements;
80     }
81
82     public new void @set (string arg1_name,
83                           Type arg1_type,
84                           string arg1_val,
85                           string arg2_name,
86                           Type arg2_type,
87                           string arg2_val) {
88         assert (arg1_name == "ObjectID");
89         assert (arg1_type == typeof (string));
90
91         assert (arg2_name == "Result");
92         assert (arg2_type == typeof (string));
93     }
94 }
95
96 public class Rygel.HTTPServer : GLib.Object {
97 }
98
99 public class Rygel.ItemRemovalQueue : GLib.Object {
100     public static ItemRemovalQueue get_default () {
101         return new ItemRemovalQueue ();
102     }
103
104     public void queue (MediaItem item, Cancellable? cancellable) {
105     }
106 }
107
108 public class Rygel.MediaObject : GLib.Object {
109     public string id;
110     public string ref_id;
111     public unowned MediaContainer parent;
112     public string upnp_class;
113     public string title;
114     public GUPnP.OCMFlags ocm_flags;
115     public Gee.ArrayList<string> uris;
116
117     public void add_uri (string uri) {
118         this.uris.add (uri);
119     }
120 }
121
122 public class Rygel.MediaItem : Rygel.MediaObject {
123     public string dlna_profile;
124     public string mime_type;
125     public long size;
126     public bool place_holder;
127     public string date;
128
129     public MediaItem (string id, MediaContainer parent, string title) {
130         this.id = id;
131         this.parent = parent;
132         this.title = title;
133     }
134
135     public void serialize (GUPnP.DIDLLiteWriter writer, HTTPServer server) {
136     }
137 }
138
139 public class Rygel.MusicItem : Rygel.AudioItem {
140     public const string UPNP_CLASS = "object.item.audioItem.musicTrack";
141
142     public MusicItem (string id, MediaContainer parent, string title) {
143         base (id, parent, title);
144     }
145 }
146
147 public class Rygel.AudioItem : Rygel.MediaItem {
148     public const string UPNP_CLASS = "object.item.audioItem";
149     public string artist;
150     public string album;
151
152     public AudioItem (string id, MediaContainer parent, string title) {
153         base (id, parent, title);
154     }
155 }
156 public class Rygel.ImageItem : Rygel.MediaItem {
157     public const string UPNP_CLASS = "object.item.imageItem";
158     public ImageItem (string id, MediaContainer parent, string title) {
159         base (id, parent, title);
160     }
161 }
162
163 public class Rygel.VideoItem : Rygel.MediaItem {
164     public const string UPNP_CLASS = "object.item.videoItem";
165     public VideoItem (string id, MediaContainer parent, string title) {
166         base (id, parent, title);
167     }
168 }
169
170 public class Rygel.PhotoItem : Rygel.MediaItem {
171     public const string UPNP_CLASS = "object.item.imageItem.photo";
172     public string creator;
173
174     public PhotoItem (string id, MediaContainer parent, string title) {
175         base (id, parent, title);
176     }
177 }
178 public class Rygel.ContentDirectory : GLib.Object {
179     public Cancellable cancellable;
180     public MediaContainer root_container;
181     public HTTPServer http_server;
182 }
183
184 public class Rygel.MediaContainer : Rygel.MediaObject {
185     public Gee.ArrayList<string> create_classes = new Gee.ArrayList<string> ();
186     public int child_count;
187
188     // mockable elements
189     public MediaObject found_object = null;
190
191     public async MediaObject? find_object (string       id,
192                                            Cancellable? cancellable = null) {
193         Idle.add (() => { find_object.callback (); return false; });
194         yield;
195
196         return found_object;
197     }
198
199     public signal void container_updated (MediaContainer container);
200 }
201
202 public class Rygel.MediaObjects : Gee.ArrayList<MediaObject> {
203 }
204
205 public class Rygel.WritableContainer : Rygel.MediaContainer {
206     public bool can_create (string upnp_class) {
207         return this.create_classes.contains (upnp_class);
208     }
209
210     public async File? get_writable (Cancellable? cancellable = null) {
211         return File.new_for_commandline_arg ("/tmp");
212     }
213
214     public async void add_item (MediaItem    item,
215                                 Cancellable? cancellable = null) {
216     }
217 }
218
219 public class Rygel.SearchableContainer : Rygel.MediaContainer {
220     public MediaObjects result = new MediaObjects ();
221
222     public async MediaObjects search (SearchExpression expression,
223                                       int              offset,
224                                       int              count,
225                                       out int          total_matches,
226                                       string           soer_criteria,
227                                       Cancellable?     cancellable = null) {
228         Idle.add (() => { search.callback (); return false; });
229         yield;
230
231         return result;
232     }
233 }
234
235 public errordomain Rygel.ContentDirectoryError {
236     BAD_METADATA,
237     NO_SUCH_OBJECT,
238     INVALID_ARGS,
239     RESTRICTED_PARENT,
240     ERROR
241 }
242
243 public class Rygel.HTTPItemCreatorTest : GLib.Object {
244
245     public static int main (string[] args) {
246         var test = new HTTPItemCreatorTest ();
247         test.test_parse_args ();
248         test.test_didl_parsing ();
249         test.test_fetch_container ();
250
251         return 0;
252     }
253
254     // expected errors
255     Error no_such_object;
256     Error restricted_parent;
257     Error bad_metadata;
258     Error invalid_args;
259
260     public HTTPItemCreatorTest () {
261         this.no_such_object = new ContentDirectoryError.NO_SUCH_OBJECT("");
262         this.restricted_parent = new ContentDirectoryError.RESTRICTED_PARENT("");
263         this.bad_metadata = new ContentDirectoryError.BAD_METADATA("");
264         this.invalid_args = new ContentDirectoryError.INVALID_ARGS("");
265     }
266
267     private void test_parse_args () {
268         // check null container id
269         var content_directory = new ContentDirectory ();
270
271         var action = new ServiceAction (null, "");
272         var creator = new ItemCreator (content_directory, action);
273         creator.run ();
274         assert (action.error_code == no_such_object.code);
275
276         // check elements containing a comment
277         action = new ServiceAction ("0", "<!-- This is an XML comment -->");
278         creator = new ItemCreator (content_directory, action);
279         creator.run ();
280         assert (action.error_code == bad_metadata.code);
281
282         // check null elements
283         action = new ServiceAction ("0", null);
284         creator = new ItemCreator (content_directory, action);
285         creator.run ();
286         assert (action.error_code == bad_metadata.code);
287     }
288
289     private void test_didl_parsing_step (Xml.Doc *doc, int expected_code) {
290         string xml;
291
292         doc->dump_memory_enc (out xml);
293         var action = new ServiceAction ("0", xml);
294         var content_directory = new ContentDirectory ();
295         var creator = new ItemCreator (content_directory, action);
296         creator.run ();
297         assert (action.error_code == expected_code);
298     }
299
300     private void test_didl_parsing () {
301         var xml = Xml.Parser.read_memory (DIDL_ITEM,
302                                           DIDL_ITEM.length,
303                                           null,
304                                           null,
305                                           Xml.ParserOption.RECOVER |
306                                           Xml.ParserOption.NOBLANKS);
307         var didl_node = xml->children;
308         var item_node = didl_node->children;
309         var content_directory = new ContentDirectory ();
310
311         // test no DIDL
312         var action = new ServiceAction ("0", "");
313         var creator = new ItemCreator (content_directory, action);
314         creator.run ();
315         assert (action.error_code == bad_metadata.code);
316         assert (action.error_message == "Bad metadata");
317
318         // test empty DIDL
319         item_node->unlink ();
320         didl_node->set_content ("  ");
321         this.test_didl_parsing_step (xml, bad_metadata.code);
322
323         // test item node with missing restricted attribute
324         var tmp = item_node->copy (1);
325         tmp->unset_prop ("restricted");
326         didl_node->add_child (tmp);
327         this.test_didl_parsing_step (xml, bad_metadata.code);
328
329         // test item node with restricted=1
330         tmp->set_prop ("restricted", "1");
331         this.test_didl_parsing_step (xml, invalid_args.code);
332
333         // test item node with invalid id
334         tmp->unlink ();
335         tmp = item_node->copy (1);
336         tmp->set_prop ("id", "InvalidItemId");
337         didl_node->add_child (tmp);
338         this.test_didl_parsing_step (xml, bad_metadata.code);
339
340         // test item node with missing id
341         tmp->unset_prop ("id");
342         this.test_didl_parsing_step (xml, bad_metadata.code);
343
344         // test item node with missing title
345         tmp->unlink ();
346         tmp = item_node->copy (1);
347         var title_node = tmp->children;
348         title_node->unlink ();
349         didl_node->add_child (tmp);
350         this.test_didl_parsing_step (xml, bad_metadata.code);
351
352         // test missing, empty or non-item upnp class
353         tmp->unlink ();
354         tmp = item_node->copy (1);
355         var class_node = tmp->children->next;
356         class_node->set_content ("object.container");
357         didl_node->add_child (tmp);
358         this.test_didl_parsing_step (xml, bad_metadata.code);
359
360         class_node->set_content ("");
361         this.test_didl_parsing_step (xml, bad_metadata.code);
362
363         class_node->unlink ();
364         this.test_didl_parsing_step (xml, bad_metadata.code);
365     }
366
367     private void test_fetch_container_run (ItemCreator creator) {
368         var main_loop = new MainLoop (null, false);
369         creator.run.begin ( () => { main_loop.quit (); });
370         main_loop.run ();
371     }
372
373     private void test_fetch_container () {
374         // check case when object is not found
375         var content_directory = new ContentDirectory ();
376         var root_container = new SearchableContainer ();
377         content_directory.root_container = root_container;
378         var action = new ServiceAction ("0", DIDL_ITEM);
379         var creator = new ItemCreator (content_directory, action);
380         this.test_fetch_container_run (creator);
381         assert (action.error_code == no_such_object.code);
382
383         // check case when found object is not a container → Error 710
384         // cf. ContentDirectory:2 spec, Table 2-22
385         root_container.found_object = new MediaObject ();
386         this.test_fetch_container_run (creator);
387         assert (action.error_code == no_such_object.code);
388
389         // check case when found container does not have OCMUpload set
390         root_container.found_object = new MediaContainer ();
391         this.test_fetch_container_run (creator);
392         assert (action.error_code == restricted_parent.code);
393
394         // check case when found container is not a writable container
395         root_container.found_object.ocm_flags |= GUPnP.OCMFlags.UPLOAD;
396         this.test_fetch_container_run (creator);
397         assert (action.error_code == restricted_parent.code);
398
399         // check when found container does not have the correct create class
400         var container = new WritableContainer ();
401         container.create_classes.add ("object.item.imageItem.musicTrack");
402         container.ocm_flags |= GUPnP.OCMFlags.UPLOAD;
403         root_container.found_object = container;
404         this.test_fetch_container_run (creator);
405         assert (action.error_code == bad_metadata.code);
406
407         // check DLNA.ORG_AnyContainer when root container is not searchable
408         content_directory.root_container = new MediaContainer ();
409         action.id = "DLNA.ORG_AnyContainer";
410         this.test_fetch_container_run (creator);
411         assert (action.error_code == no_such_object.code);
412
413         // check DLNA.ORG_AnyContainer when no writable container is found
414         content_directory.root_container = new SearchableContainer ();
415         this.test_fetch_container_run (creator);
416         // We cannot distinguish this case from the "create-class doesn't match"
417         // case
418         assert (action.error_code == bad_metadata.code);
419     }
420 }