Skip to content

Commit 41d0648

Browse files
Antti Kauppilaadbridge
authored andcommitted
LoRaMacCrypto unittested
1 parent 1f24d72 commit 41d0648

File tree

9 files changed

+409
-65
lines changed

9 files changed

+409
-65
lines changed

UNITTESTS/features/lorawan/loramaccrypto/Test_LoRaMacCrypto.cpp

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,23 @@
1818
#include "gtest/gtest.h"
1919
#include "LoRaMacCrypto.h"
2020

21+
#include "cipher_stub.h"
22+
#include "cmac_stub.h"
23+
#include "aes_stub.h"
24+
2125
class Test_LoRaMacCrypto : public testing::Test {
2226
protected:
2327
LoRaMacCrypto *object;
2428

2529
virtual void SetUp()
2630
{
31+
cipher_stub.info_value = NULL;
32+
cipher_stub.int_zero_counter = 0;
33+
cipher_stub.int_value = 0;
34+
cmac_stub.int_zero_counter = 0;
35+
cmac_stub.int_value = 0;
36+
aes_stub.int_zero_counter = 0;
37+
aes_stub.int_value = 0;
2738
object = new LoRaMacCrypto();
2839
}
2940

@@ -36,5 +47,132 @@ class Test_LoRaMacCrypto : public testing::Test {
3647
TEST_F(Test_LoRaMacCrypto, constructor)
3748
{
3849
EXPECT_TRUE(object);
50+
LoRaMacCrypto obj;
51+
}
52+
53+
TEST_F(Test_LoRaMacCrypto, compute_mic)
54+
{
55+
EXPECT_TRUE(MBEDTLS_ERR_CIPHER_ALLOC_FAILED == object->compute_mic(NULL, 0, NULL, 0, 0, 0, 0, NULL));
56+
57+
mbedtls_cipher_info_t info;
58+
cipher_stub.info_value = &info;
59+
cipher_stub.int_zero_counter = 0;
60+
cipher_stub.int_value = -1;
61+
EXPECT_TRUE(-1 == object->compute_mic(NULL, 0, NULL, 0, 0, 0, 0, NULL));
62+
63+
cipher_stub.int_value = 0;
64+
cmac_stub.int_zero_counter = 0;
65+
cmac_stub.int_value = -1;
66+
EXPECT_TRUE(-1 == object->compute_mic(NULL, 0, NULL, 0, 0, 0, 0, NULL));
67+
68+
cmac_stub.int_zero_counter = 1;
69+
cmac_stub.int_value = -1;
70+
EXPECT_TRUE(-1 == object->compute_mic(NULL, 0, NULL, 0, 0, 0, 0, NULL));
71+
72+
cmac_stub.int_zero_counter = 2;
73+
cmac_stub.int_value = -1;
74+
EXPECT_TRUE(-1 == object->compute_mic(NULL, 0, NULL, 0, 0, 0, 0, NULL));
75+
76+
cmac_stub.int_zero_counter = 3;
77+
cmac_stub.int_value = -1;
78+
EXPECT_TRUE(-1 == object->compute_mic(NULL, 0, NULL, 0, 0, 0, 0, NULL));
79+
80+
uint32_t mic[16];
81+
cmac_stub.int_zero_counter = 3;
82+
cmac_stub.int_value = 0;
83+
EXPECT_TRUE(0 == object->compute_mic(NULL, 0, NULL, 0, 0, 0, 0, mic));
84+
85+
}
86+
87+
TEST_F(Test_LoRaMacCrypto, encrypt_payload)
88+
{
89+
aes_stub.int_zero_counter = 0;
90+
aes_stub.int_value = -1;
91+
EXPECT_TRUE(-1 == object->encrypt_payload(NULL, 0, NULL, 0, 0, 0, 0, NULL));
92+
93+
aes_stub.int_zero_counter = 1;
94+
aes_stub.int_value = -2;
95+
uint8_t buf[60];
96+
uint8_t enc[60];
97+
EXPECT_TRUE(-2 == object->encrypt_payload(buf, 20, NULL, 0, 0, 0, 0, enc));
98+
99+
aes_stub.int_zero_counter = 2;
100+
aes_stub.int_value = -3;
101+
EXPECT_TRUE(-3 == object->encrypt_payload(buf, 20, NULL, 0, 0, 0, 0, enc));
102+
103+
aes_stub.int_value = 0;
104+
EXPECT_TRUE(0 == object->encrypt_payload(buf, 20, NULL, 0, 0, 0, 0, enc));
105+
106+
EXPECT_TRUE(0 == object->encrypt_payload(buf, 60, NULL, 0, 0, 0, 0, enc));
107+
108+
aes_stub.int_zero_counter = 0;
109+
EXPECT_TRUE(0 == object->encrypt_payload(NULL, 0, NULL, 0, 0, 0, 0, NULL));
39110
}
40111

112+
TEST_F(Test_LoRaMacCrypto, decrypt_payload)
113+
{
114+
EXPECT_TRUE(0 == object->decrypt_payload(NULL, 0, NULL, 0, 0, 0, 0, NULL));
115+
}
116+
117+
TEST_F(Test_LoRaMacCrypto, compute_join_frame_mic)
118+
{
119+
uint32_t mic[16];
120+
EXPECT_TRUE(MBEDTLS_ERR_CIPHER_ALLOC_FAILED == object->compute_join_frame_mic(NULL, 0, NULL, 0, NULL));
121+
mbedtls_cipher_info_t info;
122+
cipher_stub.info_value = &info;
123+
cipher_stub.int_zero_counter = 0;
124+
cipher_stub.int_value = -1;
125+
EXPECT_TRUE(-1 == object->compute_join_frame_mic(NULL, 0, NULL, 0, NULL));
126+
127+
cipher_stub.int_value = 0;
128+
cmac_stub.int_zero_counter = 0;
129+
cmac_stub.int_value = -1;
130+
EXPECT_TRUE(-1 == object->compute_join_frame_mic(NULL, 0, NULL, 0, NULL));
131+
132+
cmac_stub.int_zero_counter = 1;
133+
cmac_stub.int_value = -1;
134+
EXPECT_TRUE(-1 == object->compute_join_frame_mic(NULL, 0, NULL, 0, NULL));
135+
136+
cmac_stub.int_zero_counter = 2;
137+
cmac_stub.int_value = -1;
138+
EXPECT_TRUE(-1 == object->compute_join_frame_mic(NULL, 0, NULL, 0, NULL));
139+
140+
cmac_stub.int_zero_counter = 3;
141+
cmac_stub.int_value = 0;
142+
EXPECT_TRUE(0 == object->compute_join_frame_mic(NULL, 0, NULL, 0, mic));
143+
}
144+
145+
TEST_F(Test_LoRaMacCrypto, decrypt_join_frame)
146+
{
147+
aes_stub.int_zero_counter = 0;
148+
aes_stub.int_value = -1;
149+
EXPECT_TRUE(-1 == object->decrypt_join_frame(NULL, 0, NULL, 0, NULL));
150+
151+
aes_stub.int_zero_counter = 1;
152+
aes_stub.int_value = -1;
153+
EXPECT_TRUE(-1 == object->decrypt_join_frame(NULL, 0, NULL, 0, NULL));
154+
155+
aes_stub.int_value = 0;
156+
uint8_t buf[60];
157+
uint8_t enc[60];
158+
EXPECT_TRUE(0 == object->decrypt_join_frame(buf, 60, NULL, 0, enc));
159+
}
160+
161+
TEST_F(Test_LoRaMacCrypto, compute_skeys_for_join_frame)
162+
{
163+
uint8_t nwk_key[16];
164+
uint8_t app_key[16];
165+
uint8_t nonce[16];
166+
167+
aes_stub.int_zero_counter = 0;
168+
aes_stub.int_value = -1;
169+
EXPECT_TRUE(-1 == object->compute_skeys_for_join_frame(NULL, 0, nonce, 0, nwk_key, app_key));
170+
171+
aes_stub.int_zero_counter = 1;
172+
aes_stub.int_value = -2;
173+
EXPECT_TRUE(-2 == object->compute_skeys_for_join_frame(NULL, 0, nonce, 0, nwk_key, app_key));
174+
175+
aes_stub.int_zero_counter = 0;
176+
aes_stub.int_value = 0;
177+
EXPECT_TRUE(0 == object->compute_skeys_for_join_frame(NULL, 0, nonce, 0, nwk_key, app_key));
178+
}

UNITTESTS/stubs/aes_stub.c

Lines changed: 68 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626

2727
#include "mbedtls/aes.h"
2828

29+
#include "aes_stub.h"
30+
aes_stub_def aes_stub;
31+
2932

3033
void mbedtls_aes_init( mbedtls_aes_context *ctx )
3134
{
@@ -49,15 +52,23 @@ void mbedtls_aes_xts_free( mbedtls_aes_xts_context *ctx )
4952
int mbedtls_aes_setkey_enc( mbedtls_aes_context *ctx, const unsigned char *key,
5053
unsigned int keybits )
5154
{
52-
return( 0 );
55+
if (aes_stub.int_zero_counter) {
56+
aes_stub.int_zero_counter--;
57+
return 0;
58+
}
59+
return aes_stub.int_value;
5360
}
5461
#endif
5562

5663
#if !defined(MBEDTLS_AES_SETKEY_DEC_ALT)
5764
int mbedtls_aes_setkey_dec( mbedtls_aes_context *ctx, const unsigned char *key,
5865
unsigned int keybits )
5966
{
60-
return 0;
67+
if (aes_stub.int_zero_counter) {
68+
aes_stub.int_zero_counter--;
69+
return 0;
70+
}
71+
return aes_stub.int_value;
6172
}
6273
#endif
6374

@@ -66,22 +77,34 @@ int mbedtls_aes_xts_setkey_enc( mbedtls_aes_xts_context *ctx,
6677
const unsigned char *key,
6778
unsigned int keybits)
6879
{
69-
return 0;
80+
if (aes_stub.int_zero_counter) {
81+
aes_stub.int_zero_counter--;
82+
return 0;
83+
}
84+
return aes_stub.int_value;
7085
}
7186

7287
int mbedtls_aes_xts_setkey_dec( mbedtls_aes_xts_context *ctx,
7388
const unsigned char *key,
7489
unsigned int keybits)
7590
{
76-
return 0;
91+
if (aes_stub.int_zero_counter) {
92+
aes_stub.int_zero_counter--;
93+
return 0;
94+
}
95+
return aes_stub.int_value;
7796
}
7897
#endif
7998

8099
int mbedtls_internal_aes_encrypt( mbedtls_aes_context *ctx,
81100
const unsigned char input[16],
82101
unsigned char output[16] )
83102
{
84-
return( 0 );
103+
if (aes_stub.int_zero_counter) {
104+
aes_stub.int_zero_counter--;
105+
return 0;
106+
}
107+
return aes_stub.int_value;
85108
}
86109

87110
void mbedtls_aes_encrypt( mbedtls_aes_context *ctx,
@@ -94,7 +117,11 @@ int mbedtls_internal_aes_decrypt( mbedtls_aes_context *ctx,
94117
const unsigned char input[16],
95118
unsigned char output[16] )
96119
{
97-
return( 0 );
120+
if (aes_stub.int_zero_counter) {
121+
aes_stub.int_zero_counter--;
122+
return 0;
123+
}
124+
return aes_stub.int_value;
98125
}
99126

100127
void mbedtls_aes_decrypt( mbedtls_aes_context *ctx,
@@ -108,7 +135,11 @@ int mbedtls_aes_crypt_ecb( mbedtls_aes_context *ctx,
108135
const unsigned char input[16],
109136
unsigned char output[16] )
110137
{
111-
return 0;
138+
if (aes_stub.int_zero_counter) {
139+
aes_stub.int_zero_counter--;
140+
return 0;
141+
}
142+
return aes_stub.int_value;
112143
}
113144

114145
int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx,
@@ -118,7 +149,11 @@ int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx,
118149
const unsigned char *input,
119150
unsigned char *output )
120151
{
121-
return( 0 );
152+
if (aes_stub.int_zero_counter) {
153+
aes_stub.int_zero_counter--;
154+
return 0;
155+
}
156+
return aes_stub.int_value;
122157
}
123158

124159

@@ -130,7 +165,11 @@ int mbedtls_aes_crypt_xts( mbedtls_aes_xts_context *ctx,
130165
const unsigned char *input,
131166
unsigned char *output )
132167
{
133-
return( 0 );
168+
if (aes_stub.int_zero_counter) {
169+
aes_stub.int_zero_counter--;
170+
return 0;
171+
}
172+
return aes_stub.int_value;
134173
}
135174
#endif
136175

@@ -142,7 +181,11 @@ int mbedtls_aes_crypt_cfb128( mbedtls_aes_context *ctx,
142181
const unsigned char *input,
143182
unsigned char *output )
144183
{
145-
return( 0 );
184+
if (aes_stub.int_zero_counter) {
185+
aes_stub.int_zero_counter--;
186+
return 0;
187+
}
188+
return aes_stub.int_value;
146189
}
147190

148191
int mbedtls_aes_crypt_cfb8( mbedtls_aes_context *ctx,
@@ -152,7 +195,11 @@ int mbedtls_aes_crypt_cfb8( mbedtls_aes_context *ctx,
152195
const unsigned char *input,
153196
unsigned char *output )
154197
{
155-
return( 0 );
198+
if (aes_stub.int_zero_counter) {
199+
aes_stub.int_zero_counter--;
200+
return 0;
201+
}
202+
return aes_stub.int_value;
156203
}
157204

158205
int mbedtls_aes_crypt_ofb( mbedtls_aes_context *ctx,
@@ -162,7 +209,11 @@ int mbedtls_aes_crypt_ofb( mbedtls_aes_context *ctx,
162209
const unsigned char *input,
163210
unsigned char *output )
164211
{
165-
return( 0 );
212+
if (aes_stub.int_zero_counter) {
213+
aes_stub.int_zero_counter--;
214+
return 0;
215+
}
216+
return aes_stub.int_value;
166217
}
167218

168219
int mbedtls_aes_crypt_ctr( mbedtls_aes_context *ctx,
@@ -173,5 +224,9 @@ int mbedtls_aes_crypt_ctr( mbedtls_aes_context *ctx,
173224
const unsigned char *input,
174225
unsigned char *output )
175226
{
176-
return( 0 );
227+
if (aes_stub.int_zero_counter) {
228+
aes_stub.int_zero_counter--;
229+
return 0;
230+
}
231+
return aes_stub.int_value;
177232
}

UNITTESTS/stubs/aes_stub.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright (c) , Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
19+
#include <inttypes.h>
20+
21+
typedef struct {
22+
int int_value;
23+
uint8_t int_zero_counter;
24+
} aes_stub_def;
25+
26+
extern aes_stub_def aes_stub;

0 commit comments

Comments
 (0)