Repository navigation
Expand file tree
/
Copy pathtest_client.py
More file actions
executable file
·553 lines (456 loc) · 16.8 KB
/
Copy pathtest_client.py
File metadata and controls
executable file
·553 lines (456 loc) · 16.8 KB
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
import ctypes
import struct
import unittest
import logging
import time
import mock
from subprocess import Popen
from os import path, kill
import snap7
from snap7.snap7exceptions import Snap7Exception
from snap7.snap7types import S7AreaDB, S7WLByte, S7DataItem
from snap7 import util
logging.basicConfig(level=logging.WARNING)
ip = '127.0.0.1'
tcpport = 1102
db_number = 1
rack = 1
slot = 1
class TestClient(unittest.TestCase):
@classmethod
def setUpClass(cls):
server_path = path.join(path.dirname(path.realpath(snap7.__file__)),
"bin/snap7-server.py")
cls.server_pid = Popen([server_path]).pid
time.sleep(2) # wait for server to start
@classmethod
def tearDownClass(cls):
kill(cls.server_pid, 1)
def setUp(self):
self.client = snap7.client.Client()
self.client.connect(ip, rack, slot, tcpport)
def tearDown(self):
self.client.disconnect()
self.client.destroy()
def test_db_read(self):
size = 40
start = 0
db = 1
data = bytearray(40)
self.client.db_write(db_number=db, start=start, data=data)
result = self.client.db_read(db_number=db, start=start, size=size)
self.assertEqual(data, result)
def test_db_write(self):
size = 40
data = bytearray(size)
self.client.db_write(db_number=1, start=0, data=data)
def test_db_get(self):
self.client.db_get(db_number=db_number)
def test_read_multi_vars(self):
db = 1
# build and write test values
test_value_1 = 129.5
test_bytes_1 = bytearray(struct.pack('>f', test_value_1))
self.client.db_write(db, 0, test_bytes_1)
test_value_2 = -129.5
test_bytes_2 = bytearray(struct.pack('>f', test_value_2))
self.client.db_write(db, 4, test_bytes_2)
test_value_3 = 123
test_bytes_3 = bytearray([0, 0])
util.set_int(test_bytes_3, 0, test_value_3)
self.client.db_write(db, 8, test_bytes_3)
test_values = [test_value_1, test_value_2, test_value_3]
# build up our requests
data_items = (S7DataItem * 3)()
data_items[0].Area = ctypes.c_int32(S7AreaDB)
data_items[0].WordLen = ctypes.c_int32(S7WLByte)
data_items[0].Result = ctypes.c_int32(0)
data_items[0].DBNumber = ctypes.c_int32(db)
data_items[0].Start = ctypes.c_int32(0)
data_items[0].Amount = ctypes.c_int32(4) # reading a REAL, 4 bytes
data_items[1].Area = ctypes.c_int32(S7AreaDB)
data_items[1].WordLen = ctypes.c_int32(S7WLByte)
data_items[1].Result = ctypes.c_int32(0)
data_items[1].DBNumber = ctypes.c_int32(db)
data_items[1].Start = ctypes.c_int32(4)
data_items[1].Amount = ctypes.c_int32(4) # reading a REAL, 4 bytes
data_items[2].Area = ctypes.c_int32(S7AreaDB)
data_items[2].WordLen = ctypes.c_int32(S7WLByte)
data_items[2].Result = ctypes.c_int32(0)
data_items[2].DBNumber = ctypes.c_int32(db)
data_items[2].Start = ctypes.c_int32(8)
data_items[2].Amount = ctypes.c_int32(2) # reading an INT, 2 bytes
# create buffers to receive the data
# use the Amount attribute on each item to size the buffer
for di in data_items:
# create the buffer
dataBuffer = ctypes.create_string_buffer(di.Amount)
# get a pointer to the buffer
pBuffer = ctypes.cast(ctypes.pointer(dataBuffer),
ctypes.POINTER(ctypes.c_uint8))
di.pData = pBuffer
result, data_items = self.client.read_multi_vars(data_items)
result_values = []
# function to cast bytes to match data_types[] above
byte_to_value = [util.get_real, util.get_real, util.get_int]
# unpack and test the result of each read
for i in range(0, len(data_items)):
btv = byte_to_value[i]
di = data_items[i]
value = btv(di.pData, 0)
result_values.append(value)
self.assertEqual(result_values[0], test_values[0])
self.assertEqual(result_values[1], test_values[1])
self.assertEqual(result_values[2], test_values[2])
def test_upload(self):
"""
this raises an exception due to missing authorization? maybe not
implemented in server emulator
"""
self.assertRaises(Snap7Exception, self.client.upload, db_number)
@unittest.skip("TODO: invalid block size")
def test_download(self):
data = bytearray(1024)
self.client.download(block_num=db_number, data=data)
def test_read_area(self):
area = snap7.snap7types.areas.DB
dbnumber = 1
amount = 1
start = 1
self.client.read_area(area, dbnumber, start, amount)
def test_write_area(self):
area = snap7.snap7types.areas.DB
dbnumber = 1
size = 1
start = 1
data = bytearray(size)
self.client.write_area(area, dbnumber, start, data)
def test_list_blocks(self):
blockList = self.client.list_blocks()
def test_list_blocks_of_type(self):
self.client.list_blocks_of_type('DB', 10)
self.assertRaises(Exception, self.client.list_blocks_of_type,
'NOblocktype', 10)
def test_get_block_info(self):
"""test Cli_GetAgBlockInfo"""
self.client.get_block_info('DB', 1)
self.assertRaises(Exception, self.client.get_block_info,
'NOblocktype', 10)
self.assertRaises(Exception, self.client.get_block_info, 'DB', 10)
def test_get_cpu_state(self):
"""this tests the get_cpu_state function"""
self.client.get_cpu_state()
def test_set_session_password(self):
password = 'abcdefgh'
self.client.set_session_password(password)
def test_clear_session_password(self):
self.client.clear_session_password()
def test_set_connection_params(self):
self.client.set_connection_params("10.0.0.2", 10, 10)
def test_set_connection_type(self):
self.client.set_connection_type(1)
self.client.set_connection_type(2)
self.client.set_connection_type(3)
self.client.set_connection_type(20)
def test_get_connected(self):
self.client.get_connected()
def test_ab_read(self):
start = 1
size = 1
data = bytearray(size)
self.client.ab_write(start=start, data=data)
self.client.ab_read(start=start, size=size)
@unittest.skip("TODO: crash client: FATAL: exception not rethrown")
def test_ab_write(self):
start = 1
size = 10
data = bytearray(size)
self.client.ab_write(start=start, data=data)
@unittest.skip("TODO: crash client: FATAL: exception not rethrown")
def test_as_ab_read(self):
start = 1
size = 1
self.client.as_ab_read(start=start, size=size)
@unittest.skip("TODO: not yet fully implemented")
def test_as_ab_write(self):
start = 1
size = 10
data = bytearray(size)
self.client.as_ab_write(start=start, data=data)
def test_compress(self):
time = 1000
self.client.compress(time)
def test_as_compress(self):
time = 1000
self.client.as_compress(time)
def test_set_param(self):
values = (
(snap7.snap7types.PingTimeout, 800),
(snap7.snap7types.SendTimeout, 15),
(snap7.snap7types.RecvTimeout, 3500),
(snap7.snap7types.SrcRef, 128),
(snap7.snap7types.DstRef, 128),
(snap7.snap7types.SrcTSap, 128),
(snap7.snap7types.PDURequest, 470),
)
for param, value in values:
self.client.set_param(param, value)
self.assertRaises(Exception, self.client.set_param,
snap7.snap7types.RemotePort, 1)
def test_get_param(self):
expected = (
(snap7.snap7types.RemotePort, tcpport),
(snap7.snap7types.PingTimeout, 750),
(snap7.snap7types.SendTimeout, 10),
(snap7.snap7types.RecvTimeout, 3000),
(snap7.snap7types.SrcRef, 256),
(snap7.snap7types.DstRef, 0),
(snap7.snap7types.SrcTSap, 256),
(snap7.snap7types.PDURequest, 480),
)
for param, value in expected:
self.assertEqual(self.client.get_param(param), value)
non_client = snap7.snap7types.LocalPort, snap7.snap7types.WorkInterval,\
snap7.snap7types.MaxClients, snap7.snap7types.BSendTimeout,\
snap7.snap7types.BRecvTimeout, snap7.snap7types.RecoveryTime,\
snap7.snap7types.KeepAliveTime
# invalid param for client
for param in non_client:
self.assertRaises(Exception, self.client.get_param, non_client)
@unittest.skip("TODO: not yet fully implemented")
def test_as_copy_ram_to_rom(self):
self.client.copy_ram_to_rom()
@unittest.skip("TODO: not yet fully implemented")
def test_as_ct_read(self):
self.client.as_ct_read()
@unittest.skip("TODO: not yet fully implemented")
def test_as_ct_write(self):
self.client.as_ct_write()
@unittest.skip("TODO: not yet fully implemented")
def test_as_db_fill(self):
self.client.as_db_fill()
def test_as_db_get(self):
self.client.db_get(db_number=db_number)
@unittest.skip("TODO: crash client: FATAL: exception not rethrown")
def test_as_db_read(self):
size = 40
start = 0
db = 1
data = bytearray(40)
self.client.db_write(db_number=db, start=start, data=data)
result = self.client.as_db_read(db_number=db, start=start, size=size)
self.assertEqual(data, result)
@unittest.skip("TODO: crash client: FATAL: exception not rethrown")
def test_as_db_write(self):
size = 40
data = bytearray(size)
self.client.as_db_write(db_number=1, start=0, data=data)
def test_as_download(self):
data = bytearray(128)
self.client.as_download(block_num=-1, data=data)
def test_plc_stop(self):
self.client.plc_stop()
def test_plc_hot_start(self):
self.client.plc_hot_start()
def test_plc_cold_start(self):
self.client.plc_cold_start()
def test_get_pdu_length(self):
pduRequested = self.client.get_param(10)
pduSize = self.client.get_pdu_length()
self.assertEqual(pduSize, pduRequested)
def test_get_cpu_info(self):
expected = (
('ModuleTypeName', 'CPU 315-2 PN/DP'),
('SerialNumber', 'S C-C2UR28922012'),
('ASName', 'SNAP7-SERVER'),
('Copyright', 'Original Siemens Equipment'),
('ModuleName', 'CPU 315-2 PN/DP')
)
cpuInfo = self.client.get_cpu_info()
for param, value in expected:
self.assertEqual(getattr(cpuInfo, param).decode('utf-8'), value)
def test_db_write_with_byte_literal_does_not_throw(self):
mock_write = mock.MagicMock()
mock_write.return_value = None
original = self.client.library.Cli_DBWrite
self.client.library.Cli_DBWrite = mock_write
data = b'\xDE\xAD\xBE\xEF'
try:
self.client.db_write(db_number=1, start=0, data=data)
except TypeError as e:
self.fail(str(e))
finally:
self.client.library.Cli_DBWrite = original
def test_download_with_byte_literal_does_not_throw(self):
mock_download = mock.MagicMock()
mock_download.return_value = None
original = self.client.library.Cli_Download
self.client.library.Cli_Download = mock_download
data = b'\xDE\xAD\xBE\xEF'
try:
self.client.download(block_num=db_number, data=data)
except TypeError as e:
self.fail(str(e))
finally:
self.client.library.Cli_Download = original
def test_write_area_with_byte_literal_does_not_throw(self):
mock_writearea = mock.MagicMock()
mock_writearea.return_value = None
original = self.client.library.Cli_WriteArea
self.client.library.Cli_WriteArea = mock_writearea
area = snap7.snap7types.areas.DB
dbnumber = 1
size = 4
start = 1
data = b'\xDE\xAD\xBE\xEF'
try:
self.client.write_area(area, dbnumber, start, data)
except TypeError as e:
self.fail(str(e))
finally:
self.client.library.Cli_WriteArea = original
def test_ab_write_with_byte_literal_does_not_throw(self):
mock_write = mock.MagicMock()
mock_write.return_value = None
original = self.client.library.Cli_ABWrite
self.client.library.Cli_ABWrite = mock_write
start = 1
data = b'\xDE\xAD\xBE\xEF'
try:
self.client.ab_write(start=start, data=data)
except TypeError as e:
self.fail(str(e))
finally:
self.client.library.Cli_ABWrite = original
def test_as_ab_write_with_byte_literal_does_not_throw(self):
mock_write = mock.MagicMock()
mock_write.return_value = None
original = self.client.library.Cli_AsABWrite
self.client.library.Cli_AsABWrite = mock_write
start = 1
data = b'\xDE\xAD\xBE\xEF'
try:
self.client.as_ab_write(start=start, data=data)
except TypeError as e:
self.fail(str(e))
finally:
self.client.library.Cli_AsABWrite = original
def test_as_db_write_with_byte_literal_does_not_throw(self):
mock_write = mock.MagicMock()
mock_write.return_value = None
original = self.client.library.Cli_AsDBWrite
self.client.library.Cli_AsDBWrite = mock_write
data = b'\xDE\xAD\xBE\xEF'
try:
self.client.db_write(db_number=1, start=0, data=data)
except TypeError as e:
self.fail(str(e))
finally:
self.client.library.Cli_AsDBWrite = original
def test_as_download_with_byte_literal_does_not_throw(self):
mock_download = mock.MagicMock()
mock_download.return_value = None
original = self.client.library.Cli_AsDownload
self.client.library.Cli_AsDownload = mock_download
data = b'\xDE\xAD\xBE\xEF'
try:
self.client.as_download(block_num=db_number, data=data)
except TypeError as e:
self.fail(str(e))
finally:
self.client.library.Cli_AsDownload = original
class TestClientBeforeConnect(unittest.TestCase):
"""
Test suite of items that should run without an open connection.
"""
def setUp(self):
self.client = snap7.client.Client()
def test_set_param(self):
values = (
(snap7.snap7types.RemotePort, 1102),
(snap7.snap7types.PingTimeout, 800),
(snap7.snap7types.SendTimeout, 15),
(snap7.snap7types.RecvTimeout, 3500),
(snap7.snap7types.SrcRef, 128),
(snap7.snap7types.DstRef, 128),
(snap7.snap7types.SrcTSap, 128),
(snap7.snap7types.PDURequest, 470),
)
for param, value in values:
self.client.set_param(param, value)
class TestLibraryIntegration(unittest.TestCase):
def setUp(self):
# replace the function load_library with a mock
self.loadlib_patch = mock.patch('snap7.client.load_library')
self.loadlib_func = self.loadlib_patch.start()
# have load_library return another mock
self.mocklib = mock.MagicMock()
self.loadlib_func.return_value = self.mocklib
# have the Cli_Create of the mock return None
self.mocklib.Cli_Create.return_value = None
def tearDown(self):
# restore load_library
self.loadlib_patch.stop()
def test_create(self):
client = snap7.client.Client()
self.mocklib.Cli_Create.assert_called_once()
def test_gc(self):
client = snap7.client.Client()
del client
self.mocklib.Cli_Destroy.assert_called_once()
if __name__ == '__main__':
unittest.main()
# TODO: implement
"""
Cli_AsEBRead
Cli_AsEBWrite
Cli_AsFullUpload
Cli_AsListBlocksOfType
Cli_AsMBRead
Cli_AsMBWrite
Cli_AsReadArea
Cli_AsReadSZL
Cli_AsReadSZLList
Cli_AsTMRead
Cli_AsTMWrite
Cli_AsUpload
Cli_AsWriteArea
Cli_CheckAsCompletion
Cli_Connect
Cli_CopyRamToRom
Cli_CTRead
Cli_CTWrite
Cli_DBFill
Cli_Delete
Cli_EBRead
Cli_EBWrite
Cli_ErrorText
Cli_FullUpload
Cli_GetAgBlockInfo
Cli_GetCpInfo
Cli_GetExecTime
Cli_GetLastError
Cli_GetOrderCode
Cli_GetParam
Cli_GetPduLength
Cli_GetPgBlockInfo
Cli_GetPlcDateTime
Cli_GetPlcStatus
Cli_GetProtection
Cli_IsoExchangeBuffer
Cli_MBRead
Cli_MBWrite
Cli_ReadArea
Cli_ReadMultiVars
Cli_ReadSZL
Cli_ReadSZLList
Cli_SetAsCallback
Cli_SetParam
Cli_SetPlcDateTime
Cli_SetPlcSystemDateTime
Cli_SetSessionPassword
Cli_TMRead
Cli_TMWrite
Cli_WaitAsCompletion
Cli_WriteMultiVars
"""