takashi kadono / Mbed OS Nucleo_446

Dependencies:   ssd1331

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Test_LoRaMacCrypto.cpp Source File

Test_LoRaMacCrypto.cpp

00001 /*
00002  * Copyright (c) 2018, Arm Limited and affiliates
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 #include "gtest/gtest.h"
00019 #include "LoRaMacCrypto.h"
00020 
00021 #include "cipher_stub.h"
00022 #include "cmac_stub.h"
00023 #include "aes_stub.h"
00024 
00025 class Test_LoRaMacCrypto : public testing::Test {
00026 protected:
00027     LoRaMacCrypto *object;
00028 
00029     virtual void SetUp()
00030     {
00031         cipher_stub.info_value = NULL;
00032         cipher_stub.int_zero_counter = 0;
00033         cipher_stub.int_value = 0;
00034         cmac_stub.int_zero_counter = 0;
00035         cmac_stub.int_value = 0;
00036         aes_stub.int_zero_counter = 0;
00037         aes_stub.int_value = 0;
00038         object = new LoRaMacCrypto();
00039     }
00040 
00041     virtual void TearDown()
00042     {
00043         delete object;
00044     }
00045 };
00046 
00047 TEST_F(Test_LoRaMacCrypto, constructor)
00048 {
00049     EXPECT_TRUE(object);
00050     LoRaMacCrypto obj;
00051 }
00052 
00053 TEST_F(Test_LoRaMacCrypto, compute_mic)
00054 {
00055     EXPECT_TRUE(MBEDTLS_ERR_CIPHER_ALLOC_FAILED == object->compute_mic(NULL, 0, NULL, 0, 0, 0, 0, NULL));
00056 
00057     mbedtls_cipher_info_t info;
00058     cipher_stub.info_value = &info;
00059     cipher_stub.int_zero_counter = 0;
00060     cipher_stub.int_value = -1;
00061     EXPECT_TRUE(-1 == object->compute_mic(NULL, 0, NULL, 0, 0, 0, 0, NULL));
00062 
00063     cipher_stub.int_value = 0;
00064     cmac_stub.int_zero_counter = 0;
00065     cmac_stub.int_value = -1;
00066     EXPECT_TRUE(-1 == object->compute_mic(NULL, 0, NULL, 0, 0, 0, 0, NULL));
00067 
00068     cmac_stub.int_zero_counter = 1;
00069     cmac_stub.int_value = -1;
00070     EXPECT_TRUE(-1 == object->compute_mic(NULL, 0, NULL, 0, 0, 0, 0, NULL));
00071 
00072     cmac_stub.int_zero_counter = 2;
00073     cmac_stub.int_value = -1;
00074     EXPECT_TRUE(-1 == object->compute_mic(NULL, 0, NULL, 0, 0, 0, 0, NULL));
00075 
00076     cmac_stub.int_zero_counter = 3;
00077     cmac_stub.int_value = -1;
00078     EXPECT_TRUE(-1 == object->compute_mic(NULL, 0, NULL, 0, 0, 0, 0, NULL));
00079 
00080     uint32_t mic[16];
00081     cmac_stub.int_zero_counter = 3;
00082     cmac_stub.int_value = 0;
00083     EXPECT_TRUE(0 == object->compute_mic(NULL, 0, NULL, 0, 0, 0, 0, mic));
00084 
00085 }
00086 
00087 TEST_F(Test_LoRaMacCrypto, encrypt_payload)
00088 {
00089     aes_stub.int_zero_counter = 0;
00090     aes_stub.int_value = -1;
00091     EXPECT_TRUE(-1 == object->encrypt_payload(NULL, 0, NULL, 0, 0, 0, 0, NULL));
00092 
00093     aes_stub.int_zero_counter = 1;
00094     aes_stub.int_value = -2;
00095     uint8_t buf[60];
00096     uint8_t enc[60];
00097     EXPECT_TRUE(-2 == object->encrypt_payload(buf, 20, NULL, 0, 0, 0, 0, enc));
00098 
00099     aes_stub.int_zero_counter = 2;
00100     aes_stub.int_value = -3;
00101     EXPECT_TRUE(-3 == object->encrypt_payload(buf, 20, NULL, 0, 0, 0, 0, enc));
00102 
00103     aes_stub.int_value = 0;
00104     EXPECT_TRUE(0 == object->encrypt_payload(buf, 20, NULL, 0, 0, 0, 0, enc));
00105 
00106     EXPECT_TRUE(0 == object->encrypt_payload(buf, 60, NULL, 0, 0, 0, 0, enc));
00107 
00108     aes_stub.int_zero_counter = 0;
00109     EXPECT_TRUE(0 == object->encrypt_payload(NULL, 0, NULL, 0, 0, 0, 0, NULL));
00110 }
00111 
00112 TEST_F(Test_LoRaMacCrypto, decrypt_payload)
00113 {
00114     EXPECT_TRUE(0 == object->decrypt_payload(NULL, 0, NULL, 0, 0, 0, 0, NULL));
00115 }
00116 
00117 TEST_F(Test_LoRaMacCrypto, compute_join_frame_mic)
00118 {
00119     uint32_t mic[16];
00120     EXPECT_TRUE(MBEDTLS_ERR_CIPHER_ALLOC_FAILED == object->compute_join_frame_mic(NULL, 0, NULL, 0, NULL));
00121     mbedtls_cipher_info_t info;
00122     cipher_stub.info_value = &info;
00123     cipher_stub.int_zero_counter = 0;
00124     cipher_stub.int_value = -1;
00125     EXPECT_TRUE(-1 == object->compute_join_frame_mic(NULL, 0, NULL, 0, NULL));
00126 
00127     cipher_stub.int_value = 0;
00128     cmac_stub.int_zero_counter = 0;
00129     cmac_stub.int_value = -1;
00130     EXPECT_TRUE(-1 == object->compute_join_frame_mic(NULL, 0, NULL, 0, NULL));
00131 
00132     cmac_stub.int_zero_counter = 1;
00133     cmac_stub.int_value = -1;
00134     EXPECT_TRUE(-1 == object->compute_join_frame_mic(NULL, 0, NULL, 0, NULL));
00135 
00136     cmac_stub.int_zero_counter = 2;
00137     cmac_stub.int_value = -1;
00138     EXPECT_TRUE(-1 == object->compute_join_frame_mic(NULL, 0, NULL, 0, NULL));
00139 
00140     cmac_stub.int_zero_counter = 3;
00141     cmac_stub.int_value = 0;
00142     EXPECT_TRUE(0 == object->compute_join_frame_mic(NULL, 0, NULL, 0, mic));
00143 }
00144 
00145 TEST_F(Test_LoRaMacCrypto, decrypt_join_frame)
00146 {
00147     aes_stub.int_zero_counter = 0;
00148     aes_stub.int_value = -1;
00149     EXPECT_TRUE(-1 == object->decrypt_join_frame(NULL, 0, NULL, 0, NULL));
00150 
00151     aes_stub.int_zero_counter = 1;
00152     aes_stub.int_value = -1;
00153     EXPECT_TRUE(-1 == object->decrypt_join_frame(NULL, 0, NULL, 0, NULL));
00154 
00155     aes_stub.int_value = 0;
00156     uint8_t buf[60];
00157     uint8_t enc[60];
00158     EXPECT_TRUE(0 == object->decrypt_join_frame(buf, 60, NULL, 0, enc));
00159 }
00160 
00161 TEST_F(Test_LoRaMacCrypto, compute_skeys_for_join_frame)
00162 {
00163     uint8_t nwk_key[16];
00164     uint8_t app_key[16];
00165     uint8_t nonce[16];
00166 
00167     aes_stub.int_zero_counter = 0;
00168     aes_stub.int_value = -1;
00169     EXPECT_TRUE(-1 == object->compute_skeys_for_join_frame(NULL, 0, nonce, 0, nwk_key, app_key));
00170 
00171     aes_stub.int_zero_counter = 1;
00172     aes_stub.int_value = -2;
00173     EXPECT_TRUE(-2 == object->compute_skeys_for_join_frame(NULL, 0, nonce, 0, nwk_key, app_key));
00174 
00175     aes_stub.int_zero_counter = 0;
00176     aes_stub.int_value = 0;
00177     EXPECT_TRUE(0 == object->compute_skeys_for_join_frame(NULL, 0, nonce, 0, nwk_key, app_key));
00178 }