Skip to content
This repository has been archived by the owner on Dec 17, 2020. It is now read-only.

Commit

Permalink
Quick updates. Add test.
Browse files Browse the repository at this point in the history
  • Loading branch information
rcrafton committed Jun 26, 2008
1 parent 6cd9529 commit 4d3a903
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 1 deletion.
60 changes: 60 additions & 0 deletions src/z3c/encryptedpersistent/README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
Encrypted Persistence
---------------------

This package also provides integration with persistent objects. Ususally,
objects are stored in the ZODB in plain text. The ``EncryptedPersistent`` base
class ensures that all data of the class is encrypted before being stored.

>>> from z3c.encryptedpersistent import encryptedpersistent
>>> class MyObject(encryptedpersistent.EncryptedPersistent):
... name = None

>>> myObj = MyObject()
>>> myObj.name = u'Stephan Richter'


When an object is stored to a database, its ``__getstate__`` method is called:


>> myObj.__getstate__()
('key1',
"psHem+cmqG{(dp1\nS'name'\np2\nVStephan Richter\np3\nsS'__key__'\np4\nS'key1'\np5\ns.}")

When an object is loaded from the database, the state is passed into the
``__setstate__`` method:

>> state = myObj.__getstate__()

>>> myObj2 = MyObject()
>>> myObj2.__setstate__(state)
>>> myObj2.name
u'Stephan Richter'

Let's now test this with a full database. Since we want to test, whether the
data is stored encrypted, we have to create a file:

>>> import tempfile
>>> dbFile = tempfile.mktemp('.fs')

Let's now add one of the encrypted persistent objects to the database:

>>> from ZODB.DB import DB
>>> from ZODB.FileStorage import FileStorage
>>> db = DB(FileStorage(dbFile))
>>> conn = db.open()
>>> root = conn.root()

>>> root['obj'] = myObj

>>> import transaction
>>> transaction.commit()

When the database is loaded again, the object's data is still there, ...

>>> db.open().root()['obj'].name
u'Stephan Richter'

and the data is truly encrypted in the file:

>>> state[1] in open(dbFile).read()
True
4 changes: 3 additions & 1 deletion src/z3c/encryptedpersistent/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#
##############################################################################
"""
$Id:$
$Id$
"""
__docformat__ = "reStructuredText"

Expand All @@ -29,3 +29,5 @@ def encrypt(data):
def decrypt(data):
"""Returns the decrypted data"""

class IEncryptedPersistent(zope.interface.Interface):
""" """
21 changes: 21 additions & 0 deletions src/z3c/encryptedpersistent/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
##############################################################################
#
# Copyright (c) 2008 by JSA Technologies, Inc
#
##############################################################################
"""
$Id$
"""
import unittest
import doctest
from zope.testing.doctestunit import DocFileSuite
from zope.app.testing import placelesssetup, setup


def test_suite():
return unittest.TestSuite((
DocFileSuite(
'README.txt',
setUp=placelesssetup.setUp, tearDown=placelesssetup.tearDown,
optionflags=doctest.NORMALIZE_WHITESPACE|doctest.ELLIPSIS),
))

0 comments on commit 4d3a903

Please sign in to comment.