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 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
|
/*
* Copyright (C) 2010 Nokia Corporation.
*
* Author: Zeeshan Ali (Khattak) <zeeshanak@gnome.org>
* <zeeshan.ali@nokia.com>
*
* This file is part of Rygel.
*
* Rygel 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.
*
* Rygel 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
using Soup;
using Gee;
public errordomain Rygel.TestError {
SKIP = 77,
TIMEOUT
}
public errordomain Rygel.ClientHacksError {
NA
}
public class Rygel.ClientHacks {
public static ClientHacks create (Message? message) throws Error {
throw new ClientHacksError.NA ("");
}
public void apply (MediaFileItem item) {
}
}
public class Rygel.TestRequestFactory {
public Soup.Message msg;
public Soup.Status? expected_code;
public bool cancel;
public TestRequestFactory (Soup.Message msg,
Soup.Status? expected_code,
bool cancel = false) {
this.msg = msg;
this.expected_code = expected_code;
this.cancel = cancel;
}
internal HTTPPost create_post (HTTPServer http_server,
Soup.Server server,
Soup.Message msg) {
HTTPPost request = new HTTPPost (http_server, server, msg);
return request;
}
}
public class Rygel.HTTPPostTest : GLib.Object {
protected HTTPServer server;
protected HTTPClient client;
private bool server_done;
private bool client_done;
private bool ready;
private MainLoop main_loop;
private Error error;
private ArrayList<TestRequestFactory> requests;
private TestRequestFactory current_request;
public static int main (string[] args) {
try {
var test = new HTTPPostTest ();
test.run ();
} catch (TestError.SKIP error) {
return error.code;
} catch (Error error) {
critical ("%s", error.message);
return -1;
}
return 0;
}
public HTTPPostTest () throws Error {
this.server = new HTTPServer ();
this.client = new HTTPClient (this.server.context);
this.main_loop = new MainLoop (null, false);
this.create_test_messages();
}
public virtual void run () throws Error {
// cleanup
var file = File.new_for_uri (MediaFileItem.URI);
FileUtils.remove (file.get_path ());
Timeout.add_seconds (10, this.on_timeout);
this.server.message_received.connect (this.on_message_received);
this.client.completed.connect (this.on_client_completed);
this.start_next_test_request ();
this.main_loop.run ();
if (this.error != null) {
throw this.error;
}
}
private void create_test_messages () {
requests = new ArrayList<TestRequestFactory> ();
var request = new Soup.Message ("POST", this.server.uri);
requests.add (new TestRequestFactory (request, Soup.Status.OK));
request = new Soup.Message ("POST", this.server.uri);
requests.add (new TestRequestFactory (request,
Soup.Status.NOT_FOUND));
request = new Soup.Message ("POST", this.server.create_uri ("NullItem"));
requests.add (new TestRequestFactory (request,
Soup.Status.BAD_REQUEST));
request = new Soup.Message ("POST",
this.server.create_uri ("ErrorItem"));
requests.add (new TestRequestFactory (request,
Soup.Status.OK));
request = new Soup.Message ("POST",
this.server.create_uri ("CancelItem"));
requests.add (new TestRequestFactory (request,
Soup.Status.OK, true));
request = new Soup.Message ("POST",
this.server.create_uri ("VanishingItem"));
requests.add (new TestRequestFactory (request, Soup.Status.OK));
}
private HTTPRequest create_request (Soup.Message msg) throws Error {
var srv = this.server.context.server;
var request = this.current_request.create_post (this.server, srv, msg);
return request;
}
private void start_next_test_request() {
this.current_request = requests.remove_at (0);
this.client.msg = this.current_request.msg;
this.client.run.begin ();
}
private void on_client_completed (StateMachine client) {
if (requests.size > 0) {
if (this.server_done) {
this.server_done = false;
this.start_next_test_request ();
} else {
this.ready = true;
}
} else {
if (this.server_done) {
this.main_loop.quit ();
}
this.client_done = true;
}
}
private void on_message_received (HTTPServer server,
Soup.Message msg) {
this.handle_client_message.begin (msg);
}
private async void handle_client_message (Soup.Message msg) {
try {
var request = this.create_request (msg);
if (this.current_request.cancel) {
request.cancellable.cancel ();
} else {
yield request.run ();
debug ("status.code: %d", (int) msg.status_code);
assert (msg.status_code == this.current_request.expected_code);
this.check_result.begin ();
}
if (this.client_done) {
this.main_loop.quit ();
} else if (this.ready) {
this.ready = false;
start_next_test_request ();
} else {
this.server_done = true;
}
} catch (Error error) {
this.error = error;
this.main_loop.quit ();
return;
}
}
private bool on_timeout () {
this.error = new TestError.TIMEOUT ("Timeout");
this.main_loop.quit ();
return false;
}
private async void check_result () {
try {
var file = this.server.root_container.item.file;
var stream = yield file.read_async (Priority.HIGH, null);
var buffer = new uint8[HTTPClient.LENGTH];
yield stream.read_async (buffer, Priority.HIGH, null);
for (var i = 0; i < HTTPClient.LENGTH; i++) {
assert (buffer[i] == this.client.content[i]);
}
} catch (IOError.NOT_FOUND e) {
return;
} catch (Error error) {
this.error = error;
this.main_loop.quit ();
}
}
}
public class Rygel.HTTPServer : GLib.Object {
private const string SERVER_PATH = "/RygelHTTPServer/Rygel/Test";
public string path_root {
get {
return SERVER_PATH;
}
}
public MediaContainer root_container;
public GUPnP.Context context;
public string uri {
owned get {
var item = new MediaFileItem (MediaContainer.ITEM_ID, this.root_container);
var item_uri = new HTTPItemURI (item, this);
return item_uri.to_string ();
}
}
public string create_uri(string item_id) {
var item = new MediaFileItem (item_id, this.root_container);
var item_uri = new HTTPItemURI (item, this);
return item_uri.to_string ();
}
public signal void message_received (Soup.Message message);
public HTTPServer () throws TestError {
try {
this.context = new GUPnP.Context (null, "lo", 0);
} catch (Error error) {
throw new TestError.SKIP ("Network context not available");
}
assert (this.context != null);
assert (this.context.host_ip != null);
assert (this.context.port > 0);
context.server.request_started.connect (this.on_request_started);
this.root_container = new MediaContainer ();
}
private void on_request_started (Soup.Server server,
Soup.Message msg,
Soup.ClientContext client) {
msg.got_headers.connect (this.on_got_headers);
}
private void on_got_headers (Soup.Message msg) {
this.message_received (msg);
}
public Transcoder get_transcoder (string target) throws Error {
if (target == "MP3") {
return new Transcoder ("mp3");
}
throw new HTTPRequestError.NOT_FOUND (
"No transcoder available for target format '%s'",
target);
}
}
public class Rygel.HTTPClient : GLib.Object, StateMachine {
public const size_t LENGTH = 1024;
public uint8[] content;
public GUPnP.Context context;
public Soup.Message msg;
public Cancellable cancellable { get; set; }
public HTTPClient (GUPnP.Context context) {
this.context = context;
this.content = new uint8[1024];
}
public async void run () {
SourceFunc run_continue = run.callback;
this.msg.request_body.append (MemoryUse.COPY, content);
this.context.session.queue_message (this.msg, (session, msg) => {
run_continue ();
});
yield;
this.completed ();
}
}
public class Rygel.MediaContainer : Rygel.MediaObject {
public const string ITEM_ID = "TestItem";
public signal void container_updated (MediaContainer container);
public MediaFileItem item;
private bool vanish;
private bool error;
public File file;
private FileMonitor monitor;
public MediaContainer () {
this.file = File.new_for_uri (MediaFileItem.URI);
this.item = new MediaFileItem (ITEM_ID, this);
this.vanish = false;
this.error = false;
this.id = "TesContainer";
try {
this.monitor = this.file.monitor_file (FileMonitorFlags.NONE);
} catch (GLib.Error error) {
assert_not_reached ();
}
this.monitor.changed.connect (this.on_file_changed);
}
public async MediaObject? find_object (string item_id,
Cancellable? cancellable)
throws Error {
SourceFunc find_object_continue = find_object.callback;
Idle.add (() => {
find_object_continue ();
return false;
});
yield;
if (item_id == "ErrorItem" && this.error) {
var msg = _("Fake error caused by %s object.");
this.file.delete (null);
throw new ContentDirectoryError.INVALID_ARGS (msg, item_id);
} else if (item_id == "ErrorItem" && !this.error) {
this.error = true;
}
if (item_id == "VanishingItem" && this.vanish) {
this.file.delete (null);
return null;
} else if (item_id == "VanishingItem" && !this.vanish) {
this.vanish = true;
}
if (item_id != this.item.id) {
this.item = new MediaFileItem (item_id, this);
}
return this.item;
}
public void on_file_changed (FileMonitor monitor,
File file,
File? other_file,
FileMonitorEvent event_type) {
this.item.place_holder = false;
this.container_updated (this);
}
~MediaContainer() {
try {
this.file.delete (null);
} catch (GLib.Error error) {
assert_not_reached ();
}
}
}
public class Rygel.MediaFileItem : Rygel.MediaObject {
public const string URI = "file:///tmp/rygel-upload-test.wav";
public long size = 1024;
public long duration = 1024;
public ArrayList<string> uris = new ArrayList<string> ();
public Gee.ArrayList<string> get_uris () { return this.uris; }
public bool place_holder = true;
public File file;
public MediaFileItem.for_visual_item () {}
public MediaFileItem (string id, MediaContainer parent) {
this.id = id;
this.parent = parent;
this.file = parent.file;
}
public async File? get_writable (Cancellable? cancellable) throws Error {
SourceFunc get_writable_continue = get_writable.callback;
Idle.add (() => {
get_writable_continue ();
return false;
});
yield;
if (this.id == "NullItem") {
this.file.delete (null);
return null;
} else {
return this.file;
}
}
}
internal class Rygel.HTTPResponse : Rygel.StateMachine, GLib.Object {
public Cancellable cancellable { get; set; }
private Soup.Message msg;
private Soup.Server server;
public HTTPResponse (HTTPPost get_request) {
this.msg = get_request.msg;
this.server = get_request.server;
}
public async void run () {
SourceFunc run_continue = run.callback;
Idle.add (() => {
run_continue ();
return false;
});
yield;
this.msg.set_status (Soup.Status.OK);
this.msg.unpause ();
this.completed ();
}
}
public class Rygel.ObjectRemovalQueue: GLib.Object {
public static ObjectRemovalQueue get_default () {
return new ObjectRemovalQueue ();
}
public bool dequeue (MediaObject item) {
return true;
}
public async void remove_now (MediaObject item, Cancellable? cancellable) {
Idle.add (remove_now.callback);
yield;
}
}
public class Rygel.MediaResource : GLib.Object {
public string extension;
}
public class Rygel.MediaObject : GLib.Object {
public string id;
public unowned MediaContainer parent;
public string mime_type = "";
public MediaResource? get_resource_by_name (string name) {
return null;
}
}
public class Rygel.Thumbnail : GLib.Object {
public string file_extension;
}
public class Rygel.VisualItem : Rygel.MediaFileItem {
public ArrayList<Thumbnail> thumbnails = new ArrayList<Thumbnail> ();
public VisualItem () {
base.for_visual_item();
}
}
private class Rygel.Subtitle : GLib.Object {
public string caption_type;
}
private class Rygel.VideoItem : Rygel.VisualItem {
public ArrayList<Subtitle> subtitles = new ArrayList<Subtitle> ();
}
private class Rygel.MusicItem : MediaFileItem {
public Thumbnail album_art;
public MusicItem (string id, MediaContainer parent) {
base (id, parent);
}
}
public errordomain Rygel.ContentDirectoryError {
INVALID_ARGS = 402
}
public class Rygel.Transcoder : GLib.Object {
public string extension { get; protected set; }
public Transcoder (string extension) {
this.extension = extension;
}
}
|