2009-07-05 01:17:38 +00:00
|
|
|
from twisted.trial import unittest
|
2009-07-21 02:45:16 +00:00
|
|
|
from twisted.python.failure import Failure
|
2009-07-05 01:17:38 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
from hashlib import sha1 as sha
|
|
|
|
except ImportError:
|
|
|
|
from sha import sha
|
|
|
|
|
|
|
|
import common
|
|
|
|
|
|
|
|
from deluge.core.rpcserver import RPCServer
|
|
|
|
from deluge.core.core import Core
|
|
|
|
import deluge.component as component
|
2009-07-24 23:36:43 +00:00
|
|
|
import deluge.error
|
2009-07-05 01:17:38 +00:00
|
|
|
|
|
|
|
class CoreTestCase(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
common.set_tmp_config_dir()
|
|
|
|
self.rpcserver = RPCServer(listen=False)
|
|
|
|
self.core = Core()
|
2010-03-21 02:11:32 +00:00
|
|
|
d = component.start()
|
|
|
|
return d
|
2009-07-05 01:17:38 +00:00
|
|
|
|
|
|
|
def tearDown(self):
|
2010-03-21 02:11:32 +00:00
|
|
|
|
|
|
|
def on_shutdown(result):
|
|
|
|
component._ComponentRegistry.components = {}
|
|
|
|
del self.rpcserver
|
|
|
|
del self.core
|
|
|
|
|
|
|
|
return component.shutdown().addCallback(on_shutdown)
|
2009-07-05 01:17:38 +00:00
|
|
|
|
|
|
|
def test_add_torrent_file(self):
|
|
|
|
options = {}
|
|
|
|
filename = "../test.torrent"
|
|
|
|
import base64
|
|
|
|
torrent_id = self.core.add_torrent_file(filename, base64.encodestring(open(filename).read()), options)
|
|
|
|
|
|
|
|
# Get the info hash from the test.torrent
|
|
|
|
from deluge.bencode import bdecode, bencode
|
|
|
|
info_hash = sha(bencode(bdecode(open(filename).read())["info"])).hexdigest()
|
|
|
|
|
|
|
|
self.assertEquals(torrent_id, info_hash)
|
|
|
|
|
|
|
|
def test_add_torrent_url(self):
|
2009-07-23 21:19:01 +00:00
|
|
|
url = "http://deluge-torrent.org/ubuntu-9.04-desktop-i386.iso.torrent"
|
2009-07-05 01:17:38 +00:00
|
|
|
options = {}
|
|
|
|
info_hash = "60d5d82328b4547511fdeac9bf4d0112daa0ce00"
|
|
|
|
|
|
|
|
d = self.core.add_torrent_url(url, options)
|
|
|
|
d.addCallback(self.assertEquals, info_hash)
|
|
|
|
return d
|
|
|
|
|
2009-07-21 02:45:16 +00:00
|
|
|
def test_add_torrent_url_with_cookie(self):
|
2009-08-04 01:01:26 +00:00
|
|
|
url = "http://deluge-torrent.org/test_torrent.php"
|
2009-07-21 02:45:16 +00:00
|
|
|
options = {}
|
|
|
|
headers = { "Cookie" : "password=deluge" }
|
|
|
|
info_hash = "60d5d82328b4547511fdeac9bf4d0112daa0ce00"
|
|
|
|
|
|
|
|
d = self.core.add_torrent_url(url, options)
|
2010-04-26 17:01:54 +00:00
|
|
|
d.addCallbacks(self.fail, self.assertIsInstance, errbackArgs=(Failure,))
|
2009-07-21 02:45:16 +00:00
|
|
|
|
|
|
|
d = self.core.add_torrent_url(url, options, headers)
|
|
|
|
d.addCallback(self.assertEquals, info_hash)
|
|
|
|
|
|
|
|
return d
|
|
|
|
|
2009-07-05 01:17:38 +00:00
|
|
|
def test_add_magnet(self):
|
|
|
|
info_hash = "60d5d82328b4547511fdeac9bf4d0112daa0ce00"
|
|
|
|
import deluge.common
|
|
|
|
uri = deluge.common.create_magnet_uri(info_hash)
|
|
|
|
options = {}
|
|
|
|
|
|
|
|
torrent_id = self.core.add_torrent_magnet(uri, options)
|
|
|
|
self.assertEquals(torrent_id, info_hash)
|
2009-07-23 04:14:08 +00:00
|
|
|
|
|
|
|
def test_remove_torrent(self):
|
|
|
|
options = {}
|
|
|
|
filename = "../test.torrent"
|
|
|
|
import base64
|
|
|
|
torrent_id = self.core.add_torrent_file(filename, base64.encodestring(open(filename).read()), options)
|
2009-08-04 01:01:26 +00:00
|
|
|
|
2009-07-23 21:19:01 +00:00
|
|
|
self.assertRaises(deluge.error.InvalidTorrentError, self.core.remove_torrent, "torrentidthatdoesntexist", True)
|
2009-08-04 01:01:26 +00:00
|
|
|
|
2009-07-23 21:19:01 +00:00
|
|
|
ret = self.core.remove_torrent(torrent_id, True)
|
2009-08-04 01:01:26 +00:00
|
|
|
|
2009-07-23 21:19:01 +00:00
|
|
|
self.assertTrue(ret)
|
2009-07-23 04:14:08 +00:00
|
|
|
self.assertEquals(len(self.core.get_session_state()), 0)
|
|
|
|
|
2009-07-23 21:19:01 +00:00
|
|
|
def test_get_session_status(self):
|
|
|
|
status = self.core.get_session_status(["upload_rate", "download_rate"])
|
|
|
|
self.assertEquals(type(status), dict)
|
|
|
|
self.assertEquals(status["upload_rate"], 0.0)
|
2009-08-04 01:01:26 +00:00
|
|
|
|
2009-07-23 21:19:01 +00:00
|
|
|
def test_get_cache_status(self):
|
|
|
|
status = self.core.get_cache_status()
|
|
|
|
self.assertEquals(type(status), dict)
|
|
|
|
self.assertEquals(status["write_hit_ratio"], 0.0)
|
|
|
|
self.assertEquals(status["read_hit_ratio"], 0.0)
|
2009-07-24 23:36:43 +00:00
|
|
|
|
|
|
|
def test_get_free_space(self):
|
|
|
|
space = self.core.get_free_space(".")
|
2009-07-25 01:49:05 +00:00
|
|
|
self.assertTrue(type(space) in (int, long))
|
2009-07-24 23:36:43 +00:00
|
|
|
self.assertTrue(space >= 0)
|
|
|
|
self.assertRaises(deluge.error.InvalidPathError, self.core.get_free_space, "/someinvalidpath")
|
2009-08-04 01:01:26 +00:00
|
|
|
|
2009-07-25 00:30:45 +00:00
|
|
|
def test_test_listen_port(self):
|
|
|
|
d = self.core.test_listen_port()
|
2009-08-04 01:01:26 +00:00
|
|
|
|
2009-07-25 00:30:45 +00:00
|
|
|
def result(r):
|
|
|
|
self.assertTrue(r in (True, False))
|
2009-08-04 01:01:26 +00:00
|
|
|
|
2009-07-25 00:30:45 +00:00
|
|
|
d.addCallback(result)
|
|
|
|
return d
|