mutable.Publish: rearrange create() to use more of it in unit tests

This commit is contained in:
Brian Warner 2007-11-06 00:33:40 -07:00
parent 8cdda6e199
commit 85a56a3d74
2 changed files with 37 additions and 28 deletions

View File

@ -126,6 +126,8 @@ class MutableFileNode:
self._client = client self._client = client
self._pubkey = None # filled in upon first read self._pubkey = None # filled in upon first read
self._privkey = None # filled in if we're mutable self._privkey = None # filled in if we're mutable
self._required_shares = None # ditto
self._total_shares = None # ditto
self._sharemap = {} # known shares, shnum-to-[nodeids] self._sharemap = {} # known shares, shnum-to-[nodeids]
self._current_data = None # SDMF: we're allowed to cache the contents self._current_data = None # SDMF: we're allowed to cache the contents
@ -149,18 +151,27 @@ class MutableFileNode:
the initial contents. Returns a Deferred that fires (with the the initial contents. Returns a Deferred that fires (with the
MutableFileNode instance you should use) when it completes. MutableFileNode instance you should use) when it completes.
""" """
self._privkey = "very private" self._required_shares = 3
self._pubkey = "public" self._total_shares = 10
self._writekey = hashutil.ssk_writekey_hash(self._privkey) self._pubkey, self._privkey = self._generate_pubprivkeys()
privkey_s = self._privkey.serialize()
self._encprivkey = self._encrypt_privkey(self._writekey, privkey_s)
pubkey_s = self._pubkey.serialize() pubkey_s = self._pubkey.serialize()
privkey_s = self._privkey.serialize()
self._writekey = hashutil.ssk_writekey_hash(privkey_s)
self._encprivkey = self._encrypt_privkey(self._writekey, privkey_s)
self._fingerprint = hashutil.ssk_pubkey_fingerprint_hash(pubkey_s) self._fingerprint = hashutil.ssk_pubkey_fingerprint_hash(pubkey_s)
self._uri = WriteableSSKFileURI(self._writekey, self._fingerprint) self._uri = WriteableSSKFileURI(self._writekey, self._fingerprint)
self._current_seqnum = 0 self._readkey = self._uri.readkey
self._storage_index = self._uri.storage_index
self._current_seqnum = 0 # TODO: really we mean "doesn't matter
# since nobody knows about us yet"
self._current_roothash = "\x00"*32 self._current_roothash = "\x00"*32
return self._publish(initial_contents) return self._publish(initial_contents)
def _generate_pubprivkeys(self):
privkey = "very private"
pubkey = "public"
return pubkey, privkey
def _publish(self, initial_contents): def _publish(self, initial_contents):
# TODO: actually do all that stuff # TODO: actually do all that stuff
p = Publish(self) p = Publish(self)
@ -196,9 +207,9 @@ class MutableFileNode:
return self._pubkey return self._pubkey
def get_required_shares(self): def get_required_shares(self):
return 3 # TODO: where should this come from? return self._required_shares
def get_total_shares(self): def get_total_shares(self):
return 10 # TODO: same return self._total_shares
def get_uri(self): def get_uri(self):

View File

@ -47,21 +47,13 @@ class FakeFilenode(mutable.MutableFileNode):
counter = itertools.count(1) counter = itertools.count(1)
all_contents = {} all_contents = {}
def create(self, initial_contents): def _generate_pubprivkeys(self):
count = self.counter.next() count = self.counter.next()
self.init_from_uri(uri.WriteableSSKFileURI("key%d" % count, return FakePubKey(count), FakePrivKey(count)
"fingerprint%d" % count)) def _publish(self, initial_contents):
self.all_contents[self._uri] = initial_contents self.all_contents[self._uri] = initial_contents
self._privkey = FakePrivKey()
self._pubkey = FakePubKey()
self._encprivkey = "encrypted private key"
privkey_s = self._privkey.serialize()
#self._encprivkey = self._encrypt_privkey(self._writekey, privkey_s)
pubkey_s = self._pubkey.serialize()
self._fingerprint = hashutil.ssk_pubkey_fingerprint_hash(pubkey_s)
self._current_seqnum = 0
self._current_roothash = "\x00"*32
return defer.succeed(self) return defer.succeed(self)
def download_to_data(self): def download_to_data(self):
return defer.succeed(self.all_contents[self._uri]) return defer.succeed(self.all_contents[self._uri])
def replace(self, newdata): def replace(self, newdata):
@ -211,7 +203,7 @@ class Publish(unittest.TestCase):
len(CONTENTS), len(CONTENTS),
"IV"*8), "IV"*8),
3, # seqnum 3, # seqnum
FakePrivKey(), "encprivkey", FakePubKey(), FakePrivKey(0), "encprivkey", FakePubKey(0),
) )
def _done( (seqnum, root_hash, final_shares) ): def _done( (seqnum, root_hash, final_shares) ):
self.failUnlessEqual(seqnum, 3) self.failUnlessEqual(seqnum, 3)
@ -221,7 +213,7 @@ class Publish(unittest.TestCase):
self.failUnlessEqual(sorted(final_shares.keys()), range(10)) self.failUnlessEqual(sorted(final_shares.keys()), range(10))
for i,sh in final_shares.items(): for i,sh in final_shares.items():
self.failUnless(isinstance(sh, str)) self.failUnless(isinstance(sh, str))
self.failUnlessEqual(len(sh), 367) self.failUnlessEqual(len(sh), 369)
# feed the share through the unpacker as a sanity-check # feed the share through the unpacker as a sanity-check
pieces = mutable.unpack_share(sh) pieces = mutable.unpack_share(sh)
(u_seqnum, u_root_hash, k, N, segsize, datalen, (u_seqnum, u_root_hash, k, N, segsize, datalen,
@ -233,11 +225,11 @@ class Publish(unittest.TestCase):
self.failUnlessEqual(N, 10) self.failUnlessEqual(N, 10)
self.failUnlessEqual(segsize, 21) self.failUnlessEqual(segsize, 21)
self.failUnlessEqual(datalen, len(CONTENTS)) self.failUnlessEqual(datalen, len(CONTENTS))
self.failUnlessEqual(pubkey, FakePubKey().serialize()) self.failUnlessEqual(pubkey, FakePubKey(0).serialize())
sig_material = struct.pack(">BQ32s BBQQ", 0, seqnum, root_hash, sig_material = struct.pack(">BQ32s BBQQ", 0, seqnum, root_hash,
k, N, segsize, datalen) k, N, segsize, datalen)
self.failUnlessEqual(signature, self.failUnlessEqual(signature,
FakePrivKey().sign(sig_material)) FakePrivKey(0).sign(sig_material))
self.failUnless(isinstance(share_hash_chain, list)) self.failUnless(isinstance(share_hash_chain, list))
self.failUnlessEqual(len(share_hash_chain), 4) # ln2(10)++ self.failUnlessEqual(len(share_hash_chain), 4) # ln2(10)++
for i in share_hash_chain: for i in share_hash_chain:
@ -356,7 +348,7 @@ class Publish(unittest.TestCase):
len(CONTENTS), len(CONTENTS),
"IV"*8), "IV"*8),
3, # seqnum 3, # seqnum
FakePrivKey(), "encprivkey", FakePubKey(), FakePrivKey(0), "encprivkey", FakePubKey(0),
) )
return d, p return d, p
@ -384,7 +376,9 @@ class Publish(unittest.TestCase):
def test_publish(self): def test_publish(self):
c, fn, p = self.setup_for_publish(20) c, fn, p = self.setup_for_publish(20)
d = p.publish("new contents of the mutable filenode") # make sure the length of our contents string is not a multiple of k,
# to exercise the padding code.
d = p.publish("New contents of the mutable filenode.")
def _done(res): def _done(res):
# TODO: examine peers and check on their shares # TODO: examine peers and check on their shares
pass pass
@ -393,11 +387,15 @@ class Publish(unittest.TestCase):
class FakePubKey: class FakePubKey:
def __init__(self, count):
self.count = count
def serialize(self): def serialize(self):
return "PUBKEY" return "PUBKEY-%d" % self.count
class FakePrivKey: class FakePrivKey:
def __init__(self, count):
self.count = count
def serialize(self): def serialize(self):
return "PRIVKEY" return "PRIVKEY-%d" % self.count
def sign(self, data): def sign(self, data):
return "SIGN(%s)" % data return "SIGN(%s)" % data