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 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)
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)))
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)
def testTypeSecurity(self): """ test for type-level security of serialization """ taster = jelly.SecurityOptions() dct = jelly.jelly({}) try: jelly.unjelly(dct, taster) assert 0, "Insecure Jelly unjellied successfully." except jelly.InsecureJelly: # OK, works pass
def test_deprecatedUnjellyingInstanceAtom(self): """ Unjellying the instance atom is deprecated with 15.0.0. """ jelly.unjelly(["instance", ["class", "twisted.test.test_jelly.A"], ["dictionary"]]) warnings = self.flushWarnings() self.assertEqual(len(warnings), 1) self.assertEqual( warnings[0]["message"], "Unjelly support for the instance atom is deprecated since " "Twisted 15.0.0. Upgrade peer for modern instance support.", ) self.assertEqual(warnings[0]["category"], DeprecationWarning)
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."
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.")
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)
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)
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__)
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])
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)
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)
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)
def decodeObjectRaw(toDecode): """ Decodes the object the same as decodeObject but doesn't upgrade it. """ jellyData = decodeToList(toDecode) fixDataForMovedObjects(jellyData) decoded = jelly.unjelly(jellyData[0]) return decoded
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)
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)
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_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))
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)
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))
def _testSecurity(self, inputList, atom): """ Helper test method to test security options for a type. @param inputList: a sample input for the type. @param inputList: C{list} @param atom: atom identifier for the type. @type atom: C{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)
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 unserialize(self, sexp, perspective=None): """Unjelly an sexp according to the local security rules for this broker. """ self.unserializingPerspective = perspective try: return unjelly(sexp, self.security, None, self) finally: self.unserializingPerspective = None
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 testNewStyle(self): n = NewStyle() n.x = 1 n2 = NewStyle() n.n2 = n2 n.n3 = n2 c = jelly.jelly(n) m = jelly.unjelly(c) self.failUnless(isinstance(m, NewStyle)) self.assertIdentical(m.n2, m.n3)
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)
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)
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)
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)
def testUnicode(self): if hasattr(types, 'UnicodeType'): x = unicode('blah') y = jelly.unjelly(jelly.jelly(x)) self.assertEquals(x, y) self.assertEquals(type(x), type(y))
def setUp(self): self.mstate = planet.ManagerComponentState() self.astate = jelly.unjelly(jelly.jelly(self.mstate)) self.astate.addListener(self, set_=self.stateSet) self.changes = []
def setUp(self): self.mstate = planet.ManagerComponentState() self.astate = jelly.unjelly(jelly.jelly(self.mstate)) self.failUnless(isinstance(self.astate, planet.AdminComponentState))
def mset(self, key, value): # helper function to set on manager state and propagate self.mstate.set(key, value) self.astate = jelly.unjelly(jelly.jelly(self.mstate))
def _createComponent(self, dict): mstate = planet.ManagerComponentState() for key in dict.keys(): mstate.set(key, dict[key]) astate = jelly.unjelly(jelly.jelly(mstate)) return astate
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")
def decode(self, data): security = SecurityOptions() security.allowBasicTypes() bananaMessage = bananaDecode(data) return unjelly(bananaMessage, taster=security)
def test_newStyleClasses(self): j = jelly.jelly(D) uj = jelly.unjelly(D) self.assertIdentical(D, uj)
def test_unicode(self): x = unicode('blah') y = jelly.unjelly(jelly.jelly(x)) self.assertEqual(x, y) self.assertEqual(type(x), type(y))
def test_newStyleClasses(self): uj = jelly.unjelly(D) self.assertIs(D, uj)
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
def testJelly(self): a = enum.EnumClass('FooType', ('Foo', 'Bar')) self.assertEquals(jelly.unjelly(jelly.jelly(a.Foo)), a.Foo) self.assertEquals(jelly.unjelly(jelly.jelly(a.Bar)), a.Bar) self.assertNotEquals(jelly.unjelly(jelly.jelly(a.Foo)), a.Bar) self.assertNotEquals(jelly.unjelly(jelly.jelly(a.Bar)), a.Foo)
def test_str(self): x = "blah" y = jelly.unjelly(jelly.jelly(x)) self.assertEqual(x, y) self.assertEqual(type(x), type(y))
def setUp(self): self.wizard = ConfigurationAssistant() self.wizard.admin = TestAdmin('user', 'test') s = worker.ManagerWorkerHeavenState() s.set('names', ['localhost']) self.workerHeavenState = jelly.unjelly(jelly.jelly(s))