6
6
INCLUDE
7
7
**************************************************************************************/
8
8
9
+ #include < vector>
9
10
#include < algorithm>
10
11
11
12
#include < catch.hpp>
13
+ #include < fakeit.hpp>
12
14
13
15
#include < OTATestData.h>
14
- #include < OTAStorage_Mock.h>
15
16
16
17
#include < OTALogic.h>
18
+ #include < OTAStorage.h>
19
+
20
+ /* *************************************************************************************
21
+ NAMESPACE
22
+ **************************************************************************************/
23
+
24
+ using namespace fakeit ;
17
25
18
26
/* *************************************************************************************
19
27
TEST HELPER
@@ -43,22 +51,39 @@ void simulateOTABinaryReception(OTALogic & ota_logic, OTAData const & ota_test_d
43
51
44
52
TEST_CASE (" Valid OTA data is received " , " [OTALogic]" )
45
53
{
46
- OTAStorage_Mock ota_storage;
47
- OTALogic ota_logic (ota_storage);
54
+ Mock<OTAStorage> ota_storage;
55
+ std::vector<uint8_t > ota_binary_data;
56
+
57
+ /* Configure mock object */
58
+ When (Method (ota_storage, init)).Return (true );
59
+ When (Method (ota_storage, open)).Return (true );
60
+ When (Method (ota_storage, write)).AlwaysDo (
61
+ [&ota_binary_data](uint8_t const * const buf, size_t const num_bytes) -> size_t
62
+ {
63
+ std::copy (buf, buf + num_bytes, std::back_inserter (ota_binary_data));
64
+ return num_bytes;
65
+ });
66
+ Fake (Method (ota_storage, close));
67
+ Fake (Method (ota_storage, remove));
68
+ Fake (Method (ota_storage, deinit));
69
+
70
+
71
+ /* Generate test data */
48
72
OTAData valid_ota_test_data;
49
-
50
- ota_storage._init_return_val = true ;
51
- ota_storage._open_return_val = true ;
52
-
53
73
generate_valid_ota_data (valid_ota_test_data);
54
74
75
+
76
+ /* Perform test */
77
+ OTALogic ota_logic (ota_storage.get ());
55
78
simulateOTABinaryReception (ota_logic, valid_ota_test_data);
56
79
80
+
81
+ /* Perform checks */
57
82
THEN (" the complete binary file should have been written to the OTA storage" )
58
83
{
59
- REQUIRE (ota_storage. _binary .size () == valid_ota_test_data.data .len );
60
- REQUIRE (std::equal (ota_storage. _binary .begin (),
61
- ota_storage. _binary .end (),
84
+ REQUIRE (ota_binary_data .size () == valid_ota_test_data.data .len );
85
+ REQUIRE (std::equal (ota_binary_data .begin (),
86
+ ota_binary_data .end (),
62
87
valid_ota_test_data.data .bin ));
63
88
}
64
89
@@ -75,20 +100,32 @@ TEST_CASE("Valid OTA data is received ", "[OTALogic]")
75
100
76
101
TEST_CASE (" Invalid OTA data is received " , " [OTALogic - CRC wrong]" )
77
102
{
78
- OTAStorage_Mock ota_storage;
79
- OTALogic ota_logic (ota_storage);
80
- OTAData invalid_valid_ota_test_data_crc_wrong;
103
+ Mock<OTAStorage> ota_storage;
104
+
81
105
82
- ota_storage._init_return_val = true ;
83
- ota_storage._open_return_val = true ;
106
+ /* Configure mock object */
107
+ When (Method (ota_storage, init)).Return (true );
108
+ When (Method (ota_storage, open)).Return (true );
109
+ When (Method (ota_storage, write)).AlwaysDo ([](uint8_t const * const /* buf */ , size_t const num_bytes) -> size_t { return num_bytes; });
110
+ Fake (Method (ota_storage, close));
111
+ Fake (Method (ota_storage, remove));
112
+ Fake (Method (ota_storage, deinit));
84
113
114
+
115
+ /* Generate test data */
116
+ OTAData invalid_valid_ota_test_data_crc_wrong;
85
117
generate_invalid_ota_data_crc_wrong (invalid_valid_ota_test_data_crc_wrong);
86
118
119
+
120
+ /* Perform test */
121
+ OTALogic ota_logic (ota_storage.get ());
87
122
simulateOTABinaryReception (ota_logic, invalid_valid_ota_test_data_crc_wrong);
88
123
124
+
125
+ /* Perform checks */
89
126
THEN (" there should be no binary file be stored on the OTA storage" )
90
127
{
91
- REQUIRE ( ota_storage. _binary . size () == 0 );
128
+ Verify ( Method ( ota_storage, remove)). Once ( );
92
129
}
93
130
94
131
THEN (" The OTA logic should be in the 'Error' state" )
0 commit comments