-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_ecode_decoder.py
128 lines (111 loc) · 3.69 KB
/
test_ecode_decoder.py
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# -*- encoding: utf-8 -*-
import pytest
from base64 import urlsafe_b64encode
from ecode import (
EcodeAlign,
EcodeDecoder,
EcodeFlag,
EcodeFmt,
EcodeLocale,
EcodeSize,
)
def test_decode():
ecode = EcodeDecoder().decode('BA0hzxI0VniavN7wYWIKYw')
assert ecode.version == 1
assert ecode.locale == EcodeLocale.EN
assert ecode.flags == frozenset([EcodeFlag.SIZE_FIXED, EcodeFlag.STRETCH])
assert ecode.align == EcodeAlign.CENTER
assert ecode.size == EcodeSize.XHDPI
assert ecode.fmt == EcodeFmt.WEBP
assert ecode.font_id == 0b1100_1111
assert ecode.foreground_color == 0x12345678
assert ecode.background_color == 0x9abcdef0
assert ecode.text == 'ab\nc'
def test_decode_illegal_locale():
code_bytes = bytes([
0b0000_1111, # Version:4, Locale:4
0b0000_1101, # Flags:6, Align:2
0b0010_0001, # Size:4, Fmt:4
0b1100_1111, # FontId:8
0x12, # ForegroundColor_R:8
0x34, # ForegroundColor_G:8
0x56, # ForegroundColor_B:8
0x78, # ForegroundColor_A:8
0x9a, # BackgroundColor_R:8
0xbc, # BackgroundColor_G:8
0xde, # BackgroundColor_B:8
0xf0, # BackgroundColor_A:8
0x61, # Text:8,
0x62, # Text:8
0x0a, # Text:8
0x63, # Text:8
])
code = urlsafe_b64encode(code_bytes).decode('utf-8')
with pytest.raises(ValueError, match='EcodeLocale'):
EcodeDecoder().decode(code)
def test_decode_illegal_align():
code_bytes = bytes([
0b0000_0100, # Version:4, Locale:4
0b0000_1111, # Flags:6, Align:2
0b0010_0001, # Size:4, Fmt:4
0b1100_1111, # FontId:8
0x12, # ForegroundColor_R:8
0x34, # ForegroundColor_G:8
0x56, # ForegroundColor_B:8
0x78, # ForegroundColor_A:8
0x9a, # BackgroundColor_R:8
0xbc, # BackgroundColor_G:8
0xde, # BackgroundColor_B:8
0xf0, # BackgroundColor_A:8
0x61, # Text:8,
0x62, # Text:8
0x0a, # Text:8
0x63, # Text:8
])
code = urlsafe_b64encode(code_bytes).decode('utf-8')
with pytest.raises(ValueError, match='EcodeAlign'):
EcodeDecoder().decode(code)
def test_decode_illegal_size():
code_bytes = bytes([
0b0000_0100, # Version:4, Locale:4
0b0000_1101, # Flags:6, Align:2
0b1111_0001, # Size:4, Fmt:4
0b1100_1111, # FontId:8
0x12, # ForegroundColor_R:8
0x34, # ForegroundColor_G:8
0x56, # ForegroundColor_B:8
0x78, # ForegroundColor_A:8
0x9a, # BackgroundColor_R:8
0xbc, # BackgroundColor_G:8
0xde, # BackgroundColor_B:8
0xf0, # BackgroundColor_A:8
0x61, # Text:8,
0x62, # Text:8
0x0a, # Text:8
0x63, # Text:8
])
code = urlsafe_b64encode(code_bytes).decode('utf-8')
with pytest.raises(ValueError, match='EcodeSize'):
EcodeDecoder().decode(code)
def test_decode_illegal_fmt():
code_bytes = bytes([
0b0000_0100, # Version:4, Locale:4
0b0000_1101, # Flags:6, Align:2
0b0010_1111, # Size:4, Fmt:4
0b1100_1111, # FontId:8
0x12, # ForegroundColor_R:8
0x34, # ForegroundColor_G:8
0x56, # ForegroundColor_B:8
0x78, # ForegroundColor_A:8
0x9a, # BackgroundColor_R:8
0xbc, # BackgroundColor_G:8
0xde, # BackgroundColor_B:8
0xf0, # BackgroundColor_A:8
0x61, # Text:8,
0x62, # Text:8
0x0a, # Text:8
0x63, # Text:8
])
code = urlsafe_b64encode(code_bytes).decode('utf-8')
with pytest.raises(ValueError, match='EcodeFmt'):
EcodeDecoder().decode(code)