def test_feed_room(self): nicks = [self.accounts[0].get_address().encode("hex")] r = pss.Room(self.bzz, "abc", self.accounts[0]) r.start("foo") for i in range(1, len(self.accounts)): nicks.append(str(i)) p = Participant(nicks[i], pubkey) p.set_public_key(self.accounts[i].get_public_key()) r.add(nicks[i], p) # get the serialized representation of room serializedroom = r.serialize() # save the room savedhsh = r.save() # retrieve the pubkey from the saved room format # and create account with retrieved public key # \todo more intuitive feed injection on load unserializedroom = json.loads(serializedroom) acc = pss.Account() cleanpub = clean_pubkey(unserializedroom['pubkey']) acc.set_public_key(cleanpub.decode("hex")) return # create feed with account from newly (re)created account recreatedownerfeed = pss.Feed(self.bzz, acc, unserializedroom['name']) # instantiate room with feed recreated from saved state rr = pss.Room(self.bzz, recreatedownerfeed) rr.load(r.hsh, self.accounts[0]) # check that for all"cleanpub: " + in-feeds (read peer's updates) the feed user field is the address of the peer matchesleft = len(self.accounts) for f in rr.feedcollection_in.feeds.values(): matched = False for a in self.accounts: if f['obj'].account.get_address() == a.get_address(): matched = True matchesleft -= 1 if not matched: self.fail("found unknown address " + f['obj'].account.get_address().encode("hex")) if matchesleft != 0: self.fail("have " + str(matchesleft) + " unmatched addresses") # for the outfeed, check that we are owner self.assertTrue(rr.feed_out.account.is_owner())
def test_feed_room_name(self): # start room roomname = "foo" nick = "bar" r = pss.Room(self.bzz, roomname, self.accounts[0]) r.start(nick) # create participant object to ensure correct representation of nick p = Participant(nick, pubkey) p.set_public_key(self.accounts[0].get_public_key()) resulttopic = r.feedcollection.feeds[p.nick].obj.topic # the name will be xor'd to the leftmost bits in the topic byte array # it will still be intact as a substring self.assertEqual(resulttopic[0:len(roomname)], roomname) # the last 12 bytes should be identical to the root topic self.assertEqual(resulttopic[20:], new_topic_mask(feedRootTopic, "", "\x06")[20:])
def test_feed_room_name(self): roomname = "foo" nick = "bar" r = pss.Room(self.bzz, roomname, self.accounts[0]) r.start(nick) addrhx = self.accounts[0].address.encode("hex") pubkeyhx = "04"+self.accounts[0].publickeybytes.encode("hex") p = Participant(nick, pubkeyhx, addrhx, "04"+pubkey) resulttopic = r.feedcollection_in.feeds[p.nick]['obj'].topic self.assertEqual(resulttopic[0:len(roomname)], roomname) self.assertEqual(resulttopic[20:], feedRootTopic[20:])
def test_room(self): acc = pss.Account() acc.set_public_key(self.pubkey[0]) r = pss.Room(self.bzz, "root", acc) #r.start("bar", "foo") for i in range(len(self.pubkey)): r.participants[str(i)] = Participant(str(i), self.nodekey[i]) r.participants[str(i)].set_public_key(self.pubkey[i]) s = r.serialize() try: roomobj = json.loads(s) except ValueError as e: self.fail("json deserialize error: " + (str(e)))
def test_feed_room_send(self): msg = "heyho" roomname = hex(now_int()) nicks = ["0"] r = pss.Room(self.bzz, roomname, self.accounts[0]) r.start("0") for i in range(1, len(self.accounts)): nicks.append(str(i)) p = Participant(nicks[i], pubkey) p.set_public_key(self.accounts[i].get_public_key()) r.add(nicks[i], p) hsh = r.send(msg) body = self.bzz.get(hsh) self.assertEqual(body[37:69], r.hsh_room) roomparticipants = json.loads(self.bzz.get(r.hsh_room.encode("hex"))) crsr = 69 participantcount = len(roomparticipants['participants']) datathreshold = 69 + (participantcount * 3) for i in range(participantcount): lenbytes = body[crsr:crsr + 3] offset = struct.unpack("<I", lenbytes + "\x00")[0] self.assertEqual(offset, i * len(msg)) self.assertEqual( body[datathreshold + offset:datathreshold + offset + len(msg)], msg) body = self.bzz.get(hsh) ciphermsg = r.extract_message(body[37:], r.participants[nicks[i]]) self.assertEqual(ciphermsg, msg) crsr += 3 self.assertEqual(len(body), datathreshold + (participantcount * len(msg)))