示例#1
0
 def test_classSecurity(self):
     """
     Test for class-level security of serialization.
     """
     taster = jelly.SecurityOptions()
     taster.allowInstancesOf(A, B)
     a = A()
     b = B()
     c = C()
     # add a little complexity to the data
     a.b = b
     a.c = c
     # and a backreference
     a.x = b
     b.c = c
     # first, a friendly insecure serialization
     friendly = jelly.jelly(a, taster)
     x = jelly.unjelly(friendly, taster)
     self.assertIsInstance(x.c, jelly.Unpersistable)
     # now, a malicious one
     mean = jelly.jelly(a)
     self.assertRaises(jelly.InsecureJelly, jelly.unjelly, mean, taster)
     self.assertIdentical(x.x, x.b, "Identity mismatch")
     # test class serialization
     friendly = jelly.jelly(A, taster)
     x = jelly.unjelly(friendly, taster)
     self.assertIdentical(x, A, "A came back: %s" % x)
示例#2
0
 def testClassSecurity(self):
     """
     test for class-level security of serialization
     """
     taster = jelly.SecurityOptions()
     taster.allowInstancesOf(A, B)
     a = A()
     b = B()
     c = C()
     # add a little complexity to the data
     a.b = b
     a.c = c
     # and a backreference
     a.x = b
     b.c = c
     # first, a friendly insecure serialization
     friendly = jelly.jelly(a, taster)
     x = jelly.unjelly(friendly, taster)
     assert isinstance(x.c, jelly.Unpersistable), "C came back: %s" % x.c.__class__
     # now, a malicious one
     mean = jelly.jelly(a)
     try:
         x = jelly.unjelly(mean, taster)
         assert 0, "x came back: %s" % x
     except jelly.InsecureJelly:
         # OK
         pass
     assert x.x is x.b, "Identity mismatch"
 def testLotsaTypes(self):
     """
     test for all types currently supported in jelly
     """
     a = A()
     jelly.unjelly(jelly.jelly(a))
     jelly.unjelly(jelly.jelly(a.amethod))
     items = [afunc, [1, 2, 3], not bool(1), bool(1), 'test', 20.3, (1,2,3), None, A, unittest, {'a':1}, A.amethod]
     for i in items:
         self.assertEquals(i, jelly.unjelly(jelly.jelly(i)))
示例#4
0
 def testSerialize(self):
     text = N_("Something is really wrong.")
     self.cmsg = messages.Error(T_(text))
     self.mmsg = jelly.unjelly(jelly.jelly(self.cmsg))
     t = self.mmsg.translatables[0]
     self.assertEquals(t.format, "Something is really wrong.")
     self.assertEquals(self.mmsg.level, messages.ERROR)
     self.amsg = jelly.unjelly(jelly.jelly(self.mmsg))
     t = self.amsg.translatables[0]
     self.assertEquals(t.format, "Something is really wrong.")
     self.assertEquals(self.amsg.level, messages.ERROR)
示例#5
0
 def test_oldSets(self):
     """
     Test jellying C{sets.Set}: it should serialize to the same thing as
     C{set} jelly, and be unjellied as C{set} if available.
     """
     inputList = [jelly._sets.Set([1, 2, 3])]
     inputJelly = jelly.jelly(inputList)
     self.assertEqual(inputJelly, jelly.jelly([set([1, 2, 3])]))
     output = jelly.unjelly(inputJelly)
     # Even if the class is different, it should coerce to the same list
     self.assertEqual(list(inputList[0]), list(output[0]))
     if set is jelly._sets.Set:
         self.assertIsInstance(output[0], jelly._sets.Set)
     else:
         self.assertIsInstance(output[0], set)
示例#6
0
    def save(self, statefile=None):
        """Save state as a pickled jelly to a file on disk."""
        if not statefile:
            if not self._statefile:
                raise MissingState("Could not find a state file to load.")
            statefile = self._statefile
        logging.debug("Saving state to: \t'%s'" % statefile)

        fh = None
        err = ''

        try:
            fh = open(statefile, 'w')
        except MissingState as error:
            err += error.message
        except (IOError, OSError) as error:
            err += "Error writing state file to '%s': %s" % (statefile, error)
        else:
            try:
                pickle.dump(jelly.jelly(self), fh)
            except AttributeError as error:
                logging.debug("Tried jellying an unjelliable object: %s"
                              % error.message)

        if fh is not None:
            fh.flush()
            fh.close()
示例#7
0
 def test_typeSecurity(self):
     """
     Test for type-level security of serialization.
     """
     taster = jelly.SecurityOptions()
     dct = jelly.jelly({})
     self.assertRaises(jelly.InsecureJelly, jelly.unjelly, dct, taster)
示例#8
0
 def test_twiceUnjelliedFailureCheck(self):
     """
     The object which results from jellying a L{CopyableFailure}, unjellying
     the result, creating a new L{CopyableFailure} from the result of that,
     jellying it, and finally unjellying the result of that has a check
     method which behaves the same way as the original L{CopyableFailure}'s
     check method.
     """
     original = pb.CopyableFailure(ZeroDivisionError())
     self.assertIdentical(original.check(ZeroDivisionError), ZeroDivisionError)
     self.assertIdentical(original.check(ArithmeticError), ArithmeticError)
     copiedOnce = jelly.unjelly(jelly.jelly(original, invoker=DummyInvoker()))
     derivative = pb.CopyableFailure(copiedOnce)
     copiedTwice = jelly.unjelly(jelly.jelly(derivative, invoker=DummyInvoker()))
     self.assertIdentical(copiedTwice.check(ZeroDivisionError), ZeroDivisionError)
     self.assertIdentical(copiedTwice.check(ArithmeticError), ArithmeticError)
示例#9
0
 def test_moreReferences(self):
     a = []
     t = (a,)
     a.append((t,))
     s = jelly.jelly(t)
     z = jelly.unjelly(s)
     self.assertIdentical(z[0][0][0], z)
示例#10
0
    def testInvalidate(self):
        mcomp = planet.ManagerComponentState()
        mflow = planet.ManagerFlowState()
        mstate = planet.ManagerPlanetState()

        mflow.append('components', mcomp)
        mstate.append('flows', mflow)

        astate = jelly.unjelly(jelly.jelly(mstate))
        self.failUnless(isinstance(astate, planet.AdminPlanetState))

        aflow, = astate.get('flows')
        acomp, = aflow.get('components')

        invalidates = []

        def invalidate(obj):
            invalidates.append(obj)

        astate.addListener(self, invalidate=invalidate)
        aflow.addListener(self, invalidate=invalidate)
        acomp.addListener(self, invalidate=invalidate)

        self.assertEquals(invalidates, [])
        astate.invalidate()
        self.assertEquals(invalidates, [acomp, aflow, astate])
示例#11
0
    def serialize(self, object, perspective=None, method=None, args=None, kw=None):
        """Jelly an object according to the remote security rules for this broker.
        """

        if isinstance(object, defer.Deferred):
            object.addCallbacks(
                self.serialize,
                lambda x: x,
                callbackKeywords={"perspective": perspective, "method": method, "args": args, "kw": kw},
            )
            return object

        # XXX This call is NOT REENTRANT and testing for reentrancy is just
        # crazy, so it likely won't be.  Don't ever write methods that call the
        # broker's serialize() method recursively (e.g. sending a method call
        # from within a getState (this causes concurrency problems anyway so
        # you really, really shouldn't do it))

        # self.jellier = _NetJellier(self)
        self.serializingPerspective = perspective
        self.jellyMethod = method
        self.jellyArgs = args
        self.jellyKw = kw
        try:
            return jelly(object, self.security, None, self)
        finally:
            self.serializingPerspective = None
            self.jellyMethod = None
            self.jellyArgs = None
            self.jellyKw = None
示例#12
0
    def testPersistentStorage(self):
        perst = [{}, 1]
        def persistentStore(obj, jel, perst = perst):
            perst[1] = perst[1] + 1
            perst[0][perst[1]] = obj
            return str(perst[1])

        def persistentLoad(pidstr, unj, perst = perst):
            pid = int(pidstr)
            return perst[0][pid]

        a = SimpleJellyTest(1, 2)
        b = SimpleJellyTest(3, 4)
        c = SimpleJellyTest(5, 6)

        a.b = b
        a.c = c
        c.b = b

        jel = jelly.jelly(a, persistentStore = persistentStore)
        x = jelly.unjelly(jel, persistentLoad = persistentLoad)

        assert x.b is x.c.b, "Identity failure."
        # assert len(perst) == 3, "persistentStore should only be called 3 times."
        assert perst[0], "persistentStore was not called."
        assert x.b is a.b, "Persistent storage identity failure."
示例#13
0
 def test_methodSelfIdentity(self):
     a = A()
     b = B()
     a.bmethod = b.bmethod
     b.a = a
     im_ = jelly.unjelly(jelly.jelly(b)).a.bmethod
     self.assertEqual(im_.im_class, im_.im_self.__class__)
示例#14
0
    def test_persistentStorage(self):
        perst = [{}, 1]

        def persistentStore(obj, jel, perst=perst):
            perst[1] = perst[1] + 1
            perst[0][perst[1]] = obj
            return str(perst[1])

        def persistentLoad(pidstr, unj, perst=perst):
            pid = int(pidstr)
            return perst[0][pid]

        a = SimpleJellyTest(1, 2)
        b = SimpleJellyTest(3, 4)
        c = SimpleJellyTest(5, 6)

        a.b = b
        a.c = c
        c.b = b

        jel = jelly.jelly(a, persistentStore=persistentStore)
        x = jelly.unjelly(jel, persistentLoad=persistentLoad)

        self.assertIdentical(x.b, x.c.b)
        self.failUnless(perst[0], "persistentStore was not called.")
        self.assertIdentical(x.b, a.b, "Persistent storage identity failure.")
示例#15
0
 def test_typeNewStyle(self):
     """
     Test that a new style class type can be jellied and unjellied
     to the original type.
     """
     t = [D]
     r = jelly.unjelly(jelly.jelly(t))
     self.assertEqual(t, r)
示例#16
0
 def test_typeBuiltin(self):
     """
     Test that a builtin type can be jellied and unjellied to the original
     type.
     """
     t = [str]
     r = jelly.unjelly(jelly.jelly(t))
     self.assertEqual(t, r)
示例#17
0
 def test_stressReferences(self):
     reref = []
     toplevelTuple = ({'list': reref}, reref)
     reref.append(toplevelTuple)
     s = jelly.jelly(toplevelTuple)
     z = jelly.unjelly(s)
     self.assertIs(z[0]['list'], z[1])
     self.assertIs(z[0]['list'][0], z)
 def test_typeOldStyle(self):
     """
     Test that an old style class type can be jellied and unjellied
     to the original type.
     """
     t = [C]
     r = jelly.unjelly(jelly.jelly(t))
     self.assertEquals(t, r)
示例#19
0
 def test_stressReferences(self):
     reref = []
     toplevelTuple = ({"list": reref}, reref)
     reref.append(toplevelTuple)
     s = jelly.jelly(toplevelTuple)
     z = jelly.unjelly(s)
     self.assertIdentical(z[0]["list"], z[1])
     self.assertIdentical(z[0]["list"][0], z)
示例#20
0
 def test_dateTime(self):
     dtn = datetime.datetime.now()
     dtd = datetime.datetime.now() - dtn
     input = [dtn, dtd]
     c = jelly.jelly(input)
     output = jelly.unjelly(c)
     self.assertEqual(input, output)
     self.assertNotIdentical(input, output)
示例#21
0
 def testSimple(self):
     """
     simplest test case
     """
     assert SimpleJellyTest('a', 'b').isTheSameAs(SimpleJellyTest('a', 'b'))
     a = SimpleJellyTest(1, 2)
     cereal = jelly.jelly(a)
     b = jelly.unjelly(cereal)
     assert a.isTheSameAs(b)
示例#22
0
 def test_simple(self):
     """
     Simplest test case.
     """
     self.failUnless(SimpleJellyTest("a", "b").isTheSameAs(SimpleJellyTest("a", "b")))
     a = SimpleJellyTest(1, 2)
     cereal = jelly.jelly(a)
     b = jelly.unjelly(cereal)
     self.failUnless(a.isTheSameAs(b))
示例#23
0
 def testStressReferences(self):
     reref = []
     toplevelTuple = ({'list': reref}, reref)
     reref.append(toplevelTuple)
     s = jelly.jelly(toplevelTuple)
     print s
     z = jelly.unjelly(s)
     assert z[0]['list'] is z[1]
     assert z[0]['list'][0] is z
 def testSimple(self):
     """
     simplest test case
     """
     self.failUnless(SimpleJellyTest('a', 'b').isTheSameAs(SimpleJellyTest('a', 'b')))
     a = SimpleJellyTest(1, 2)
     cereal = jelly.jelly(a)
     b = jelly.unjelly(cereal)
     self.failUnless(a.isTheSameAs(b))
示例#25
0
 def test_simple(self):
     """
     Simplest test case.
     """
     self.assertTrue(SimpleJellyTest('a', 'b').isTheSameAs(
                     SimpleJellyTest('a', 'b')))
     a = SimpleJellyTest(1, 2)
     cereal = jelly.jelly(a)
     b = jelly.unjelly(cereal)
     self.assertTrue(a.isTheSameAs(b))
 def _cbGotUpdate(self, newState):
     self.__dict__.update(newState)
     self.isActivated = 1
     # send out notifications
     for listener in self._activationListeners:
         listener(self)
     self._activationListeners = []
     self.activated()
     with open(self.getFileName(), "wb") as dataFile:
         dataFile.write(banana.encode(jelly.jelly(self)))
示例#27
0
 def test_newStyleClassesAttributes(self):
     n = TestNode()
     n1 = TestNode(n)
     n11 = TestNode(n1)
     n2 = TestNode(n)
     # Jelly it
     jel = jelly.jelly(n)
     m = jelly.unjelly(jel)
     # Check that it has been restored ok
     self._check_newstyle(n, m)
示例#28
0
    def test_serializable(self):
        """Confirm containers can travel the wire"""

        b = Container()
        b.name = 'Device'
        b.type = 'PseudoDevice'
        b.description = "A string describing this device"

        # Jelly is the low-level serializer of the twisted framework
        assert jelly(b)
示例#29
0
 def test_frozenset(self):
     """
     Jellying C{frozenset} instances and then unjellying the result
     should produce objects which represent the values of the original
     inputs.
     """
     inputList = [frozenset([1, 2, 3])]
     output = jelly.unjelly(jelly.jelly(inputList))
     self.assertEqual(inputList, output)
     self.assertNotIdentical(inputList, output)
示例#30
0
 def test_newStyle(self):
     n = D()
     n.x = 1
     n2 = D()
     n.n2 = n2
     n.n3 = n2
     c = jelly.jelly(n)
     m = jelly.unjelly(c)
     self.assertIsInstance(m, D)
     self.assertIdentical(m.n2, m.n3)
示例#31
0
 def test_circleWithInvoker(self):
     class DummyInvokerClass:
         pass
     dummyInvoker = DummyInvokerClass()
     dummyInvoker.serializingPerspective = None
     a0 = ClassA()
     jelly.setUnjellyableForClass(ClassA, ClassA)
     jelly.setUnjellyableForClass(ClassB, ClassB)
     j = jelly.jelly(a0, invoker=dummyInvoker)
     a1 = jelly.unjelly(j)
     self.failUnlessIdentical(a1.ref.ref, a1,
         "Identity not preserved in circular reference")
示例#32
0
    def testTranslateMessage(self):
        cmsg = messages.Error(T_(N_("Something is really wrong. ")))
        t = T_(N_("But does %s know what ?"), "Andy")
        cmsg.add(t)
        mmsg = jelly.unjelly(jelly.jelly(cmsg))

        translator = Translator()
        localedir = os.path.join(configure.localedatadir, 'locale')
        translator.addLocaleDir(configure.PACKAGE, localedir)

        text = translator.translate(mmsg, lang=["nl_NL"])
        self.assertEquals(text, "Er is iets echt mis. Maar weet Andy wat ?")
 def test_twiceUnjelliedFailureCheck(self):
     """
     The object which results from jellying a L{CopyableFailure}, unjellying
     the result, creating a new L{CopyableFailure} from the result of that,
     jellying it, and finally unjellying the result of that has a check
     method which behaves the same way as the original L{CopyableFailure}'s
     check method.
     """
     original = pb.CopyableFailure(ZeroDivisionError())
     self.assertIdentical(
         original.check(ZeroDivisionError), ZeroDivisionError)
     self.assertIdentical(original.check(ArithmeticError), ArithmeticError)
     copiedOnce = jelly.unjelly(
         jelly.jelly(original, invoker=DummyInvoker()))
     derivative = pb.CopyableFailure(copiedOnce)
     copiedTwice = jelly.unjelly(
         jelly.jelly(derivative, invoker=DummyInvoker()))
     self.assertIdentical(
         copiedTwice.check(ZeroDivisionError), ZeroDivisionError)
     self.assertIdentical(
         copiedTwice.check(ArithmeticError), ArithmeticError)
示例#34
0
    def testSerializeWithError(self):
        wresult = messages.Result()
        wresult.add(messages.Error(T_(N_("uh oh"))))

        mresult = jelly.unjelly(jelly.jelly(wresult))
        self.failUnless(mresult.failed)
        self.assertEquals(mresult.value, None)
        m = mresult.messages[0]
        self.assertEquals(m.level, messages.ERROR)
        text = self.translator.translate(m, lang=[
            "nl_NL",
        ])
        self.assertEquals(text, "o jeetje")
示例#35
0
 def test_unjellyable(self):
     """
     Test that if Unjellyable is used to deserialize a jellied object,
     state comes out right.
     """
     class JellyableTestClass(jelly.Jellyable):
         pass
     jelly.setUnjellyableForClass(JellyableTestClass, jelly.Unjellyable)
     input = JellyableTestClass()
     input.attribute = 'value'
     output = jelly.unjelly(jelly.jelly(input))
     self.assertEqual(output.attribute, 'value')
     self.assertIsInstance(output, jelly.Unjellyable)
示例#36
0
 def test_dateTime(self):
     """
     Jellying L{datetime.timedelta} instances and then unjellying the result
     should produce objects which represent the values of the original
     inputs.
     """
     dtn = datetime.datetime.now()
     dtd = datetime.datetime.now() - dtn
     inputList = [dtn, dtd]
     c = jelly.jelly(inputList)
     output = jelly.unjelly(c)
     self.assertEqual(inputList, output)
     self.assertIsNot(inputList, output)
    def testSerializeWithWarning(self):
        wresult = messages.Result()
        wresult.add(messages.Warning(T_(N_("Warning"))))
        wresult.succeed("I did it")

        mresult = jelly.unjelly(jelly.jelly(wresult))
        self.failIf(mresult.failed)
        self.assertEquals(mresult.value, "I did it")
        m = mresult.messages[0]
        self.assertEquals(m.level, messages.WARNING)
        text = self.translator.translate(
            m, lang=["nl_NL", ])
        self.assertEquals(text, "Waarschuwing")
示例#38
0
 def test_unjelliedFailureCheck(self):
     """
     An unjellied L{CopyableFailure} has a check method which behaves the
     same way as the original L{CopyableFailure}'s check method.
     """
     original = pb.CopyableFailure(ZeroDivisionError())
     self.assertIdentical(original.check(ZeroDivisionError),
                          ZeroDivisionError)
     self.assertIdentical(original.check(ArithmeticError), ArithmeticError)
     copied = jelly.unjelly(jelly.jelly(original, invoker=DummyInvoker()))
     self.assertIdentical(copied.check(ZeroDivisionError),
                          ZeroDivisionError)
     self.assertIdentical(copied.check(ArithmeticError), ArithmeticError)
示例#39
0
 def testIdentity(self):
     """
     test to make sure that objects retain identity properly
     """
     x = []
     y = (x)
     x.append(y)
     x.append(y)
     assert x[0] is x[1]
     assert x[0][0] is x
     s = jelly.jelly(x)
     z = jelly.unjelly(s)
     assert z[0] is z[1]
     assert z[0][0] is z
示例#40
0
 def test_decimal(self):
     """
     Jellying L{decimal.Decimal} instances and then unjellying the result
     should produce objects which represent the values of the original
     inputs.
     """
     inputList = [decimal.Decimal('9.95'),
                  decimal.Decimal(0),
                  decimal.Decimal(123456),
                  decimal.Decimal('-78.901')]
     c = jelly.jelly(inputList)
     output = jelly.unjelly(c)
     self.assertEqual(inputList, output)
     self.assertIsNot(inputList, output)
示例#41
0
 def test_identity(self):
     """
     Test to make sure that objects retain identity properly.
     """
     x = []
     y = (x)
     x.append(y)
     x.append(y)
     self.assertIs(x[0], x[1])
     self.assertIs(x[0][0], x)
     s = jelly.jelly(x)
     z = jelly.unjelly(s)
     self.assertIs(z[0], z[1])
     self.assertIs(z[0][0], z)
示例#42
0
 def test_printTracebackIncludesValue(self):
     """
     When L{CopiedFailure.printTraceback} is used to print a copied failure
     which was unjellied from a L{CopyableFailure} with C{unsafeTracebacks}
     set to C{False}, the string representation of the exception value is
     included in the output.
     """
     original = pb.CopyableFailure(Exception("some reason"))
     copied = jelly.unjelly(jelly.jelly(original, invoker=DummyInvoker()))
     output = StringIO()
     copied.printTraceback(output)
     self.assertEqual(
         "Traceback from remote host -- Traceback unavailable\n"
         "exceptions.Exception: some reason\n", output.getvalue())
 def test_lotsaTypes(self):
     """
     Test for all types currently supported in jelly
     """
     a = A()
     jelly.unjelly(jelly.jelly(a))
     jelly.unjelly(jelly.jelly(a.amethod))
     items = [
         afunc,
         [1, 2, 3],
         not bool(1),
         bool(1),
         "test",
         20.3,
         (1, 2, 3),
         None,
         A,
         unittest,
         {"a": 1},
         A.amethod,
     ]
     for i in items:
         self.assertEqual(i, jelly.unjelly(jelly.jelly(i)))
示例#44
0
def encodeObject(toEncode):
    """
    Take a object and turn it into an string
    """
    log.debug("encodeObject")

    encoder = Banana()
    encoder.connectionMade()
    encoder._selectDialect("none")
    strBuffer = io.StringIO()
    encoder.transport = strBuffer
    encoder.sendEncoded(jelly.jelly(toEncode))

    return strBuffer.getvalue()
示例#45
0
 def test_newStyle(self):
     """
     Test that a new style class can be jellied and unjellied with its
     objects and attribute values preserved.
     """
     n = D()
     n.x = 1
     n2 = D()
     n.n2 = n2
     n.n3 = n2
     c = jelly.jelly(n)
     m = jelly.unjelly(c)
     self.assertIsInstance(m, D)
     self.assertIs(m.n2, m.n3)
     self.assertEqual(m.x, 1)
示例#46
0
    def testUnjellyable(self):
        """
        Test that if Unjellyable is used to deserialize a jellied object,
        state comes out right.
        """
        class JellyableTestClass(jelly.Jellyable):
            pass

        jelly.setUnjellyableForClass(JellyableTestClass, jelly.Unjellyable)
        input = JellyableTestClass()
        input.attribute = 'value'
        output = jelly.unjelly(jelly.jelly(input))
        self.assertEquals(output.attribute, 'value')
        self.failUnless(
            isinstance(output, jelly.Unjellyable),
            "Got instance of %r, not Unjellyable" % (output.__class__, ))
示例#47
0
    def test_throwExceptionIntoGenerator(self):
        """
        L{pb.CopiedFailure.throwExceptionIntoGenerator} will throw a
        L{RemoteError} into the given paused generator at the point where it
        last yielded.
        """
        original = pb.CopyableFailure(AttributeError("foo"))
        copy = jelly.unjelly(jelly.jelly(original, invoker=DummyInvoker()))
        exception = []

        def generatorFunc():
            try:
                yield None
            except pb.RemoteError, exc:
                exception.append(exc)
            else:
示例#48
0
 def test_methodsNotSelfIdentity(self):
     """
     If a class change after an instance has been created, L{jelly.unjelly}
     shoud raise a C{TypeError} when trying to unjelly the instance.
     """
     a = A()
     b = B()
     c = C()
     a.bmethod = c.cmethod
     b.a = a
     savecmethod = C.cmethod
     del C.cmethod
     try:
         self.assertRaises(TypeError, jelly.unjelly, jelly.jelly(b))
     finally:
         C.cmethod = savecmethod
示例#49
0
 def testLotsaTypes(self):
     """
     test for all types currently supported in jelly
     """
     a = A()
     # instance
     jelly.unjelly(jelly.jelly(a))
     a = A()
     jelly.unjelly(jelly.jelly(a.amethod))  # method
     jelly.unjelly(jelly.jelly(afunc))  # function
     jelly.unjelly(jelly.jelly([1, 2, 3]))  # list
     jelly.unjelly(jelly.jelly('test'))  # string
     jelly.unjelly(jelly.jelly(50.3))  # float
     jelly.unjelly(jelly.jelly((1, 2, 3)))  # tuple
示例#50
0
    def test_referenceable(self):
        """
        A L{pb.Referenceable} instance jellies to a structure which unjellies to
        a L{pb.RemoteReference}.  The C{RemoteReference} has a I{luid} that
        matches up with the local object key in the L{pb.Broker} which sent the
        L{Referenceable}.
        """
        ref = pb.Referenceable()
        jellyBroker = pb.Broker()
        jellyBroker.makeConnection(StringTransport())
        j = jelly.jelly(ref, invoker=jellyBroker)

        unjellyBroker = pb.Broker()
        unjellyBroker.makeConnection(StringTransport())

        uj = jelly.unjelly(j, invoker=unjellyBroker)
        self.assertIn(uj.luid, jellyBroker.localObjects)
示例#51
0
 def test_setState(self):
     global TupleState
     class TupleState:
         def __init__(self, other):
             self.other = other
         def __getstate__(self):
             return (self.other,)
         def __setstate__(self, state):
             self.other = state[0]
         def __hash__(self):
             return hash(self.other)
     a = A()
     t1 = TupleState(a)
     t2 = TupleState(a)
     t3 = TupleState((t1, t2))
     d = {t1: t1, t2: t2, t3: t3, "t3": t3}
     t3prime = jelly.unjelly(jelly.jelly(d))["t3"]
     self.assertIs(t3prime.other[0].other, t3prime.other[1].other)
示例#52
0
    def _testSecurity(self, inputList, atom):
        """
        Helper test method to test security options for a type.

        @param inputList: a sample input for the type.
        @type inputList: L{list}

        @param atom: atom identifier for the type.
        @type atom: L{str}
        """
        c = jelly.jelly(inputList)
        taster = jelly.SecurityOptions()
        taster.allowBasicTypes()
        # By default, it should succeed
        jelly.unjelly(c, taster)
        taster.allowedTypes.pop(atom)
        # But it should raise an exception when disallowed
        self.assertRaises(jelly.InsecureJelly, jelly.unjelly, c, taster)
示例#53
0
文件: pb.py 项目: jobajuba/Tsunami
    def serialize(self,
                  object,
                  perspective=None,
                  method=None,
                  args=None,
                  kw=None):
        """Jelly an object according to the remote security rules for this broker.
        """

        if isinstance(object, defer.Deferred):
            object.addCallbacks(self.serialize,
                                lambda x: x,
                                callbackKeywords={
                                    'perspective': perspective,
                                    'method': method,
                                    'args': args,
                                    'kw': kw
                                })
            return object

        # XXX This call is NOT REENTRANT and testing for reentrancy is just
        # crazy, so it likely won't be.  Don't ever write methods that call the
        # broker's serialize() method recursively (e.g. sending a method call
        # from within a getState (this causes concurrency problems anyway so
        # you really, really shouldn't do it))

        # self.jellier = _NetJellier(self)
        self.serializingPerspective = perspective
        self.jellyMethod = method
        self.jellyArgs = args
        self.jellyKw = kw
        try:
            return jelly(object, self.security, None, self)
        finally:
            self.serializingPerspective = None
            self.jellyMethod = None
            self.jellyArgs = None
            self.jellyKw = None
示例#54
0
    def save(self, statefile=None):
        """Save state as a pickled jelly to a file on disk."""
        if not statefile:
            if not self._statefile:
                raise MissingState("Could not find a state file to load.")
            statefile = self._statefile
        logging.debug("Saving state to: \t'%s'" % statefile)

        fh = None
        try:
            fh = open(statefile, 'w')
        except (IOError, OSError) as error:  # pragma: no cover
            logging.warn("Error writing state file to '%s': %s" %
                         (statefile, error))
        else:
            try:
                pickle.dump(jelly.jelly(self), fh)
            except AttributeError as error:
                logging.debug("Tried jellying an unjelliable object: %s" %
                              error.message)

        if fh is not None:
            fh.flush()
            fh.close()
示例#55
0
from   twisted.spread.jelly import jelly, unjelly
import pyfix.messages.enum.ExecType as ExecType

pn = ExecType.PENDING_NEW
print jelly(pn)


 def test_str(self):
     x = "blah"
     y = jelly.unjelly(jelly.jelly(x))
     self.assertEqual(x, y)
     self.assertEqual(type(x), type(y))
示例#57
0
 def encode(self, data):
     security = SecurityOptions()
     security.allowBasicTypes()
     jellyMessage = jelly(data, taster=security)
     return bananaEncode(jellyMessage)
示例#58
0
 def test_newStyleClasses(self):
     j = jelly.jelly(D)
     uj = jelly.unjelly(D)
     self.assertIdentical(D, uj)
示例#59
0
 def test_unicode(self):
     x = unicode('blah')
     y = jelly.unjelly(jelly.jelly(x))
     self.assertEqual(x, y)
     self.assertEqual(type(x), type(y))
示例#60
0
 def test_simpleCircle(self):
     jelly.setUnjellyableForClass(ClassA, ClassA)
     jelly.setUnjellyableForClass(ClassB, ClassB)
     a = jelly.unjelly(jelly.jelly(ClassA()))
     self.assertIs(a.ref.ref, a,
                   "Identity not preserved in circular reference")