-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathtest_buspirate_spi.py
More file actions
92 lines (78 loc) · 3.6 KB
/
test_buspirate_spi.py
File metadata and controls
92 lines (78 loc) · 3.6 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
# Created by Sean Nelson on 2018-08-19.
# Copyright 2018 Sean Nelson <audiohacked@gmail.com>
#
# This file is part of pyBusPirate.
#
# pyBusPirate 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 2 of the License, or
# (at your option) any later version.
#
# pyBusPirate 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 pyBusPirate. If not, see <https://www.gnu.org/licenses/>.
"""
Unit Tests for BusPirate SPI class
"""
import unittest
from unittest import mock
from buspirate import spi
# pylint: disable=C0111,E1101
class BusPirateSpiTest(unittest.TestCase):
""" Unit Test class """
@mock.patch('serial.Serial', autospec=True)
def setUp(self, mock_serial): # pylint: disable=W0613,W0221
""" Unit Test setup """
self.bus_pirate = spi.SPI("/dev/ttyUSB0")
def tearDown(self):
""" Unit Test cleanup """
pass
def test_enter(self):
self.bus_pirate.serial.read.return_value = "SPI1"
self.assertEqual(self.bus_pirate.enter, True)
self.bus_pirate.serial.write.assert_called_with(0x01)
def test_chip_select_high(self):
self.bus_pirate.serial.read.return_value = 0x01
self.assertEqual(self.bus_pirate.chip_select(spi.CsLevel.HIGH), True)
self.bus_pirate.serial.write.assert_called_with(0x02|0x01)
def test_chip_select_low(self):
self.bus_pirate.serial.read.return_value = 0x01
self.assertEqual(self.bus_pirate.chip_select(spi.CsLevel.LOW), True)
self.bus_pirate.serial.write.assert_called_with(0x02|0x00)
def test_cs(self):
self.bus_pirate.serial.read.return_value = 0x01
self.bus_pirate.cs = spi.CsLevel.LOW
self.assertEqual(self.bus_pirate.cs, spi.CsLevel.LOW)
self.bus_pirate.serial.write.assert_called_with(0x02|0x00)
def test_sniff(self):
self.bus_pirate.serial.read.return_value = 0x01
self.assertEqual(self.bus_pirate.sniff(spi.CsSniffTrigger.LOW), True)
self.bus_pirate.serial.write.assert_called_with(0x0C|0x02)
def test_spi_speed(self):
self.bus_pirate.serial.read.return_value = 0x01
self.bus_pirate.speed = spi.SpiSpeed.SPEED_8MHZ
self.assertEqual(self.bus_pirate.speed, 0b111)
self.bus_pirate.serial.write.assert_called_with(0x60|0x07)
def test_spi_config(self):
self.bus_pirate.serial.read.return_value = 0x01
self.bus_pirate.config = 0b0000
self.assertEqual(self.bus_pirate.config, 0b0000)
self.bus_pirate.serial.write.assert_called_with(0x80|0x00)
def test_write_then_read(self):
data_len = 128
data = [idx for idx in range(1, data_len+1)]
self.bus_pirate.serial.read.return_value = [0x01] + data
result = self.bus_pirate.write_then_read(data_len, data_len, data)
self.assertEqual(result, data)
self.bus_pirate.serial.write.assert_called_with([0x04, 128, 128, data])
def test_write_then_read_with_no_cs(self):
data_len = 128
data = [idx for idx in range(1, data_len+1)]
self.bus_pirate.serial.read.return_value = [0x01] + data
result = self.bus_pirate.write_then_read_with_no_cs(data_len, data_len, data)
self.assertEqual(result, data)
self.bus_pirate.serial.write.assert_called_with([0x05, 128, 128, data])