class TestLuaHandler(StreamTest):
    def setUp(self):
        StreamTest.setUp(self)

        self.doc = handler_document(self.name)
        self.identity = Identity("*****@*****.**", ["rotate", 5], ["local", None, "kirk"])
        self.owner = Owner(self.identity, make_jack=False)
        self.owner.own_document(self.doc)
class TestQuorumSpace(unittest.TestCase):

    def setUp(self):
        self.doc = handler_document("tag_team")
        self.qs  = self.doc._qs
        self.mitzi = identity('mitzi')
        self.atlas = identity('atlas')
        self.ev1 = Event({"hello":"world"}, self.mitzi, self.doc.version)
        self.ev2 = Event({"turtle":"food"}, self.mitzi, self.doc.version)
        self.q1 = Quorum(self.ev1, self.qs)
        self.q2 = Quorum(self.ev2, self.qs)
        self.owner = Owner(self.mitzi, make_jack=False)
        self.owner.own_document(self.doc)
        self.owner.identities.update_ident(self.atlas)

    def test_initial_state(self):
        self.assertEqual(
            sorted(self.qs.participants, key = lambda x: x.key),
            [self.atlas, self.mitzi]
        )
        self.assertTrue(self.qs.is_free(self.mitzi))
        self.assertTrue(self.qs.is_free(self.atlas))
        self.assertTrue(self.q1.competing)

    def test_double_sign(self):
        # TODO: Break this apart when I better understand the side
        # effects that this test depends on.

        self.q1.sign(self.mitzi)
        self.assertFalse(self.qs.is_free(self.mitzi))
        self.assertTrue(self.qs.is_free(self.atlas))
        self.assertEqual(len(self.qs.by_hash), 2)

        self.assertIsInstance(self.qs.by_hash[self.ev1.hash()], Quorum)
        self.assertIsInstance(self.qs.by_hash[self.ev2.hash()], Quorum)

        self.assertEquals(
            self.qs.by_author,
            {self.mitzi.key: self.q1}
        )
        self.assertTrue(self.q1.competing)
        self.assertFalse(self.q1.done)

        # Not double signing, same person and ev
        self.q1.sign(self.mitzi)

        # Double signing, same person but different ev
        self.assertRaises(
            QSDoubleSigning,
            self.q2.sign,
            self.mitzi
        )

        self.q1.sign(self.atlas)
        self.assertTrue(self.qs.is_free(self.mitzi))
        self.assertTrue(self.qs.is_free(self.atlas))
        self.assertFalse(self.q1.competing)
        self.assertTrue(self.q1.done)
class TestQuorum(StreamTest):

    def setUp(self):
        StreamTest.setUp(self)

        self.doc = handler_document("echo_chamber")
        self.ev  = Event({'x':'y'}, identity("atlas"), self.doc.version)
        self.quorum = Quorum(self.ev, self.doc._qs)
        self.quorum.document = self.doc
        self.ident = identity()
        self.owner = Owner(self.ident, make_jack=False)
        self.owner.own_document(self.doc)
        self.owner.identities.update_ident(self.ev.author)

    def test_clear(self):
        self.quorum.sign(self.ident)
        self.assertEqual(self.quorum.completion, 1)

        self.quorum.clear()
        self.assertEqual(self.quorum.completion, 0)

    def test_completion(self):
        self.assertEqual(self.quorum.completion, 0)
        self.quorum.sign(self.ident)
        self.assertEqual(self.quorum.completion, 1)

    def test_outdated(self):
        self.assertEqual(self.doc.version, 'current')
        self.assertEqual(self.doc._qs.version, 'current')
        self.assertEqual(self.ev.version, 'current')
        self.assertEqual(self.quorum.version, 'current')
        self.assertFalse(self.quorum.outdated)

        self.quorum.sign(self.ident)
        self.assertFalse(self.quorum.outdated)

        self.owner.protocol.paxos.check_quorum(self.doc, self.ev)
        # Stupid Python 2 makes this the easiest way to put up with string crap
        output = self.getOutput()
        self.assertIn(output, [
            "Event '{'x': 'y'}' achieved.\n",
            "Event '{u'x': u'y'}' achieved.\n",
        ])

        self.assertEqual(self.doc.version, self.ev.hash())
        self.assertEqual(self.quorum.version, 'current')
        self.assertTrue(self.quorum.outdated)

    def test_participants(self):
        self.assertEqual(self.quorum.participants, [self.ident])

    def test_thresholds(self):
        self.assertEqual(self.quorum.thresholds, {'read':1, 'write':1})

    def test_threshold(self):
        self.assertEqual(self.quorum.threshold, 1)
    def test_init(self):
        # Do setup for testing a good owner.
        owner = Owner(identity(), make_jack = False)

        key = String('["local",null,"mitzi"]')
        self.assertIsInstance(owner.identities, IdentityCache)
        self.assertIn(key, owner.identities)
        self.assertIsInstance(owner.identities[key], Identity)

        doc = handler_document("echo_chamber")
        self.assertIsInstance(doc.handler, Resource)
        owner.own_document(doc)
class TestQuorum(StreamTest):

    def setUp(self):
        StreamTest.setUp(self)

        self.doc = handler_document("echo_chamber")
        self.ev  = Event({'x':'y'}, identity("atlas"), self.doc.version)
        self.quorum = Quorum(self.ev, self.doc._qs)
        self.quorum.document = self.doc
        self.ident = identity()
        self.owner = Owner(self.ident, make_jack=False)
        self.owner.own_document(self.doc)

    def test_clear(self):
        self.quorum.sign(self.ident)
        self.assertEqual(self.quorum.completion, 1)

        self.quorum.clear()
        self.assertEqual(self.quorum.completion, 0)

    def test_completion(self):
        self.assertEqual(self.quorum.completion, 0)
        self.quorum.sign(self.ident)
        self.assertEqual(self.quorum.completion, 1)

    def test_outdated(self):
        self.assertEqual(self.doc.version, 'current')
        self.assertEqual(self.doc._qs.version, 'current')
        self.assertEqual(self.ev.version, 'current')
        self.assertEqual(self.quorum.version, 'current')
        self.assertFalse(self.quorum.outdated)

        self.quorum.sign(self.ident)
        self.assertFalse(self.quorum.outdated)

        self.quorum.enact(self.doc)
        self.assertOutput(
            "Event '{'x': 'y'}' achieved.\n" +
            "No known address for String('[\"local\",null,\"atlas\"]'), skipping\n"
        )

        self.assertEqual(self.doc.version, self.ev.hash())
        self.assertEqual(self.quorum.version, 'current')
        self.assertTrue(self.quorum.outdated)

    def test_participants(self):
        self.assertEqual(self.quorum.participants, [self.ident])

    def test_thresholds(self):
        self.assertEqual(self.quorum.thresholds, {'read':1, 'write':1})

    def test_threshold(self):
        self.assertEqual(self.quorum.threshold, 1)
class TestEvent(unittest.TestCase):

    def setUp(self):
        self.doc = handler_document('echo_chamber')
        self.ident = identity()
        self.ev  = Event({'x':'y'}, self.ident, 'stormageddon')
        self.quorum = Quorum(self.ev, self.doc._qs)
        self.owner = Owner(self.ident, make_jack=False)
        self.owner.own_document(self.doc)

    def test_init(self):
        self.assertEqual(self.ev.version, 'stormageddon')
        self.quorum.sign(self.ident)
        self.assertEqual(
            self.quorum.participants,
            [self.ident]
        )
        self.assertTrue(self.quorum.sig_valid(self.ident.key))
        self.assertRaises(
            TypeError,
            self.quorum.sign,
            "some string"
        )
        self.assertFalse(self.quorum.sig_valid("some string"))

    def test_authorname(self):
        # Override ev for this test
        self.ev = Event(None, self.owner.identity)

        self.assertTrue(
            isinstance(self.ev.author, Identity)
        )
        self.assertEqual(
            self.ev.authorname,
            '*****@*****.**'
        )
        self.assertEqual(
            self.doc.identity.name,
            '*****@*****.**'
        )

    def test_hash(self):
        self.assertEqual(
            self.ev.hash(),
            String('92395fb63e777d342d75915ad25dde1172e20f7c')
        )

    def test_hashcontent(self):
        self.assertEqual(
            self.ev.hashcontent,
            [{'x': 'y'}, 'stormageddon', '*****@*****.**']
        )
 def setUp(self):
     self.doc = handler_document('echo_chamber')
     self.ident = identity()
     self.ev  = Event({'x':'y'}, self.ident, 'stormageddon')
     self.quorum = Quorum(self.ev, self.doc._qs)
     self.owner = Owner(self.ident, make_jack=False)
     self.owner.own_document(self.doc)
Exemple #8
0
    def setUp(self):
        StreamTest.setUp(self)

        self.router = Router()
        self.mitzi = Owner(identity("mitzi"), self.router)
        self.atlas = Owner(identity("atlas"), self.router)
        self.victor = Owner(identity("victor"), self.router)
        self.mitzi.identities.sync(self.atlas.identities, self.victor.identities)

        # Document that mitzi and atlas are part of, but victor is not.
        # Separate identical starting points for all of them.
        self.mdoc = handler_document("tag_team")
        self.adoc = handler_document("tag_team")
        self.vdoc = handler_document("tag_team")
        self.mitzi.own_document(self.mdoc)
        self.atlas.own_document(self.adoc)
        self.victor.own_document(self.vdoc)
    def setUp(self):
        StreamTest.setUp(self)

        self.doc = handler_document("echo_chamber")
        self.ev  = Event({'x':'y'}, identity("atlas"), self.doc.version)
        self.quorum = Quorum(self.ev, self.doc._qs)
        self.quorum.document = self.doc
        self.ident = identity()
        self.owner = Owner(self.ident, make_jack=False)
        self.owner.own_document(self.doc)
    def setUp(self):
        StreamTest.setUp(self)

        self.doc = handler_document(self.name)
        self.identity = Identity(
            "*****@*****.**",
            ['rotate',5],
            ['local', None, "kirk"]
        )
        self.owner = Owner(self.identity, make_jack = False)
        self.owner.own_document(self.doc)
 def setUp(self):
     self.doc = handler_document("tag_team")
     self.qs  = self.doc._qs
     self.mitzi = identity('mitzi')
     self.atlas = identity('atlas')
     self.ev1 = Event({"hello":"world"}, self.mitzi, self.doc.version)
     self.ev2 = Event({"turtle":"food"}, self.mitzi, self.doc.version)
     self.q1 = Quorum(self.ev1, self.qs)
     self.q2 = Quorum(self.ev2, self.qs)
     self.owner = Owner(self.mitzi, make_jack=False)
     self.owner.own_document(self.doc)
     self.owner.identities.update_ident(self.atlas)
Exemple #12
0
    def do_dinit(self, args):
        '''
        Initialize DEJE interactivity.

        This command must be used before any of the other d*
        commands. It reads from a few of the values in variable
        storage as initialization parameters:

        * idcache - EJTP identity cache
        * identity - location of EJTP identity in cache
        * docname - Name of the document for network sync
        * docserialized - serialized version of document

        The dinit command can be run more than once, but it's
        a bit of a reset, and may cause data loss in the
        stateful parts of the protocol. But it's also the only
        way to update the parameters used by the DEJE code -
        for example, any changes to the 'idcache' variable after
        initialization will have no effect.
        '''
        try:
            params = self.get_vars(
                'idcache',
                'identity',
                'docname',
                'docserialized'
            )
        except KeyError as e:
            self.fail('Need to set variable %r' % e.args[0])
        
        cache = IdentityCache()
        try:
            cache.deserialize(params['idcache'])
        except:
            self.fail('Could not deserialize data in idcache')

        try:
            ident = cache.find_by_location(params['identity'])
        except KeyError:
            loc_string = strict(params['identity']).export()
            self.fail('No identity in cache for ' + loc_string)

        owner = Owner(ident)
        owner.identities = cache
        owner.client.rcv_callback = self.on_ejtp

        self.write_json = owner.client.write_json
        owner.client.write_json = self.write_json_wrapped

        if type(params['docname']) != str:
            json_str = strict(params['docname']).export()
            self.fail('Not a valid docname: ' + json_str)

        doc = Document(params['docname'], owner=owner)

        try:
            doc.deserialize(params['docserialized'])
        except Exception as e:
            self.fail('Failed to deserialize data:\n%r' % e)

        doc.signals['enact-event'].connect(self.on_event)
        doc.debug = self.debug

        # Wait until everything that could fail has gone right
        self.interface.owner = owner
        self.interface.document = doc
        self.output('DEJE initialized')
class TestEvent(unittest.TestCase):

    def setUp(self):
        self.doc = handler_document('echo_chamber')
        self.ident = identity()
        self.ev  = Event({'x':'y'}, self.ident, 'stormageddon')
        self.quorum = Quorum(self.ev, self.doc._qs)
        self.owner = Owner(self.ident, make_jack=False)
        self.owner.own_document(self.doc)
        self.io = IOMock()

    def test_init(self):
        self.assertEqual(self.ev.version, 'stormageddon')
        self.quorum.sign(self.ident)
        self.assertEqual(
            self.quorum.participants,
            [self.ident]
        )
        self.assertTrue(self.quorum.sig_valid(self.ident.key))
        self.assertRaises(
            TypeError,
            self.quorum.sign,
            "some string"
        )
        self.assertFalse(self.quorum.sig_valid("some string"))

    def test_authorname(self):
        # Override ev for this test
        self.ev = Event(None, self.owner.identity)

        self.assertTrue(
            isinstance(self.ev.author, Identity)
        )
        self.assertEqual(
            self.ev.authorname,
            '*****@*****.**'
        )
        self.assertEqual(
            self.doc.identity.name,
            '*****@*****.**'
        )

    def test_hash(self):
        self.assertEqual(
            self.ev.hash(),
            String('ce9950bb5a8db63712ae1ecfe9269e22289673ec')
        )

    def test_serialize(self):
        self.assertEqual(
            self.ev.serialize(),
            {
                'type': 'event',
                'author': ['local', None, 'mitzi'],
                'content': {'x':'y'},
                'version': 'stormageddon',
            }
        )

    def test_enact(self):
        self.assertEqual(self.ev.is_done(self.doc), False)
        with self.io:
            self.ev.enact(self.quorum, self.doc)
        self.assertEqual(self.ev.is_done(self.doc), True)