From bcb2b3e8d1acd8da3380e2840a8c7223c3a67c33 Mon Sep 17 00:00:00 2001 From: Cameron Dale Date: Mon, 7 Jan 2008 11:28:14 -0800 Subject: [PATCH] DHT normalizes key lengths by chopping or adding 0. --- apt_dht_Khashmir/DHT.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/apt_dht_Khashmir/DHT.py b/apt_dht_Khashmir/DHT.py index dd4d5c4..42e2e9a 100644 --- a/apt_dht_Khashmir/DHT.py +++ b/apt_dht_Khashmir/DHT.py @@ -100,6 +100,15 @@ class DHT: self.joined = False self.khashmir.shutdown() + def normalizeKey(self, key): + """Normalize a key's length suitable for insertion in the DHT.""" + key_bytes = (self.config['HASH_LENGTH'] - 1) // 8 + 1 + if len(key) < key_bytes: + key = key + '\000'*(key_bytes - len(key)) + elif len(key) > key_bytes: + key = key[:key_bytes] + return key + def getValue(self, key): """See L{apt_dht.interfaces.IDHT}.""" if self.config is None: @@ -108,6 +117,7 @@ class DHT: raise DHTError, "have not joined a network yet" d = defer.Deferred() + key = self.normalizeKey(key) if key not in self.retrieving: self.khashmir.valueForKey(key, self._getValue) self.retrieving.setdefault(key, []).append(d) @@ -133,6 +143,7 @@ class DHT: if not self.joined: raise DHTError, "have not joined a network yet" + key = self.normalizeKey(key) if key in self.storing and value in self.storing[key]: raise DHTError, "already storing that key with the same value" @@ -175,6 +186,13 @@ class TestSimpleDHT(unittest.TestCase): self.b.bootstrap = ["127.0.0.1:4044"] self.b.cache_dir = '/tmp' + def test_normalizeKey(self): + self.failUnless(self.a.normalizeKey('12345678901234567890') == '12345678901234567890') + self.failUnless(self.a.normalizeKey('12345678901234567') == '12345678901234567\000\000\000') + self.failUnless(self.a.normalizeKey('1234567890123456789012345') == '12345678901234567890') + self.failUnless(self.a.normalizeKey('1234567890123456789') == '1234567890123456789\000') + self.failUnless(self.a.normalizeKey('123456789012345678901') == '12345678901234567890') + def test_bootstrap_join(self): d = self.a.join() return d -- 2.39.5