tests/test_lib_pdfencrypt.py
author robin <robin@reportlab.com>
Tue, 07 Mar 2017 10:00:34 +0000
changeset 4330 617ffa6bbdc8
parent 4252 fe660f227cac
permissions -rw-r--r--
changes for release 3.4.0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3861
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
     1
#!/bin/env python
4330
617ffa6bbdc8 changes for release 3.4.0
robin <robin@reportlab.com>
parents: 4252
diff changeset
     2
#Copyright ReportLab Europe Ltd. 2000-2017
3861
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
     3
#see license.txt for license details
4252
fe660f227cac changes for release 3.3.0
robin
parents: 3861
diff changeset
     4
__version__='3.3.0'
3861
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
     5
__doc__="""Testing to encrypt a very minimal pdf using a Canvas and a DocTemplate.
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
     6
TODO: Automatiocally test that this pdf is really encrypted.
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
     7
"""
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
     8
import unittest
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
     9
from reportlab.lib.testutils import setOutDir,makeSuiteForClasses, outputfile, printLocation
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    10
setOutDir(__name__)
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    11
from reportlab.pdfgen.canvas import Canvas
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    12
from reportlab.lib import pdfencrypt
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    13
from reportlab.lib.styles import ParagraphStyle
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    14
from reportlab.platypus import SimpleDocTemplate, Paragraph
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    15
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    16
def parsedoc(fileName):
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    17
    """
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    18
    Using PDFParseContext object from Pagecatcher module to check for encryption.
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    19
    """
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    20
    try:
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    21
        from rlextra.pageCatcher.pageCatcher import PDFParseContext
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    22
    except ImportError:
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    23
        return
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    24
    pdfContent = open(fileName, 'rb').read()
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    25
    p = PDFParseContext(pdfContent, prefix="PageForms")
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    26
    p.parse()
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    27
    assert p.encrypt
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    28
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    29
def testStdEnc(expectedO,expectedU,expectedKey,
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    30
                strength=40, overrideID='xxxxxxxxxxxxxxxx',
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    31
                userPass='User',ownerPass='Owner',
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    32
                ):
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    33
    # do a 40 bit example known to work in Acrobat Reader 4.0
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    34
    enc = pdfencrypt.StandardEncryption(userPass,ownerPass, strength=strength)
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    35
    enc.setAllPermissions(0)
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    36
    enc.canPrint = 1
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    37
    enc.prepare(None,overrideID)
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    38
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    39
    pdfencrypt.equalityCheck(pdfencrypt.hexText(enc.O),expectedO, '%d bit O value'%strength)
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    40
    pdfencrypt.equalityCheck(pdfencrypt.hexText(enc.U),expectedU, '%d bit U value'%strength)
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    41
    pdfencrypt.equalityCheck(pdfencrypt.hexText(enc.key),expectedKey, '%d bit key value'%strength)
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    42
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    43
class EncryptTestCase(unittest.TestCase):
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    44
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    45
    def test_canvas(self):
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    46
        "Test generating an encrypted pdf by setting a user password on the Canvas."
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    47
        fname = outputfile('test_encrypt_canvas.pdf')
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    48
        c = Canvas(fname, encrypt='User')
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    49
        c.setAuthor('Anonymous')
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    50
        c.setFont('Helvetica-Bold', 36)
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    51
        c.drawString(100,700, 'Top secret')
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    52
        c.save()
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    53
        parsedoc(fname)
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    54
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    55
    def test_standardencryption(self):
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    56
        "Test generating an encrypted pdf by passing a StandardEncryption object to the Canvas."
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    57
        encrypt = pdfencrypt.StandardEncryption(userPassword='User', ownerPassword='Owner')
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    58
        encrypt.setAllPermissions(0)
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    59
        encrypt.canPrint = 1
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    60
        fname = outputfile('test_encrypt_canvas2.pdf')
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    61
        c = Canvas(fname, encrypt=encrypt)
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    62
        c.setAuthor('Anonymous')
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    63
        c.setFont('Helvetica-Bold', 36)
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    64
        c.drawString(100,700, 'Top secret')
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    65
        c.save()
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    66
        parsedoc(fname)
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    67
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    68
    def test_doctemplate(self):
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    69
        "Test generating an encrypted pdf by setting a user password on the DocTemplate."
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    70
        header = ParagraphStyle(name='Heading', fontSize=36)
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    71
        story = [Paragraph("Top secret", header)]
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    72
        fname = outputfile('test_encrypt_doctemplate.pdf')
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    73
        doc = SimpleDocTemplate(fname, encrypt='User')
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    74
        doc.build(story)
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    75
        parsedoc(fname)
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    76
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    77
    def test_standardencryption128(self):
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    78
        "Test generating an encrypted pdf by passing a StandardEncryption object to the Canvas."
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    79
        encrypt = pdfencrypt.StandardEncryption(userPassword='User', ownerPassword='Owner',strength=128)
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    80
        encrypt.setAllPermissions(0)
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    81
        encrypt.canPrint = 1
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    82
        fname = outputfile('test_encrypt_canvas2_128.pdf')
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    83
        c = Canvas(fname, encrypt=encrypt)
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    84
        c.setAuthor('Anonymous')
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    85
        c.setFont('Helvetica-Bold', 36)
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    86
        c.drawString(100,700, 'More Top secret uses 128 bit encryption!')
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    87
        c.save()
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    88
        parsedoc(fname)
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    89
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    90
    def test_calcs(self):
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    91
        testStdEnc(
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    92
                expectedO = '<FA7F558FACF8205D25A7F1ABFA02629F707AE7B0211A2BB26F5DF4C30F684301>',
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    93
                expectedU = '<4E6F35A0356D6A624A72387D71FFB92EA4457E435307A9DD0BA9DE0618F5BABD>',
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    94
                expectedKey = '<DC96439AD9>',
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    95
                strength=40, overrideID='xxxxxxxxxxxxxxxx',
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    96
                userPass='User',ownerPass='Owner',
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    97
                )
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    98
        testStdEnc(
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
    99
                expectedO = '<E60E73846B1C9EB09986B2C20DEAEF48BCC2210F75AE640655EDDFF8B67E7DD6>',
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
   100
                expectedU = '<D9E804E73FB37EDECF35176140304BD600000000000000000000000000000000>',
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
   101
                expectedKey = '<7C0C9D27068EB81B291698B2CC278A48>',
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
   102
                strength=128, overrideID='xxxxxxxxxxxxxxxx',
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
   103
                userPass='User',ownerPass='Owner',
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
   104
                )
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
   105
        testStdEnc(
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
   106
                expectedO = '<FA7F558FACF8205D25A7F1ABFA02629F707AE7B0211A2BB26F5DF4C30F684301>',
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
   107
                expectedU = '<CB749297C89784B4BC94228F38B5FD4318398CA68F327A615A6CD8A5FCDB8C52>',
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
   108
                expectedKey = '<1528D3A2F9>',
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
   109
                strength=40, overrideID=b'\xbf\x08\xdf\x96I\x959e\x8f\x03\xcb\xb4\x11 W\xa9',
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
   110
                userPass='User',ownerPass='Owner',
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
   111
                )
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
   112
        testStdEnc(
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
   113
                expectedO = '<E60E73846B1C9EB09986B2C20DEAEF48BCC2210F75AE640655EDDFF8B67E7DD6>',
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
   114
                expectedU = '<9A823CD6C39CEA508577319287621BF800000000000000000000000000000000>',
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
   115
                expectedKey = '<6F8DB99AA5D9693CA757C2F03DAB71FF>',
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
   116
                strength=128, overrideID=b'\xbf\x08\xdf\x96I\x959e\x8f\x03\xcb\xb4\x11 W\xa9',
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
   117
                userPass='User',ownerPass='Owner',
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
   118
                )
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
   119
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
   120
def makedoc(fileName, userPass="User", ownerPass="Owner"):
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
   121
    """
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
   122
    Creates a simple encrypted pdf.
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
   123
    """
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
   124
    encrypt = pdfencrypt.StandardEncryption(userPass, ownerPass)
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
   125
    encrypt.setAllPermissions(0)
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
   126
    encrypt.canPrint = 1
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
   127
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
   128
    c = Canvas(fileName)
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
   129
    c._doc.encrypt = encrypt
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
   130
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
   131
    c.drawString(100, 500, "hello world")
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
   132
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
   133
    c.save()
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
   134
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
   135
class ManualTestCase(unittest.TestCase):
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
   136
    "Runs manual encrypted file builders."
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
   137
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
   138
    def test(self):
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
   139
        filepath = outputfile('test_pdfencryption.pdf')
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
   140
        makedoc(filepath)
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
   141
        parsedoc(filepath)
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
   142
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
   143
def makeSuite():
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
   144
    return makeSuiteForClasses(EncryptTestCase,ManualTestCase)
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
   145
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
   146
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
   147
#noruntests
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
   148
if __name__ == "__main__":
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
   149
    unittest.TextTestRunner().run(makeSuite())
2c6bef69fa95 pdfencrypt.py: encryption works
robin
parents:
diff changeset
   150
    printLocation()