srsLTE/srsue/test/upper/nas_test.cc

219 lines
5.7 KiB
C++
Raw Normal View History

2017-05-18 03:52:29 -07:00
/**
*
* \section COPYRIGHT
*
* Copyright 2013-2015 Software Radio Systems Limited
*
* \section LICENSE
*
* This file is part of the srsUE library.
*
* srsUE is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* srsUE 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 Affero General Public License for more details.
*
* A copy of the GNU Affero General Public License can be found in
* the LICENSE file in the top-level directory of this distribution
* and at http://www.gnu.org/licenses/.
*
*/
#include <iostream>
2018-01-08 01:47:20 -08:00
#include <assert.h>
2017-05-18 03:52:29 -07:00
#include "upper/usim.h"
#include "upper/nas.h"
#include "srslte/upper/rlc.h"
2017-05-18 03:52:29 -07:00
#include "upper/rrc.h"
#include "mac/mac.h"
2018-01-08 01:47:20 -08:00
#include "srslte/common/log_filter.h"
#include "srslte/upper/pdcp_entity.h"
#include "srslte/upper/pdcp.h"
#include "srslte/interfaces/ue_interfaces.h"
2018-01-08 01:47:20 -08:00
#include "srslte/common/bcd_helpers.h"
2017-05-18 03:52:29 -07:00
2018-01-08 01:47:20 -08:00
using namespace srsue;
2017-05-18 03:52:29 -07:00
2018-01-08 01:47:20 -08:00
#define LCID 1
2017-05-18 03:52:29 -07:00
2018-01-08 01:47:20 -08:00
uint8_t auth_request_pdu[] = { 0x07, 0x52, 0x01, 0x17, 0xf2, 0x2b, 0x2b, 0xaa, 0x2c, 0x53,
0x52, 0xf1, 0xaf, 0x2e, 0x2f, 0x6f, 0x72, 0xd0, 0x54, 0x10,
0x18, 0xee, 0x79, 0x35, 0x25, 0x3b, 0x90, 0x01, 0x17, 0xe3,
0x09, 0x18, 0xee, 0x3b, 0xa5, 0x24 };
2017-05-18 03:52:29 -07:00
2018-01-08 01:47:20 -08:00
uint8_t sec_mode_command_pdu[] = { 0x37, 0x45, 0x62, 0x29, 0xdf, 0x00, 0x07, 0x5d, 0x02, 0x01,
0x02, 0x80, 0xe0, 0xc1 };
2017-05-18 03:52:29 -07:00
2018-01-08 01:47:20 -08:00
uint16 mcc = 61441;
uint16 mnc = 65281;
2017-05-18 03:52:29 -07:00
2018-01-08 01:47:20 -08:00
using namespace srslte;
2017-05-18 03:52:29 -07:00
2018-01-08 01:47:20 -08:00
namespace srslte {
2017-05-18 03:52:29 -07:00
// fake classes
class pdcp_dummy : public rrc_interface_pdcp
{
public:
void write_pdu(uint32_t lcid, byte_buffer_t *pdu) {}
void write_pdu_bcch_bch(byte_buffer_t *pdu) {}
void write_pdu_bcch_dlsch(byte_buffer_t *pdu) {}
void write_pdu_pcch(byte_buffer_t *pdu) {}
2018-01-08 01:47:20 -08:00
std::string get_rb_name(uint32_t lcid) { return std::string("lcid"); }
2017-05-18 03:52:29 -07:00
};
class rrc_dummy : public rrc_interface_nas
{
public:
void write_sdu(uint32_t lcid, byte_buffer_t *sdu)
{
2018-01-08 01:47:20 -08:00
printf("NAS generated SDU (len=%d):\n", sdu->N_bytes);
last_sdu_len = sdu->N_bytes;
srslte_vec_fprint_byte(stdout, sdu->msg, sdu->N_bytes);
2017-05-18 03:52:29 -07:00
}
2018-01-08 01:47:20 -08:00
std::string get_rb_name(uint32_t lcid) { return std::string("lcid"); }
uint32_t get_last_sdu_len() { return last_sdu_len; }
2017-05-18 03:52:29 -07:00
2018-01-08 01:47:20 -08:00
void plmn_search() {};
void plmn_select(LIBLTE_RRC_PLMN_IDENTITY_STRUCT plmn_id) {};
2017-05-18 03:52:29 -07:00
2018-01-08 01:47:20 -08:00
uint16_t get_mcc() { return mcc; }
uint16_t get_mnc() { return mnc; }
void enable_capabilities() {}
private:
uint32_t last_sdu_len;
2017-05-18 03:52:29 -07:00
};
class gw_dummy : public gw_interface_nas, public gw_interface_pdcp
{
2018-01-08 01:47:20 -08:00
error_t setup_if_addr(uint32_t ip_addr, char *err_str) { return ERROR_NONE; }
2017-05-18 03:52:29 -07:00
void write_pdu(uint32_t lcid, byte_buffer_t *pdu) {}
};
}
2018-01-08 01:47:20 -08:00
int security_command_test()
2017-05-18 03:52:29 -07:00
{
2018-01-08 01:47:20 -08:00
int ret = SRSLTE_ERROR;
srslte::log_filter nas_log("NAS");
srslte::log_filter rrc_log("RRC");
srslte::log_filter mac_log("MAC");
srslte::log_filter usim_log("USIM");
2017-05-18 03:52:29 -07:00
nas_log.set_level(srslte::LOG_LEVEL_DEBUG);
rrc_log.set_level(srslte::LOG_LEVEL_DEBUG);
nas_log.set_hex_limit(100000);
rrc_log.set_hex_limit(100000);
rrc_dummy rrc_dummy;
gw_dummy gw;
2018-01-08 01:47:20 -08:00
usim_args_t args;
args.algo = "xor";
args.amf = "9001";
args.imei = "353490069873319";
args.imsi = "001010123456789";
args.k = "00112233445566778899aabbccddeeff";
args.op = "63BFA50EE6523365FF14C1F45F88737D";
2017-05-18 03:52:29 -07:00
2018-01-08 01:47:20 -08:00
// init USIM
srsue::usim usim;
bool net_valid;
uint8_t res[16];
usim.init(&args, &usim_log);
2017-05-18 03:52:29 -07:00
2018-01-08 01:47:20 -08:00
srslte::byte_buffer_pool *pool;
pool = byte_buffer_pool::get_instance();
2017-05-18 03:52:29 -07:00
srsue::nas nas;
2018-01-08 01:47:20 -08:00
srslte_nas_config_t cfg;
nas.init(&usim, &rrc_dummy, &gw, &nas_log, cfg);
2017-05-18 03:52:29 -07:00
2018-01-08 01:47:20 -08:00
// push auth request PDU to NAS to generate security context
byte_buffer_t* tmp = pool->allocate();
memcpy(tmp->msg, auth_request_pdu, sizeof(auth_request_pdu));
tmp->N_bytes = sizeof(auth_request_pdu);
nas.write_pdu(LCID, tmp);
2017-05-18 03:52:29 -07:00
2018-01-08 01:47:20 -08:00
// TODO: add check for authentication response
2017-05-18 03:52:29 -07:00
2018-01-08 01:47:20 -08:00
// reuse buffer for security mode command
memcpy(tmp->msg, sec_mode_command_pdu, sizeof(sec_mode_command_pdu));
tmp->N_bytes = sizeof(sec_mode_command_pdu);
nas.write_pdu(LCID, tmp);
2017-05-18 03:52:29 -07:00
2018-01-08 01:47:20 -08:00
// check length of generated NAS SDU
if (rrc_dummy.get_last_sdu_len() > 3) {
ret = SRSLTE_SUCCESS;
}
2017-05-18 03:52:29 -07:00
2018-01-08 01:47:20 -08:00
pool->cleanup();
2017-05-18 03:52:29 -07:00
2018-01-08 01:47:20 -08:00
return ret;
}
2017-05-18 03:52:29 -07:00
2018-01-08 01:47:20 -08:00
int mme_attach_request_test()
{
int ret = SRSLTE_ERROR;
srslte::log_filter nas_log("NAS");
srslte::log_filter rrc_log("RRC");
srslte::log_filter mac_log("MAC");
srslte::log_filter usim_log("USIM");
2017-05-18 03:52:29 -07:00
2018-01-08 01:47:20 -08:00
nas_log.set_level(srslte::LOG_LEVEL_DEBUG);
rrc_log.set_level(srslte::LOG_LEVEL_DEBUG);
nas_log.set_hex_limit(100000);
rrc_log.set_hex_limit(100000);
2017-05-18 03:52:29 -07:00
2018-01-08 01:47:20 -08:00
rrc_dummy rrc_dummy;
gw_dummy gw;
srsue::usim usim;
usim_args_t args;
args.algo = "xor";
args.amf = "9001";
args.imei = "353490069873319";
args.imsi = "001010123456789";
args.k = "00112233445566778899aabbccddeeff";
args.op = "63BFA50EE6523365FF14C1F45F88737D";
usim.init(&args, &usim_log);
srslte_nas_config_t nas_cfg;
srsue::nas nas;
nas.init(&usim, &rrc_dummy, &gw, &nas_log, nas_cfg);
nas.attach_request();
nas.notify_connection_setup();
2017-05-18 03:52:29 -07:00
2018-01-08 01:47:20 -08:00
// check length of generated NAS SDU
if (rrc_dummy.get_last_sdu_len() > 3) {
ret = SRSLTE_SUCCESS;
}
2017-05-18 03:52:29 -07:00
2018-01-08 01:47:20 -08:00
return ret;
}
2017-05-18 03:52:29 -07:00
2018-01-08 01:47:20 -08:00
int main(int argc, char **argv)
{
if (security_command_test()) {
printf("Security command test failed.\n");
return -1;
}
if (mme_attach_request_test()) {
printf("Attach request test failed.\n");
return -1;
}
2017-05-18 03:52:29 -07:00
2018-01-08 01:47:20 -08:00
return 0;
2017-05-18 03:52:29 -07:00
}