1
# Copyright (c) 2001-2009 Twisted Matrix Laboratories.
2
# See LICENSE for details.
5
Test cases for L{jelly} object serialization.
15
from twisted.spread import jelly, pb
16
from twisted.python.compat import set, frozenset
18
from twisted.trial import unittest
22
class TestNode(object, jelly.Jellyable):
24
An object to test jellyfying of new style class instances.
28
def __init__(self, parent=None):
30
self.id = parent.id + 1
31
parent.children.append(self)
46
Method tp be used in serialization tests.
53
A dummy function to test function serialization.
65
Method to be used in serialization tests.
77
Method to be used in serialization tests.
84
Dummy new-style class.
89
class SimpleJellyTest:
90
def __init__(self, x, y):
94
def isTheSameAs(self, other):
95
return self.__dict__ == other.__dict__
99
class JellyTestCase(unittest.TestCase):
101
Testcases for L{jelly} module serialization.
103
@cvar decimalData: serialized version of decimal data, to be used in tests.
104
@type decimalData: C{list}
107
def _testSecurity(self, inputList, atom):
109
Helper test method to test security options for a type.
111
@param inputList: a sample input for the type.
112
@param inputList: C{list}
114
@param atom: atom identifier for the type.
117
c = jelly.jelly(inputList)
118
taster = jelly.SecurityOptions()
119
taster.allowBasicTypes()
120
# By default, it should succeed
121
jelly.unjelly(c, taster)
122
taster.allowedTypes.pop(atom)
123
# But it should raise an exception when disallowed
124
self.assertRaises(jelly.InsecureJelly, jelly.unjelly, c, taster)
127
def test_methodSelfIdentity(self):
130
a.bmethod = b.bmethod
132
im_ = jelly.unjelly(jelly.jelly(b)).a.bmethod
133
self.assertEquals(im_.im_class, im_.im_self.__class__)
136
def test_methodsNotSelfIdentity(self):
138
If a class change after an instance has been created, L{jelly.unjelly}
139
shoud raise a C{TypeError} when trying to unjelly the instance.
144
a.bmethod = c.cmethod
146
savecmethod = C.cmethod
149
self.assertRaises(TypeError, jelly.unjelly, jelly.jelly(b))
151
C.cmethod = savecmethod
154
def test_newStyle(self):
162
self.assertIsInstance(m, D)
163
self.assertIdentical(m.n2, m.n3)
166
def test_typeOldStyle(self):
168
Test that an old style class type can be jellied and unjellied
169
to the original type.
172
r = jelly.unjelly(jelly.jelly(t))
173
self.assertEquals(t, r)
176
def test_typeNewStyle(self):
178
Test that a new style class type can be jellied and unjellied
179
to the original type.
182
r = jelly.unjelly(jelly.jelly(t))
183
self.assertEquals(t, r)
186
def test_typeBuiltin(self):
188
Test that a builtin type can be jellied and unjellied to the original
192
r = jelly.unjelly(jelly.jelly(t))
193
self.assertEquals(t, r)
196
def test_dateTime(self):
197
dtn = datetime.datetime.now()
198
dtd = datetime.datetime.now() - dtn
200
c = jelly.jelly(input)
201
output = jelly.unjelly(c)
202
self.assertEquals(input, output)
203
self.assertNotIdentical(input, output)
206
def test_decimal(self):
208
Jellying L{decimal.Decimal} instances and then unjellying the result
209
should produce objects which represent the values of the original
212
inputList = [decimal.Decimal('9.95'),
214
decimal.Decimal(123456),
215
decimal.Decimal('-78.901')]
216
c = jelly.jelly(inputList)
217
output = jelly.unjelly(c)
218
self.assertEquals(inputList, output)
219
self.assertNotIdentical(inputList, output)
222
decimalData = ['list', ['decimal', 995, -2], ['decimal', 0, 0],
223
['decimal', 123456, 0], ['decimal', -78901, -3]]
226
def test_decimalUnjelly(self):
228
Unjellying the s-expressions produced by jelly for L{decimal.Decimal}
229
instances should result in L{decimal.Decimal} instances with the values
230
represented by the s-expressions.
232
This test also verifies that C{self.decimalData} contains valid jellied
233
data. This is important since L{test_decimalMissing} re-uses
234
C{self.decimalData} and is expected to be unable to produce
235
L{decimal.Decimal} instances even though the s-expression correctly
236
represents a list of them.
238
expected = [decimal.Decimal('9.95'),
240
decimal.Decimal(123456),
241
decimal.Decimal('-78.901')]
242
output = jelly.unjelly(self.decimalData)
243
self.assertEquals(output, expected)
246
def test_decimalMissing(self):
248
If decimal is unavailable on the unjelly side, L{jelly.unjelly} should
249
gracefully return L{jelly.Unpersistable} objects.
251
self.patch(jelly, 'decimal', None)
252
output = jelly.unjelly(self.decimalData)
253
self.assertEquals(len(output), 4)
255
self.assertIsInstance(output[i], jelly.Unpersistable)
256
self.assertEquals(output[0].reason,
257
"Could not unpersist decimal: 9.95")
258
self.assertEquals(output[1].reason,
259
"Could not unpersist decimal: 0")
260
self.assertEquals(output[2].reason,
261
"Could not unpersist decimal: 123456")
262
self.assertEquals(output[3].reason,
263
"Could not unpersist decimal: -78.901")
266
def test_decimalSecurity(self):
268
By default, C{decimal} objects should be allowed by
269
L{jelly.SecurityOptions}. If not allowed, L{jelly.unjelly} should raise
270
L{jelly.InsecureJelly} when trying to unjelly it.
272
inputList = [decimal.Decimal('9.95')]
273
self._testSecurity(inputList, "decimal")
276
skipReason = "decimal not available"
277
test_decimal.skip = skipReason
278
test_decimalUnjelly.skip = skipReason
279
test_decimalSecurity.skip = skipReason
284
Jellying C{set} instances and then unjellying the result
285
should produce objects which represent the values of the original
288
inputList = [set([1, 2, 3])]
289
output = jelly.unjelly(jelly.jelly(inputList))
290
self.assertEquals(inputList, output)
291
self.assertNotIdentical(inputList, output)
294
def test_frozenset(self):
296
Jellying C{frozenset} instances and then unjellying the result
297
should produce objects which represent the values of the original
300
inputList = [frozenset([1, 2, 3])]
301
output = jelly.unjelly(jelly.jelly(inputList))
302
self.assertEquals(inputList, output)
303
self.assertNotIdentical(inputList, output)
306
def test_setSecurity(self):
308
By default, C{set} objects should be allowed by
309
L{jelly.SecurityOptions}. If not allowed, L{jelly.unjelly} should raise
310
L{jelly.InsecureJelly} when trying to unjelly it.
312
inputList = [set([1, 2, 3])]
313
self._testSecurity(inputList, "set")
316
def test_frozensetSecurity(self):
318
By default, C{frozenset} objects should be allowed by
319
L{jelly.SecurityOptions}. If not allowed, L{jelly.unjelly} should raise
320
L{jelly.InsecureJelly} when trying to unjelly it.
322
inputList = [frozenset([1, 2, 3])]
323
self._testSecurity(inputList, "frozenset")
326
def test_oldSets(self):
328
Test jellying C{sets.Set}: it should serialize to the same thing as
329
C{set} jelly, and be unjellied as C{set} if available.
331
inputList = [jelly._sets.Set([1, 2, 3])]
332
inputJelly = jelly.jelly(inputList)
333
self.assertEquals(inputJelly, jelly.jelly([set([1, 2, 3])]))
334
output = jelly.unjelly(inputJelly)
335
# Even if the class is different, it should coerce to the same list
336
self.assertEquals(list(inputList[0]), list(output[0]))
337
if set is jelly._sets.Set:
338
self.assertIsInstance(output[0], jelly._sets.Set)
340
self.assertIsInstance(output[0], set)
343
def test_oldImmutableSets(self):
345
Test jellying C{sets.ImmutableSet}: it should serialize to the same
346
thing as C{frozenset} jelly, and be unjellied as C{frozenset} if
349
inputList = [jelly._sets.ImmutableSet([1, 2, 3])]
350
inputJelly = jelly.jelly(inputList)
351
self.assertEquals(inputJelly, jelly.jelly([frozenset([1, 2, 3])]))
352
output = jelly.unjelly(inputJelly)
353
# Even if the class is different, it should coerce to the same list
354
self.assertEquals(list(inputList[0]), list(output[0]))
355
if frozenset is jelly._sets.ImmutableSet:
356
self.assertIsInstance(output[0], jelly._sets.ImmutableSet)
358
self.assertIsInstance(output[0], frozenset)
361
def test_simple(self):
365
self.failUnless(SimpleJellyTest('a', 'b').isTheSameAs(
366
SimpleJellyTest('a', 'b')))
367
a = SimpleJellyTest(1, 2)
368
cereal = jelly.jelly(a)
369
b = jelly.unjelly(cereal)
370
self.failUnless(a.isTheSameAs(b))
373
def test_identity(self):
375
Test to make sure that objects retain identity properly.
381
self.assertIdentical(x[0], x[1])
382
self.assertIdentical(x[0][0], x)
385
self.assertIdentical(z[0], z[1])
386
self.assertIdentical(z[0][0], z)
389
def test_unicode(self):
391
y = jelly.unjelly(jelly.jelly(x))
392
self.assertEquals(x, y)
393
self.assertEquals(type(x), type(y))
396
def test_stressReferences(self):
398
toplevelTuple = ({'list': reref}, reref)
399
reref.append(toplevelTuple)
400
s = jelly.jelly(toplevelTuple)
402
self.assertIdentical(z[0]['list'], z[1])
403
self.assertIdentical(z[0]['list'][0], z)
406
def test_moreReferences(self):
412
self.assertIdentical(z[0][0][0], z)
415
def test_typeSecurity(self):
417
Test for type-level security of serialization.
419
taster = jelly.SecurityOptions()
420
dct = jelly.jelly({})
421
self.assertRaises(jelly.InsecureJelly, jelly.unjelly, dct, taster)
424
def test_newStyleClasses(self):
426
uj = jelly.unjelly(D)
427
self.assertIdentical(D, uj)
430
def test_lotsaTypes(self):
432
Test for all types currently supported in jelly
435
jelly.unjelly(jelly.jelly(a))
436
jelly.unjelly(jelly.jelly(a.amethod))
437
items = [afunc, [1, 2, 3], not bool(1), bool(1), 'test', 20.3,
438
(1, 2, 3), None, A, unittest, {'a': 1}, A.amethod]
440
self.assertEquals(i, jelly.unjelly(jelly.jelly(i)))
443
def test_setState(self):
446
def __init__(self, other):
448
def __getstate__(self):
450
def __setstate__(self, state):
451
self.other = state[0]
453
return hash(self.other)
457
t3 = TupleState((t1, t2))
458
d = {t1: t1, t2: t2, t3: t3, "t3": t3}
459
t3prime = jelly.unjelly(jelly.jelly(d))["t3"]
460
self.assertIdentical(t3prime.other[0].other, t3prime.other[1].other)
463
def test_classSecurity(self):
465
Test for class-level security of serialization.
467
taster = jelly.SecurityOptions()
468
taster.allowInstancesOf(A, B)
472
# add a little complexity to the data
475
# and a backreference
478
# first, a friendly insecure serialization
479
friendly = jelly.jelly(a, taster)
480
x = jelly.unjelly(friendly, taster)
481
self.assertIsInstance(x.c, jelly.Unpersistable)
482
# now, a malicious one
483
mean = jelly.jelly(a)
484
self.assertRaises(jelly.InsecureJelly, jelly.unjelly, mean, taster)
485
self.assertIdentical(x.x, x.b, "Identity mismatch")
486
# test class serialization
487
friendly = jelly.jelly(A, taster)
488
x = jelly.unjelly(friendly, taster)
489
self.assertIdentical(x, A, "A came back: %s" % x)
492
def test_unjellyable(self):
494
Test that if Unjellyable is used to deserialize a jellied object,
495
state comes out right.
497
class JellyableTestClass(jelly.Jellyable):
499
jelly.setUnjellyableForClass(JellyableTestClass, jelly.Unjellyable)
500
input = JellyableTestClass()
501
input.attribute = 'value'
502
output = jelly.unjelly(jelly.jelly(input))
503
self.assertEquals(output.attribute, 'value')
504
self.assertIsInstance(output, jelly.Unjellyable)
507
def test_persistentStorage(self):
509
def persistentStore(obj, jel, perst = perst):
510
perst[1] = perst[1] + 1
511
perst[0][perst[1]] = obj
514
def persistentLoad(pidstr, unj, perst = perst):
518
a = SimpleJellyTest(1, 2)
519
b = SimpleJellyTest(3, 4)
520
c = SimpleJellyTest(5, 6)
526
jel = jelly.jelly(a, persistentStore = persistentStore)
527
x = jelly.unjelly(jel, persistentLoad = persistentLoad)
529
self.assertIdentical(x.b, x.c.b)
530
self.failUnless(perst[0], "persistentStore was not called.")
531
self.assertIdentical(x.b, a.b, "Persistent storage identity failure.")
534
def test_newStyleClassesAttributes(self):
541
m = jelly.unjelly(jel)
542
# Check that it has been restored ok
543
self._check_newstyle(n, m)
546
def _check_newstyle(self, a, b):
547
self.assertEqual(a.id, b.id)
548
self.assertEqual(a.classAttr, 4)
549
self.assertEqual(b.classAttr, 4)
550
self.assertEqual(len(a.children), len(b.children))
551
for x, y in zip(a.children, b.children):
552
self._check_newstyle(x, y)
556
class ClassA(pb.Copyable, pb.RemoteCopy):
558
self.ref = ClassB(self)
562
class ClassB(pb.Copyable, pb.RemoteCopy):
563
def __init__(self, ref):
568
class CircularReferenceTestCase(unittest.TestCase):
570
Tests for circular references handling in the jelly/unjelly process.
573
def test_simpleCircle(self):
574
jelly.setUnjellyableForClass(ClassA, ClassA)
575
jelly.setUnjellyableForClass(ClassB, ClassB)
576
a = jelly.unjelly(jelly.jelly(ClassA()))
577
self.assertIdentical(a.ref.ref, a,
578
"Identity not preserved in circular reference")
581
def test_circleWithInvoker(self):
582
class DummyInvokerClass:
584
dummyInvoker = DummyInvokerClass()
585
dummyInvoker.serializingPerspective = None
587
jelly.setUnjellyableForClass(ClassA, ClassA)
588
jelly.setUnjellyableForClass(ClassB, ClassB)
589
j = jelly.jelly(a0, invoker=dummyInvoker)
590
a1 = jelly.unjelly(j)
591
self.failUnlessIdentical(a1.ref.ref, a1,
592
"Identity not preserved in circular reference")
597
Check that a C{set} can contain a circular reference and be serialized
598
and unserialized without losing the reference.
601
a = SimpleJellyTest(s, None)
603
res = jelly.unjelly(jelly.jelly(a))
604
self.assertIsInstance(res.x, set)
605
self.assertEquals(list(res.x), [res])
608
def test_frozenset(self):
610
Check that a C{frozenset} can contain a circular reference and be
611
serializeserialized without losing the reference.
613
a = SimpleJellyTest(None, None)
616
res = jelly.unjelly(jelly.jelly(a))
617
self.assertIsInstance(res.x, frozenset)
618
self.assertEquals(list(res.x), [res])