Skip to content

Commit 8cac1d8

Browse files
committed
CLI test
1 parent 4b7acba commit 8cac1d8

3 files changed

Lines changed: 96 additions & 43 deletions

File tree

ECO2/cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import click
22

3-
from eco2 import Eco2
3+
from ECO2.eco2 import Eco2
44

55

66
@click.group()

ECO2/eco2.py

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from itertools import cycle
22
from pathlib import Path
3-
from typing import Union
43

54

65
class Eco2:
@@ -15,7 +14,8 @@ class Eco2:
1514
(8, 'unknown'),
1615
)
1716
key = (172, 41, 85, 66)
18-
encoding = 'UTF-8'
17+
header_encoding = 'EUC-KR'
18+
value_encoding = 'UTF-8'
1919
value_ext = '.xml'
2020

2121
@classmethod
@@ -43,8 +43,9 @@ def _decode_header(cls, data: bytes):
4343
b = data
4444
for length, name in cls.header:
4545
value, b = cls._decode_chunk(b=b, length=length)
46+
4647
try:
47-
value = value.decode('euc-kr')
48+
value = value.decode(cls.header_encoding)
4849
except ValueError:
4950
pass
5051

@@ -53,42 +54,39 @@ def _decode_header(cls, data: bytes):
5354
return header
5455

5556
@classmethod
56-
def _decrypt_eco2_data(cls, data: bytes):
57-
decrypted = cls.decrypt_bytes(data)
58-
hl = cls.header_length()
57+
def _print_header_info(cls, header: bytes):
58+
header_dict = cls._decode_header(header)
5959

60-
header_bytes = decrypted[:hl]
61-
value_bytes = decrypted[hl:]
62-
value = value_bytes.decode()
60+
print('Header info:')
6361

64-
return header_bytes, value
62+
for key, value in header_dict.items():
63+
if key == 'unknown':
64+
continue
65+
66+
print(f' {key:10s}: {value}')
6567

6668
@classmethod
6769
def _write_value(cls, path: Path, value: str):
68-
path.write_text(value.replace('\r\n', '\n'), encoding=cls.encoding)
70+
path.write_text(value.replace('\r\n', '\n'),
71+
encoding=cls.value_encoding)
6972

7073
@classmethod
7174
def _read_value(cls, path: Path):
72-
return path.read_text(encoding=cls.encoding).replace('\n', '\r\n')
75+
return path.read_text(encoding=cls.value_encoding).replace('\n', '\r\n')
7376

7477
@classmethod
75-
def _print_header_info(cls, header: bytes):
76-
header_dict = cls._decode_header(header)
77-
78-
print('Header info:')
78+
def _decrypt_eco2_data(cls, data: bytes):
79+
decrypted = cls.decrypt_bytes(data)
80+
hl = cls.header_length()
7981

80-
for key, value in header_dict.items():
81-
if key == 'unknown':
82-
continue
82+
header_bytes = decrypted[:hl]
83+
value_bytes = decrypted[hl:]
84+
value = value_bytes.decode()
8385

84-
print(f' {key:10s}: {value}')
86+
return header_bytes, value
8587

8688
@classmethod
87-
def decrypt(cls,
88-
path: Union[str, Path],
89-
save_dir=None,
90-
header_name=None,
91-
value_name=None):
89+
def decrypt(cls, path, save_dir=None, header_name=None, value_name=None):
9290
path = Path(path)
9391
save_dir = path.parent if save_dir is None else Path(save_dir)
9492
if not save_dir.exists():
@@ -110,6 +108,13 @@ def decrypt(cls,
110108
value_path = save_dir.joinpath(value_name + cls.value_ext)
111109
cls._write_value(path=value_path, value=value)
112110

111+
@classmethod
112+
def _encrypt(cls, header: bytes, value_path: str, save_path: Path):
113+
value = cls._read_value(path=value_path)
114+
bvalue = value.encode(cls.value_encoding)
115+
encrypted = cls.encrypt_bytes(header + bvalue)
116+
save_path.write_bytes(encrypted)
117+
113118
@classmethod
114119
def encrypt(cls, header_path, value_path, save_path=None):
115120
if save_path is None:
@@ -119,22 +124,19 @@ def encrypt(cls, header_path, value_path, save_path=None):
119124
value_path = Path(value_path)
120125
save_path = Path(save_path)
121126

122-
bheader = header_path.read_bytes()
123-
cls._print_header_info(bheader)
124-
125-
value = cls._read_value(path=value_path)
126-
bvalue = value.encode()
127+
header = header_path.read_bytes()
128+
cls._print_header_info(header)
127129

128-
data = bheader + bvalue
129-
encrypted = cls.encrypt_bytes(data)
130-
131-
save_path.write_bytes(encrypted)
130+
cls._encrypt(header=header, value_path=value_path, save_path=save_path)
132131

133132
@classmethod
134133
def encrypt_dir(cls, header_path, value_path, save_dir=None):
135134
header_path = Path(header_path)
136135
value_path = Path(value_path)
137136

137+
header = header_path.read_bytes()
138+
cls._print_header_info(header)
139+
138140
save_dir = value_path if save_dir is None else Path(save_dir)
139141
if not save_dir.is_dir():
140142
save_dir = save_dir.parent
@@ -145,6 +147,6 @@ def encrypt_dir(cls, header_path, value_path, save_dir=None):
145147
vps = [value_path]
146148

147149
for vp in vps:
148-
cls.encrypt(header_path=header_path,
149-
value_path=vp,
150-
save_path=save_dir.joinpath(f'{vp.stem}.eco'))
150+
cls._encrypt(header=header,
151+
value_path=vp,
152+
save_path=save_dir.joinpath(f'{vp.stem}.eco'))

tests/test_eco2.py

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,22 @@
22

33
import pytest
44

5+
from ECO2.cli import cli
56
from ECO2.eco2 import Eco2
67

8+
data_dir = Path(__file__).parent.joinpath('data')
9+
eco2_path = data_dir.joinpath('test.eco')
10+
header_path = data_dir.joinpath('header')
11+
value_path = data_dir.joinpath('value.xml')
712

8-
def test_eco2():
9-
data_dir = Path(__file__).parent.joinpath('data')
1013

11-
eco2_path = data_dir.joinpath('test.eco')
12-
header_path = data_dir.joinpath('header')
13-
value_path = data_dir.joinpath('value.xml')
14+
def test_eco2():
1415
encrypted_path = data_dir.joinpath('encrypted.eco')
1516

17+
header_path.unlink(missing_ok=True)
18+
value_path.unlink(missing_ok=True)
19+
encrypted_path.unlink(missing_ok=True)
20+
1621
Eco2.decrypt(path=eco2_path,
1722
save_dir=data_dir,
1823
header_name=header_path.stem,
@@ -34,6 +39,52 @@ def test_eco2():
3439

3540
assert hash(eco2) == hash(encrypted)
3641

42+
header_path.unlink(missing_ok=True)
43+
value_path.unlink(missing_ok=True)
44+
encrypted_path.unlink(missing_ok=True)
45+
46+
47+
def test_cli():
48+
encrypted_path = value_path.with_suffix('.eco')
49+
50+
header_path.unlink(missing_ok=True)
51+
value_path.unlink(missing_ok=True)
52+
encrypted_path.unlink(missing_ok=True)
53+
54+
try:
55+
cli([
56+
'decrypt',
57+
eco2_path.as_posix(),
58+
'-s',
59+
eco2_path.parent.as_posix(),
60+
'-h',
61+
header_path.as_posix(),
62+
'-v',
63+
value_path.with_suffix('').as_posix(),
64+
])
65+
except SystemExit:
66+
pass
67+
68+
assert header_path.exists()
69+
assert value_path.exists()
70+
71+
try:
72+
cli([
73+
'encrypt',
74+
header_path.as_posix(),
75+
value_path.parent.as_posix(),
76+
'-s',
77+
header_path.parent.as_posix(),
78+
])
79+
except SystemExit:
80+
pass
81+
82+
assert encrypted_path.exists()
83+
84+
header_path.unlink(missing_ok=True)
85+
value_path.unlink(missing_ok=True)
86+
encrypted_path.unlink(missing_ok=True)
87+
3788

3889
if __name__ == '__main__':
3990
pytest.main(['-v'])

0 commit comments

Comments
 (0)