File size: 2,353 Bytes
3315103
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import print_function
from __future__ import unicode_literals
from hashlib import sha1
from io import BytesIO
import zlib

from hwp5.filestructure import Hwp5DistDoc
from hwp5.distdoc import decode_head_to_sha1
from hwp5.distdoc import decode_head_to_key
from hwp5.distdoc import decrypt_tail
from hwp5.recordstream import read_record
from hwp5.tagids import HWPTAG_PARA_HEADER
import hwp5.distdoc
import hwp5.compressed

from .test_filestructure import TestBase


class TestHwp5DistDocFunctions(TestBase):

    hwp5file_name = 'viewtext.hwp'
    password_sha1 = sha1(b'12345').hexdigest()

    @property
    def hwp5distdoc(self):
        return Hwp5DistDoc(self.olestg)

    @property
    def section(self):
        return self.hwp5distdoc['ViewText']['Section0']

    def test_distdoc_decode_head_to_sha1(self):
        password_sha1 = self.password_sha1
        password_sha1 = password_sha1.upper()
        password_sha1 = password_sha1.encode('utf-16le')
        expected = password_sha1
        section_head = self.section.head()
        decoded = decode_head_to_sha1(section_head)
        self.assertEqual(expected, decoded)

    def test_distdoc_decode_head_to_key(self):
        section = self.section
        expected = self.password_sha1.upper().encode('utf-16le')[:16]
        self.assertEqual(expected, decode_head_to_key(section.head()))
        self.assertEqual(expected, section.head_key())

    def test_distdoc_decrypt_tail(self):
        section = self.section

        key = section.head_key()
        tail = section.tail()
        decrypted = decrypt_tail(key, tail)
        decompressed = zlib.decompress(decrypted, -15)
        record = read_record(BytesIO(decompressed), 0)
        self.assertEqual(0, record['level'])
        self.assertEqual(HWPTAG_PARA_HEADER, record['tagid'])
        self.assertEqual(22, record['size'])

        self.assertEqual(390, len(decompressed))

    def test_distdoc_decode(self):
        section = self.section

        stream = hwp5.distdoc.decode(section.wrapped.open())
        stream = hwp5.compressed.decompress(stream)
        record = read_record(stream, 0)
        self.assertEqual(0, record['level'])
        self.assertEqual(HWPTAG_PARA_HEADER, record['tagid'])
        self.assertEqual(22, record['size'])