Skip to content

Commit b2b14ca

Browse files
David SaadaYossi Levy
authored andcommitted
Fix TRNG test to use reset from python script and not from code
1 parent 9b0b631 commit b2b14ca

File tree

2 files changed

+87
-68
lines changed

2 files changed

+87
-68
lines changed

TESTS/host_tests/trng_reset.py

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,29 +24,29 @@
2424
import time
2525
from mbed_host_tests import BaseHostTest
2626
from mbed_host_tests.host_tests_runner.host_test_default import DefaultTestSelector
27+
from time import sleep
2728

2829
DEFAULT_CYCLE_PERIOD = 1.0
2930
MSG_VALUE_DUMMY = '0'
3031
MSG_TRNG_READY = 'ready'
3132
MSG_TRNG_BUFFER = 'buffer'
32-
MSG_TRNG_FINISH = 'finish'
3333
MSG_TRNG_TEST_STEP1 = 'check_step1'
3434
MSG_TRNG_TEST_STEP2 = 'check_step2'
3535
MSG_KEY_SYNC = '__sync'
36-
MSG_KEY_TEST_SUITE_ENDED = 'Test suite ended'
36+
MSG_KEY_RESET_COMPLETE = 'reset_complete'
37+
MSG_KEY_TEST_SUITE_ENDED = 'Test_suite_ended'
38+
MSG_KEY_EXIT = 'exit'
3739

3840
class TRNGResetTest(BaseHostTest):
3941
"""Test for the TRNG API.
4042
"""
4143

4244
def __init__(self):
4345
super(TRNGResetTest, self).__init__()
44-
self.reset = False
45-
self.finish = False
46+
self.did_reset = False
47+
self.ready = False
4648
self.suite_ended = False
4749
self.buffer = 0
48-
cycle_s = self.get_config_item('program_cycle_s')
49-
self.program_cycle_s = cycle_s if cycle_s is not None else DEFAULT_CYCLE_PERIOD
5050
self.test_steps_sequence = self.test_steps()
5151
# Advance the coroutine to it's first yield statement.
5252
self.test_steps_sequence.send(None)
@@ -55,30 +55,36 @@ def __init__(self):
5555
def setup(self):
5656
self.register_callback(MSG_TRNG_READY, self.cb_device_ready)
5757
self.register_callback(MSG_TRNG_BUFFER, self.cb_trng_buffer)
58-
self.register_callback(MSG_TRNG_FINISH, self.cb_device_finish)
5958
self.register_callback(MSG_KEY_TEST_SUITE_ENDED, self.cb_device_test_suit_ended)
59+
self.register_callback(MSG_KEY_RESET_COMPLETE, self.cb_reset_complete)
6060

6161
#receive sent data from device before reset
6262
def cb_trng_buffer(self, key, value, timestamp):
6363
"""Acknowledge device rebooted correctly and feed the test execution
6464
"""
6565
self.buffer = value
6666

67+
try:
68+
if self.test_steps_sequence.send(value):
69+
self.notify_complete(True)
70+
except (StopIteration, RuntimeError) as exc:
71+
self.notify_complete(False)
72+
6773
def cb_device_ready(self, key, value, timestamp):
6874
"""Acknowledge device rebooted correctly and feed the test execution
6975
"""
70-
self.reset = True
76+
self.ready = True
7177

7278
try:
7379
if self.test_steps_sequence.send(value):
7480
self.notify_complete(True)
7581
except (StopIteration, RuntimeError) as exc:
7682
self.notify_complete(False)
7783

78-
def cb_device_finish(self, key, value, timestamp):
79-
"""Acknowledge device finished a test step correctly and feed the test execution
84+
def cb_reset_complete(self, key, value, timestamp):
85+
"""Acknowledge reset complete
8086
"""
81-
self.finish = True
87+
self.did_reset = True
8288

8389
try:
8490
if self.test_steps_sequence.send(value):
@@ -103,31 +109,37 @@ def test_steps(self):
103109
"""
104110
wait_for_communication = yield
105111

106-
self.reset = False
112+
self.ready = False
113+
self.did_reset = False
114+
self.suite_ended = False
107115
self.send_kv(MSG_TRNG_TEST_STEP1, MSG_VALUE_DUMMY)
108-
time.sleep(self.program_cycle_s)
109-
self.send_kv(MSG_KEY_SYNC, MSG_VALUE_DUMMY)
110-
111116
wait_for_communication = yield
117+
if self.buffer == 0:
118+
raise RuntimeError('Phase 1: No buffer received.')
119+
120+
self.reset()
112121

113-
if self.reset == False:
114-
raise RuntimeError('Phase 1: Platform did not reset as expected.')
115-
116122
"""Test step 2 (After reset)
117123
"""
118-
self.finish = False
119-
self.send_kv(MSG_TRNG_TEST_STEP2, self.buffer)
120-
time.sleep(self.program_cycle_s)
124+
wait_for_communication = yield
125+
if self.did_reset == False:
126+
raise RuntimeError('Phase 1: Platform did not reset as expected.')
127+
128+
self.send_kv(MSG_KEY_SYNC, MSG_VALUE_DUMMY)
121129

122130
wait_for_communication = yield
123131

124-
if self.finish == False:
125-
raise RuntimeError('Test failed.')
132+
if self.ready == False:
133+
raise RuntimeError('Phase 1: Platform not ready as expected.')
134+
135+
self.send_kv(MSG_TRNG_TEST_STEP2, self.buffer)
126136

127137
wait_for_communication = yield
128138

129139
if self.suite_ended == False:
130140
raise RuntimeError('Test failed.')
131141

142+
self.send_kv(MSG_KEY_EXIT, MSG_VALUE_DUMMY)
143+
132144
# The sequence is correct -- test passed.
133-
yield True
145+
yield

TESTS/mbed_hal/trng/main.cpp

Lines changed: 51 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959

6060
#define MSG_TRNG_READY "ready"
6161
#define MSG_TRNG_BUFFER "buffer"
62+
#define MSG_TRNG_EXIT "exit"
6263

6364
#define MSG_TRNG_TEST_STEP1 "check_step1"
6465
#define MSG_TRNG_TEST_STEP2 "check_step2"
@@ -102,11 +103,19 @@ static int fill_buffer_trng(uint8_t *buffer, trng_t *trng_obj, size_t trng_len)
102103
static void compress_and_compare(char *key, char *value)
103104
{
104105
trng_t trng_obj;
105-
uint8_t out_comp_buf[(BUFFER_LEN * 5) + 32] = {0}, buffer[BUFFER_LEN] = {0};
106-
uint8_t input_buf[BUFFER_LEN * 4] = {0}, temp_buf[BUFFER_LEN * 2] = {0};
106+
uint8_t *out_comp_buf, *buffer;
107+
uint8_t *input_buf, *temp_buf;
107108
size_t comp_sz = 0;
108109
unsigned int result = 0;
109110

111+
#define OUT_COMP_BUF_SIZE ((BUFFER_LEN * 5) + 32)
112+
#define TEMP_BUF_SIZE (BUFFER_LEN * 2)
113+
114+
out_comp_buf = new uint8_t[OUT_COMP_BUF_SIZE];
115+
buffer = new uint8_t[BUFFER_LEN];
116+
input_buf = new uint8_t[BUFFER_LEN * 4];
117+
temp_buf = new uint8_t[BUFFER_LEN * 2];
118+
110119
#if NVSTORE_RESET
111120
NVStore& nvstore = NVStore::get_instance();
112121
#endif
@@ -115,7 +124,7 @@ static void compress_and_compare(char *key, char *value)
115124
if (strcmp(key, MSG_TRNG_TEST_STEP2) == 0) {
116125
#if NVSTORE_RESET
117126
uint16_t actual = 0;
118-
result = nvstore.get(NVKEY, sizeof(buffer), buffer, actual);
127+
result = nvstore.get(NVKEY, BUFFER_LEN, buffer, actual);
119128
TEST_ASSERT_EQUAL(RESULT_SUCCESS, result);
120129
#else
121130
/*Using base64 to decode data sent from host*/
@@ -130,105 +139,104 @@ static void compress_and_compare(char *key, char *value)
130139
&charsProcessed);
131140
TEST_ASSERT_EQUAL(0, result);
132141
#endif
133-
memcpy(input_buf, buffer, sizeof(buffer));
142+
memcpy(input_buf, buffer, BUFFER_LEN);
134143
}
135144

136145
if (strcmp(key, MSG_TRNG_TEST_STEP1) == 0) {
137146
/*Fill buffer with trng values*/
138-
result = fill_buffer_trng(buffer, &trng_obj, sizeof(buffer));
147+
result = fill_buffer_trng(buffer, &trng_obj, BUFFER_LEN);
139148
TEST_ASSERT_EQUAL(0, result);
140-
memcpy(input_buf, buffer, sizeof(buffer));
149+
memcpy(input_buf, buffer, BUFFER_LEN);
141150
}
142151
/*pithy_Compress will try to compress the random data, if it succeeded it means the data is not really random*/
143152
else if (strcmp(key, MSG_TRNG_TEST_STEP2) == 0) {
144153

145154
comp_sz = pithy_Compress((char *)buffer,
146-
sizeof(buffer),
155+
BUFFER_LEN,
147156
(char *)out_comp_buf,
148-
sizeof(out_comp_buf),
157+
OUT_COMP_BUF_SIZE,
149158
9);
150-
TEST_ASSERT_MESSAGE(comp_sz > sizeof(buffer),
159+
TEST_ASSERT_MESSAGE(comp_sz > BUFFER_LEN,
151160
"TRNG_TEST_STEP1: trng_get_bytes was able to compress thus not random");
152161

153162
/*pithy_Compress will try to compress the random data with a different buffer sizem*/
154-
result = fill_buffer_trng(temp_buf, &trng_obj, sizeof(temp_buf));
163+
result = fill_buffer_trng(temp_buf, &trng_obj, TEMP_BUF_SIZE);
155164
TEST_ASSERT_EQUAL(0, result);
156165

157166
comp_sz = pithy_Compress((char *)temp_buf,
158-
sizeof(temp_buf),
167+
TEMP_BUF_SIZE,
159168
(char *)out_comp_buf,
160-
sizeof(out_comp_buf),
169+
OUT_COMP_BUF_SIZE,
161170
9);
162-
TEST_ASSERT_MESSAGE(comp_sz > sizeof(temp_buf),
171+
TEST_ASSERT_MESSAGE(comp_sz > TEMP_BUF_SIZE,
163172
"TRNG_TEST_STEP2: trng_get_bytes was able to compress thus not random");
164173

165-
memcpy(input_buf + sizeof(buffer), temp_buf, sizeof(temp_buf));
174+
memcpy(input_buf + BUFFER_LEN, temp_buf, TEMP_BUF_SIZE);
166175
/*pithy_Compress will try to compress the random data from before reset concatenated with new random data*/
167176
comp_sz = pithy_Compress((char *)input_buf,
168-
sizeof(temp_buf) + sizeof(buffer),
177+
TEMP_BUF_SIZE + BUFFER_LEN,
169178
(char *)out_comp_buf,
170-
sizeof(out_comp_buf),
179+
OUT_COMP_BUF_SIZE,
171180
9);
172-
TEST_ASSERT_MESSAGE(comp_sz > sizeof(temp_buf) + sizeof(buffer),
181+
TEST_ASSERT_MESSAGE(comp_sz > TEMP_BUF_SIZE + BUFFER_LEN,
173182
"TRNG_TEST_STEP3: concatenated buffer after reset was able to compress thus not random");
183+
greentea_send_kv(MSG_TRNG_TEST_SUITE_ENDED, MSG_VALUE_DUMMY);
174184
}
175185

176186
/*At the end of step 1 store trng buffer and reset the device*/
177187
if (strcmp(key, MSG_TRNG_TEST_STEP1) == 0) {
178188
int result = 0;
179189
#if NVSTORE_RESET
180-
result = nvstore.set(NVKEY, sizeof(buffer), buffer);
190+
result = nvstore.set(NVKEY, BUFFER_LEN, buffer);
181191
TEST_ASSERT_EQUAL(RESULT_SUCCESS, result);
182192
#else
183193
/*Using base64 to encode data sending from host*/
184194
result = trng_EncodeBase64(buffer,
185195
BUFFER_LEN,
186196
(char *)out_comp_buf,
187-
sizeof(out_comp_buf));
197+
OUT_COMP_BUF_SIZE);
188198
TEST_ASSERT_EQUAL(RESULT_SUCCESS, result);
189199

190200
greentea_send_kv(MSG_TRNG_BUFFER, (const char *)out_comp_buf);
191201
#endif
192-
system_reset();
193-
TEST_ASSERT_MESSAGE(false, "system_reset() did not reset the device as expected.");
194202
}
195203

196-
return;
204+
delete[] out_comp_buf;
205+
delete[] buffer;
206+
delete[] input_buf;
207+
delete[] temp_buf;
197208
}
198209

199210
/*This method call first and second steps, it directs by the key received from the host*/
200211
void trng_test()
201212
{
202213
greentea_send_kv(MSG_TRNG_READY, MSG_VALUE_DUMMY);
203214

204-
static char key[MSG_KEY_LEN + 1] = { };
205-
static char value[MSG_VALUE_LEN + 1] = { };
206-
memset(key, 0, MSG_KEY_LEN + 1);
207-
memset(value, 0, MSG_VALUE_LEN + 1);
215+
char key[MSG_KEY_LEN + 1] = { };
216+
char *value = new char[MSG_VALUE_LEN + 1];
217+
do {
218+
memset(key, 0, MSG_KEY_LEN + 1);
219+
memset(value, 0, MSG_VALUE_LEN + 1);
208220

209-
greentea_parse_kv(key, value, MSG_KEY_LEN, MSG_VALUE_LEN);
221+
greentea_parse_kv(key, value, MSG_KEY_LEN, MSG_VALUE_LEN);
210222

211-
if (strcmp(key, MSG_TRNG_TEST_STEP1) == 0) {
212-
/*create trng data buffer and try to compress it, store it for later checks*/
213-
compress_and_compare(key, value);
214-
return trng_test();
215-
}
223+
if (strcmp(key, MSG_TRNG_TEST_STEP1) == 0) {
224+
/*create trng data buffer and try to compress it, store it for later checks*/
225+
compress_and_compare(key, value);
226+
}
216227

217-
if (strcmp(key, MSG_TRNG_TEST_STEP2) == 0) {
218-
/*create another trng data buffer and concatenate it to the stored trng data buffer
219-
try to compress them both*/
220-
compress_and_compare(key, value);
221-
}
222-
}
228+
if (strcmp(key, MSG_TRNG_TEST_STEP2) == 0) {
229+
/*create another trng data buffer and concatenate it to the stored trng data buffer
230+
try to compress them both*/
231+
compress_and_compare(key, value);
232+
}
233+
} while (strcmp(key, MSG_TRNG_EXIT) != 0);
223234

224-
utest::v1::status_t greentea_failure_handler(const Case *const source, const failure_t reason)
225-
{
226-
greentea_case_failure_abort_handler(source, reason);
227-
return STATUS_CONTINUE;
235+
delete[] value;
228236
}
229237

230238
Case cases[] = {
231-
Case("TRNG: trng_test", trng_test, greentea_failure_handler),
239+
Case("TRNG: trng_test", trng_test),
232240
};
233241

234242
utest::v1::status_t greentea_test_setup(const size_t number_of_cases)
@@ -242,7 +250,6 @@ Specification specification(greentea_test_setup, cases, greentea_test_teardown_h
242250
int main()
243251
{
244252
bool ret = !Harness::run(specification);
245-
greentea_send_kv(MSG_TRNG_TEST_SUITE_ENDED, MSG_VALUE_DUMMY);
246253

247254
return ret;
248255
}

0 commit comments

Comments
 (0)