1
# Copyright (c) 2001-2004 Twisted Matrix Laboratories.
2
# See LICENSE for details.
8
import Crypto.Cipher.AES
12
from twisted.trial import unittest
13
from twisted.persisted import sob
14
from twisted.python import components
16
class Dummy(components.Componentized):
27
class FakeModule(object):
30
class PersistTestCase(unittest.TestCase):
33
p = sob.Persistent(o, '')
34
for style in 'source pickle'.split():
36
p.save(filename='persisttest.'+style)
37
o1 = sob.load('persisttest.'+style, style)
38
self.failUnlessEqual(o, o1)
40
def testStylesBeingSet(self):
43
o.setComponent(sob.IPersistable, sob.Persistent(o, 'lala'))
44
for style in 'source pickle'.split():
45
sob.IPersistable(o).setStyle(style)
46
sob.IPersistable(o).save(filename='lala.'+style)
47
o1 = sob.load('lala.'+style, style)
48
self.failUnlessEqual(o.foo, o1.foo)
49
self.failUnlessEqual(sob.IPersistable(o1).style, style)
54
p = sob.Persistent(o, 'object')
55
for style in 'source pickle'.split():
58
o1 = sob.load('object.ta'+style[0], style)
59
self.failUnlessEqual(o, o1)
60
for tag in 'lala lolo'.split():
62
o1 = sob.load('object-'+tag+'.ta'+style[0], style)
63
self.failUnlessEqual(o, o1)
65
def testEncryptedStyles(self):
67
phrase='once I was the king of spain'
68
p = sob.Persistent(o, '')
69
for style in 'source pickle'.split():
71
p.save(filename='epersisttest.'+style, passphrase=phrase)
72
o1 = sob.load('epersisttest.'+style, style, phrase)
73
self.failUnlessEqual(o, o1)
75
testEncryptedStyles.skip = "PyCrypto required for encrypted config"
78
f = open("persisttest.python", 'w')
79
f.write('foo=[1,2,3] ')
81
o = sob.loadValueFromFile('persisttest.python', 'foo')
82
self.failUnlessEqual(o, [1,2,3])
84
def testEncryptedPython(self):
85
phrase='once I was the king of spain'
86
f = open("epersisttest.python", 'w')
88
sob._encrypt(phrase, 'foo=[1,2,3]'))
90
o = sob.loadValueFromFile('epersisttest.python', 'foo', phrase)
91
self.failUnlessEqual(o, [1,2,3])
93
testEncryptedPython.skip = "PyCrypto required for encrypted config"
95
def testTypeGuesser(self):
96
self.assertRaises(KeyError, sob.guessType, "file.blah")
97
self.assertEqual('python', sob.guessType("file.py"))
98
self.assertEqual('python', sob.guessType("file.tac"))
99
self.assertEqual('python', sob.guessType("file.etac"))
100
self.assertEqual('pickle', sob.guessType("file.tap"))
101
self.assertEqual('pickle', sob.guessType("file.etap"))
102
self.assertEqual('source', sob.guessType("file.tas"))
103
self.assertEqual('source', sob.guessType("file.etas"))
105
def testEverythingEphemeralGetattr(self):
107
Verify that _EverythingEphermal.__getattr__ works.
109
self.fakeMain.testMainModGetattr = 1
111
dirname = self.mktemp()
114
filename = os.path.join(dirname, 'persisttest.ee_getattr')
116
f = file(filename, 'w')
117
f.write('import __main__\n')
118
f.write('if __main__.testMainModGetattr != 1: raise AssertionError\n')
119
f.write('app = None\n')
122
sob.load(filename, 'source')
124
def testEverythingEphemeralSetattr(self):
126
Verify that _EverythingEphemeral.__setattr__ won't affect __main__.
128
self.fakeMain.testMainModSetattr = 1
130
dirname = self.mktemp()
133
filename = os.path.join(dirname, 'persisttest.ee_setattr')
134
f = file(filename, 'w')
135
f.write('import __main__\n')
136
f.write('__main__.testMainModSetattr = 2\n')
137
f.write('app = None\n')
140
sob.load(filename, 'source')
142
self.assertEqual(self.fakeMain.testMainModSetattr, 1)
144
def testEverythingEphemeralException(self):
146
Test that an exception during load() won't cause _EE to mask __main__
148
dirname = self.mktemp()
150
filename = os.path.join(dirname, 'persisttest.ee_exception')
152
f = file(filename, 'w')
153
f.write('raise ValueError\n')
156
self.assertRaises(ValueError, sob.load, filename, 'source')
157
self.assertEqual(type(sys.modules['__main__']), FakeModule)
161
Replace the __main__ module with a fake one, so that it can be mutated
164
self.realMain = sys.modules['__main__']
165
self.fakeMain = sys.modules['__main__'] = FakeModule()
169
Restore __main__ to its original value
171
sys.modules['__main__'] = self.realMain