-
Notifications
You must be signed in to change notification settings - Fork 20
/
test_printer.py
187 lines (137 loc) · 5.64 KB
/
test_printer.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
'''
miniprint - a medium interaction printer honeypot
Copyright (C) 2019 Dan Salmon - [email protected]
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
'''
from printer import Printer
import logging
from glob import glob
import os
logger = logging.getLogger('miniprint')
logger.setLevel(logging.DEBUG)
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(levelname)5s - %(message)s')
ch.setFormatter(formatter)
logger.addHandler(ch)
def test_echo():
p = Printer(logger)
r = p.command_echo('ECHO DELIMITER20687')
assert r == '@PJL ECHO DELIMITER20687\x1b'
def test_fsdownload():
p = Printer(logger)
c = b'FSDOWNLOAD FORMAT:BINARY SIZE=52 NAME="0:/test2.txt"\r\nthis is a file with only one line and no line breaks\r\n'
r = p.command_fsdownload(c.decode('UTF-8'))
assert r == ''
r = p.command_fsquery('@PJL FSQUERY NAME="0:/test2.txt"')
assert r == '@PJL FSQUERY NAME="0:/test2.txt" TYPE=FILE SIZE=52'
c = b'FSDOWNLOAD FORMAT:BINARY SIZE=77 NAME="0:/test2.txt"\r\nthis is a file with 2 lines\nhere is the second line with no ending line break\r\n'
r = p.command_fsdownload(c.decode('UTF-8'))
assert r == ''
r = p.command_fsquery('@PJL FSQUERY NAME="0:/test2.txt"')
assert r == '@PJL FSQUERY NAME="0:/test2.txt" TYPE=FILE SIZE=77'
def test_fsquery():
p = Printer(logger)
r = p.command_fsquery('@PJL FSQUERY NAME="0:/webServer"')
assert r == '@PJL FSQUERY NAME="0:/webServer" TYPE=DIR'
def test_fsmkdir():
p = Printer(logger)
r = p.command_fsmkdir('@PJL FSMKDIR NAME="0:/testdir"')
assert r == ''
assert p.does_path_exist("/testdir") == True
def test_fsupload_bad():
p = Printer(logger)
r = p.command_fsupload('@PJL FSUPLOAD NAME="0:/none"')
assert r == '@PJL FSUPLOAD NAME="0:/none"\r\nFILEERROR=3\r\n'
def test_fsupload_good():
p = Printer(logger)
r = p.command_fsupload('@PJL FSUPLOAD NAME="0:/webServer/home/device.html"')
assert r.encode('UTF-8') == b'@PJL FSUPLOAD FORMAT:BINARY NAME="0:/webServer/home/device.html" OFFSET=0 SIZE=165\r\n<html><head>\n<meta http-equiv="Refresh" content="0; URL=this.LCDispatcher?dispatch=html&cat=1&pos=0">\n<title>Printer Content</title></head>\n<body>\n</body></html>'
def test_get_parameters():
p = Printer(logger)
params = p.get_parameters('@PJL RDYMSG DISPLAY = "rdymsg"')
assert params['DISPLAY'] == "rdymsg"
params = p.get_parameters('@PJL COMMAND A=1 B=2')
assert params['A'] == "1"
assert params['B'] == "2"
params = p.get_parameters('@PJL COMMAND A="value" B=2')
assert params['A'] == '"value"'
assert params['B'] == "2"
params = p.get_parameters('@PJL COMMAND A = 1 B = 2')
assert params['A'] == "1"
assert params['B'] == "2"
params = p.get_parameters('@PJL COMMAND A = 1 B = 2')
assert params['A'] == "1"
assert params['B'] == "2"
params = p.get_parameters('@PJL COMMAND A=45 B="0:/test.txt"\r\nheres a bunch of other data')
assert params['A'] == "45"
assert params['B'] == '"0:/test.txt"'
def test_info_id_default():
p = Printer(logger)
r = p.command_info_id("")
assert r == '@PJL INFO ID\r\nhp LaserJet 4200\r\n\x1b'
def test_info_id_custom():
printer_id = "my custom printer"
p2 = Printer(logger, printer_id=printer_id)
r2 = p2.command_info_id("")
assert r2 == '@PJL INFO ID\r\n' + printer_id + '\r\n\x1b'
def test_info_status_default():
p = Printer(logger)
r = p.command_info_status("")
assert r == '@PJL INFO STATUS\r\nCODE=10001\r\nDISPLAY="Ready"\r\nONLINE=True'
def test_info_status_custom():
p = Printer(logger, code=140, ready_msg="testing")
r = p.command_info_status("")
assert r == '@PJL INFO STATUS\r\nCODE=140\r\nDISPLAY="testing"\r\nONLINE=True'
def test_postscript_job():
p = Printer(logger)
t = '''%!
(Hello World) print
%%EOF'''
p.receiving_postscript = True
p.postscript_data += t
assert p.postscript_data == t
p.save_postscript()
assert p.postscript_data == ''
assert p.receiving_postscript == False
files_list = glob(os.path.join("uploads", '*.ps'))
a = sorted(files_list, reverse=True)[0]
assert os.path.isfile(a) == True
contents = ''
with open(a, 'r') as f:
contents = f.read()
assert contents == t
def test_raw_print_job():
p = Printer(logger)
t = "TEST - this is the first line of my raw print job"
p.append_raw_print_job(t)
assert p.current_raw_print_job == t
assert p.printing_raw_job == True
p.save_raw_print_job()
assert p.printing_raw_job == False
assert p.current_raw_print_job == ''
files_list = glob(os.path.join("uploads", '*.txt'))
a = sorted(files_list, reverse=True)[0]
assert os.path.isfile(a) == True
contents = ''
with open(a, 'r') as f:
contents = f.read()
assert contents == t
def test_rdymsg():
p = Printer(logger)
r = p.command_rdymsg('@PJL RDYMSG DISPLAY="hello"')
assert p.ready_msg == 'hello'
assert r == ''
def test_ustatusoff():
p = Printer(logger)
r = p.command_ustatusoff("")
assert r == ''