core: Hide XBox album art handling
[profile/ivi/rygel.git] / tests / rygel-http-post-test.vala
1 /*
2  * Copyright (C) 2010 Nokia Corporation.
3  *
4  * Author: Zeeshan Ali (Khattak) <zeeshanak@gnome.org>
5  *                               <zeeshan.ali@nokia.com>
6  *
7  * This file is part of Rygel.
8  *
9  * Rygel is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Rygel is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22  */
23
24 using Soup;
25 using Gee;
26
27 public errordomain Rygel.TestError {
28     SKIP = 77,
29     TIMEOUT
30 }
31
32 public errordomain Rygel.ClientHacksError {
33     NA
34 }
35
36 public class Rygel.ClientHacks {
37     public static ClientHacks create (Message? message) throws Error {
38         throw new ClientHacksError.NA ("");
39     }
40
41     public void apply (MediaItem item) {
42     }
43 }
44
45 public class Rygel.HTTPPostTest : GLib.Object {
46     protected HTTPServer server;
47     protected HTTPClient client;
48
49     private bool server_done;
50     private bool client_done;
51
52     private MainLoop main_loop;
53
54     private Error error;
55
56     public static int main (string[] args) {
57         try {
58             var test = new HTTPPostTest ();
59
60             test.run ();
61         } catch (TestError.SKIP error) {
62             return error.code;
63         } catch (Error error) {
64             critical ("%s", error.message);
65
66             return -1;
67         }
68
69         return 0;
70     }
71
72     public HTTPPostTest () throws Error {
73         this.server = new HTTPServer ();
74         this.client = new HTTPClient (this.server.context,
75                                       this.server.uri);
76         this.main_loop = new MainLoop (null, false);
77     }
78
79     public virtual void run () throws Error {
80         // cleanup
81         var file = File.new_for_uri (MediaItem.URI);
82         FileUtils.remove (file.get_path ());
83         Timeout.add_seconds (3, this.on_timeout);
84         this.server.message_received.connect (this.on_message_received);
85         this.client.completed.connect (this.on_client_completed);
86
87         this.client.run.begin ();
88
89         this.main_loop.run ();
90
91         if (this.error != null) {
92             throw this.error;
93         }
94     }
95
96     private HTTPRequest create_request (Soup.Message msg) throws Error {
97         return new HTTPPost (this.server,
98                             this.server.context.server,
99                             msg);
100     }
101
102     private void on_client_completed (StateMachine client) {
103         this.client_done = true;
104         this.check_and_exit.begin ();
105     }
106
107     private void on_message_received (HTTPServer   server,
108                                       Soup.Message msg) {
109         this.handle_client_message.begin (msg);
110     }
111
112     private async void handle_client_message (Soup.Message msg) {
113         try {
114             var request = this.create_request (msg);
115
116             yield request.run ();
117
118             assert ((request as HTTPPost).item != null);
119
120             this.server_done = true;
121             this.check_and_exit.begin ();
122         } catch (Error error) {
123             this.error = error;
124             this.main_loop.quit ();
125
126             return;
127         }
128     }
129
130     private bool on_timeout () {
131         this.error = new TestError.TIMEOUT ("Timeout");
132         this.main_loop.quit ();
133
134         return false;
135     }
136
137     private async void check_and_exit () {
138         if (!(this.server_done && this.client_done)) {
139             return;
140         }
141
142         try {
143             var file = this.server.root_container.item.file;
144             var stream = yield file.read_async (Priority.HIGH, null);
145             var buffer = new uint8[HTTPClient.LENGTH];
146
147             yield stream.read_async (buffer, Priority.HIGH, null);
148
149             for (var i = 0; i < HTTPClient.LENGTH; i++) {
150                 assert (buffer[i] == this.client.content[i]);
151             }
152         } catch (Error error) {
153             this.error = error;
154         }
155
156         this.main_loop.quit ();
157     }
158 }
159
160 public class Rygel.HTTPServer : GLib.Object {
161     private const string SERVER_PATH = "/RygelHTTPServer/Rygel/Test";
162     public string path_root {
163         get {
164             return SERVER_PATH;
165         }
166     }
167
168     public MediaContainer root_container;
169     public GUPnP.Context context;
170
171     public string uri {
172         owned get {
173             var item_uri = new HTTPItemURI (this.root_container.ITEM_ID,
174                                             this);
175
176             return item_uri.to_string ();
177         }
178     }
179
180     public signal void message_received (Soup.Message message);
181
182     public HTTPServer () throws TestError {
183         try {
184             this.context = new GUPnP.Context (null, "lo", 0);
185         } catch (Error error) {
186             throw new TestError.SKIP ("Network context not available");
187         }
188
189         assert (this.context != null);
190         assert (this.context.host_ip != null);
191         assert (this.context.port > 0);
192
193         context.server.request_started.connect (this.on_request_started);
194
195         this.root_container = new MediaContainer ();
196     }
197
198     private void on_request_started (Soup.Server        server,
199                                      Soup.Message       msg,
200                                      Soup.ClientContext client) {
201         msg.got_headers.connect (this.on_got_headers);
202     }
203
204     private void on_got_headers (Soup.Message msg) {
205         this.message_received (msg);
206     }
207 }
208
209 public class Rygel.HTTPClient : GLib.Object, StateMachine {
210     public const size_t LENGTH = 1024;
211
212     public uint8[] content;
213
214     public GUPnP.Context context;
215     public Soup.Message msg;
216
217     public Cancellable cancellable { get; set; }
218
219     public HTTPClient (GUPnP.Context context,
220                        string        uri) {
221         this.context = context;
222         this.content = new uint8[1024];
223
224         this.msg = new Soup.Message ("POST",  uri);
225         assert (this.msg != null);
226     }
227
228     public async void run () {
229         SourceFunc run_continue = run.callback;
230
231         this.msg.request_body.append (MemoryUse.COPY, content);
232
233         this.context.session.queue_message (this.msg, (session, msg) => {
234             run_continue ();
235         });
236
237         yield;
238
239         this.completed ();
240     }
241 }
242
243 public class Rygel.MediaContainer : Rygel.MediaObject {
244     public const string ITEM_ID = "TestItem";
245
246     public signal void container_updated (MediaContainer container);
247
248     public string id = "TesContainer";
249     public MediaItem item;
250
251     private FileMonitor monitor;
252
253     public MediaContainer () {
254         this.item = new MediaItem (ITEM_ID, this);
255
256         this.monitor = this.item.file.monitor_file (FileMonitorFlags.NONE);
257         this.monitor.changed.connect (this.on_file_changed);
258     }
259
260     public async MediaObject? find_object (string       item_id,
261                                            Cancellable? cancellable)
262                                            throws Error {
263         SourceFunc find_object_continue = find_object.callback;
264         Idle.add (() => {
265             find_object_continue ();
266
267             return false;
268         });
269
270         yield;
271
272         if (item_id == ITEM_ID) {
273             return this.item;
274         } else {
275             return null;
276         }
277     }
278
279     public void on_file_changed (FileMonitor      monitor,
280                                  File             file,
281                                  File?            other_file,
282                                  FileMonitorEvent event_type) {
283         this.item.place_holder = false;
284         this.container_updated (this);
285     }
286 }
287
288 public class Rygel.MediaItem : Rygel.MediaObject {
289     public const string URI = "file:///tmp/rygel-upload-test.wav";
290
291     public weak MediaContainer parent;
292
293     public string id;
294     public long size = 1024;
295     public long duration = 1024;
296
297     public bool place_holder = true;
298
299     public File file;
300
301     public MediaItem (string id, MediaContainer parent) {
302         this.id = id;
303         this.parent = parent;
304
305         this.file = File.new_for_uri (URI);
306     }
307
308     ~MediaItem() {
309         try {
310             this.file.delete (null);
311         } catch (GLib.Error error) {
312             assert_not_reached ();
313         }
314     }
315
316     public async File? get_writable (Cancellable? cancellable) throws Error {
317         SourceFunc get_writable_continue = get_writable.callback;
318
319         Idle.add (() => {
320             get_writable_continue ();
321
322             return false;
323         });
324
325         yield;
326
327         return this.file;
328     }
329 }
330
331 internal class Rygel.HTTPResponse : Rygel.StateMachine, GLib.Object {
332     public Cancellable cancellable { get; set; }
333
334     private Soup.Message msg;
335     private Soup.Server server;
336
337     public HTTPResponse (HTTPPost get_request) {
338         this.msg = get_request.msg;
339         this.server = get_request.server;
340     }
341
342     public async void run () {
343         SourceFunc run_continue = run.callback;
344
345         Idle.add (() => {
346             run_continue ();
347
348             return false;
349         });
350
351         yield;
352
353         this.msg.set_status (Soup.KnownStatusCode.OK);
354         this.server.unpause_message (msg);
355
356         this.completed ();
357     }
358 }
359
360 public class Rygel.ItemRemovalQueue: GLib.Object {
361     public static ItemRemovalQueue get_default () {
362        return new ItemRemovalQueue ();
363     }
364
365     public bool dequeue (MediaItem item) {
366         return true;
367     }
368
369     public async void remove_now (MediaItem item, Cancellable? cancellable) {
370         Idle.add (remove_now.callback);
371
372         yield;
373     }
374 }
375
376 public class Rygel.MediaObject : GLib.Object {}
377
378 public errordomain Rygel.ContentDirectoryError {
379     INVALID_ARGS = 402
380 }