chore: migrate project into clean repository

This commit is contained in:
yuuux
2026-08-13 16:50:52 +08:00
commit d1d25a09e7
27405 changed files with 9422808 additions and 0 deletions

View File

@@ -0,0 +1,5 @@
target_include_directories(${EXECUTABLE_NAME} PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}
)

View File

@@ -0,0 +1,25 @@
#ifndef __LISA_COMMON_ERR__
#define __LISA_COMMON_ERR__
#include <stdint.h>
#include "lisa_typedef.h"
typedef int32_t lisa_err_t;
#define LISA_OK (int32_t)(0)
#define LISA_FAIL (int32_t)(-1)
#define LISA_APP_ASSERT(exp, fmt, ...) \
while (!(exp)) { \
while (1) { \
}; \
break; \
}
#define LISA_ISR_ASSERT(exp, fmt, ...) \
while (!(exp)) { \
while (1) { \
}; \
break; \
}
#endif // __LISA_COMMON_ERR__

View File

@@ -0,0 +1,26 @@
#ifndef __LISA_COMMON_TYPEDEF__
#define __LISA_COMMON_TYPEDEF__
#include <stdbool.h>
#ifndef false
#define false (0)
#endif
#ifndef true
#define true (1)
#endif
#define LISA_OS_WAIT_FOREVER (0xffffffffU) /* Wait forever timeout value */
typedef enum {
LISA_OS_PRIORITY_IDLE = 5,
LISA_OS_PRIORITY_LOW = 6,
LISA_OS_PRIORITY_BELOW_NORMAL = 7,
LISA_OS_PRIORITY_NORMAL = 8,
LISA_OS_PRIORITY_ABOVE_NORMAL = 9,
LISA_OS_PRIORITY_HIGH = 10,
LISA_OS_PRIORITY_REAL_TIME = 11
} LISA_OS_Priority;
#endif // v

View File

@@ -0,0 +1,27 @@
#pragma once
// 这是一个模拟的lisa_log.h文件,用于测试mac_manager.c
#include <stdio.h>
#include <stdarg.h>
#ifdef __cplusplus
extern "C" {
#endif
#define LISA_LOGD(tag, fmt, ...) printf("[D][%s] " fmt "\n", tag, ##__VA_ARGS__)
#define LISA_LOGI(tag, fmt, ...) printf("[I][%s] " fmt "\n", tag, ##__VA_ARGS__)
#define LISA_LOGE(tag, fmt, ...) printf("[E][%s] " fmt "\n", tag, ##__VA_ARGS__)
#define LISA_LOGW(tag, fmt, ...) printf("[W][%s] " fmt "\n", tag, ##__VA_ARGS__)
#define LOGE(fmt, ...) printf("[E] " fmt "\n", ##__VA_ARGS__)
#define LOGI(fmt, ...) printf("[I] " fmt "\n", ##__VA_ARGS__)
#define LOGW(fmt, ...) printf("[W] " fmt "\n", ##__VA_ARGS__)
#define LOGD(fmt, ...) printf("[D] " fmt "\n", ##__VA_ARGS__)
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,10 @@
target_sources(${EXECUTABLE_NAME} PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/fake_lisa_mutex.c
)
target_include_directories(${EXECUTABLE_NAME} PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/../fake_lisa_common
)

View File

@@ -0,0 +1,110 @@
/**
* Copyright (c) 2025, LISTENAI
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "fake_lisa_mutex.h"
#include "fff.h"
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
DEFINE_FAKE_VALUE_FUNC(lisa_mutex_t*, lisa_mutex_create);
DEFINE_FAKE_VALUE_FUNC(lisa_err_t, lisa_mutex_lock, lisa_mutex_t *, int32_t);
DEFINE_FAKE_VALUE_FUNC(lisa_err_t, lisa_mutex_unlock, lisa_mutex_t *);
DEFINE_FAKE_VALUE_FUNC(lisa_err_t, lisa_mutex_delete, lisa_mutex_t *);
static lisa_mutex_t* custom_lisa_mutex_create(void)
{
lisa_mutex_t* mutex = (lisa_mutex_t*)malloc(sizeof(lisa_mutex_t));
if (mutex == NULL) {
return NULL;
}
pthread_mutex_t* pmutex = (pthread_mutex_t*)malloc(sizeof(pthread_mutex_t));
if (pmutex == NULL) {
free(mutex);
return NULL;
}
if (pthread_mutex_init(pmutex, NULL) != 0) {
free(pmutex);
free(mutex);
return NULL;
}
mutex->handle = pmutex;
mutex->locked = false;
return mutex;
}
static lisa_err_t custom_lisa_mutex_lock(lisa_mutex_t *mutex, int32_t block_time)
{
pthread_mutex_t* pmutex = (pthread_mutex_t*)mutex->handle;
int result;
if (block_time < 0) {
// 阻塞式等待
result = pthread_mutex_lock(pmutex);
} else if (block_time == 0) {
// 非阻塞尝试
result = pthread_mutex_trylock(pmutex);
} else {
// 超时等待 (Linux不直接支持超时互斥锁,这里简化处理)
result = pthread_mutex_lock(pmutex);
}
if (result == 0) {
mutex->locked = true;
return LISA_OK;
}
return LISA_FAIL;
}
static lisa_err_t custom_lisa_mutex_unlock(lisa_mutex_t *mutex)
{
pthread_mutex_t* pmutex = (pthread_mutex_t*)mutex->handle;
if (pthread_mutex_unlock(pmutex) == 0) {
mutex->locked = false;
return LISA_OK;
}
return LISA_FAIL;
}
static lisa_err_t custom_lisa_mutex_delete(lisa_mutex_t *mutex)
{
if (mutex && mutex->handle) {
pthread_mutex_t* pmutex = (pthread_mutex_t*)mutex->handle;
pthread_mutex_destroy(pmutex);
free(pmutex);
free(mutex);
return LISA_OK;
}
return LISA_FAIL;
}
void fake_lisa_mutex_init(void)
{
RESET_FAKE(lisa_mutex_create);
RESET_FAKE(lisa_mutex_lock);
RESET_FAKE(lisa_mutex_unlock);
RESET_FAKE(lisa_mutex_delete);
lisa_mutex_create_fake.custom_fake = custom_lisa_mutex_create;
lisa_mutex_lock_fake.custom_fake = custom_lisa_mutex_lock;
lisa_mutex_unlock_fake.custom_fake = custom_lisa_mutex_unlock;
lisa_mutex_delete_fake.custom_fake = custom_lisa_mutex_delete;
}
void fake_lisa_mutex_reset(void)
{
fake_lisa_mutex_init();
}

View File

@@ -0,0 +1,30 @@
/**
* Copyright (c) 2025, LISTENAI
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include "fff.h"
#include "lisa_err.h"
#include "lisa_mutex.h"
#ifdef __cplusplus
extern "C" {
#endif
DECLARE_FAKE_VALUE_FUNC(lisa_mutex_t*, lisa_mutex_create);
DECLARE_FAKE_VALUE_FUNC(lisa_err_t, lisa_mutex_lock, lisa_mutex_t *, int32_t);
DECLARE_FAKE_VALUE_FUNC(lisa_err_t, lisa_mutex_unlock, lisa_mutex_t *);
DECLARE_FAKE_VALUE_FUNC(lisa_err_t, lisa_mutex_delete, lisa_mutex_t *);
void fake_lisa_mutex_init(void);
void fake_lisa_mutex_reset(void);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,28 @@
/**
* Copyright (c) 2025, LISTENAI
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include "lisa_err.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef struct {
void *handle;
bool locked;
} lisa_mutex_t;
lisa_mutex_t * lisa_mutex_create();
lisa_err_t lisa_mutex_lock(lisa_mutex_t *mutex, int32_t block_time);
lisa_err_t lisa_mutex_unlock(lisa_mutex_t *mutex);
lisa_err_t lisa_mutex_delete(lisa_mutex_t *mutex);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,10 @@
target_sources(${EXECUTABLE_NAME} PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/fake_lisa_thread.c
)
target_include_directories(${EXECUTABLE_NAME} PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/../fake_lisa_common
)

View File

@@ -0,0 +1,81 @@
/**
* Copyright (c) 2025, LISTENAI
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "fake_lisa_thread.h"
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
DEFINE_FAKE_VALUE_FUNC(lisa_thread_t *, lisa_thread_create, const lisa_thread_attr_t *, lisa_thread_entry_t, void *);
DEFINE_FAKE_VALUE_FUNC(lisa_err_t, lisa_thread_delete, lisa_thread_t *);
static void *thread_entry_adapter(void *arg) {
LisaThreadArg *thread_arg = (LisaThreadArg *)arg;
if (thread_arg && thread_arg->fn) {
// printf("func:%s, line:%d, thread_arg->arg: %p\n", __FUNCTION__, __LINE__, thread_arg->arg);
thread_arg->fn(thread_arg->arg);
}
// printf("func:%s, line:%d, free thread_arg: %p\n", __FUNCTION__, __LINE__, thread_arg);
free(thread_arg);
return NULL;
}
static lisa_thread_t *custom_fake_thread_create(const lisa_thread_attr_t *attr, lisa_thread_entry_t entry, void *arg)
{
if (!entry) return NULL;
lisa_thread_t *thread = (lisa_thread_t *)malloc(sizeof(lisa_thread_t));
if (!thread) return NULL;
LisaThreadArg *thread_arg = (LisaThreadArg *)malloc(sizeof(LisaThreadArg));
if (!thread_arg) {
free(thread);
return NULL;
}
thread_arg->fn = entry;
thread_arg->arg = arg;
pthread_attr_t pthread_attr;
pthread_attr_init(&pthread_attr);
if (attr && attr->stack_size) {
pthread_attr_setstacksize(&pthread_attr, attr->stack_size);
}
// 优先级设置略过,pthread 不直接支持
int ret = pthread_create(&thread->handle, &pthread_attr, thread_entry_adapter, thread_arg);
pthread_attr_destroy(&pthread_attr);
if (ret != 0) {
printf("pthread_create failed, ret: %d\n", ret);
free(thread_arg);
free(thread);
return NULL;
}
return thread;
}
static lisa_err_t custom_fake_thread_delete(lisa_thread_t *thread)
{
if (!thread) return LISA_OK;
pthread_join(thread->handle, NULL);
free(thread);
return LISA_OK;
}
void fake_lisa_thread_init(void)
{
RESET_FAKE(lisa_thread_create);
RESET_FAKE(lisa_thread_delete);
lisa_thread_create_fake.custom_fake = custom_fake_thread_create;
lisa_thread_delete_fake.custom_fake = custom_fake_thread_delete;
}
void fake_lisa_thread_reset(void)
{
fake_lisa_thread_init();
}

View File

@@ -0,0 +1,29 @@
/**
* Copyright (c) 2025, LISTENAI
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include "lisa_thread.h"
#include "fff.h"
#ifdef __cplusplus
extern "C" {
#endif
DECLARE_FAKE_VALUE_FUNC(lisa_thread_t *, lisa_thread_create, const lisa_thread_attr_t *, lisa_thread_entry_t, void *);
DECLARE_FAKE_VALUE_FUNC(lisa_err_t, lisa_thread_delete, lisa_thread_t *);
void fake_lisa_thread_init(void);
void fake_lisa_thread_reset(void);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,41 @@
/**
* Copyright (c) 2025, LISTENAI
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include "lisa_err.h"
#include "fff.h"
#include <pthread.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef struct LisaThreadArg {
void (*fn)(void *);
void *arg;
} LisaThreadArg;
typedef struct {
uint8_t *name;
uint32_t stack_size;
uint32_t priority;
} lisa_thread_attr_t;
typedef struct {
pthread_t handle;
} lisa_thread_t;
typedef void (*lisa_thread_entry_t)(void *);
lisa_thread_t *lisa_thread_create(const lisa_thread_attr_t *, lisa_thread_entry_t, void *arg);
lisa_err_t lisa_thread_delete(lisa_thread_t *thread);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,7 @@
target_include_directories(${EXECUTABLE_NAME} PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}
)
target_sources(${EXECUTABLE_NAME} PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/lisa_time.c
)

View File

@@ -0,0 +1,35 @@
/**
* Copyright (c) 2025, LISTENAI
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "lisa_time.h"
#include <stdlib.h>
uint64_t lisa_os_get_ticks(void)
{
return 0;
}
uint32_t lisa_os_get_time(void)
{
return 0;
}
uint32_t lisa_rand32(void)
{
return (uint32_t)rand();
}
uint64_t lisa_os_get_time_ssl(void *arg)
{
(void)arg;
return 0;
}
uint64_t lisa_os_get_tick_ms(void)
{
return 0;
}

View File

@@ -0,0 +1,44 @@
/**
* Copyright (c) 2025, LISTENAI
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
#include <stddef.h>
#include <stdint.h>
#define LISA_WAIT_FOREVER 0xffffffffU /* Wait forever timeout value */
#define LISA_NO_WAIT 0 /* wait 0 second timeout value */
/**
* @brief get ticks
* @return uint32_t 秒
*/
uint64_t lisa_os_get_ticks(void);
/**
* @brief gettime
* @return uint32_t 秒
*/
uint32_t lisa_os_get_time(void);
/**
* @brief rand
* @return uint32_t
*/
uint32_t lisa_rand32(void);
uint64_t lisa_os_get_time_ssl(void *arg);
uint64_t lisa_os_get_tick_ms();
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,4 @@
target_include_directories(${EXECUTABLE_NAME} PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}
)

View File

@@ -0,0 +1,47 @@
/**
* Copyright (c) 2025, LISTENAI
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include <sys/types.h>
#include <stdbool.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
struct nvs_fs {
/** File system offset in flash **/
off_t offset;
/** Allocation table entry write address.
* Addresses are stored as uint32_t:
* - high 2 bytes correspond to the sector
* - low 2 bytes are the offset in the sector
*/
uint32_t ate_wra;
/** Data write address */
uint32_t data_wra;
/** File system is split into sectors, each sector must be multiple of erase-block-size */
uint16_t sector_size;
/** Number of sectors in the file system */
uint16_t sector_count;
/** Flag indicating if the file system is initialized */
bool ready;
/** Mutex */
// struct k_mutex nvs_lock;
/** Flash device runtime structure */
struct FLASH_DEV *flash_device;
/** Flash memory parameters structure */
const struct flash_parameters *flash_parameters;
};
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,9 @@
target_sources(${EXECUTABLE_NAME} PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/mock_task.c
)
target_include_directories(${EXECUTABLE_NAME} PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}
)

View File

@@ -0,0 +1,17 @@
/**
* Copyright (c) 2025, LISTENAI
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
#define pdMS_TO_TICKS(ms) (ms)
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,53 @@
/**
* Copyright (c) 2025, LISTENAI
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
typedef void *EventGroupHandle_t;
typedef uint32_t EventBits_t;
static inline EventGroupHandle_t xEventGroupCreate(void)
{
return (EventGroupHandle_t)1;
}
static inline EventBits_t xEventGroupSetBits(EventGroupHandle_t group, EventBits_t bits)
{
(void)group;
return bits;
}
static inline EventBits_t xEventGroupClearBits(EventGroupHandle_t group, EventBits_t bits)
{
(void)group;
return bits;
}
static inline EventBits_t xEventGroupWaitBits(EventGroupHandle_t group, EventBits_t bits_to_wait_for,
int clear_on_exit, int wait_for_all_bits, uint32_t timeout)
{
(void)group;
(void)bits_to_wait_for;
(void)clear_on_exit;
(void)wait_for_all_bits;
(void)timeout;
return bits_to_wait_for;
}
static inline void vEventGroupDelete(EventGroupHandle_t group)
{
(void)group;
}
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,30 @@
/**
* Copyright (c) 2025, LISTENAI
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "mock_task.h"
#include <time.h>
DEFINE_FAKE_VALUE_FUNC(unsigned int, xTaskGetTickCount);
static unsigned int custom_xTaskGetTickCount(void)
{
return (unsigned int)time(NULL);
}
void mock_task_init(void)
{
RESET_FAKE(xTaskGetTickCount);
xTaskGetTickCount_fake.custom_fake = custom_xTaskGetTickCount;
}
void mock_task_reset(void)
{
mock_task_init();
}

View File

@@ -0,0 +1,24 @@
/**
* Copyright (c) 2025, LISTENAI
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include "task.h"
#include "fff.h"
#ifdef __cplusplus
extern "C" {
#endif
DECLARE_FAKE_VALUE_FUNC(unsigned int, xTaskGetTickCount);
void mock_task_init(void);
void mock_task_reset(void);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,16 @@
/**
* Copyright (c) 2025, LISTENAI
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
unsigned int xTaskGetTickCount(void);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,23 @@
/**
* Copyright (c) 2025, LISTENAI
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include <stdint.h>
#include "fff.h"
#ifdef __cplusplus
extern "C" {
#endif
#define HAL_DCACHE_CFG_LINE_SIZE 32
DECLARE_FAKE_VOID_FUNC(HAL_InvalidateDCache_by_Addr, uint32_t *, uint32_t);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,9 @@
/**
* Copyright (c) 2025, LISTENAI
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "cache.h"
DEFINE_FAKE_VOID_FUNC(HAL_InvalidateDCache_by_Addr, uint32_t *, uint32_t);

View File

@@ -0,0 +1,9 @@
target_sources(${EXECUTABLE_NAME} PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/mock_esp_heap_caps.c
)
target_include_directories(${EXECUTABLE_NAME} PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}
)

View File

@@ -0,0 +1,29 @@
/**
* Copyright (c) 2025, LISTENAI
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include <stdint.h>
#include <string.h>
#ifdef __cplusplus
extern "C" {
#endif
#define MALLOC_CAP_SPIRAM (1<<10) ///< Memory must be in SPI RAM
#define MALLOC_CAP_INTERNAL (1<<11) ///< Internal memory
void *heap_caps_malloc(size_t size, uint32_t caps);
void *heap_caps_calloc(size_t n, size_t size, uint32_t caps);
void *heap_caps_aligned_alloc(size_t alignment, size_t size, uint32_t caps);
void *heap_caps_realloc(void *ptr, size_t size, uint32_t caps);
void heap_caps_free(void *ptr);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,72 @@
/**
* Copyright (c) 2025, LISTENAI
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "mock_esp_heap_caps.h"
#include "fff.h"
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
DEFINE_FAKE_VALUE_FUNC(void *, heap_caps_malloc, size_t, uint32_t);
DEFINE_FAKE_VALUE_FUNC(void *, heap_caps_calloc, size_t, size_t, uint32_t);
DEFINE_FAKE_VALUE_FUNC(void *, heap_caps_aligned_alloc, size_t, size_t, uint32_t);
DEFINE_FAKE_VALUE_FUNC(void *, heap_caps_realloc, void *, size_t, uint32_t);
DEFINE_FAKE_VOID_FUNC(heap_caps_free, void *);
void* custom_esp_heap_caps_malloc(size_t size, uint32_t caps)
{
void *p = malloc(size);
// printf("malloc size: %zu, ptr: %p\n", size, p);
return p;
}
void* custom_esp_heap_caps_calloc(size_t n, size_t size, uint32_t caps)
{
void *p = calloc(n, size);
// printf("calloc n: %zu, size: %zu, ptr: %p\n", n, size, p);
return p;
}
void *custom_esp_heap_caps_aligned_alloc(size_t alignment, size_t size, uint32_t caps) {
(void)caps;
void *p = aligned_alloc(alignment, size);
// printf("aligned_alloc alignment: %zu, size: %zu, ptr: %p\n", alignment, size, p);
return p;
}
void *custom_esp_heap_caps_realloc(void *ptr, size_t size, uint32_t caps) {
(void)caps;
void *p = realloc(ptr, size);
// printf("realloc ptr: %p, size: %zu\n", p, size);
return p;
}
void custom_esp_heap_caps_free(void *ptr) {
// printf("esp heap free ptr: %p\n", ptr);
free(ptr);
}
void mock_esp_heap_caps_init()
{
RESET_FAKE(heap_caps_malloc);
RESET_FAKE(heap_caps_calloc);
RESET_FAKE(heap_caps_aligned_alloc);
RESET_FAKE(heap_caps_realloc);
RESET_FAKE(heap_caps_free);
heap_caps_malloc_fake.custom_fake = custom_esp_heap_caps_malloc;
heap_caps_calloc_fake.custom_fake = custom_esp_heap_caps_calloc;
heap_caps_aligned_alloc_fake.custom_fake = custom_esp_heap_caps_aligned_alloc;
heap_caps_realloc_fake.custom_fake = custom_esp_heap_caps_realloc;
heap_caps_free_fake.custom_fake = custom_esp_heap_caps_free;
}
void mock_esp_heap_caps_reset()
{
mock_esp_heap_caps_init();
}

View File

@@ -0,0 +1,28 @@
/**
* Copyright (c) 2025, LISTENAI
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include "esp_heap_caps.h"
#include "fff.h"
#ifdef __cplusplus
extern "C" {
#endif
DECLARE_FAKE_VALUE_FUNC(void *, heap_caps_malloc, size_t, uint32_t);
DECLARE_FAKE_VALUE_FUNC(void *, heap_caps_calloc, size_t, size_t, uint32_t);
DECLARE_FAKE_VALUE_FUNC(void *, heap_caps_aligned_alloc, size_t, size_t, uint32_t);
DECLARE_FAKE_VALUE_FUNC(void *, heap_caps_realloc, void *, size_t, uint32_t);
DECLARE_FAKE_VOID_FUNC(heap_caps_free, void *);
void mock_esp_heap_caps_init();
void mock_esp_heap_caps_reset();
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,9 @@
target_sources(${EXECUTABLE_NAME} PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/mock_lisa_kv.c
)
target_include_directories(${EXECUTABLE_NAME} PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}
)

View File

@@ -0,0 +1,34 @@
/**
* Copyright (c) 2025, LISTENAI
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include <stdint.h>
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif
void lisa_kv_free(void *value);
int lisa_kv_get_blob(const char *key, uint8_t **data, int *len);
int lisa_kv_set_blob(const char *key, uint8_t *data, int len);
int lisa_kv_get_int(const char *key, int *value);
int lisa_kv_set_int(const char *key, int value);
int lisa_kv_get_string(const char *key, char **value);
int lisa_kv_set_string(const char *key, const char *value);
int lisa_kv_get_bool(const char *key, bool *value);
int lisa_kv_set_bool(const char *key, bool value);
int lisa_kv_del(const char *key);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,278 @@
/**
* Copyright (c) 2025, LISTENAI
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "mock_lisa_kv.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define MOCK_KV_MAX_ENTRIES 64
typedef struct {
char *key;
char *value;
} mock_kv_string_entry_t;
typedef struct {
char *key;
uint8_t *data;
int len;
} mock_kv_blob_entry_t;
static mock_kv_string_entry_t s_string_entries[MOCK_KV_MAX_ENTRIES];
static mock_kv_blob_entry_t s_blob_entries[MOCK_KV_MAX_ENTRIES];
DEFINE_FAKE_VOID_FUNC(lisa_kv_free, void *);
DEFINE_FAKE_VALUE_FUNC(int, lisa_kv_get_blob, const char *, uint8_t **, int *);
DEFINE_FAKE_VALUE_FUNC(int, lisa_kv_set_blob, const char *, uint8_t *, int);
DEFINE_FAKE_VALUE_FUNC(int, lisa_kv_del, const char *);
DEFINE_FAKE_VALUE_FUNC(int, lisa_kv_get_int, const char *, int *);
DEFINE_FAKE_VALUE_FUNC(int, lisa_kv_set_int, const char *, int);
DEFINE_FAKE_VALUE_FUNC(int, lisa_kv_get_string, const char *, char **);
DEFINE_FAKE_VALUE_FUNC(int, lisa_kv_set_string, const char *, const char *);
DEFINE_FAKE_VALUE_FUNC(int, lisa_kv_get_bool, const char *, bool *);
DEFINE_FAKE_VALUE_FUNC(int, lisa_kv_set_bool, const char *, bool);
static void mock_kv_clear_entries(void)
{
for (int i = 0; i < MOCK_KV_MAX_ENTRIES; i++) {
free(s_string_entries[i].key);
free(s_string_entries[i].value);
s_string_entries[i].key = NULL;
s_string_entries[i].value = NULL;
free(s_blob_entries[i].key);
free(s_blob_entries[i].data);
s_blob_entries[i].key = NULL;
s_blob_entries[i].data = NULL;
s_blob_entries[i].len = 0;
}
}
static char *mock_kv_strdup(const char *value)
{
size_t len = value ? strlen(value) : 0;
char *copy = malloc(len + 1);
if (!copy) {
return NULL;
}
if (len > 0) {
memcpy(copy, value, len);
}
copy[len] = '\0';
return copy;
}
static int mock_kv_find_string_index(const char *key)
{
for (int i = 0; i < MOCK_KV_MAX_ENTRIES; i++) {
if (s_string_entries[i].key && strcmp(s_string_entries[i].key, key) == 0) {
return i;
}
}
return -1;
}
static int mock_kv_find_blob_index(const char *key)
{
for (int i = 0; i < MOCK_KV_MAX_ENTRIES; i++) {
if (s_blob_entries[i].key && strcmp(s_blob_entries[i].key, key) == 0) {
return i;
}
}
return -1;
}
static int mock_kv_find_free_string_slot(void)
{
for (int i = 0; i < MOCK_KV_MAX_ENTRIES; i++) {
if (s_string_entries[i].key == NULL) {
return i;
}
}
return -1;
}
static int mock_kv_find_free_blob_slot(void)
{
for (int i = 0; i < MOCK_KV_MAX_ENTRIES; i++) {
if (s_blob_entries[i].key == NULL) {
return i;
}
}
return -1;
}
static int mem_lisa_kv_set_string(const char *key, const char *value)
{
int idx = mock_kv_find_string_index(key);
if (idx < 0) {
idx = mock_kv_find_free_string_slot();
}
if (idx < 0) {
return -1;
}
free(s_string_entries[idx].key);
free(s_string_entries[idx].value);
s_string_entries[idx].key = mock_kv_strdup(key);
s_string_entries[idx].value = mock_kv_strdup(value ? value : "");
if (!s_string_entries[idx].key || !s_string_entries[idx].value) {
return -1;
}
return 0;
}
static int mem_lisa_kv_get_string(const char *key, char **value)
{
int idx = mock_kv_find_string_index(key);
if (idx < 0) {
return -1;
}
*value = mock_kv_strdup(s_string_entries[idx].value);
return *value ? 0 : -1;
}
static int mem_lisa_kv_set_int(const char *key, int value)
{
char buffer[32];
snprintf(buffer, sizeof(buffer), "%d", value);
return mem_lisa_kv_set_string(key, buffer);
}
static int mem_lisa_kv_get_int(const char *key, int *value)
{
char *stored = NULL;
if (mem_lisa_kv_get_string(key, &stored) != 0) {
return -1;
}
*value = atoi(stored);
free(stored);
return 0;
}
static int mem_lisa_kv_set_bool(const char *key, bool value)
{
return mem_lisa_kv_set_int(key, value ? 1 : 0);
}
static int mem_lisa_kv_get_bool(const char *key, bool *value)
{
int stored = 0;
if (mem_lisa_kv_get_int(key, &stored) != 0) {
return -1;
}
*value = stored != 0;
return 0;
}
static int mem_lisa_kv_set_blob(const char *key, uint8_t *data, int len)
{
int idx = mock_kv_find_blob_index(key);
if (idx < 0) {
idx = mock_kv_find_free_blob_slot();
}
if (idx < 0) {
return -1;
}
free(s_blob_entries[idx].key);
free(s_blob_entries[idx].data);
s_blob_entries[idx].key = mock_kv_strdup(key);
s_blob_entries[idx].data = NULL;
s_blob_entries[idx].len = 0;
if (data && len > 0) {
s_blob_entries[idx].data = malloc(len);
if (!s_blob_entries[idx].data) {
return -1;
}
memcpy(s_blob_entries[idx].data, data, len);
s_blob_entries[idx].len = len;
}
return 0;
}
static int mem_lisa_kv_get_blob(const char *key, uint8_t **data, int *len)
{
int idx = mock_kv_find_blob_index(key);
if (idx < 0) {
return -1;
}
if (s_blob_entries[idx].data == NULL || s_blob_entries[idx].len <= 0) {
return -1;
}
*data = malloc(s_blob_entries[idx].len);
if (!*data) {
return -1;
}
memcpy(*data, s_blob_entries[idx].data, s_blob_entries[idx].len);
*len = s_blob_entries[idx].len;
return 0;
}
static int mem_lisa_kv_del(const char *key)
{
int idx = mock_kv_find_string_index(key);
if (idx >= 0) {
free(s_string_entries[idx].key);
free(s_string_entries[idx].value);
s_string_entries[idx].key = NULL;
s_string_entries[idx].value = NULL;
}
idx = mock_kv_find_blob_index(key);
if (idx >= 0) {
free(s_blob_entries[idx].key);
free(s_blob_entries[idx].data);
s_blob_entries[idx].key = NULL;
s_blob_entries[idx].data = NULL;
s_blob_entries[idx].len = 0;
}
return 0;
}
static void mem_lisa_kv_free(void *value)
{
free(value);
}
void mock_lisa_kv_init(void)
{
RESET_FAKE(lisa_kv_free);
RESET_FAKE(lisa_kv_get_blob);
RESET_FAKE(lisa_kv_set_blob);
RESET_FAKE(lisa_kv_del);
RESET_FAKE(lisa_kv_get_int);
RESET_FAKE(lisa_kv_set_int);
RESET_FAKE(lisa_kv_get_string);
RESET_FAKE(lisa_kv_set_string);
RESET_FAKE(lisa_kv_get_bool);
RESET_FAKE(lisa_kv_set_bool);
mock_kv_clear_entries();
lisa_kv_get_blob_fake.custom_fake = mem_lisa_kv_get_blob;
lisa_kv_set_blob_fake.custom_fake = mem_lisa_kv_set_blob;
lisa_kv_free_fake.custom_fake = mem_lisa_kv_free;
lisa_kv_del_fake.custom_fake = mem_lisa_kv_del;
lisa_kv_get_int_fake.custom_fake = mem_lisa_kv_get_int;
lisa_kv_set_int_fake.custom_fake = mem_lisa_kv_set_int;
lisa_kv_get_string_fake.custom_fake = mem_lisa_kv_get_string;
lisa_kv_set_string_fake.custom_fake = mem_lisa_kv_set_string;
lisa_kv_get_bool_fake.custom_fake = mem_lisa_kv_get_bool;
lisa_kv_set_bool_fake.custom_fake = mem_lisa_kv_set_bool;
}
void mock_lisa_kv_reset(void)
{
mock_lisa_kv_init();
}

View File

@@ -0,0 +1,34 @@
/**
* Copyright (c) 2025, LISTENAI
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include "lisa_kv.h"
#include "fff.h"
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
DECLARE_FAKE_VOID_FUNC(lisa_kv_free, void *);
DECLARE_FAKE_VALUE_FUNC(int, lisa_kv_get_blob, const char *, uint8_t **, int *);
DECLARE_FAKE_VALUE_FUNC(int, lisa_kv_set_blob, const char *, uint8_t *, int);
DECLARE_FAKE_VALUE_FUNC(int, lisa_kv_del, const char *);
DECLARE_FAKE_VALUE_FUNC(int, lisa_kv_get_int, const char *, int *);
DECLARE_FAKE_VALUE_FUNC(int, lisa_kv_set_int, const char *, int);
DECLARE_FAKE_VALUE_FUNC(int, lisa_kv_get_string, const char *, char **);
DECLARE_FAKE_VALUE_FUNC(int, lisa_kv_set_string, const char *, const char *);
DECLARE_FAKE_VALUE_FUNC(int, lisa_kv_get_bool, const char *, bool *);
DECLARE_FAKE_VALUE_FUNC(int, lisa_kv_set_bool, const char *, bool);
void mock_lisa_kv_init(void);
void mock_lisa_kv_reset(void);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,47 @@
/**
* Copyright (c) 2025, LISTENAI
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef int ls_err_t;
typedef enum {
EVENT_WIFI = 0,
EVENT_BT,
EVENT_APP_NETCFG,
EVENT_MAX,
} event_module_t;
typedef ls_err_t (*event_cb_t)(void *arg, event_module_t event_module, int event_id, void *event_data);
typedef struct {
int erro_code;
int status_code;
int reason_code;
} event_connect_fail_param_t;
#define EVENT_WIFI_CONNECTED 1
#define EVENT_WIFI_DISCONNECT 2
#define EVENT_WIFI_STA_CONNECT_FAIL 3
#define EVENT_WIFI_SCAN_DONE 4
#define LS_NEVER_TIMEOUT 0xFFFFFFFFU
ls_err_t ls_event_register_cb(event_module_t event_module_id, int event_id,
event_cb_t event_cb, void *event_cb_arg);
ls_err_t ls_event_unregister_cb(event_module_t event_module_id, int event_id,
event_cb_t event_cb);
ls_err_t ls_event_wait(event_module_t event_module_id, int event_id, uint32_t timeout);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,89 @@
/**
* Copyright (c) 2025, LISTENAI
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "mock_ls_event.h"
#include <string.h>
#define MOCK_EVENT_MAX 8
typedef struct {
event_module_t module;
int event_id;
event_cb_t cb;
void *cb_arg;
} mock_event_entry_t;
static mock_event_entry_t s_entries[MOCK_EVENT_MAX];
DEFINE_FAKE_VALUE_FUNC(ls_err_t, ls_event_register_cb, event_module_t, int, event_cb_t, void *);
DEFINE_FAKE_VALUE_FUNC(ls_err_t, ls_event_unregister_cb, event_module_t, int, event_cb_t);
DEFINE_FAKE_VALUE_FUNC(ls_err_t, ls_event_wait, event_module_t, int, uint32_t);
static ls_err_t custom_event_register(event_module_t event_module_id, int event_id,
event_cb_t event_cb, void *event_cb_arg)
{
for (int i = 0; i < MOCK_EVENT_MAX; i++) {
if (s_entries[i].cb == NULL) {
s_entries[i].module = event_module_id;
s_entries[i].event_id = event_id;
s_entries[i].cb = event_cb;
s_entries[i].cb_arg = event_cb_arg;
return 0;
}
}
return -1;
}
static ls_err_t custom_event_unregister(event_module_t event_module_id, int event_id,
event_cb_t event_cb)
{
for (int i = 0; i < MOCK_EVENT_MAX; i++) {
if (s_entries[i].cb == event_cb &&
s_entries[i].module == event_module_id &&
s_entries[i].event_id == event_id) {
memset(&s_entries[i], 0, sizeof(s_entries[i]));
return 0;
}
}
return -1;
}
static ls_err_t custom_event_wait(event_module_t event_module_id, int event_id, uint32_t timeout)
{
(void)event_module_id;
(void)event_id;
(void)timeout;
return 0;
}
void mock_ls_event_init(void)
{
RESET_FAKE(ls_event_register_cb);
RESET_FAKE(ls_event_unregister_cb);
RESET_FAKE(ls_event_wait);
ls_event_register_cb_fake.custom_fake = custom_event_register;
ls_event_unregister_cb_fake.custom_fake = custom_event_unregister;
ls_event_wait_fake.custom_fake = custom_event_wait;
}
void mock_ls_event_reset(void)
{
mock_ls_event_init();
memset(s_entries, 0, sizeof(s_entries));
}
void mock_ls_event_trigger(event_module_t module, int event_id, void *event_data)
{
for (int i = 0; i < MOCK_EVENT_MAX; i++) {
if (s_entries[i].cb &&
s_entries[i].module == module &&
s_entries[i].event_id == event_id) {
s_entries[i].cb(s_entries[i].cb_arg, module, event_id, event_data);
}
}
}

View File

@@ -0,0 +1,26 @@
/**
* Copyright (c) 2025, LISTENAI
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include "ls_event.h"
#include "fff.h"
#ifdef __cplusplus
extern "C" {
#endif
DECLARE_FAKE_VALUE_FUNC(ls_err_t, ls_event_register_cb, event_module_t, int, event_cb_t, void *);
DECLARE_FAKE_VALUE_FUNC(ls_err_t, ls_event_unregister_cb, event_module_t, int, event_cb_t);
DECLARE_FAKE_VALUE_FUNC(ls_err_t, ls_event_wait, event_module_t, int, uint32_t);
void mock_ls_event_init(void);
void mock_ls_event_reset(void);
void mock_ls_event_trigger(event_module_t module, int event_id, void *event_data);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,9 @@
target_sources(${EXECUTABLE_NAME} PRIVATE
mock_mem_ops.c
)
target_include_directories(${EXECUTABLE_NAME} PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}
${PRJ_ROOT}/include/priv/port/mem
)

View File

@@ -0,0 +1,87 @@
/**
* @file mock_mem_ops.c
* @brief WiFi管理器测试中使用的内存操作实现
*/
#include "mock_mem_ops.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
DEFINE_FAKE_VALUE_FUNC(void *, mock_malloc, size_t);
DEFINE_FAKE_VALUE_FUNC(void *, mock_calloc, size_t, size_t);
DEFINE_FAKE_VALUE_FUNC(void *, mock_align_malloc, size_t, size_t);
DEFINE_FAKE_VALUE_FUNC(void *, mock_nocache_malloc, size_t);
DEFINE_FAKE_VOID_FUNC(mock_free, void *);
static wifi_manager_mem_ops_t mem_ops = {
.malloc = mock_malloc,
.calloc = mock_calloc,
.align_malloc = mock_align_malloc,
.nocache_malloc = mock_nocache_malloc,
.free = mock_free
};
void mock_mem_ops_reset(void)
{
mock_mem_ops_init();
}
/**
* @brief 测试用的内存分配函数
*/
static void* custom_mock_malloc(size_t size) {
return malloc(size);
}
/**
* @brief 测试用的内存清零分配函数
*/
static void* custom_mock_calloc(size_t nmemb, size_t size) {
void *ptr = malloc(nmemb * size);
if (ptr) {
memset(ptr, 0, nmemb * size);
}
return ptr;
}
/**
* @brief 测试用的对齐内存分配函数
*/
static void* custom_mock_align_malloc(size_t align, size_t size) {
// 简单实现,不考虑对齐
return malloc(size);
}
/**
* @brief 测试用的无缓存内存分配函数
*/
static void* custom_mock_nocache_malloc(size_t size) {
return malloc(size);
}
/**
* @brief 测试用的内存释放函数
*/
static void custom_mock_free(void* ptr) {
free(ptr);
}
void mock_mem_ops_init(void)
{
RESET_FAKE(mock_malloc);
RESET_FAKE(mock_calloc);
RESET_FAKE(mock_align_malloc);
RESET_FAKE(mock_nocache_malloc);
RESET_FAKE(mock_free);
mock_malloc_fake.custom_fake = custom_mock_malloc;
mock_calloc_fake.custom_fake = custom_mock_calloc;
mock_align_malloc_fake.custom_fake = custom_mock_align_malloc;
mock_nocache_malloc_fake.custom_fake = custom_mock_nocache_malloc;
mock_free_fake.custom_fake = custom_mock_free;
}
wifi_manager_mem_ops_t* mock_mem_ops_get(void)
{
return &mem_ops;
}

View File

@@ -0,0 +1,22 @@
/**
* @file mock_mem_ops.h
* @brief WiFi管理器测试中使用的内存操作接口
*/
#ifndef MOCK_MEM_OPS_H
#define MOCK_MEM_OPS_H
#include <stdlib.h>
#include "wifi_manager/wifi_manager.h"
#include "fff.h"
DECLARE_FAKE_VALUE_FUNC(void *, mock_malloc, size_t);
DECLARE_FAKE_VALUE_FUNC(void *, mock_calloc, size_t, size_t);
DECLARE_FAKE_VALUE_FUNC(void *, mock_align_malloc, size_t, size_t);
DECLARE_FAKE_VALUE_FUNC(void *, mock_nocache_malloc, size_t);
DECLARE_FAKE_VOID_FUNC(mock_free, void *);
void mock_mem_ops_init(void);
void mock_mem_ops_reset(void);
wifi_manager_mem_ops_t* mock_mem_ops_get(void);
#endif /* MOCK_MEM_OPS_H */

View File

@@ -0,0 +1,9 @@
target_sources(${EXECUTABLE_NAME} PRIVATE
mock_os_ops.c
)
target_include_directories(${EXECUTABLE_NAME} PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}
${PRJ_ROOT}/include/priv/port/os
)

View File

@@ -0,0 +1,737 @@
/**
* @file mock_os_ops.c
* @brief WiFi管理器测试中使用的系统操作实现
*/
// 添加特性宏以支持pthread_mutex_timedlock
#include "mock_os_ops.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <errno.h>
#include <sys/time.h>
#include <time.h>
#include <signal.h>
#define DEBUG_LOG 0
#define LOGD(fmt, args...) do { if (DEBUG_LOG) printf(fmt, ##args); } while (0)
// 全局变量,用于跟踪已创建的所有队列,便于终止时清理
#define MAX_QUEUES 32
static void *g_all_queues[MAX_QUEUES] = {NULL};
static pthread_mutex_t g_queues_mutex = PTHREAD_MUTEX_INITIALIZER;
static int g_shutdown_requested = 0; // 全局终止请求标志
// 互斥锁操作实现
DEFINE_FAKE_VALUE_FUNC(void *, mock_mutex_create);
DEFINE_FAKE_VALUE_FUNC(int, mock_mutex_lock, void *, uint32_t);
DEFINE_FAKE_VALUE_FUNC(int, mock_mutex_unlock, void *);
DEFINE_FAKE_VOID_FUNC(mock_mutex_delete, void *);
// 队列操作实现
DEFINE_FAKE_VALUE_FUNC(void *, mock_queue_create, uint32_t, const char *, size_t);
DEFINE_FAKE_VALUE_FUNC(int, mock_queue_push, void *, const void *, size_t, uint32_t);
DEFINE_FAKE_VALUE_FUNC(int, mock_queue_pop, void *, void *, size_t, uint32_t);
DEFINE_FAKE_VOID_FUNC(mock_queue_delete, void *);
// 线程操作实现
DEFINE_FAKE_VALUE_FUNC(void *, mock_thread_create, wifi_manager_os_thread_attr_t *, wifi_manager_os_thread_entry_t, void *);
DEFINE_FAKE_VOID_FUNC(mock_thread_delete, void *);
// 全局线程追踪
typedef struct linux_thread_t linux_thread_t;
#define MAX_THREADS 32
static linux_thread_t *g_all_threads[MAX_THREADS] = {NULL};
static pthread_mutex_t g_threads_mutex = PTHREAD_MUTEX_INITIALIZER;
// 互斥锁Linux实现
static void *linux_mutex_create(void)
{
pthread_mutex_t *mutex = (pthread_mutex_t *)malloc(sizeof(pthread_mutex_t));
if (mutex == NULL) {
return NULL;
}
if (pthread_mutex_init(mutex, NULL) != 0) {
free(mutex);
return NULL;
}
return mutex;
}
static int linux_mutex_lock(void *mutex, uint32_t timeout)
{
pthread_mutex_t *pmutex = (pthread_mutex_t *)mutex;
// 如果请求了关闭,快速失败
if (g_shutdown_requested) {
return -ECANCELED;
}
if (timeout == 0xFFFFFFFFU) { // LISA_WAIT_FOREVER
LOGD("func: %s, line: %d, lock mutex: %p\n", __FUNCTION__, __LINE__, pmutex);
return pthread_mutex_lock(pmutex);
} else if (timeout == 0) { // LISA_NO_WAIT
LOGD("func: %s, line: %d, lock mutex: %p\n", __FUNCTION__, __LINE__, pmutex);
return pthread_mutex_trylock(pmutex);
} else {
// 有超时的锁定实现 - 使用替代方案
// 一些Linux系统可能不支持pthread_mutex_timedlock,采用轮询方式实现
struct timespec start_time, current_time;
clock_gettime(CLOCK_MONOTONIC, &start_time);
uint64_t timeout_ns = (uint64_t)timeout * 1000000; // 毫秒转纳秒
while (1) {
// 检查是否请求了关闭
if (g_shutdown_requested) {
return -ECANCELED;
}
// 尝试获取锁
int ret = pthread_mutex_trylock(pmutex);
if (ret == 0) {
return 0; // 成功获取锁
}
if (ret != EBUSY) {
return ret; // 其他错误
}
// 检查是否超时
clock_gettime(CLOCK_MONOTONIC, &current_time);
uint64_t elapsed_ns =
(current_time.tv_sec - start_time.tv_sec) * 1000000000ULL +
(current_time.tv_nsec - start_time.tv_nsec);
if (elapsed_ns >= timeout_ns) {
return ETIMEDOUT;
}
// 短暂睡眠避免CPU占用过高
struct timespec sleep_time = {0, 1000000}; // 1ms
nanosleep(&sleep_time, NULL);
}
}
}
static int linux_mutex_unlock(void *mutex)
{
return pthread_mutex_unlock((pthread_mutex_t *)mutex);
}
static void linux_mutex_delete(void *mutex)
{
pthread_mutex_t *pmutex = (pthread_mutex_t *)mutex;
pthread_mutex_destroy(pmutex);
free(pmutex);
}
// 简单队列实现
typedef struct queue_node {
void *data;
size_t size;
struct queue_node *next;
} queue_node_t;
typedef struct {
queue_node_t *head;
queue_node_t *tail;
pthread_mutex_t mutex;
pthread_cond_t cond;
uint32_t count;
uint32_t max_count;
size_t item_size;
char name[32];
int id; // 队列ID
volatile int valid; // 队列是否有效
} linux_queue_t;
// 添加队列到全局列表
static void add_queue_to_list(void *queue)
{
pthread_mutex_lock(&g_queues_mutex);
for (int i = 0; i < MAX_QUEUES; i++) {
if (g_all_queues[i] == NULL) {
g_all_queues[i] = queue;
((linux_queue_t*)queue)->id = i;
break;
}
}
pthread_mutex_unlock(&g_queues_mutex);
}
// 从全局列表中移除队列
static void remove_queue_from_list(void *queue)
{
pthread_mutex_lock(&g_queues_mutex);
for (int i = 0; i < MAX_QUEUES; i++) {
if (g_all_queues[i] == queue) {
g_all_queues[i] = NULL;
break;
}
}
pthread_mutex_unlock(&g_queues_mutex);
}
static void *linux_queue_create(uint32_t queue_length, const char *name, size_t item_size)
{
linux_queue_t *queue = (linux_queue_t *)malloc(sizeof(linux_queue_t));
if (queue == NULL) {
return NULL;
}
queue->head = NULL;
queue->tail = NULL;
queue->count = 0;
queue->max_count = queue_length;
queue->item_size = item_size;
queue->valid = 1; // 标记为有效
if (name != NULL) {
strncpy(queue->name, name, sizeof(queue->name) - 1);
queue->name[sizeof(queue->name) - 1] = '\0';
} else {
queue->name[0] = '\0';
}
if (pthread_mutex_init(&queue->mutex, NULL) != 0) {
free(queue);
return NULL;
}
if (pthread_cond_init(&queue->cond, NULL) != 0) {
pthread_mutex_destroy(&queue->mutex);
free(queue);
return NULL;
}
// 添加到全局队列列表
add_queue_to_list(queue);
mock_queue_create_fake.return_val = queue;
return queue;
}
static int linux_queue_push(void *q, const void *item, size_t item_size, uint32_t timeout)
{
linux_queue_t *queue = (linux_queue_t *)q;
int ret = 0;
LOGD("func: %s, line: %d, lock queue: %p\n", __FUNCTION__, __LINE__, queue);
// 检查退出标志
if (g_shutdown_requested || !queue->valid) {
return -ECANCELED;
}
pthread_mutex_lock(&queue->mutex);
// 重新检查队列是否有效
if (!queue->valid) {
pthread_mutex_unlock(&queue->mutex);
return -ECANCELED;
}
if (queue->count >= queue->max_count) {
if (timeout == 0) { // LISA_NO_WAIT
pthread_mutex_unlock(&queue->mutex);
return -EAGAIN;
} else {
// 等待直到有空间或超时
struct timespec ts;
struct timespec now;
clock_gettime(CLOCK_REALTIME, &now);
// 如果是无限等待,设置一个较短的超时时间,以便定期检查退出标志
if (timeout == 0xFFFFFFFFU) {
timeout = 100; // 100毫秒
}
ts.tv_sec = now.tv_sec + (timeout / 1000);
ts.tv_nsec = now.tv_nsec + ((timeout % 1000) * 1000000);
if (ts.tv_nsec >= 1000000000) {
ts.tv_sec++;
ts.tv_nsec -= 1000000000;
}
while (queue->count >= queue->max_count) {
ret = pthread_cond_timedwait(&queue->cond, &queue->mutex, &ts);
// 检查退出标志
if (g_shutdown_requested || !queue->valid) {
pthread_mutex_unlock(&queue->mutex);
return -ECANCELED;
}
if (ret != 0) {
pthread_mutex_unlock(&queue->mutex);
return -ETIMEDOUT;
}
}
}
}
queue_node_t *node = (queue_node_t *)malloc(sizeof(queue_node_t));
if (node == NULL) {
pthread_mutex_unlock(&queue->mutex);
return -ENOMEM;
}
node->data = malloc(item_size);
if (node->data == NULL) {
free(node);
pthread_mutex_unlock(&queue->mutex);
return -ENOMEM;
}
memcpy(node->data, item, item_size);
node->size = item_size;
node->next = NULL;
if (queue->tail == NULL) {
queue->head = queue->tail = node;
} else {
queue->tail->next = node;
queue->tail = node;
}
queue->count++;
pthread_cond_signal(&queue->cond);
pthread_mutex_unlock(&queue->mutex);
return 0;
}
static int linux_queue_pop(void *q, void *item, size_t item_size, uint32_t timeout)
{
linux_queue_t *queue = (linux_queue_t *)q;
int ret = 0;
LOGD("func: %s, line: %d, lock queue: %p, timeout: %d\n", __FUNCTION__, __LINE__, queue, timeout);
// 检查退出标志
if (g_shutdown_requested || !queue->valid) {
return -ECANCELED;
}
pthread_mutex_lock(&queue->mutex);
LOGD("func: %s, line: %d, finally lock queue: %p\n", __FUNCTION__, __LINE__, queue);
// 重新检查队列是否有效
if (!queue->valid) {
pthread_mutex_unlock(&queue->mutex);
return -ECANCELED;
}
if (queue->count == 0) {
if (timeout == 0) { // LISA_NO_WAIT
pthread_mutex_unlock(&queue->mutex);
LOGD("func: %s, line: %d, unlock queue: %p\n", __FUNCTION__, __LINE__, queue);
return -EAGAIN;
} else {
LOGD("func: %s, line: %d, wait queue timeout: %d\n", __FUNCTION__, __LINE__, timeout);
// 定义一个较短的等待周期,以便定期检查退出标志
uint32_t wait_interval = 100; // 100毫秒
uint32_t remaining_timeout = timeout;
// 主等待循环
while (queue->count == 0) {
// 检查退出标志
if (g_shutdown_requested || !queue->valid) {
pthread_mutex_unlock(&queue->mutex);
return -ECANCELED;
}
// 计算本次等待的超时时间
uint32_t current_timeout = (timeout == 0xFFFFFFFFU) ?
wait_interval :
(remaining_timeout < wait_interval ? remaining_timeout : wait_interval);
// 设置超时时间
struct timespec ts;
struct timespec now;
clock_gettime(CLOCK_REALTIME, &now);
ts.tv_sec = now.tv_sec + (current_timeout / 1000);
ts.tv_nsec = now.tv_nsec + ((current_timeout % 1000) * 1000000);
if (ts.tv_nsec >= 1000000000) {
ts.tv_sec++;
ts.tv_nsec -= 1000000000;
}
// 等待
ret = pthread_cond_timedwait(&queue->cond, &queue->mutex, &ts);
// 再次检查退出标志
if (g_shutdown_requested || !queue->valid) {
pthread_mutex_unlock(&queue->mutex);
return -ECANCELED;
}
// 如果有数据了,跳出循环
if (queue->count > 0) {
break;
}
// 检查超时
if (ret != 0 && timeout != 0xFFFFFFFFU) {
// 更新剩余超时时间
if (remaining_timeout <= wait_interval) {
// 已经超时
pthread_mutex_unlock(&queue->mutex);
LOGD("func: %s, line: %d, unlock queue: %p\n", __FUNCTION__, __LINE__, queue);
return -ETIMEDOUT;
}
remaining_timeout -= wait_interval;
}
}
}
}
queue_node_t *node = queue->head;
LOGD("func: %s, line: %d, node: %p\n", __FUNCTION__, __LINE__, node);
if (node == NULL) {
pthread_mutex_unlock(&queue->mutex);
return -EAGAIN;
}
LOGD("func: %s, line: %d, copy_size: %d\n", __FUNCTION__, __LINE__, node->size);
size_t copy_size = (item_size < node->size) ? item_size : node->size;
memcpy(item, node->data, copy_size);
queue->head = node->next;
if (queue->head == NULL) {
queue->tail = NULL;
}
LOGD("func: %s, line: %d, count: %d\n", __FUNCTION__, __LINE__, queue->count);
queue->count--;
free(node->data);
free(node);
LOGD("func: %s, line: %d, unlock queue: %p\n", __FUNCTION__, __LINE__, queue);
pthread_cond_signal(&queue->cond);
pthread_mutex_unlock(&queue->mutex);
return 0;
}
// 安全清理队列内存,不获取互斥锁
static void linux_queue_cleanup_unsafe(linux_queue_t *queue)
{
queue_node_t *node, *next;
// 标记队列为无效
queue->valid = 0;
// 清理队列中的所有节点
for (node = queue->head; node != NULL; node = next) {
next = node->next;
free(node->data);
free(node);
}
queue->head = NULL;
queue->tail = NULL;
// 唤醒所有等待的线程
pthread_cond_broadcast(&queue->cond);
}
static void linux_queue_delete(void *q)
{
linux_queue_t *queue = (linux_queue_t *)q;
if (!queue) return;
LOGD("func: %s, line: %d, lock queue: %p\n", __FUNCTION__, __LINE__, queue);
// 从全局队列列表中移除
remove_queue_from_list(queue);
// 标记队列为无效
queue->valid = 0;
// 尝试获取锁,但不阻塞
if (pthread_mutex_trylock(&queue->mutex) == 0) {
// 成功获得锁,清理队列
linux_queue_cleanup_unsafe(queue);
pthread_mutex_unlock(&queue->mutex);
} else {
// 无法获得锁,发送广播信号让等待的线程退出
pthread_cond_broadcast(&queue->cond);
}
LOGD("func: %s, line: %d\n", __FUNCTION__, __LINE__);
// 延迟销毁互斥锁和条件变量
usleep(10000); // 等待10毫秒,让所有线程有时间退出
pthread_mutex_destroy(&queue->mutex);
pthread_cond_destroy(&queue->cond);
free(queue);
}
// 线程实现
struct linux_thread_t {
pthread_t thread;
wifi_manager_os_thread_entry_t entry;
void *arg;
volatile int running;
pthread_mutex_t mutex; // 保护running标志的互斥锁
int id; // 线程ID
};
// 添加线程到全局列表
static void add_thread_to_list(linux_thread_t *thread)
{
pthread_mutex_lock(&g_threads_mutex);
for (int i = 0; i < MAX_THREADS; i++) {
if (g_all_threads[i] == NULL) {
g_all_threads[i] = thread;
thread->id = i;
break;
}
}
pthread_mutex_unlock(&g_threads_mutex);
}
// 从全局列表中移除线程
static void remove_thread_from_list(linux_thread_t *thread)
{
pthread_mutex_lock(&g_threads_mutex);
for (int i = 0; i < MAX_THREADS; i++) {
if (g_all_threads[i] == thread) {
g_all_threads[i] = NULL;
break;
}
}
pthread_mutex_unlock(&g_threads_mutex);
}
static void *thread_wrapper(void *arg)
{
linux_thread_t *thread = (linux_thread_t *)arg;
// 设置取消类型为异步取消,允许在任何点取消线程
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
// 检查全局终止标志
if (g_shutdown_requested) {
pthread_mutex_lock(&thread->mutex);
thread->running = 0;
pthread_mutex_unlock(&thread->mutex);
return NULL;
}
// 执行用户函数
thread->entry(thread->arg);
// 标记线程已完成
pthread_mutex_lock(&thread->mutex);
thread->running = 0;
pthread_mutex_unlock(&thread->mutex);
return NULL;
}
static void *linux_thread_create(wifi_manager_os_thread_attr_t *attr, wifi_manager_os_thread_entry_t entry, void *arg)
{
linux_thread_t *thread = (linux_thread_t *)malloc(sizeof(linux_thread_t));
if (thread == NULL) {
return NULL;
}
thread->entry = entry;
thread->arg = arg;
thread->running = 1;
// 初始化互斥锁
if (pthread_mutex_init(&thread->mutex, NULL) != 0) {
free(thread);
return NULL;
}
pthread_attr_t pthread_attr;
pthread_attr_init(&pthread_attr);
if (attr != NULL) {
if (attr->stack_size > 0) {
pthread_attr_setstacksize(&pthread_attr, attr->stack_size);
}
// Linux优先级设置较复杂,这里简化处理
}
// 添加到全局线程列表
add_thread_to_list(thread);
int ret = pthread_create(&thread->thread, &pthread_attr, thread_wrapper, thread);
pthread_attr_destroy(&pthread_attr);
if (ret != 0) {
pthread_mutex_destroy(&thread->mutex);
remove_thread_from_list(thread);
free(thread);
return NULL;
}
return thread;
}
static void linux_thread_delete(void *t)
{
linux_thread_t *thread = (linux_thread_t *)t;
if (thread == NULL) {
return;
}
// 从全局线程列表中移除
remove_thread_from_list(thread);
// 检查线程是否仍在运行
pthread_mutex_lock(&thread->mutex);
int running = thread->running;
pthread_mutex_unlock(&thread->mutex);
if (running) {
// 先尝试发送取消信号
pthread_cancel(thread->thread);
// 等待线程完成,但设置超时避免永久阻塞
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
ts.tv_sec += 1; // 1秒超时
int ret = pthread_timedjoin_np(thread->thread, NULL, &ts);
if (ret != 0) {
// 如果超时,发送SIGUSR1信号强制终止线程
pthread_kill(thread->thread, SIGUSR1);
// 再次尝试等待,但不阻塞
pthread_tryjoin_np(thread->thread, NULL);
}
}
// 清理资源
pthread_mutex_destroy(&thread->mutex);
free(thread);
}
// 清理所有资源的函数
static void cleanup_all_resources(void)
{
g_shutdown_requested = 1;
// 关闭所有队列
pthread_mutex_lock(&g_queues_mutex);
for (int i = 0; i < MAX_QUEUES; i++) {
linux_queue_t *queue = (linux_queue_t*)g_all_queues[i];
if (queue) {
// 标记队列为无效
queue->valid = 0;
// 广播唤醒所有等待的线程
pthread_cond_broadcast(&queue->cond);
// 不要在这里销毁队列,让每个队列的delete函数处理
g_all_queues[i] = NULL;
}
}
pthread_mutex_unlock(&g_queues_mutex);
// 让队列线程有时间响应退出
usleep(100000); // 100毫秒
// 取消所有线程
pthread_mutex_lock(&g_threads_mutex);
for (int i = 0; i < MAX_THREADS; i++) {
linux_thread_t *thread = g_all_threads[i];
if (thread) {
if (thread->running) {
pthread_cancel(thread->thread);
// 不要在这里等待线程,让每个线程的delete函数处理
}
g_all_threads[i] = NULL;
}
}
pthread_mutex_unlock(&g_threads_mutex);
}
static wifi_manager_os_ops_t os_ops = {
// 互斥锁操作
.mutex_create = mock_mutex_create,
.mutex_lock = mock_mutex_lock,
.mutex_unlock = mock_mutex_unlock,
.mutex_delete = mock_mutex_delete,
// 队列操作
.queue_create = mock_queue_create,
.queue_push = mock_queue_push,
.queue_pop = mock_queue_pop,
.queue_delete = mock_queue_delete,
// 线程操作
.thread_create = mock_thread_create,
.thread_delete = mock_thread_delete
};
void mock_os_ops_init(void)
{
// 初始化全局变量
g_shutdown_requested = 0;
// 重置所有模拟函数
RESET_FAKE(mock_mutex_create);
RESET_FAKE(mock_mutex_lock);
RESET_FAKE(mock_mutex_unlock);
RESET_FAKE(mock_mutex_delete);
RESET_FAKE(mock_queue_create);
RESET_FAKE(mock_queue_push);
RESET_FAKE(mock_queue_pop);
RESET_FAKE(mock_queue_delete);
RESET_FAKE(mock_thread_create);
RESET_FAKE(mock_thread_delete);
// 配置使用Linux实现
mock_mutex_create_fake.custom_fake = linux_mutex_create;
mock_mutex_lock_fake.custom_fake = linux_mutex_lock;
mock_mutex_unlock_fake.custom_fake = linux_mutex_unlock;
mock_mutex_delete_fake.custom_fake = linux_mutex_delete;
mock_queue_create_fake.custom_fake = linux_queue_create;
mock_queue_push_fake.custom_fake = linux_queue_push;
mock_queue_pop_fake.custom_fake = linux_queue_pop;
mock_queue_delete_fake.custom_fake = linux_queue_delete;
mock_thread_create_fake.custom_fake = linux_thread_create;
mock_thread_delete_fake.custom_fake = linux_thread_delete;
}
void mock_os_ops_reset(void)
{
// 清理所有资源
cleanup_all_resources();
// 重新初始化
mock_os_ops_init();
}
wifi_manager_os_ops_t* mock_os_ops_get(void)
{
return &os_ops;
}

View File

@@ -0,0 +1,31 @@
/**
* @file mock_os_ops.h
* @brief WiFi管理器测试中使用的系统操作接口
*/
#ifndef MOCK_OS_OPS_H
#define MOCK_OS_OPS_H
#include "wifi_manager/wifi_manager_os_ops.h"
#include "fff.h"
// 互斥锁操作
DECLARE_FAKE_VALUE_FUNC(void *, mock_mutex_create);
DECLARE_FAKE_VALUE_FUNC(int, mock_mutex_lock, void *, uint32_t);
DECLARE_FAKE_VALUE_FUNC(int, mock_mutex_unlock, void *);
DECLARE_FAKE_VOID_FUNC(mock_mutex_delete, void *);
// 队列操作
DECLARE_FAKE_VALUE_FUNC(void *, mock_queue_create, uint32_t, const char *, size_t);
DECLARE_FAKE_VALUE_FUNC(int, mock_queue_push, void *, const void *, size_t, uint32_t);
DECLARE_FAKE_VALUE_FUNC(int, mock_queue_pop, void *, void *, size_t, uint32_t);
DECLARE_FAKE_VOID_FUNC(mock_queue_delete, void *);
// 线程操作
DECLARE_FAKE_VALUE_FUNC(void *, mock_thread_create, wifi_manager_os_thread_attr_t *, wifi_manager_os_thread_entry_t, void *);
DECLARE_FAKE_VOID_FUNC(mock_thread_delete, void *);
void mock_os_ops_init(void);
void mock_os_ops_reset(void);
wifi_manager_os_ops_t* mock_os_ops_get(void);
#endif /* MOCK_OS_OPS_H */

View File

@@ -0,0 +1,10 @@
target_sources(${EXECUTABLE_NAME} PRIVATE
mock_platform_dev.c
)
target_include_directories(${EXECUTABLE_NAME} PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/../fake_nvs
${PRJ_ROOT}/include/wifi_manager
)

View File

@@ -0,0 +1,21 @@
/**
* Copyright (c) 2025, LISTENAI
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "mock_platform_dev.h"
DEFINE_FAKE_VALUE_FUNC(void *, platform_get_flash_dev);
void mock_platform_dev_init(void)
{
RESET_FAKE(platform_get_flash_dev);
}
void mock_platform_dev_reset(void)
{
mock_platform_dev_init();
}

View File

@@ -0,0 +1,25 @@
/**
* Copyright (c) 2025, LISTENAI
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include "nvs.h"
#include "fff.h"
#ifdef __cplusplus
extern "C" {
#endif
DECLARE_FAKE_VALUE_FUNC(void *, platform_get_flash_dev);
void mock_platform_dev_init(void);
void mock_platform_dev_reset(void);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,8 @@
target_sources(${EXECUTABLE_NAME} PRIVATE
mock_wifi_ops.c
)
target_include_directories(${EXECUTABLE_NAME} PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}
)

View File

@@ -0,0 +1,86 @@
/**
* @file mock_wifi_ops.c
* @brief WiFi管理器测试中使用的WiFi操作实现
*/
#include "mock_wifi_ops.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
DEFINE_FAKE_VALUE_FUNC(int, mock_wifi_init);
DEFINE_FAKE_VALUE_FUNC(int, mock_wifi_deinit);
DEFINE_FAKE_VALUE_FUNC(int, mock_wifi_sta_enable);
DEFINE_FAKE_VALUE_FUNC(int, mock_wifi_sta_disable);
DEFINE_FAKE_VALUE_FUNC(bool, mock_wifi_sta_is_enable);
DEFINE_FAKE_VALUE_FUNC(int, mock_wifi_add_callback, wifi_mgr_wifi_event_cb_t *);
DEFINE_FAKE_VALUE_FUNC(int, mock_wifi_remove_callback, wifi_mgr_wifi_event_cb_t *);
DEFINE_FAKE_VALUE_FUNC(int, mock_wifi_scan_ap, wifi_mgr_wifi_scan_info_t *, uint32_t, uint32_t);
DEFINE_FAKE_VALUE_FUNC(int, mock_wifi_sta_connect, wifi_mgr_wifi_sta_config_t *, uint32_t);
DEFINE_FAKE_VALUE_FUNC(int, mock_wifi_sta_disconnect, uint32_t);
DEFINE_FAKE_VALUE_FUNC(wifi_mgr_wifi_status_t, mock_wifi_sta_get_status);
DEFINE_FAKE_VALUE_FUNC(int, mock_wifi_get_mac, uint8_t *);
static wifi_manager_wifi_ops_t wifi_ops = {
.init = mock_wifi_init,
.deinit = mock_wifi_deinit,
.sta_enable = mock_wifi_sta_enable,
.sta_disable = mock_wifi_sta_disable,
.sta_is_enable = mock_wifi_sta_is_enable,
.add_callback = mock_wifi_add_callback,
.remove_callback = mock_wifi_remove_callback,
.scan_ap = mock_wifi_scan_ap,
.sta_connect = mock_wifi_sta_connect,
.sta_disconnect = mock_wifi_sta_disconnect,
.sta_get_status = mock_wifi_sta_get_status,
.get_mac = mock_wifi_get_mac,
};
static bool mock_wifi_is_enable = false;
static int custom_mock_wifi_sta_enable(void)
{
mock_wifi_is_enable = true;
return 0;
}
static int custom_mock_wifi_sta_disable(void)
{
mock_wifi_is_enable = false;
return 0;
}
static bool custom_mock_wifi_sta_is_enable(void)
{
return mock_wifi_is_enable;
}
void mock_wifi_ops_reset(void)
{
mock_wifi_ops_init();
}
void mock_wifi_ops_init(void)
{
RESET_FAKE(mock_wifi_init);
RESET_FAKE(mock_wifi_deinit);
RESET_FAKE(mock_wifi_sta_enable);
RESET_FAKE(mock_wifi_sta_disable);
RESET_FAKE(mock_wifi_sta_is_enable);
RESET_FAKE(mock_wifi_add_callback);
RESET_FAKE(mock_wifi_remove_callback);
RESET_FAKE(mock_wifi_scan_ap);
RESET_FAKE(mock_wifi_sta_connect);
RESET_FAKE(mock_wifi_sta_disconnect);
RESET_FAKE(mock_wifi_sta_get_status);
RESET_FAKE(mock_wifi_get_mac);
mock_wifi_sta_enable_fake.custom_fake = custom_mock_wifi_sta_enable;
mock_wifi_sta_disable_fake.custom_fake = custom_mock_wifi_sta_disable;
mock_wifi_sta_is_enable_fake.custom_fake = custom_mock_wifi_sta_is_enable;
}
wifi_manager_wifi_ops_t* mock_wifi_ops_get(void)
{
return &wifi_ops;
}

View File

@@ -0,0 +1,30 @@
/**
* @file mock_wifi_ops.h
* @brief WiFi管理器测试中使用的WiFi操作接口
*/
#ifndef MOCK_WIFI_OPS_H
#define MOCK_WIFI_OPS_H
#include <stdint.h>
#include <stdbool.h>
#include "wifi_manager/wifi_manager_wifi_ops.h"
#include "fff.h"
DECLARE_FAKE_VALUE_FUNC(int, mock_wifi_init);
DECLARE_FAKE_VALUE_FUNC(int, mock_wifi_deinit);
DECLARE_FAKE_VALUE_FUNC(int, mock_wifi_sta_enable);
DECLARE_FAKE_VALUE_FUNC(int, mock_wifi_sta_disable);
DECLARE_FAKE_VALUE_FUNC(bool, mock_wifi_sta_is_enable);
DECLARE_FAKE_VALUE_FUNC(int, mock_wifi_add_callback, wifi_mgr_wifi_event_cb_t *);
DECLARE_FAKE_VALUE_FUNC(int, mock_wifi_remove_callback, wifi_mgr_wifi_event_cb_t *);
DECLARE_FAKE_VALUE_FUNC(int, mock_wifi_scan_ap, wifi_mgr_wifi_scan_info_t *, uint32_t, uint32_t);
DECLARE_FAKE_VALUE_FUNC(int, mock_wifi_sta_connect, wifi_mgr_wifi_sta_config_t *, uint32_t);
DECLARE_FAKE_VALUE_FUNC(int, mock_wifi_sta_disconnect, uint32_t);
DECLARE_FAKE_VALUE_FUNC(wifi_mgr_wifi_status_t, mock_wifi_sta_get_status);
DECLARE_FAKE_VALUE_FUNC(int, mock_wifi_get_mac, uint8_t *);
void mock_wifi_ops_init(void);
void mock_wifi_ops_reset(void);
wifi_manager_wifi_ops_t* mock_wifi_ops_get(void);
#endif /* MOCK_WIFI_OPS_H */

View File

@@ -0,0 +1,182 @@
/**
* Copyright (c) 2025, LISTENAI
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "mock_wifi_api.h"
#include <string.h>
static uint8_t s_pmk[32];
static bool s_pmk_valid = false;
static wifi_link_status_t s_link_status = {0};
static bool s_link_status_valid = false;
static wifi_connect_cfg_t s_last_connect_cfg;
static bool s_has_last_connect_cfg = false;
DEFINE_FAKE_VALUE_FUNC(ls_err_t, wifi_get_pmk, uint8_t *);
DEFINE_FAKE_VALUE_FUNC(ls_err_t, wifi_set_pmk, const uint8_t *);
DEFINE_FAKE_VALUE_FUNC(ls_err_t, wifi_get_link_status, wifi_link_status_t *);
DEFINE_FAKE_VALUE_FUNC(ls_err_t, wifi_sta_connect, wifi_connect_cfg_t *);
DEFINE_FAKE_VALUE_FUNC0(ls_err_t, wifi_sta_disconnect);
DEFINE_FAKE_VALUE_FUNC0(ls_err_t, wifi_sta_mode_enable);
DEFINE_FAKE_VALUE_FUNC0(ls_err_t, wifi_sta_mode_disable);
DEFINE_FAKE_VALUE_FUNC(ls_err_t, wifi_get_mode, int, wifi_mode_e *);
DEFINE_FAKE_VALUE_FUNC(ls_err_t, wifi_scan_start, wifi_scan_params_t *);
DEFINE_FAKE_VALUE_FUNC(ls_err_t, wifi_sta_scanlist_dump, wifi_scan_result_t *, int, int *);
DEFINE_FAKE_VALUE_FUNC(ls_err_t, wifi_get_sta_mac, uint8_t *);
static ls_err_t custom_wifi_get_pmk(uint8_t *pmk)
{
if (!s_pmk_valid || pmk == NULL) {
return -1;
}
memcpy(pmk, s_pmk, sizeof(s_pmk));
return LS_OK;
}
static ls_err_t custom_wifi_set_pmk(const uint8_t *pmk)
{
(void)pmk;
return LS_OK;
}
static ls_err_t custom_wifi_get_link_status(wifi_link_status_t *link_status)
{
if (!s_link_status_valid || link_status == NULL) {
return -1;
}
*link_status = s_link_status;
return LS_OK;
}
static ls_err_t custom_wifi_sta_connect(wifi_connect_cfg_t *cfg)
{
if (cfg) {
s_last_connect_cfg = *cfg;
s_has_last_connect_cfg = true;
} else {
memset(&s_last_connect_cfg, 0, sizeof(s_last_connect_cfg));
s_has_last_connect_cfg = false;
}
return LS_OK;
}
static ls_err_t custom_wifi_sta_disconnect(void)
{
return LS_OK;
}
static ls_err_t custom_wifi_sta_mode_enable(void)
{
return LS_OK;
}
static ls_err_t custom_wifi_sta_mode_disable(void)
{
return LS_OK;
}
static ls_err_t custom_wifi_get_mode(int vif_idx, wifi_mode_e *mode)
{
(void)vif_idx;
if (mode) {
*mode = WIFI_MODE_STA;
}
return LS_OK;
}
static ls_err_t custom_wifi_scan_start(wifi_scan_params_t *param)
{
(void)param;
return LS_OK;
}
static ls_err_t custom_wifi_sta_scanlist_dump(wifi_scan_result_t *results, int size, int *out_num)
{
(void)results;
(void)size;
if (out_num) {
*out_num = 0;
}
return LS_OK;
}
static ls_err_t custom_wifi_get_sta_mac(uint8_t *mac_addr)
{
if (mac_addr) {
memset(mac_addr, 0, 6);
}
return LS_OK;
}
void mock_wifi_api_init(void)
{
RESET_FAKE(wifi_get_pmk);
RESET_FAKE(wifi_set_pmk);
RESET_FAKE(wifi_get_link_status);
RESET_FAKE(wifi_sta_connect);
RESET_FAKE(wifi_sta_disconnect);
RESET_FAKE(wifi_sta_mode_enable);
RESET_FAKE(wifi_sta_mode_disable);
RESET_FAKE(wifi_get_mode);
RESET_FAKE(wifi_scan_start);
RESET_FAKE(wifi_sta_scanlist_dump);
RESET_FAKE(wifi_get_sta_mac);
wifi_get_pmk_fake.custom_fake = custom_wifi_get_pmk;
wifi_set_pmk_fake.custom_fake = custom_wifi_set_pmk;
wifi_get_link_status_fake.custom_fake = custom_wifi_get_link_status;
wifi_sta_connect_fake.custom_fake = custom_wifi_sta_connect;
wifi_sta_disconnect_fake.custom_fake = custom_wifi_sta_disconnect;
wifi_sta_mode_enable_fake.custom_fake = custom_wifi_sta_mode_enable;
wifi_sta_mode_disable_fake.custom_fake = custom_wifi_sta_mode_disable;
wifi_get_mode_fake.custom_fake = custom_wifi_get_mode;
wifi_scan_start_fake.custom_fake = custom_wifi_scan_start;
wifi_sta_scanlist_dump_fake.custom_fake = custom_wifi_sta_scanlist_dump;
wifi_get_sta_mac_fake.custom_fake = custom_wifi_get_sta_mac;
}
void mock_wifi_api_set_pmk(const uint8_t *pmk, bool valid)
{
if (pmk && valid) {
memcpy(s_pmk, pmk, sizeof(s_pmk));
s_pmk_valid = true;
} else {
memset(s_pmk, 0, sizeof(s_pmk));
s_pmk_valid = false;
}
}
void mock_wifi_api_set_link_status(const wifi_link_status_t *status)
{
if (status) {
s_link_status = *status;
s_link_status_valid = true;
} else {
memset(&s_link_status, 0, sizeof(s_link_status));
s_link_status_valid = false;
}
}
void mock_wifi_api_reset(void)
{
mock_wifi_api_init();
memset(s_pmk, 0, sizeof(s_pmk));
s_pmk_valid = false;
memset(&s_link_status, 0, sizeof(s_link_status));
s_link_status_valid = false;
memset(&s_last_connect_cfg, 0, sizeof(s_last_connect_cfg));
s_has_last_connect_cfg = false;
}
const wifi_connect_cfg_t *mock_wifi_api_get_last_connect_cfg(void)
{
return s_has_last_connect_cfg ? &s_last_connect_cfg : NULL;
}
bool mock_wifi_api_has_last_connect_cfg(void)
{
return s_has_last_connect_cfg;
}

View File

@@ -0,0 +1,40 @@
/**
* Copyright (c) 2025, LISTENAI
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include <stdint.h>
#include <stdbool.h>
#include "wifi_api.h"
#include "fff.h"
#ifdef __cplusplus
extern "C" {
#endif
DECLARE_FAKE_VALUE_FUNC(ls_err_t, wifi_get_pmk, uint8_t *);
DECLARE_FAKE_VALUE_FUNC(ls_err_t, wifi_set_pmk, const uint8_t *);
DECLARE_FAKE_VALUE_FUNC(ls_err_t, wifi_get_link_status, wifi_link_status_t *);
DECLARE_FAKE_VALUE_FUNC(ls_err_t, wifi_sta_connect, wifi_connect_cfg_t *);
DECLARE_FAKE_VALUE_FUNC0(ls_err_t, wifi_sta_disconnect);
DECLARE_FAKE_VALUE_FUNC0(ls_err_t, wifi_sta_mode_enable);
DECLARE_FAKE_VALUE_FUNC0(ls_err_t, wifi_sta_mode_disable);
DECLARE_FAKE_VALUE_FUNC(ls_err_t, wifi_get_mode, int, wifi_mode_e *);
DECLARE_FAKE_VALUE_FUNC(ls_err_t, wifi_scan_start, wifi_scan_params_t *);
DECLARE_FAKE_VALUE_FUNC(ls_err_t, wifi_sta_scanlist_dump, wifi_scan_result_t *, int, int *);
DECLARE_FAKE_VALUE_FUNC(ls_err_t, wifi_get_sta_mac, uint8_t *);
void mock_wifi_api_init(void);
void mock_wifi_api_set_pmk(const uint8_t *pmk, bool valid);
void mock_wifi_api_set_link_status(const wifi_link_status_t *status);
void mock_wifi_api_reset(void);
const wifi_connect_cfg_t *mock_wifi_api_get_last_connect_cfg(void);
bool mock_wifi_api_has_last_connect_cfg(void);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,96 @@
/**
* Copyright (c) 2025, LISTENAI
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include <stdint.h>
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef int ls_err_t;
#define LS_OK 0
#define WIFI_VIF_DEFAULT_IDX 0
typedef enum {
WIFI_MODE_NULL = 0,
WIFI_MODE_STA,
} wifi_mode_e;
#define WIFI_SSID_LEN 32
typedef enum {
WIFI_SEC_UNKNOWN = 0,
WIFI_SEC_AUTO,
WIFI_SEC_OPEN,
WIFI_SEC_WEP,
WIFI_SEC_WPA_PSK,
WIFI_SEC_WPA2_PSK,
WIFI_SEC_WPA_PSK_WPA2_PSK,
WIFI_SEC_WPA_ENTERPRISE,
WIFI_SEC_WPA3_SAE,
WIFI_SEC_WPA2_PSK_WPA3_SAE,
} wifi_security_e;
typedef struct {
char ssid[32];
} wifi_scan_params_t;
typedef struct {
char ssid[32];
uint8_t bssid[6];
int rssi;
int channel;
wifi_security_e auth;
} wifi_scan_result_t;
typedef struct {
uint8_t ssid[32];
uint8_t key[64];
uint8_t bssid[6];
uint16_t freq[2];
wifi_security_e sec;
uint8_t failure_retry_cnt;
} wifi_connect_cfg_t;
struct wifi_link_status {
uint8_t is_connected;
uint8_t bssid[6];
int rssi;
int noise;
char ssid[WIFI_SSID_LEN + 1];
int channel;
int state;
uint16_t aid;
};
typedef struct wifi_link_status wifi_link_status_t;
typedef enum {
STA_INACTIVE = 0,
STA_IN_CONNECTING,
STA_CONNECTED,
STA_DISCONNECTED,
} wifi_link_state_t;
ls_err_t wifi_get_pmk(uint8_t *pmk);
ls_err_t wifi_set_pmk(const uint8_t *pmk);
ls_err_t wifi_get_link_status(wifi_link_status_t *link_status);
ls_err_t wifi_sta_connect(wifi_connect_cfg_t *cfg);
ls_err_t wifi_sta_disconnect(void);
ls_err_t wifi_sta_mode_enable(void);
ls_err_t wifi_sta_mode_disable(void);
ls_err_t wifi_get_mode(int vif_idx, wifi_mode_e *mode);
ls_err_t wifi_scan_start(wifi_scan_params_t *param);
ls_err_t wifi_sta_scanlist_dump(wifi_scan_result_t *results, int size, int *out_num);
ls_err_t wifi_get_sta_mac(uint8_t *mac_addr);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,9 @@
target_sources(${EXECUTABLE_NAME} PRIVATE
mock_wifi_manager_storage.c
)
target_include_directories(${EXECUTABLE_NAME} PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}
${PRJ_ROOT}/include/wifi_manager
)

View File

@@ -0,0 +1,32 @@
/**
* Copyright (c) 2023, LISTENAI
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "mock_wifi_manager_storage.h"
DEFINE_FAKE_VALUE_FUNC(int, wifi_storage_init, wifi_storage_ctx_t *, wifi_storage_ops_t *, void *);
DEFINE_FAKE_VALUE_FUNC(int, wifi_storage_deinit, wifi_storage_ctx_t *);
DEFINE_FAKE_VALUE_FUNC(int, wifi_storage_save_ap, wifi_storage_ctx_t *, wifi_mgr_sta_config_t *);
DEFINE_FAKE_VALUE_FUNC(int, wifi_storage_delete_ap, wifi_storage_ctx_t *, wifi_mgr_sta_config_t *);
DEFINE_FAKE_VALUE_FUNC(int, wifi_storage_search_ap, wifi_storage_ctx_t *, wifi_mgr_sta_config_t **,
wifi_mgr_storage_search_mode_t, void *);
void mock_wifi_storage_init(void)
{
RESET_FAKE(wifi_storage_init);
RESET_FAKE(wifi_storage_deinit);
RESET_FAKE(wifi_storage_save_ap);
RESET_FAKE(wifi_storage_delete_ap);
RESET_FAKE(wifi_storage_search_ap);
}
void mock_wifi_storage_reset(void)
{
mock_wifi_storage_init();
}

View File

@@ -0,0 +1,30 @@
/**
* Copyright (c) 2023, LISTENAI
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include "wifi_manager_storage.h"
#include "fff.h"
#ifdef __cplusplus
extern "C" {
#endif
DECLARE_FAKE_VALUE_FUNC(int, wifi_storage_init, wifi_storage_ctx_t *, wifi_storage_ops_t *, void *);
DECLARE_FAKE_VALUE_FUNC(int, wifi_storage_deinit, wifi_storage_ctx_t *);
DECLARE_FAKE_VALUE_FUNC(int, wifi_storage_save_ap, wifi_storage_ctx_t *, wifi_mgr_sta_config_t *);
DECLARE_FAKE_VALUE_FUNC(int, wifi_storage_delete_ap, wifi_storage_ctx_t *, wifi_mgr_sta_config_t *);
DECLARE_FAKE_VALUE_FUNC(int, wifi_storage_search_ap, wifi_storage_ctx_t *, wifi_mgr_sta_config_t **,
wifi_mgr_storage_search_mode_t, void *);
void mock_wifi_storage_init(void);
void mock_wifi_storage_reset(void);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,9 @@
target_sources(${EXECUTABLE_NAME} PRIVATE
mock_wifi_manager_storage_cache.c
)
target_include_directories(${EXECUTABLE_NAME} PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}
${PRJ_ROOT}/include/wifi_manager
${PRJ_ROOT}/src
)

View File

@@ -0,0 +1,72 @@
#include "wifi_manager_storage_cache.h"
#include "wifi_manager_storage_internal.h"
#include <errno.h>
void wifi_storage_cache_init_ctx(wifi_storage_ctx_t *ctx)
{
if (ctx == NULL) {
return;
}
ctx->cache.list = NULL;
ctx->cache.count = 0;
ctx->cache.loaded = false;
}
void wifi_storage_cache_drop(wifi_storage_ctx_t *ctx)
{
if (ctx == NULL) {
return;
}
if (ctx->cache.list != NULL && ctx->ops.free != NULL) {
ctx->ops.free(ctx->cache.list);
}
ctx->cache.list = NULL;
ctx->cache.count = 0;
ctx->cache.loaded = false;
}
bool wifi_storage_cache_valid(const wifi_storage_ctx_t *ctx)
{
return ctx != NULL && ctx->cache.loaded;
}
void wifi_storage_cache_set(wifi_storage_ctx_t *ctx, wifi_storage_item_t *list, uint32_t count)
{
if (ctx == NULL) {
return;
}
ctx->cache.list = list;
ctx->cache.count = count;
ctx->cache.loaded = true;
}
int wifi_storage_cache_load(wifi_storage_ctx_t *ctx, wifi_storage_loader_fn loader)
{
if (ctx == NULL || loader == NULL) {
return -EINVAL;
}
if (wifi_storage_cache_valid(ctx)) {
return 0;
}
if (ctx->cache.list != NULL) {
wifi_storage_cache_drop(ctx);
}
wifi_storage_item_t *list = NULL;
uint32_t count = 0;
int ret = loader(ctx, &list, &count);
if (ret == 0) {
wifi_storage_cache_set(ctx, list, count);
}
return ret;
}
void wifi_storage_cache_get(const wifi_storage_ctx_t *ctx, wifi_storage_item_t **out_list, uint32_t *out_count)
{
if (out_list != NULL) {
*out_list = (ctx != NULL) ? ctx->cache.list : NULL;
}
if (out_count != NULL) {
*out_count = (ctx != NULL) ? ctx->cache.count : 0;
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,13 @@
/* Unity配置文件 */
#ifndef UNITY_CONFIG_H
#define UNITY_CONFIG_H
#include <stdio.h>
/* 自定义配置 */
#define UNITY_OUTPUT_CHAR(a) putchar(a)
#define UNITY_OUTPUT_FLUSH() fflush(stdout)
#define UNITY_OUTPUT_START() printf("\n\n")
#define UNITY_OUTPUT_COMPLETE() printf("\n")
#endif /* UNITY_CONFIG_H */

View File

@@ -0,0 +1,94 @@
cmake_minimum_required(VERSION 3.16)
project(wifi_manager_test C)
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_C_EXTENSIONS OFF)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
# 添加调试选项
set(CMAKE_BUILD_TYPE Debug)
set(DEBUG_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
# 为链接器添加选项保留调试信息
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -rdynamic")
# 配置cmake模块路径
list(APPEND CMAKE_PREFIX_PATH "/usr/local/lib/cmake")
message(STATUS "CMAKE_PREFIX_PATH: ${CMAKE_PREFIX_PATH}")
find_package(unity QUIET)
# 如果找到Unity包,使用包提供的变量
if(unity_FOUND)
message(STATUS "Found Unity package")
set(UNITY_LIB unity::framework)
else()
message(FATAL_ERROR "Not found Unity package")
endif()
set(PRJ_ROOT ../..)
# 创建测试可执行文件
add_executable(tests)
set(EXECUTABLE_NAME tests)
target_compile_definitions(${EXECUTABLE_NAME} PRIVATE
UNIT_TEST
CONFIG_WIFI_DEV_STATION=1
)
target_compile_definitions(${EXECUTABLE_NAME} PRIVATE
CONFIG_WIFI_MGR_NVS_PARTITION_OFFSET=0x100000
CONFIG_WIFI_MGR_NVS_PARTITION_SIZE=0x100000
CONFIG_WIFI_MGR_NVS_FLASH_SECTOR_SIZE=0x1000
CONFIG_WIFI_MGR_AUTO_CONNECT_TIMEOUT_MS=15000
CONFIG_WIFI_MGR_THREAD_STACK=1024
CONFIG_WIFI_MGR_THREAD_PRIORITY=5
CONFIG_WIFI_MGR_CONNECT_TIMEOUT=15000
_GNU_SOURCE
_POSIX_C_SOURCE=200809L
)
target_include_directories(tests PRIVATE
${PRJ_ROOT}/test/utils/fff
${PRJ_ROOT}/test/utils/unity_config
)
target_sources(tests PRIVATE
${PRJ_ROOT}/src/wifi_manager.c
)
target_include_directories(tests PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}
${PRJ_ROOT}/src
${PRJ_ROOT}/include
${PRJ_ROOT}/include/wifi_manager
${PRJ_ROOT}/test/common/fake_lisa_log
)
target_sources(tests PRIVATE
test_wifi_manager.c
)
add_subdirectory(${PRJ_ROOT}/test/common/fake_lisa_common fake_lisa_common)
add_subdirectory(${PRJ_ROOT}/test/common/fake_lisa_time fake_lisa_time)
add_subdirectory(${PRJ_ROOT}/test/common/fake_lisa_thread fake_lisa_thread)
add_subdirectory(${PRJ_ROOT}/test/common/fake_lisa_mutex fake_lisa_mutex)
add_subdirectory(${PRJ_ROOT}/test/common/fake_nvs fake_nvs)
add_subdirectory(${PRJ_ROOT}/test/common/mock_heap_caps mock_heap_caps)
add_subdirectory(${PRJ_ROOT}/test/common/freertos freertos)
add_subdirectory(${PRJ_ROOT}/test/common/mock_lisa_kv mock_lisa_kv)
add_subdirectory(${PRJ_ROOT}/test/common/mock_wifi mock_wifi)
add_subdirectory(${PRJ_ROOT}/test/common/mock_platform_dev mock_platform_dev)
add_subdirectory(${PRJ_ROOT}/test/common/mock_mem mock_mem)
add_subdirectory(${PRJ_ROOT}/test/common/mock_os mock_os)
add_subdirectory(${PRJ_ROOT}/test/common/mock_wifi_manager_storage mock_wifi_manager_storage)
target_link_libraries(tests PRIVATE
${UNITY_LIB}
)
# 允许使用CTest运行测试
enable_testing()
add_test(NAME wifi_manager_tests COMMAND tests)

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,7 @@
tests:
test.wifi_manager:
runner: linux
builder: cmake
log_analyzer: "unity"

View File

@@ -0,0 +1,82 @@
cmake_minimum_required(VERSION 3.16)
project(wifi_manager_arcs_test C)
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_C_EXTENSIONS OFF)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_BUILD_TYPE Debug)
set(DEBUG_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -rdynamic")
list(APPEND CMAKE_PREFIX_PATH "/usr/local/lib/cmake")
message(STATUS "CMAKE_PREFIX_PATH: ${CMAKE_PREFIX_PATH}")
find_package(unity QUIET)
if(unity_FOUND)
message(STATUS "Found Unity package")
set(UNITY_LIB unity::framework)
else()
message(FATAL_ERROR "Not found Unity package")
endif()
set(PRJ_ROOT ../..)
add_executable(tests)
set(EXECUTABLE_NAME tests)
target_compile_definitions(${EXECUTABLE_NAME} PRIVATE
UNIT_TEST
CONFIG_WIFI_DEV_STATION=1
CONFIG_WIFI_MGR_THREAD_STACK=1024
CONFIG_WIFI_MGR_THREAD_PRIORITY=5
CONFIG_WIFI_MGR_QUEUE_COUNT=4
CONFIG_WIFI_MGR_AUTO_CONNECT_TIMEOUT_MS=15000
CONFIG_WIFI_MGR_CONNECT_TIMEOUT=15000
_GNU_SOURCE
_POSIX_C_SOURCE=200809L
)
target_include_directories(tests PRIVATE
${PRJ_ROOT}/test/utils/fff
${PRJ_ROOT}/test/utils/unity_config
)
target_include_directories(tests PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}
${PRJ_ROOT}/src
${PRJ_ROOT}/include
${PRJ_ROOT}/include/wifi_manager
${PRJ_ROOT}/port/wifi_ops/arcs
${PRJ_ROOT}/test/common/fake_lisa_log
${PRJ_ROOT}/test/common/mock_wifi_api
${PRJ_ROOT}/test/common/mock_ls_event
${PRJ_ROOT}/test/common/mock_cache
${PRJ_ROOT}/test/common/mock_heap_caps
${PRJ_ROOT}/test/common/fake_nvs
${PRJ_ROOT}/test/common/freertos
)
target_sources(tests PRIVATE
test_wifi_manager_arcs.c
${PRJ_ROOT}/src/wifi_manager.c
${PRJ_ROOT}/port/wifi_ops/arcs/wifi_arcs.c
${PRJ_ROOT}/test/common/mock_wifi_api/mock_wifi_api.c
${PRJ_ROOT}/test/common/mock_ls_event/mock_ls_event.c
${PRJ_ROOT}/test/common/mock_cache/mock_cache.c
)
add_subdirectory(${PRJ_ROOT}/test/common/mock_mem mock_mem)
add_subdirectory(${PRJ_ROOT}/test/common/mock_os mock_os)
add_subdirectory(${PRJ_ROOT}/test/common/mock_wifi_manager_storage mock_wifi_manager_storage)
add_subdirectory(${PRJ_ROOT}/test/common/mock_heap_caps mock_heap_caps)
add_subdirectory(${PRJ_ROOT}/test/common/fake_lisa_time fake_lisa_time)
target_link_libraries(tests PRIVATE
${UNITY_LIB}
)
enable_testing()
add_test(NAME wifi_manager_arcs_tests COMMAND tests)

View File

@@ -0,0 +1,411 @@
/**
* Copyright (c) 2025, LISTENAI
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "unity.h"
#include <stdbool.h>
#include <string.h>
#include "fff.h"
#include "mock_ls_event.h"
#include "mock_mem_ops.h"
#include "mock_os_ops.h"
#include "mock_wifi_api.h"
#include "mock_wifi_manager_storage.h"
#include "mock_esp_heap_caps.h"
#include "wifi_manager/wifi_manager.h"
#include "wifi_arcs.h"
DEFINE_FFF_GLOBALS;
static bool s_cb_called = false;
static wifi_mgr_sta_config_t s_last_sta_info;
static wifi_mgr_connection_status_t s_last_status = WIFI_MGR_STA_MAX;
static int s_last_reason = 0;
static bool s_event_cb_called = false;
static wifi_mgr_wifi_event_t s_last_event = 0;
static wifi_mgr_connect_fail_info_t s_last_fail_info;
static bool s_last_fail_info_valid = false;
static void test_connection_cb(wifi_mgr_connection_info_t *info, void *arg)
{
(void)arg;
s_cb_called = true;
s_last_status = info->status;
s_last_reason = info->reason;
if (info->sta_info != NULL) {
memcpy(&s_last_sta_info, info->sta_info, sizeof(s_last_sta_info));
} else {
memset(&s_last_sta_info, 0, sizeof(s_last_sta_info));
}
}
static void test_wifi_event_cb(wifi_mgr_wifi_event_t event, void *event_data, uint32_t event_data_len, void *arg)
{
(void)event_data;
(void)arg;
s_event_cb_called = true;
s_last_event = event;
s_last_fail_info_valid = false;
if (event_data == NULL) {
return;
}
if (event_data_len >= sizeof(wifi_mgr_disconnect_event_info_t)) {
wifi_mgr_disconnect_event_info_t *disc_info = event_data;
s_last_fail_info = disc_info->fail_info;
s_last_fail_info_valid = true;
} else if (event_data_len >= sizeof(wifi_mgr_connect_fail_info_t)) {
memcpy(&s_last_fail_info, event_data, sizeof(s_last_fail_info));
s_last_fail_info_valid = true;
}
}
static void *test_thread_create(wifi_manager_os_thread_attr_t *attr,
wifi_manager_os_thread_entry_t entry, void *arg)
{
(void)attr;
(void)entry;
(void)arg;
return (void *)0x1;
}
static void test_thread_delete(void *thread)
{
(void)thread;
}
static void setup_wifi_manager(void)
{
wifi_mgr_ops_t ops = {
.mem_ops = mock_mem_ops_get(),
.os_ops = mock_os_ops_get(),
.wifi_ops = wifi_manager_arcs_wifi_ops_get(),
};
mock_mem_ops_init();
mock_os_ops_init();
mock_wifi_storage_init();
mock_esp_heap_caps_init();
mock_ls_event_init();
mock_wifi_api_init();
mock_ls_event_reset();
mock_wifi_api_reset();
wifi_storage_init_fake.return_val = 0;
mock_thread_create_fake.custom_fake = test_thread_create;
mock_thread_delete_fake.custom_fake = test_thread_delete;
TEST_ASSERT_EQUAL(0, wifi_mgr_init(&ops));
TEST_ASSERT_EQUAL(0, wifi_mgr_sta_add_connection_cb(test_connection_cb, NULL));
}
static void teardown_wifi_manager(void)
{
wifi_mgr_deinit();
s_cb_called = false;
memset(&s_last_sta_info, 0, sizeof(s_last_sta_info));
s_last_status = WIFI_MGR_STA_MAX;
s_last_reason = 0;
s_event_cb_called = false;
s_last_event = 0;
memset(&s_last_fail_info, 0, sizeof(s_last_fail_info));
s_last_fail_info_valid = false;
}
void setUp(void)
{
setup_wifi_manager();
}
void tearDown(void)
{
teardown_wifi_manager();
}
void test_wifi_connected_event_populates_sta_info(void)
{
uint8_t pmk[32];
for (size_t i = 0; i < sizeof(pmk); i++) {
pmk[i] = (uint8_t)(i + 1);
}
wifi_link_status_t link_status = {
.bssid = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66},
.channel = 7,
.state = STA_CONNECTED,
};
mock_wifi_api_set_pmk(pmk, true);
mock_wifi_api_set_link_status(&link_status);
mock_ls_event_trigger(EVENT_WIFI, EVENT_WIFI_CONNECTED, NULL);
TEST_ASSERT_TRUE(s_cb_called);
TEST_ASSERT_EQUAL(WIFI_MGR_STA_CONNECTED, s_last_status);
TEST_ASSERT_TRUE(s_last_sta_info.pmk_valid);
TEST_ASSERT_EQUAL_MEMORY(pmk, s_last_sta_info.pmk, sizeof(pmk));
TEST_ASSERT_EQUAL(7, s_last_sta_info.channel);
TEST_ASSERT_EQUAL_STRING("11:22:33:44:55:66", s_last_sta_info.bssid);
}
void test_wifi_connected_event_keeps_requested_ssid_and_pwd(void)
{
wifi_mgr_sta_config_t old_cfg = {0};
strcpy(old_cfg.ssid, "ssid_b");
strcpy(old_cfg.pwd, "pwd_b");
strcpy(old_cfg.bssid, "bb:bb:bb:bb:bb:bb");
old_cfg.encryption_mode = WIFI_MGR_WIFI_AUTH_WPA2_PSK;
(void)wifi_mgr_sta_connect(&old_cfg, true);
wifi_link_status_t link_status = {
.bssid = {0xcc, 0xdd, 0xee, 0xff, 0x00, 0x11},
.channel = 6,
.state = STA_CONNECTED,
};
strcpy(link_status.ssid, "ssid_from_driver");
mock_wifi_api_set_link_status(&link_status);
s_cb_called = false;
memset(&s_last_sta_info, 0, sizeof(s_last_sta_info));
s_last_status = WIFI_MGR_STA_MAX;
mock_ls_event_trigger(EVENT_WIFI, EVENT_WIFI_CONNECTED, NULL);
TEST_ASSERT_TRUE(s_cb_called);
TEST_ASSERT_EQUAL(WIFI_MGR_STA_CONNECTED, s_last_status);
TEST_ASSERT_EQUAL_STRING("ssid_b", s_last_sta_info.ssid);
TEST_ASSERT_EQUAL_STRING("pwd_b", s_last_sta_info.pwd);
TEST_ASSERT_EQUAL_STRING("cc:dd:ee:ff:00:11", s_last_sta_info.bssid);
TEST_ASSERT_EQUAL(6, s_last_sta_info.channel);
}
void test_wifi_connect_sets_bssid_and_freq(void)
{
wifi_mgr_sta_config_t cfg = {0};
strcpy(cfg.ssid, "TestSSID");
strcpy(cfg.pwd, "TestPwd");
strcpy(cfg.bssid, "11:22:33:44:55:66");
cfg.channel = 6;
cfg.encryption_mode = WIFI_MGR_WIFI_AUTH_WPA2_PSK;
cfg.pmk_valid = 0;
(void)wifi_mgr_sta_connect(&cfg, false);
TEST_ASSERT_TRUE(mock_wifi_api_has_last_connect_cfg());
const wifi_connect_cfg_t *last_cfg = mock_wifi_api_get_last_connect_cfg();
TEST_ASSERT_NOT_NULL(last_cfg);
TEST_ASSERT_EQUAL_HEX8(0x11, last_cfg->bssid[0]);
TEST_ASSERT_EQUAL_HEX8(0x22, last_cfg->bssid[1]);
TEST_ASSERT_EQUAL_HEX8(0x33, last_cfg->bssid[2]);
TEST_ASSERT_EQUAL_HEX8(0x44, last_cfg->bssid[3]);
TEST_ASSERT_EQUAL_HEX8(0x55, last_cfg->bssid[4]);
TEST_ASSERT_EQUAL_HEX8(0x66, last_cfg->bssid[5]);
TEST_ASSERT_EQUAL(2437, last_cfg->freq[0]);
TEST_ASSERT_EQUAL(2437, last_cfg->freq[1]);
}
void test_wifi_disconnect_before_connected_reports_connect_fail(void)
{
wifi_mgr_wifi_event_cb_t cb = {
.handler = test_wifi_event_cb,
.events = WIFI_MGR_WIFI_EVT_STA_CONNECTED |
WIFI_MGR_WIFI_EVT_STA_DISCONNECTED |
WIFI_MGR_WIFI_EVT_STA_CONNECTION_FAILED,
.arg = NULL,
};
wifi_manager_arcs_wifi_ops_get()->add_callback(&cb);
s_event_cb_called = false;
s_last_event = 0;
mock_ls_event_trigger(EVENT_WIFI, EVENT_WIFI_DISCONNECT, NULL);
TEST_ASSERT_TRUE(s_event_cb_called);
TEST_ASSERT_EQUAL(WIFI_MGR_WIFI_EVT_STA_CONNECTION_FAILED, s_last_event);
wifi_manager_arcs_wifi_ops_get()->remove_callback(&cb);
}
void test_wifi_disconnect_after_connected_reports_disconnected(void)
{
wifi_mgr_wifi_event_cb_t cb = {
.handler = test_wifi_event_cb,
.events = WIFI_MGR_WIFI_EVT_STA_CONNECTED |
WIFI_MGR_WIFI_EVT_STA_DISCONNECTED |
WIFI_MGR_WIFI_EVT_STA_CONNECTION_FAILED,
.arg = NULL,
};
wifi_manager_arcs_wifi_ops_get()->add_callback(&cb);
mock_ls_event_trigger(EVENT_WIFI, EVENT_WIFI_CONNECTED, NULL);
s_event_cb_called = false;
s_last_event = 0;
mock_ls_event_trigger(EVENT_WIFI, EVENT_WIFI_STA_CONNECT_FAIL, NULL);
TEST_ASSERT_TRUE(s_event_cb_called);
TEST_ASSERT_EQUAL(WIFI_MGR_WIFI_EVT_STA_DISCONNECTED, s_last_event);
wifi_manager_arcs_wifi_ops_get()->remove_callback(&cb);
}
void test_wifi_disconnect_while_switching_ap_reports_previous_connection(void)
{
s_cb_called = false;
memset(&s_last_sta_info, 0, sizeof(s_last_sta_info));
s_last_status = WIFI_MGR_STA_MAX;
wifi_mgr_sta_config_t ap_a = {
.ssid = "ssid_a",
.pwd = "pwd_a",
.bssid = "aa:aa:aa:aa:aa:aa",
.channel = 1,
.encryption_mode = WIFI_MGR_WIFI_AUTH_WPA2_PSK,
};
wifi_mgr_sta_config_t ap_b = {
.ssid = "ssid_b",
.pwd = "pwd_b",
.bssid = "bb:bb:bb:bb:bb:bb",
.channel = 6,
.encryption_mode = WIFI_MGR_WIFI_AUTH_WPA2_PSK,
};
wifi_link_status_t link_status = {
.bssid = {0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa},
.channel = 1,
.state = STA_CONNECTED,
};
// Connect to AP A and simulate successful connection event
(void)wifi_mgr_sta_connect(&ap_a, true);
mock_wifi_api_set_link_status(&link_status);
mock_ls_event_trigger(EVENT_WIFI, EVENT_WIFI_CONNECTED, NULL);
TEST_ASSERT_TRUE(s_cb_called);
TEST_ASSERT_EQUAL_STRING("ssid_a", s_last_sta_info.ssid);
// Reset callback state, start connecting to AP B
s_cb_called = false;
memset(&s_last_sta_info, 0, sizeof(s_last_sta_info));
s_last_status = WIFI_MGR_STA_MAX;
(void)wifi_mgr_sta_connect(&ap_b, true);
// Firmware reports disconnect; should still report last connected AP A
mock_ls_event_trigger(EVENT_WIFI, EVENT_WIFI_DISCONNECT, NULL);
TEST_ASSERT_TRUE(s_cb_called);
TEST_ASSERT_EQUAL(WIFI_MGR_STA_DISCONNECTED, s_last_status);
TEST_ASSERT_EQUAL_STRING("ssid_a", s_last_sta_info.ssid);
}
void test_wifi_disconnect_event_propagates_fail_info(void)
{
wifi_mgr_wifi_event_cb_t cb = {
.handler = test_wifi_event_cb,
.events = WIFI_MGR_WIFI_EVT_STA_CONNECTED |
WIFI_MGR_WIFI_EVT_STA_DISCONNECTED |
WIFI_MGR_WIFI_EVT_STA_CONNECTION_FAILED,
.arg = NULL,
};
wifi_manager_arcs_wifi_ops_get()->add_callback(&cb);
wifi_link_status_t link_status = {
.bssid = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66},
.channel = 11,
.state = STA_CONNECTED,
};
event_connect_fail_param_t fail_evt = {
.erro_code = 12,
.status_code = 7,
.reason_code = 203,
};
mock_wifi_api_set_link_status(&link_status);
mock_ls_event_trigger(EVENT_WIFI, EVENT_WIFI_CONNECTED, NULL);
s_cb_called = false;
s_event_cb_called = false;
s_last_status = WIFI_MGR_STA_MAX;
s_last_reason = -1;
s_last_fail_info_valid = false;
memset(&s_last_fail_info, 0, sizeof(s_last_fail_info));
mock_ls_event_trigger(EVENT_WIFI, EVENT_WIFI_DISCONNECT, &fail_evt);
TEST_ASSERT_TRUE(s_cb_called);
TEST_ASSERT_EQUAL(WIFI_MGR_STA_DISCONNECTED, s_last_status);
TEST_ASSERT_EQUAL(fail_evt.reason_code, s_last_reason);
TEST_ASSERT_TRUE(s_event_cb_called);
TEST_ASSERT_EQUAL(WIFI_MGR_WIFI_EVT_STA_DISCONNECTED, s_last_event);
TEST_ASSERT_TRUE(s_last_fail_info_valid);
TEST_ASSERT_EQUAL(fail_evt.erro_code, s_last_fail_info.error_code);
TEST_ASSERT_EQUAL(fail_evt.status_code, s_last_fail_info.status_code);
TEST_ASSERT_EQUAL(fail_evt.reason_code, s_last_fail_info.reason_code);
wifi_manager_arcs_wifi_ops_get()->remove_callback(&cb);
}
void test_wifi_connection_fail_propagates_fail_info(void)
{
wifi_mgr_wifi_event_cb_t cb = {
.handler = test_wifi_event_cb,
.events = WIFI_MGR_WIFI_EVT_STA_CONNECTED |
WIFI_MGR_WIFI_EVT_STA_DISCONNECTED |
WIFI_MGR_WIFI_EVT_STA_CONNECTION_FAILED,
.arg = NULL,
};
wifi_manager_arcs_wifi_ops_get()->add_callback(&cb);
event_connect_fail_param_t fail_evt = {
.erro_code = 5,
.status_code = 9,
.reason_code = 42,
};
s_cb_called = false;
s_event_cb_called = false;
s_last_status = WIFI_MGR_STA_MAX;
s_last_reason = -1;
s_last_fail_info_valid = false;
memset(&s_last_fail_info, 0, sizeof(s_last_fail_info));
mock_ls_event_trigger(EVENT_WIFI, EVENT_WIFI_STA_CONNECT_FAIL, &fail_evt);
TEST_ASSERT_TRUE(s_cb_called);
TEST_ASSERT_EQUAL(WIFI_MGR_STA_CONNECT_FAILED, s_last_status);
TEST_ASSERT_EQUAL(fail_evt.reason_code, s_last_reason);
TEST_ASSERT_TRUE(s_event_cb_called);
TEST_ASSERT_EQUAL(WIFI_MGR_WIFI_EVT_STA_CONNECTION_FAILED, s_last_event);
TEST_ASSERT_TRUE(s_last_fail_info_valid);
TEST_ASSERT_EQUAL(fail_evt.erro_code, s_last_fail_info.error_code);
TEST_ASSERT_EQUAL(fail_evt.status_code, s_last_fail_info.status_code);
TEST_ASSERT_EQUAL(fail_evt.reason_code, s_last_fail_info.reason_code);
wifi_manager_arcs_wifi_ops_get()->remove_callback(&cb);
}
int main(void)
{
UNITY_BEGIN();
RUN_TEST(test_wifi_connected_event_populates_sta_info);
RUN_TEST(test_wifi_connected_event_keeps_requested_ssid_and_pwd);
RUN_TEST(test_wifi_connect_sets_bssid_and_freq);
RUN_TEST(test_wifi_disconnect_before_connected_reports_connect_fail);
RUN_TEST(test_wifi_disconnect_after_connected_reports_disconnected);
RUN_TEST(test_wifi_disconnect_while_switching_ap_reports_previous_connection);
RUN_TEST(test_wifi_disconnect_event_propagates_fail_info);
RUN_TEST(test_wifi_connection_fail_propagates_fail_info);
return UNITY_END();
}

View File

@@ -0,0 +1,7 @@
tests:
test.wifi_manager_arcs:
runner: linux
builder: cmake
log_analyzer: "unity"

View File

@@ -0,0 +1,81 @@
cmake_minimum_required(VERSION 3.16)
project(wifi_manager_storage_test C)
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_C_EXTENSIONS OFF)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_BUILD_TYPE Debug)
set(DEBUG_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -rdynamic")
list(APPEND CMAKE_PREFIX_PATH "/usr/local/lib/cmake")
message(STATUS "CMAKE_PREFIX_PATH: ${CMAKE_PREFIX_PATH}")
find_package(unity QUIET)
if(unity_FOUND)
message(STATUS "Found Unity package")
set(UNITY_LIB unity::framework)
else()
message(FATAL_ERROR "Not found Unity package")
endif()
set(PRJ_ROOT ../..)
add_executable(tests)
set(EXECUTABLE_NAME tests)
target_compile_definitions(${EXECUTABLE_NAME} PRIVATE
UNIT_TEST
CONFIG_WIFI_DEV_STATION=1
)
target_include_directories(tests PRIVATE
${PRJ_ROOT}/test/utils/fff
${PRJ_ROOT}/test/utils/unity_config
)
target_include_directories(tests PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}
${PRJ_ROOT}/src
${PRJ_ROOT}/include
${PRJ_ROOT}/include/wifi_manager
${PRJ_ROOT}/test/common/fake_lisa_log
)
target_sources(tests PRIVATE
test_wifi_manager_storage.c
${PRJ_ROOT}/src/wifi_manager_storage.c
${PRJ_ROOT}/src/wifi_manager_storage_kv.c
${PRJ_ROOT}/src/wifi_manager_storage_blob_legacy.c
${PRJ_ROOT}/src/wifi_manager_storage_migrate.c
)
add_subdirectory(${PRJ_ROOT}/test/common/mock_lisa_kv mock_lisa_kv)
add_subdirectory(${PRJ_ROOT}/test/common/mock_wifi_manager_storage_cache mock_wifi_manager_storage_cache)
target_link_libraries(tests PRIVATE
${UNITY_LIB}
)
enable_testing()
add_test(NAME wifi_manager_storage_tests COMMAND tests)
add_executable(tests_cache)
target_compile_definitions(tests_cache PRIVATE
UNIT_TEST
)
target_include_directories(tests_cache PRIVATE
${PRJ_ROOT}/test/utils/unity_config
${PRJ_ROOT}/src
${PRJ_ROOT}/include
${PRJ_ROOT}/include/wifi_manager
)
target_sources(tests_cache PRIVATE
test_wifi_manager_storage_cache.c
${PRJ_ROOT}/src/wifi_manager_storage_cache.c
)
target_link_libraries(tests_cache PRIVATE ${UNITY_LIB})
add_test(NAME wifi_manager_storage_cache_tests COMMAND tests_cache)

View File

@@ -0,0 +1,641 @@
/**
* Copyright (c) 2025, LISTENAI
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "unity.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include "fff.h"
#include "wifi_manager/wifi_manager_storage.h"
#include "wifi_manager_storage_internal.h"
#include "mock_lisa_kv.h"
DEFINE_FFF_GLOBALS;
typedef struct {
char ssid[32];
char pwd[64];
char bssid[18];
int channel;
int rssi;
wifi_mgr_wifi_encryption_mode_t encryption_mode;
} wifi_mgr_sta_config_legacy_t;
typedef struct {
wifi_mgr_sta_config_legacy_t ap_info;
uint8_t enable;
} wifi_storage_item_legacy_v1_t;
static wifi_storage_ctx_t s_ctx;
static int s_mutex;
static int s_alloc_count;
static int s_free_count;
static int (*s_kv_set_int_passthrough)(const char *key, int value);
static bool s_fail_bool_alloc;
static void set_kv_item(int index, const char *ssid, const char *bssid);
static void set_kv_count(int count);
static void *test_malloc(size_t size)
{
return malloc(size);
}
static void *test_calloc(size_t nmemb, size_t size)
{
return calloc(nmemb, size);
}
static void *test_calloc_fail_bool(size_t nmemb, size_t size)
{
if (s_fail_bool_alloc && size == sizeof(bool)) {
return NULL;
}
return calloc(nmemb, size);
}
static void *test_calloc_track(size_t nmemb, size_t size)
{
void *ptr = calloc(nmemb, size);
if (ptr != NULL) {
s_alloc_count++;
}
return ptr;
}
static void test_free(void *ptr)
{
free(ptr);
}
static void test_free_track(void *ptr)
{
if (ptr != NULL) {
s_free_count++;
}
free(ptr);
}
static int test_mutex_lock(void *mutex, uint32_t timeout)
{
(void)mutex;
(void)timeout;
return 0;
}
static int test_mutex_unlock(void *mutex)
{
(void)mutex;
return 0;
}
static void init_storage(void)
{
wifi_storage_ops_t ops = {
.malloc = test_malloc,
.calloc = test_calloc,
.free = test_free,
.mutex_lock = test_mutex_lock,
.mutex_unlock = test_mutex_unlock,
};
memset(&s_ctx, 0, sizeof(s_ctx));
TEST_ASSERT_EQUAL(0, wifi_storage_init(&s_ctx, &ops, &s_mutex));
}
static void init_storage_with_ops(wifi_storage_ops_t *ops)
{
memset(&s_ctx, 0, sizeof(s_ctx));
TEST_ASSERT_EQUAL(0, wifi_storage_init(&s_ctx, ops, &s_mutex));
}
void setUp(void)
{
mock_lisa_kv_reset();
s_alloc_count = 0;
s_free_count = 0;
s_fail_bool_alloc = false;
s_kv_set_int_passthrough = lisa_kv_set_int_fake.custom_fake;
init_storage();
}
void tearDown(void)
{
wifi_storage_deinit(&s_ctx);
}
void test_wifi_storage_kv_round_trip(void)
{
wifi_mgr_sta_config_t ap = {0};
strcpy(ap.ssid, "TestSSID");
strcpy(ap.pwd, "TestPwd");
strcpy(ap.bssid, "aa:bb:cc:dd:ee:ff");
ap.channel = 11;
ap.rssi = -40;
ap.encryption_mode = WIFI_MGR_WIFI_AUTH_WPA2_PSK;
TEST_ASSERT_EQUAL(0, wifi_storage_save_ap(&s_ctx, &ap));
wifi_mgr_sta_config_t *matched_list = NULL;
int count = wifi_storage_search_ap(&s_ctx, &matched_list, SEARCH_BY_SSID, ap.ssid);
TEST_ASSERT_EQUAL(1, count);
TEST_ASSERT_NOT_NULL(matched_list);
TEST_ASSERT_EQUAL_STRING(ap.ssid, matched_list[0].ssid);
TEST_ASSERT_EQUAL_STRING(ap.pwd, matched_list[0].pwd);
TEST_ASSERT_EQUAL_STRING(ap.bssid, matched_list[0].bssid);
TEST_ASSERT_EQUAL(ap.channel, matched_list[0].channel);
TEST_ASSERT_EQUAL(ap.encryption_mode, matched_list[0].encryption_mode);
test_free(matched_list);
int kv_count = 0;
TEST_ASSERT_EQUAL(0, lisa_kv_get_int(WIFI_STORAGE_KV_COUNT_KEY, &kv_count));
TEST_ASSERT_EQUAL(1, kv_count);
}
void test_wifi_storage_migrate_blob_to_kv(void)
{
wifi_storage_item_legacy_v1_t legacy_items[2] = {0};
strcpy(legacy_items[0].ap_info.ssid, "LegacySSID1");
strcpy(legacy_items[0].ap_info.pwd, "LegacyPwd1");
strcpy(legacy_items[0].ap_info.bssid, "00:11:22:33:44:55");
legacy_items[0].ap_info.channel = 1;
legacy_items[0].ap_info.rssi = -30;
legacy_items[0].ap_info.encryption_mode = WIFI_MGR_WIFI_AUTH_WPA_PSK;
legacy_items[0].enable = 1;
strcpy(legacy_items[1].ap_info.ssid, "LegacySSID2");
strcpy(legacy_items[1].ap_info.pwd, "LegacyPwd2");
strcpy(legacy_items[1].ap_info.bssid, "66:77:88:99:aa:bb");
legacy_items[1].ap_info.channel = 6;
legacy_items[1].ap_info.rssi = -50;
legacy_items[1].ap_info.encryption_mode = WIFI_MGR_WIFI_AUTH_WPA2_PSK;
legacy_items[1].enable = 1;
TEST_ASSERT_EQUAL(0, lisa_kv_set_int(WIFI_STORAGE_BLOB_COUNT_KEY, 2));
TEST_ASSERT_EQUAL(0, lisa_kv_set_blob(WIFI_STORAGE_BLOB_LIST_KEY,
(uint8_t *)legacy_items,
sizeof(legacy_items)));
wifi_storage_deinit(&s_ctx);
init_storage();
wifi_mgr_sta_config_t *matched_list = NULL;
int count = wifi_storage_search_ap(&s_ctx, &matched_list, SEARCH_ALL, NULL);
TEST_ASSERT_EQUAL(2, count);
TEST_ASSERT_NOT_NULL(matched_list);
TEST_ASSERT_EQUAL_STRING("LegacySSID2", matched_list[0].ssid);
TEST_ASSERT_EQUAL_STRING("LegacySSID1", matched_list[1].ssid);
test_free(matched_list);
int kv_count = 0;
TEST_ASSERT_EQUAL(0, lisa_kv_get_int(WIFI_STORAGE_KV_COUNT_KEY, &kv_count));
TEST_ASSERT_EQUAL(2, kv_count);
char *ssid0 = NULL;
TEST_ASSERT_EQUAL(0, lisa_kv_get_string("wifi-kv-item-0-ssid", &ssid0));
TEST_ASSERT_EQUAL_STRING("LegacySSID1", ssid0);
lisa_kv_free(ssid0);
}
void test_wifi_storage_kv_preferred_over_blob(void)
{
TEST_ASSERT_EQUAL(0, lisa_kv_set_int(WIFI_STORAGE_KV_COUNT_KEY, 1));
TEST_ASSERT_EQUAL(0, lisa_kv_set_bool("wifi-kv-item-0-enable", true));
TEST_ASSERT_EQUAL(0, lisa_kv_set_string("wifi-kv-item-0-ssid", "KVSSID"));
TEST_ASSERT_EQUAL(0, lisa_kv_set_string("wifi-kv-item-0-pwd", "KVPWD"));
TEST_ASSERT_EQUAL(0, lisa_kv_set_string("wifi-kv-item-0-bssid", "11:22:33:44:55:66"));
TEST_ASSERT_EQUAL(0, lisa_kv_set_int("wifi-kv-item-0-channel", 3));
TEST_ASSERT_EQUAL(0, lisa_kv_set_int("wifi-kv-item-0-rssi", -60));
TEST_ASSERT_EQUAL(0, lisa_kv_set_int("wifi-kv-item-0-encryption", WIFI_MGR_WIFI_AUTH_OPEN));
wifi_storage_item_legacy_v1_t legacy_items[1] = {0};
strcpy(legacy_items[0].ap_info.ssid, "LegacySSID");
strcpy(legacy_items[0].ap_info.bssid, "11:22:33:44:55:66");
legacy_items[0].ap_info.channel = 8;
legacy_items[0].enable = 1;
TEST_ASSERT_EQUAL(0, lisa_kv_set_int(WIFI_STORAGE_BLOB_COUNT_KEY, 1));
TEST_ASSERT_EQUAL(0, lisa_kv_set_blob(WIFI_STORAGE_BLOB_LIST_KEY,
(uint8_t *)legacy_items,
sizeof(legacy_items)));
lisa_kv_get_blob_fake.call_count = 0;
wifi_storage_deinit(&s_ctx);
init_storage();
wifi_mgr_sta_config_t *matched_list = NULL;
int count = wifi_storage_search_ap(&s_ctx, &matched_list, SEARCH_ALL, NULL);
TEST_ASSERT_EQUAL(1, count);
TEST_ASSERT_NOT_NULL(matched_list);
TEST_ASSERT_EQUAL_STRING("KVSSID", matched_list[0].ssid);
TEST_ASSERT_EQUAL_STRING("11:22:33:44:55:66", matched_list[0].bssid);
TEST_ASSERT_EQUAL(3, matched_list[0].channel);
TEST_ASSERT_TRUE(lisa_kv_get_blob_fake.call_count > 0);
test_free(matched_list);
/* Legacy data should be removed after migration attempt */
int blob_count = 0;
TEST_ASSERT_NOT_EQUAL(0, lisa_kv_get_int(WIFI_STORAGE_BLOB_COUNT_KEY, &blob_count));
}
void test_wifi_storage_save_ap_ssid_only_should_update_when_ssid_exists(void)
{
set_kv_item(0, "ExistingSSID", "00:11:22:33:44:55");
set_kv_count(1);
wifi_mgr_sta_config_t ap = {0};
strcpy(ap.ssid, "ExistingSSID");
strcpy(ap.pwd, "NewPwd");
TEST_ASSERT_EQUAL(0, wifi_storage_save_ap(&s_ctx, &ap));
wifi_mgr_sta_config_t *matched_list = NULL;
int count = wifi_storage_search_ap(&s_ctx, &matched_list, SEARCH_BY_SSID, ap.ssid);
TEST_ASSERT_EQUAL(1, count);
TEST_ASSERT_EQUAL_STRING("00:11:22:33:44:55", matched_list[0].bssid);
TEST_ASSERT_EQUAL_STRING("NewPwd", matched_list[0].pwd);
test_free(matched_list);
}
void test_wifi_storage_save_ap_with_new_bssid_should_succeed(void)
{
set_kv_item(0, "ExistingSSID", "00:11:22:33:44:55");
set_kv_count(1);
wifi_mgr_sta_config_t ap = {0};
strcpy(ap.ssid, "ExistingSSID");
strcpy(ap.bssid, "66:77:88:99:aa:bb");
strcpy(ap.pwd, "NewPwd");
TEST_ASSERT_EQUAL(0, wifi_storage_save_ap(&s_ctx, &ap));
wifi_mgr_sta_config_t *matched_list = NULL;
int count = wifi_storage_search_ap(&s_ctx, &matched_list, SEARCH_BY_SSID, ap.ssid);
TEST_ASSERT_EQUAL(2, count);
test_free(matched_list);
}
void test_wifi_storage_save_ap_with_bssid_should_update_ssid_only_record(void)
{
set_kv_item(0, "SameSSID", "");
set_kv_count(1);
wifi_mgr_sta_config_t ap = {0};
strcpy(ap.ssid, "SameSSID");
strcpy(ap.bssid, "66:77:88:99:aa:bb");
strcpy(ap.pwd, "NewPwd");
TEST_ASSERT_EQUAL(0, wifi_storage_save_ap(&s_ctx, &ap));
wifi_mgr_sta_config_t *matched_list = NULL;
int count = wifi_storage_search_ap(&s_ctx, &matched_list, SEARCH_BY_SSID, ap.ssid);
TEST_ASSERT_EQUAL(1, count);
TEST_ASSERT_EQUAL_STRING("66:77:88:99:aa:bb", matched_list[0].bssid);
TEST_ASSERT_EQUAL_STRING("NewPwd", matched_list[0].pwd);
test_free(matched_list);
}
static int fail_kv_set_int_on_count_key(const char *key, int value)
{
if (strcmp(key, WIFI_STORAGE_KV_COUNT_KEY) == 0) {
return -EIO;
}
if (s_kv_set_int_passthrough != NULL) {
return s_kv_set_int_passthrough(key, value);
}
return 0;
}
void test_wifi_storage_migrate_kv_save_fail_should_free_list(void)
{
wifi_storage_item_legacy_v1_t legacy_items[1] = {0};
strcpy(legacy_items[0].ap_info.ssid, "LegacySSID");
legacy_items[0].enable = 1;
TEST_ASSERT_EQUAL(0, lisa_kv_set_int(WIFI_STORAGE_BLOB_COUNT_KEY, 1));
TEST_ASSERT_EQUAL(0, lisa_kv_set_blob(WIFI_STORAGE_BLOB_LIST_KEY,
(uint8_t *)legacy_items,
sizeof(legacy_items)));
wifi_storage_ops_t ops = {
.malloc = test_malloc,
.calloc = test_calloc_track,
.free = test_free_track,
.mutex_lock = test_mutex_lock,
.mutex_unlock = test_mutex_unlock,
};
wifi_storage_deinit(&s_ctx);
init_storage_with_ops(&ops);
lisa_kv_set_int_fake.custom_fake = fail_kv_set_int_on_count_key;
wifi_storage_cache_drop(&s_ctx);
lisa_kv_del(WIFI_STORAGE_KV_COUNT_KEY);
wifi_storage_item_t *out_list = NULL;
uint32_t out_count = 0;
int ret = wifi_storage_migrate_load_list(&s_ctx, &out_list, &out_count);
TEST_ASSERT_NOT_EQUAL(0, ret);
TEST_ASSERT_NULL(out_list);
TEST_ASSERT_EQUAL(0u, out_count);
TEST_ASSERT_EQUAL(s_alloc_count, s_free_count);
}
void test_wifi_storage_init_should_load_cache_once(void)
{
wifi_storage_deinit(&s_ctx);
mock_lisa_kv_reset();
wifi_storage_cache_drop(&s_ctx);
set_kv_item(0, "CacheSSID0", "00:11:22:33:44:55");
set_kv_item(1, "CacheSSID1", "66:77:88:99:aa:bb");
set_kv_count(2);
init_storage();
TEST_ASSERT_TRUE(s_ctx.cache.loaded);
TEST_ASSERT_EQUAL(2u, s_ctx.cache.count);
TEST_ASSERT_EQUAL(2u, s_ctx.list_item_count);
TEST_ASSERT_EQUAL_STRING("CacheSSID0", s_ctx.cache.list[0].ap_info.ssid);
TEST_ASSERT_EQUAL_STRING("CacheSSID1", s_ctx.cache.list[1].ap_info.ssid);
}
void test_wifi_storage_init_should_migrate_blob_on_first_init(void)
{
wifi_storage_deinit(&s_ctx);
mock_lisa_kv_reset();
wifi_storage_cache_drop(&s_ctx);
wifi_storage_item_legacy_v1_t legacy_items[2] = {0};
strcpy(legacy_items[0].ap_info.ssid, "LegacyInit1");
legacy_items[0].enable = 1;
strcpy(legacy_items[1].ap_info.ssid, "LegacyInit2");
legacy_items[1].enable = 1;
TEST_ASSERT_EQUAL(0, lisa_kv_set_int(WIFI_STORAGE_BLOB_COUNT_KEY, 2));
TEST_ASSERT_EQUAL(0, lisa_kv_set_blob(WIFI_STORAGE_BLOB_LIST_KEY,
(uint8_t *)legacy_items,
sizeof(legacy_items)));
init_storage();
int kv_count = 0;
TEST_ASSERT_EQUAL(0, lisa_kv_get_int(WIFI_STORAGE_KV_COUNT_KEY, &kv_count));
TEST_ASSERT_EQUAL(2, kv_count);
TEST_ASSERT_TRUE(s_ctx.cache.loaded);
TEST_ASSERT_EQUAL(2u, s_ctx.cache.count);
TEST_ASSERT_EQUAL_STRING("LegacyInit1", s_ctx.cache.list[0].ap_info.ssid);
TEST_ASSERT_EQUAL_STRING("LegacyInit2", s_ctx.cache.list[1].ap_info.ssid);
/* Verify legacy data is deleted after migration */
int blob_count = 0;
TEST_ASSERT_NOT_EQUAL(0, lisa_kv_get_int(WIFI_STORAGE_BLOB_COUNT_KEY, &blob_count));
uint8_t *blob = NULL;
int blob_len = 0;
TEST_ASSERT_NOT_EQUAL(0, lisa_kv_get_blob(WIFI_STORAGE_BLOB_LIST_KEY, &blob, &blob_len));
}
void test_wifi_storage_init_should_merge_legacy_even_if_kv_exists(void)
{
wifi_storage_deinit(&s_ctx);
mock_lisa_kv_reset();
wifi_storage_cache_drop(&s_ctx);
/* Setup existing KV data */
set_kv_item(0, "KVSSID", "");
set_kv_item(1, "BSSIDPreferred", "11:22:33:44:55:66");
TEST_ASSERT_EQUAL(0, lisa_kv_set_int("wifi-kv-item-0-channel", 5));
TEST_ASSERT_EQUAL(0, lisa_kv_set_int("wifi-kv-item-1-channel", 9));
set_kv_count(2);
/* Setup legacy data with duplicates and a new item */
wifi_storage_item_legacy_v1_t legacy_items[3] = {0};
strcpy(legacy_items[0].ap_info.ssid, "KVSSID"); /* duplicate by SSID */
legacy_items[0].enable = 1;
strcpy(legacy_items[1].ap_info.ssid, "OtherName"); /* duplicate by BSSID */
strcpy(legacy_items[1].ap_info.bssid, "11:22:33:44:55:66");
legacy_items[1].enable = 1;
strcpy(legacy_items[2].ap_info.ssid, "LegacyNew"); /* new item */
strcpy(legacy_items[2].ap_info.bssid, "aa:bb:cc:dd:ee:ff");
legacy_items[2].ap_info.channel = 13;
legacy_items[2].enable = 1;
TEST_ASSERT_EQUAL(0, lisa_kv_set_int(WIFI_STORAGE_BLOB_COUNT_KEY, 3));
TEST_ASSERT_EQUAL(0, lisa_kv_set_blob(WIFI_STORAGE_BLOB_LIST_KEY,
(uint8_t *)legacy_items,
sizeof(legacy_items)));
init_storage();
wifi_mgr_sta_config_t *list = NULL;
int count = wifi_storage_search_ap(&s_ctx, &list, SEARCH_ALL, NULL);
TEST_ASSERT_EQUAL(3, count);
bool found_kv_ssid = false;
bool found_kv_bssid = false;
bool found_new = false;
for (int i = 0; i < count; i++) {
if (strcmp(list[i].ssid, "KVSSID") == 0) {
found_kv_ssid = true;
TEST_ASSERT_EQUAL_STRING("", list[i].bssid);
TEST_ASSERT_EQUAL(5, list[i].channel);
}
if (strcmp(list[i].bssid, "11:22:33:44:55:66") == 0) {
found_kv_bssid = true;
TEST_ASSERT_EQUAL_STRING("BSSIDPreferred", list[i].ssid);
TEST_ASSERT_EQUAL(9, list[i].channel);
}
if (strcmp(list[i].ssid, "LegacyNew") == 0) {
found_new = true;
TEST_ASSERT_EQUAL_STRING("aa:bb:cc:dd:ee:ff", list[i].bssid);
TEST_ASSERT_EQUAL(13, list[i].channel);
}
}
TEST_ASSERT_TRUE(found_kv_ssid);
TEST_ASSERT_TRUE(found_kv_bssid);
TEST_ASSERT_TRUE(found_new);
test_free(list);
/* Legacy data should be deleted after migration */
int blob_count = 0;
TEST_ASSERT_NOT_EQUAL(0, lisa_kv_get_int(WIFI_STORAGE_BLOB_COUNT_KEY, &blob_count));
}
static void set_kv_item(int index, const char *ssid, const char *bssid)
{
char key[64];
wifi_storage_cache_drop(&s_ctx);
snprintf(key, sizeof(key), "wifi-kv-item-%d-enable", index);
TEST_ASSERT_EQUAL(0, lisa_kv_set_bool(key, true));
snprintf(key, sizeof(key), "wifi-kv-item-%d-ssid", index);
TEST_ASSERT_EQUAL(0, lisa_kv_set_string(key, ssid));
snprintf(key, sizeof(key), "wifi-kv-item-%d-bssid", index);
TEST_ASSERT_EQUAL(0, lisa_kv_set_string(key, bssid));
}
static void set_kv_count(int count)
{
wifi_storage_cache_drop(&s_ctx);
TEST_ASSERT_EQUAL(0, lisa_kv_set_int(WIFI_STORAGE_KV_COUNT_KEY, count));
}
void test_wifi_storage_delete_match_ssid_and_bssid(void)
{
set_kv_item(0, "SameSSID", "00:11:22:33:44:55");
set_kv_item(1, "SameSSID", "66:77:88:99:aa:bb");
set_kv_count(2);
wifi_mgr_sta_config_t ap = {0};
strcpy(ap.ssid, "SameSSID");
strcpy(ap.bssid, "66:77:88:99:aa:bb");
TEST_ASSERT_EQUAL(0, wifi_storage_delete_ap(&s_ctx, &ap));
wifi_mgr_sta_config_t *matched_list = NULL;
int count = wifi_storage_search_ap(&s_ctx, &matched_list, SEARCH_BY_SSID, ap.ssid);
TEST_ASSERT_EQUAL(1, count);
TEST_ASSERT_EQUAL_STRING("00:11:22:33:44:55", matched_list[0].bssid);
test_free(matched_list);
}
void test_wifi_storage_delete_match_ssid_only_should_remove_all_ssid(void)
{
set_kv_item(0, "SameSSID", "00:11:22:33:44:55");
set_kv_item(1, "SameSSID", "66:77:88:99:aa:bb");
set_kv_count(2);
wifi_mgr_sta_config_t ap = {0};
strcpy(ap.ssid, "SameSSID");
TEST_ASSERT_EQUAL(0, wifi_storage_delete_ap(&s_ctx, &ap));
wifi_mgr_sta_config_t *matched_list = NULL;
int count = wifi_storage_search_ap(&s_ctx, &matched_list, SEARCH_BY_SSID, ap.ssid);
TEST_ASSERT_EQUAL(0, count);
TEST_ASSERT_NULL(matched_list);
}
void test_wifi_storage_delete_match_bssid_only_should_remove_one(void)
{
set_kv_item(0, "SSID1", "00:11:22:33:44:55");
set_kv_item(1, "SSID2", "66:77:88:99:aa:bb");
set_kv_count(2);
wifi_mgr_sta_config_t ap = {0};
strcpy(ap.bssid, "66:77:88:99:aa:bb");
TEST_ASSERT_EQUAL(0, wifi_storage_delete_ap(&s_ctx, &ap));
wifi_mgr_sta_config_t *matched_list = NULL;
int count = wifi_storage_search_ap(&s_ctx, &matched_list, SEARCH_ALL, NULL);
TEST_ASSERT_EQUAL(1, count);
TEST_ASSERT_EQUAL_STRING("00:11:22:33:44:55", matched_list[0].bssid);
test_free(matched_list);
}
void test_wifi_storage_search_ap_alloc_fail_should_return_enomem(void)
{
wifi_storage_ops_t ops = {
.malloc = test_malloc,
.calloc = test_calloc_fail_bool,
.free = test_free,
.mutex_lock = test_mutex_lock,
.mutex_unlock = test_mutex_unlock,
};
wifi_storage_deinit(&s_ctx);
init_storage_with_ops(&ops);
set_kv_item(0, "SSID1", "00:11:22:33:44:55");
set_kv_count(1);
s_fail_bool_alloc = true;
wifi_mgr_sta_config_t *matched_list = NULL;
int count = wifi_storage_search_ap(&s_ctx, &matched_list, SEARCH_ALL, NULL);
s_fail_bool_alloc = false;
TEST_ASSERT_EQUAL(-ENOMEM, count);
TEST_ASSERT_NULL(matched_list);
}
void test_wifi_storage_search_ap_uses_cache_for_repeated_calls(void)
{
set_kv_item(0, "CachedSSID", "11:22:33:44:55:66");
set_kv_count(1);
wifi_mgr_sta_config_t *matched_list = NULL;
int count = wifi_storage_search_ap(&s_ctx, &matched_list, SEARCH_BY_SSID, "CachedSSID");
TEST_ASSERT_EQUAL(1, count);
test_free(matched_list);
int get_int_calls = lisa_kv_get_int_fake.call_count;
int get_string_calls = lisa_kv_get_string_fake.call_count;
int get_blob_calls = lisa_kv_get_blob_fake.call_count;
matched_list = NULL;
count = wifi_storage_search_ap(&s_ctx, &matched_list, SEARCH_BY_SSID, "CachedSSID");
TEST_ASSERT_EQUAL(1, count);
test_free(matched_list);
TEST_ASSERT_EQUAL(get_int_calls, lisa_kv_get_int_fake.call_count);
TEST_ASSERT_EQUAL(get_string_calls, lisa_kv_get_string_fake.call_count);
TEST_ASSERT_EQUAL(get_blob_calls, lisa_kv_get_blob_fake.call_count);
}
void test_wifi_storage_empty_cache_should_not_reload_kv(void)
{
mock_lisa_kv_reset();
wifi_storage_cache_drop(&s_ctx);
/* Explicitly set empty KV count */
TEST_ASSERT_EQUAL(0, lisa_kv_set_int(WIFI_STORAGE_KV_COUNT_KEY, 0));
int get_int_calls_before = lisa_kv_get_int_fake.call_count;
wifi_mgr_sta_config_t *matched_list = NULL;
int count = wifi_storage_search_ap(&s_ctx, &matched_list, SEARCH_ALL, NULL);
TEST_ASSERT_EQUAL(0, count);
TEST_ASSERT_NULL(matched_list);
int get_int_calls_after_first = lisa_kv_get_int_fake.call_count;
matched_list = NULL;
count = wifi_storage_search_ap(&s_ctx, &matched_list, SEARCH_ALL, NULL);
TEST_ASSERT_EQUAL(0, count);
TEST_ASSERT_NULL(matched_list);
TEST_ASSERT_EQUAL(get_int_calls_after_first, lisa_kv_get_int_fake.call_count);
TEST_ASSERT_TRUE(s_ctx.cache.loaded);
TEST_ASSERT_TRUE(s_ctx.cache.loaded);
}
int main(void)
{
UNITY_BEGIN();
RUN_TEST(test_wifi_storage_kv_round_trip);
RUN_TEST(test_wifi_storage_migrate_blob_to_kv);
RUN_TEST(test_wifi_storage_kv_preferred_over_blob);
RUN_TEST(test_wifi_storage_save_ap_ssid_only_should_update_when_ssid_exists);
RUN_TEST(test_wifi_storage_save_ap_with_new_bssid_should_succeed);
RUN_TEST(test_wifi_storage_save_ap_with_bssid_should_update_ssid_only_record);
RUN_TEST(test_wifi_storage_migrate_kv_save_fail_should_free_list);
RUN_TEST(test_wifi_storage_init_should_load_cache_once);
RUN_TEST(test_wifi_storage_init_should_migrate_blob_on_first_init);
RUN_TEST(test_wifi_storage_init_should_merge_legacy_even_if_kv_exists);
RUN_TEST(test_wifi_storage_delete_match_ssid_and_bssid);
RUN_TEST(test_wifi_storage_delete_match_ssid_only_should_remove_all_ssid);
RUN_TEST(test_wifi_storage_delete_match_bssid_only_should_remove_one);
RUN_TEST(test_wifi_storage_search_ap_alloc_fail_should_return_enomem);
RUN_TEST(test_wifi_storage_search_ap_uses_cache_for_repeated_calls);
return UNITY_END();
}

View File

@@ -0,0 +1,145 @@
/**
* Copyright (c) 2025, LISTENAI
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "unity.h"
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include "wifi_manager/wifi_manager_storage.h"
#include "wifi_manager_storage_cache.h"
#include "wifi_manager_storage_internal.h"
static int s_loader_call_count;
static int s_free_count;
static void *test_malloc(size_t size)
{
return malloc(size);
}
static void *test_calloc(size_t nmemb, size_t size)
{
return calloc(nmemb, size);
}
static void test_free(void *ptr)
{
if (ptr) {
s_free_count++;
}
free(ptr);
}
static int loader_success(wifi_storage_ctx_t *ctx, wifi_storage_item_t **out_list, uint32_t *out_count)
{
s_loader_call_count++;
*out_list = WIFI_STORAGE_CALLOC(ctx, 1, sizeof(wifi_storage_item_t));
if (*out_list == NULL) {
return -ENOMEM;
}
(*out_list)[0].enable = 1;
strcpy((*out_list)[0].ap_info.ssid, "cached");
*out_count = 1;
return 0;
}
static int loader_fail(wifi_storage_ctx_t *ctx, wifi_storage_item_t **out_list, uint32_t *out_count)
{
(void)ctx;
(void)out_list;
(void)out_count;
s_loader_call_count++;
return -EIO;
}
static void init_ctx(wifi_storage_ctx_t *ctx)
{
memset(ctx, 0, sizeof(*ctx));
ctx->ops.malloc = test_malloc;
ctx->ops.calloc = test_calloc;
ctx->ops.free = test_free;
wifi_storage_cache_init_ctx(ctx);
}
void setUp(void)
{
s_loader_call_count = 0;
s_free_count = 0;
}
void tearDown(void)
{
}
void test_cache_load_caches_and_reuses(void)
{
wifi_storage_ctx_t ctx;
init_ctx(&ctx);
TEST_ASSERT_EQUAL(0, wifi_storage_cache_load(&ctx, loader_success));
TEST_ASSERT_TRUE(wifi_storage_cache_valid(&ctx));
wifi_storage_item_t *list = NULL;
uint32_t count = 0;
wifi_storage_cache_get(&ctx, &list, &count);
TEST_ASSERT_NOT_NULL(list);
TEST_ASSERT_EQUAL(1U, count);
TEST_ASSERT_EQUAL_STRING("cached", list[0].ap_info.ssid);
// Second load should not call loader again
TEST_ASSERT_EQUAL(0, wifi_storage_cache_load(&ctx, loader_success));
TEST_ASSERT_EQUAL(1, s_loader_call_count);
wifi_storage_cache_drop(&ctx);
TEST_ASSERT_EQUAL(1, s_free_count);
}
void test_cache_set_replaces_and_frees_old_list(void)
{
wifi_storage_ctx_t ctx;
init_ctx(&ctx);
wifi_storage_item_t *first = WIFI_STORAGE_CALLOC(&ctx, 1, sizeof(wifi_storage_item_t));
wifi_storage_item_t *second = WIFI_STORAGE_CALLOC(&ctx, 1, sizeof(wifi_storage_item_t));
wifi_storage_cache_set(&ctx, first, 1);
wifi_storage_cache_drop(&ctx); // free first
wifi_storage_cache_set(&ctx, second, 1);
TEST_ASSERT_TRUE(wifi_storage_cache_valid(&ctx));
wifi_storage_item_t *list = NULL;
uint32_t count = 0;
wifi_storage_cache_get(&ctx, &list, &count);
TEST_ASSERT_EQUAL(second, list);
TEST_ASSERT_EQUAL(1U, count);
wifi_storage_cache_drop(&ctx);
TEST_ASSERT_EQUAL(2, s_free_count); // second freed by drop
}
void test_cache_load_error_does_not_mark_valid(void)
{
wifi_storage_ctx_t ctx;
init_ctx(&ctx);
TEST_ASSERT_EQUAL(-EIO, wifi_storage_cache_load(&ctx, loader_fail));
TEST_ASSERT_FALSE(wifi_storage_cache_valid(&ctx));
wifi_storage_item_t *list = (wifi_storage_item_t *)0x1;
uint32_t count = 123;
wifi_storage_cache_get(&ctx, &list, &count);
TEST_ASSERT_NULL(list);
TEST_ASSERT_EQUAL(0U, count);
}
int main(void)
{
UNITY_BEGIN();
RUN_TEST(test_cache_load_caches_and_reuses);
RUN_TEST(test_cache_set_replaces_and_frees_old_list);
RUN_TEST(test_cache_load_error_does_not_mark_valid);
return UNITY_END();
}

View File

@@ -0,0 +1,7 @@
tests:
test.wifi_manager_storage:
runner: linux
builder: cmake
log_analyzer: "unity"

View File

@@ -0,0 +1,51 @@
cmake_minimum_required(VERSION 3.16)
project(wifi_manager_storage_cache_test C)
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_C_EXTENSIONS OFF)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_BUILD_TYPE Debug)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -rdynamic")
list(APPEND CMAKE_PREFIX_PATH "/usr/local/lib/cmake")
message(STATUS "CMAKE_PREFIX_PATH: ${CMAKE_PREFIX_PATH}")
find_package(unity QUIET)
if(unity_FOUND)
message(STATUS "Found Unity package")
set(UNITY_LIB unity::framework)
else()
message(FATAL_ERROR "Not found Unity package")
endif()
set(PRJ_ROOT ../..)
add_executable(tests)
set(EXECUTABLE_NAME tests)
target_compile_definitions(${EXECUTABLE_NAME} PRIVATE
UNIT_TEST
)
target_include_directories(tests PRIVATE
${PRJ_ROOT}/test/utils/unity_config
)
target_sources(tests PRIVATE
test_wifi_manager_storage_cache.c
${PRJ_ROOT}/src/wifi_manager_storage_cache.c
)
target_include_directories(tests PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}
${PRJ_ROOT}/src
${PRJ_ROOT}/include
${PRJ_ROOT}/include/wifi_manager
)
target_link_libraries(tests PRIVATE ${UNITY_LIB})
enable_testing()
add_test(NAME wifi_manager_storage_cache_tests COMMAND tests)

View File

@@ -0,0 +1,146 @@
#include "unity.h"
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include "wifi_manager/wifi_manager_storage.h"
#include "wifi_manager_storage_cache.h"
#include "wifi_manager_storage_internal.h"
static int s_loader_call_count;
static int s_free_count;
static void *test_malloc(size_t size)
{
return malloc(size);
}
static void *test_calloc(size_t nmemb, size_t size)
{
return calloc(nmemb, size);
}
static void test_free(void *ptr)
{
if (ptr != NULL) {
s_free_count++;
}
free(ptr);
}
static int loader_ok(wifi_storage_ctx_t *ctx, wifi_storage_item_t **out_list, uint32_t *out_count)
{
s_loader_call_count++;
*out_list = WIFI_STORAGE_CALLOC(ctx, 1, sizeof(wifi_storage_item_t));
if (*out_list == NULL) {
return -ENOMEM;
}
(*out_list)[0].enable = 1;
strcpy((*out_list)[0].ap_info.ssid, "cache_ssid");
*out_count = 1;
return 0;
}
static int loader_fail(wifi_storage_ctx_t *ctx, wifi_storage_item_t **out_list, uint32_t *out_count)
{
(void)ctx;
(void)out_list;
(void)out_count;
s_loader_call_count++;
return -EIO;
}
static void init_ctx(wifi_storage_ctx_t *ctx)
{
memset(ctx, 0, sizeof(*ctx));
ctx->ops.malloc = test_malloc;
ctx->ops.calloc = test_calloc;
ctx->ops.free = test_free;
ctx->ops.mutex_lock = NULL;
ctx->ops.mutex_unlock = NULL;
wifi_storage_cache_init_ctx(ctx);
}
void setUp(void)
{
s_loader_call_count = 0;
s_free_count = 0;
}
void tearDown(void)
{
}
void test_cache_load_then_reuse_without_second_loader(void)
{
wifi_storage_ctx_t ctx;
init_ctx(&ctx);
TEST_ASSERT_EQUAL(0, wifi_storage_cache_load(&ctx, loader_ok));
TEST_ASSERT_TRUE(wifi_storage_cache_valid(&ctx));
wifi_storage_item_t *list = NULL;
uint32_t count = 0;
wifi_storage_cache_get(&ctx, &list, &count);
TEST_ASSERT_NOT_NULL(list);
TEST_ASSERT_EQUAL(1U, count);
TEST_ASSERT_EQUAL_STRING("cache_ssid", list[0].ap_info.ssid);
TEST_ASSERT_EQUAL(1, s_loader_call_count);
TEST_ASSERT_EQUAL(0, wifi_storage_cache_load(&ctx, loader_ok));
TEST_ASSERT_EQUAL(1, s_loader_call_count);
wifi_storage_cache_drop(&ctx);
TEST_ASSERT_FALSE(wifi_storage_cache_valid(&ctx));
TEST_ASSERT_EQUAL(1, s_free_count);
}
void test_cache_drop_without_valid_list_is_safe(void)
{
wifi_storage_ctx_t ctx;
init_ctx(&ctx);
wifi_storage_cache_drop(&ctx);
TEST_ASSERT_EQUAL(0, s_free_count);
TEST_ASSERT_FALSE(wifi_storage_cache_valid(&ctx));
}
void test_cache_load_error_does_not_set_valid(void)
{
wifi_storage_ctx_t ctx;
init_ctx(&ctx);
TEST_ASSERT_EQUAL(-EIO, wifi_storage_cache_load(&ctx, loader_fail));
TEST_ASSERT_FALSE(wifi_storage_cache_valid(&ctx));
wifi_storage_item_t *list = (wifi_storage_item_t *)0x1;
uint32_t count = 123;
wifi_storage_cache_get(&ctx, &list, &count);
TEST_ASSERT_NULL(list);
TEST_ASSERT_EQUAL(0U, count);
}
void test_cache_set_null_marks_empty_but_valid(void)
{
wifi_storage_ctx_t ctx;
init_ctx(&ctx);
wifi_storage_cache_set(&ctx, NULL, 0);
TEST_ASSERT_TRUE(wifi_storage_cache_valid(&ctx));
wifi_storage_item_t *list = (wifi_storage_item_t *)0x1;
uint32_t count = 123;
wifi_storage_cache_get(&ctx, &list, &count);
TEST_ASSERT_NULL(list);
TEST_ASSERT_EQUAL(0U, count);
}
int main(void)
{
UNITY_BEGIN();
RUN_TEST(test_cache_load_then_reuse_without_second_loader);
RUN_TEST(test_cache_drop_without_valid_list_is_safe);
RUN_TEST(test_cache_load_error_does_not_set_valid);
RUN_TEST(test_cache_set_null_marks_empty_but_valid);
return UNITY_END();
}

View File

@@ -0,0 +1,5 @@
tests:
test.wifi_manager_storage_cache:
runner: linux
builder: cmake
log_analyzer: "unity"

View File

@@ -0,0 +1,61 @@
cmake_minimum_required(VERSION 3.16)
project(wifi_manager_storage_migrate_test C)
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_C_EXTENSIONS OFF)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_BUILD_TYPE Debug)
set(DEBUG_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -rdynamic")
list(APPEND CMAKE_PREFIX_PATH "/usr/local/lib/cmake")
message(STATUS "CMAKE_PREFIX_PATH: ${CMAKE_PREFIX_PATH}")
find_package(unity QUIET)
if(unity_FOUND)
message(STATUS "Found Unity package")
set(UNITY_LIB unity::framework)
else()
message(FATAL_ERROR "Not found Unity package")
endif()
set(PRJ_ROOT ../..)
add_executable(tests)
set(EXECUTABLE_NAME tests)
target_compile_definitions(${EXECUTABLE_NAME} PRIVATE
UNIT_TEST
CONFIG_WIFI_DEV_STATION=1
)
target_include_directories(tests PRIVATE
${PRJ_ROOT}/test/utils/fff
${PRJ_ROOT}/test/utils/unity_config
)
target_include_directories(tests PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}
${PRJ_ROOT}/src
${PRJ_ROOT}/include
${PRJ_ROOT}/include/wifi_manager
${PRJ_ROOT}/test/common/fake_lisa_log
)
target_sources(tests PRIVATE
test_wifi_manager_storage_migrate.c
mock_storage_deps.c
${PRJ_ROOT}/src/wifi_manager_storage_migrate.c
)
add_subdirectory(${PRJ_ROOT}/test/common/mock_lisa_kv mock_lisa_kv)
target_link_libraries(tests PRIVATE
${UNITY_LIB}
)
enable_testing()
add_test(NAME wifi_manager_storage_migrate_tests COMMAND tests)

View File

@@ -0,0 +1,17 @@
/**
* Copyright (c) 2025, LISTENAI
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "mock_storage_deps.h"
DEFINE_FAKE_VALUE_FUNC(int, wifi_storage_blob_legacy_load_list, wifi_storage_ctx_t *, wifi_storage_item_t **, uint32_t *);
DEFINE_FAKE_VALUE_FUNC(int, wifi_storage_kv_load_list, wifi_storage_ctx_t *, wifi_storage_item_t **, uint32_t *);
DEFINE_FAKE_VALUE_FUNC(int, wifi_storage_kv_save_list, wifi_storage_ctx_t *, const wifi_storage_item_t *, uint32_t);
void mock_storage_deps_reset(void)
{
RESET_FAKE(wifi_storage_blob_legacy_load_list);
RESET_FAKE(wifi_storage_kv_load_list);
RESET_FAKE(wifi_storage_kv_save_list);
}

View File

@@ -0,0 +1,23 @@
/**
* Copyright (c) 2025, LISTENAI
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include "fff.h"
#include "wifi_manager/wifi_manager_storage.h"
#ifdef __cplusplus
extern "C" {
#endif
DECLARE_FAKE_VALUE_FUNC(int, wifi_storage_blob_legacy_load_list, wifi_storage_ctx_t *, wifi_storage_item_t **, uint32_t *);
DECLARE_FAKE_VALUE_FUNC(int, wifi_storage_kv_load_list, wifi_storage_ctx_t *, wifi_storage_item_t **, uint32_t *);
DECLARE_FAKE_VALUE_FUNC(int, wifi_storage_kv_save_list, wifi_storage_ctx_t *, const wifi_storage_item_t *, uint32_t);
void mock_storage_deps_reset(void);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,411 @@
/**
* Copyright (c) 2025, LISTENAI
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "unity.h"
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "fff.h"
#include "wifi_manager/wifi_manager_storage.h"
#include "wifi_manager_storage_internal.h"
#include "mock_lisa_kv.h"
#include "mock_storage_deps.h"
DEFINE_FFF_GLOBALS;
static wifi_storage_ctx_t s_ctx;
static int s_mutex;
static int s_alloc_count;
static int s_free_count;
static int kv_load_no_data(wifi_storage_ctx_t *ctx, wifi_storage_item_t **out_list, uint32_t *out_count)
{
(void)ctx;
if (out_list != NULL) {
*out_list = NULL;
}
if (out_count != NULL) {
*out_count = 0;
}
return -ENOENT;
}
static void *test_malloc(size_t size)
{
return malloc(size);
}
static void *test_calloc(size_t nmemb, size_t size)
{
void *ptr = calloc(nmemb, size);
if (ptr != NULL) {
s_alloc_count++;
}
return ptr;
}
static void test_free(void *ptr)
{
if (ptr != NULL) {
s_free_count++;
}
free(ptr);
}
static int test_mutex_lock(void *mutex, uint32_t timeout)
{
(void)mutex;
(void)timeout;
return 0;
}
static int test_mutex_unlock(void *mutex)
{
(void)mutex;
return 0;
}
static void init_ctx(void)
{
wifi_storage_ops_t ops = {
.malloc = test_malloc,
.calloc = test_calloc,
.free = test_free,
.mutex_lock = test_mutex_lock,
.mutex_unlock = test_mutex_unlock,
};
memset(&s_ctx, 0, sizeof(s_ctx));
s_ctx.ops = ops;
s_ctx.mutex = &s_mutex;
}
void setUp(void)
{
mock_lisa_kv_reset();
mock_storage_deps_reset();
wifi_storage_kv_load_list_fake.custom_fake = kv_load_no_data;
s_alloc_count = 0;
s_free_count = 0;
init_ctx();
}
void tearDown(void)
{
}
/* ========== wifi_storage_migrate_load_count tests ========== */
void test_migrate_load_count_null_param_should_return_einval(void)
{
int ret = wifi_storage_migrate_load_count(NULL);
TEST_ASSERT_EQUAL(-EINVAL, ret);
}
void test_migrate_load_count_kv_count_found_should_return_count(void)
{
TEST_ASSERT_EQUAL(0, lisa_kv_set_int(WIFI_STORAGE_KV_COUNT_KEY, 5));
uint32_t count = 0;
int ret = wifi_storage_migrate_load_count(&count);
TEST_ASSERT_EQUAL(0, ret);
TEST_ASSERT_EQUAL(5u, count);
}
void test_migrate_load_count_blob_count_found_should_return_count(void)
{
TEST_ASSERT_EQUAL(0, lisa_kv_set_int(WIFI_STORAGE_BLOB_COUNT_KEY, 3));
uint32_t count = 0;
int ret = wifi_storage_migrate_load_count(&count);
TEST_ASSERT_EQUAL(0, ret);
TEST_ASSERT_EQUAL(3u, count);
}
void test_migrate_load_count_kv_preferred_over_blob(void)
{
TEST_ASSERT_EQUAL(0, lisa_kv_set_int(WIFI_STORAGE_KV_COUNT_KEY, 10));
TEST_ASSERT_EQUAL(0, lisa_kv_set_int(WIFI_STORAGE_BLOB_COUNT_KEY, 2));
uint32_t count = 0;
int ret = wifi_storage_migrate_load_count(&count);
TEST_ASSERT_EQUAL(0, ret);
TEST_ASSERT_EQUAL(10u, count);
}
void test_migrate_load_count_no_data_should_return_enoent(void)
{
uint32_t count = 99;
int ret = wifi_storage_migrate_load_count(&count);
TEST_ASSERT_EQUAL(-ENOENT, ret);
TEST_ASSERT_EQUAL(0u, count);
}
void test_migrate_load_count_negative_count_should_return_zero(void)
{
TEST_ASSERT_EQUAL(0, lisa_kv_set_int(WIFI_STORAGE_KV_COUNT_KEY, -5));
uint32_t count = 99;
int ret = wifi_storage_migrate_load_count(&count);
TEST_ASSERT_EQUAL(0, ret);
TEST_ASSERT_EQUAL(0u, count);
}
/* ========== wifi_storage_migrate_load_list tests ========== */
static wifi_storage_item_t s_mock_list[2];
static wifi_storage_item_t *s_allocated_list = NULL;
static int blob_load_success(wifi_storage_ctx_t *ctx, wifi_storage_item_t **out_list, uint32_t *out_count)
{
wifi_storage_item_t *list = ctx->ops.calloc(2, sizeof(wifi_storage_item_t));
if (list == NULL) {
return -ENOMEM;
}
memcpy(list, s_mock_list, sizeof(s_mock_list));
*out_list = list;
*out_count = 2;
return 0;
}
static int blob_load_with_alloc(wifi_storage_ctx_t *ctx, wifi_storage_item_t **out_list, uint32_t *out_count)
{
s_allocated_list = ctx->ops.calloc(1, sizeof(wifi_storage_item_t));
if (s_allocated_list == NULL) {
return -ENOMEM;
}
strcpy(s_allocated_list->ap_info.ssid, "AllocatedSSID");
s_allocated_list->enable = 1;
*out_list = s_allocated_list;
*out_count = 1;
return 0;
}
static int blob_load_with_empty_ssid(wifi_storage_ctx_t *ctx, wifi_storage_item_t **out_list, uint32_t *out_count)
{
wifi_storage_item_t *list = ctx->ops.calloc(2, sizeof(wifi_storage_item_t));
if (list == NULL) {
return -ENOMEM;
}
strcpy(list[0].ap_info.ssid, "TestSSID1");
list[0].enable = 1;
list[0].version = WIFI_STORAGE_VERSION_CURRENT;
list[1].version = WIFI_STORAGE_VERSION_LEGACY;
list[1].enable = 1;
*out_list = list;
*out_count = 2;
return 0;
}
static int kv_load_with_empty_ssid(wifi_storage_ctx_t *ctx, wifi_storage_item_t **out_list, uint32_t *out_count)
{
wifi_storage_item_t *list = ctx->ops.calloc(1, sizeof(wifi_storage_item_t));
if (list == NULL) {
return -ENOMEM;
}
list[0].enable = 1;
/* ssid left empty */
*out_list = list;
*out_count = 1;
return 0;
}
static int blob_load_single_valid(wifi_storage_ctx_t *ctx, wifi_storage_item_t **out_list, uint32_t *out_count)
{
wifi_storage_item_t *list = ctx->ops.calloc(1, sizeof(wifi_storage_item_t));
if (list == NULL) {
return -ENOMEM;
}
strcpy(list[0].ap_info.ssid, "LegacyValid");
list[0].enable = 1;
*out_list = list;
*out_count = 1;
return 0;
}
void test_migrate_load_list_null_params_should_free_list_and_succeed(void)
{
wifi_storage_blob_legacy_load_list_fake.custom_fake = blob_load_with_alloc;
wifi_storage_kv_save_list_fake.return_val = 0;
int ret = wifi_storage_migrate_load_list(&s_ctx, NULL, NULL);
TEST_ASSERT_EQUAL(0, ret);
TEST_ASSERT_EQUAL(1, wifi_storage_kv_save_list_fake.call_count);
TEST_ASSERT_EQUAL(s_alloc_count, s_free_count);
}
void test_migrate_load_list_success_should_return_list_and_save_to_kv(void)
{
memset(s_mock_list, 0, sizeof(s_mock_list));
strcpy(s_mock_list[0].ap_info.ssid, "TestSSID1");
s_mock_list[0].enable = 1;
strcpy(s_mock_list[1].ap_info.ssid, "TestSSID2");
s_mock_list[1].enable = 1;
wifi_storage_blob_legacy_load_list_fake.custom_fake = blob_load_success;
wifi_storage_kv_save_list_fake.return_val = 0;
wifi_storage_item_t *out_list = NULL;
uint32_t out_count = 0;
int ret = wifi_storage_migrate_load_list(&s_ctx, &out_list, &out_count);
TEST_ASSERT_EQUAL(0, ret);
TEST_ASSERT_EQUAL(2u, out_count);
TEST_ASSERT_NOT_NULL(out_list);
TEST_ASSERT_EQUAL_STRING("TestSSID1", out_list[0].ap_info.ssid);
TEST_ASSERT_EQUAL_STRING("TestSSID2", out_list[1].ap_info.ssid);
TEST_ASSERT_EQUAL(1, wifi_storage_kv_save_list_fake.call_count);
}
void test_migrate_load_list_blob_load_fail_should_return_error(void)
{
wifi_storage_blob_legacy_load_list_fake.return_val = -ENOENT;
wifi_storage_item_t *out_list = NULL;
uint32_t out_count = 0;
int ret = wifi_storage_migrate_load_list(&s_ctx, &out_list, &out_count);
TEST_ASSERT_EQUAL(-ENOENT, ret);
TEST_ASSERT_NULL(out_list);
TEST_ASSERT_EQUAL(0u, out_count);
TEST_ASSERT_EQUAL(0, wifi_storage_kv_save_list_fake.call_count);
}
void test_migrate_load_list_kv_save_fail_should_free_list(void)
{
s_allocated_list = NULL;
wifi_storage_blob_legacy_load_list_fake.custom_fake = blob_load_with_alloc;
wifi_storage_kv_save_list_fake.return_val = -EIO;
wifi_storage_item_t *out_list = NULL;
uint32_t out_count = 0;
int ret = wifi_storage_migrate_load_list(&s_ctx, &out_list, &out_count);
TEST_ASSERT_NOT_EQUAL(0, ret);
TEST_ASSERT_NULL(out_list);
TEST_ASSERT_EQUAL(0u, out_count);
TEST_ASSERT_EQUAL(s_alloc_count, s_free_count);
}
void test_migrate_load_list_should_filter_empty_ssid(void)
{
wifi_storage_blob_legacy_load_list_fake.custom_fake = blob_load_with_empty_ssid;
wifi_storage_kv_save_list_fake.return_val = 0;
wifi_storage_item_t *out_list = NULL;
uint32_t out_count = 0;
int ret = wifi_storage_migrate_load_list(&s_ctx, &out_list, &out_count);
TEST_ASSERT_EQUAL(0, ret);
TEST_ASSERT_EQUAL(1u, out_count);
TEST_ASSERT_NOT_NULL(out_list);
TEST_ASSERT_EQUAL_STRING("TestSSID1", out_list[0].ap_info.ssid);
TEST_ASSERT_EQUAL(1, wifi_storage_kv_save_list_fake.call_count);
TEST_ASSERT_EQUAL(1u, wifi_storage_kv_save_list_fake.arg2_val);
const wifi_storage_item_t *saved_list = wifi_storage_kv_save_list_fake.arg1_val;
TEST_ASSERT_NOT_NULL(saved_list);
TEST_ASSERT_EQUAL_STRING("TestSSID1", saved_list[0].ap_info.ssid);
TEST_ASSERT_EQUAL(1, saved_list[0].enable);
}
void test_migrate_load_list_should_drop_empty_ssid_in_kv(void)
{
wifi_storage_blob_legacy_load_list_fake.custom_fake = blob_load_single_valid;
wifi_storage_kv_load_list_fake.custom_fake = kv_load_with_empty_ssid;
wifi_storage_kv_save_list_fake.return_val = 0;
wifi_storage_item_t *out_list = NULL;
uint32_t out_count = 0;
int ret = wifi_storage_migrate_load_list(&s_ctx, &out_list, &out_count);
TEST_ASSERT_EQUAL(0, ret);
TEST_ASSERT_EQUAL(1u, out_count);
TEST_ASSERT_NOT_NULL(out_list);
TEST_ASSERT_EQUAL_STRING("LegacyValid", out_list[0].ap_info.ssid);
TEST_ASSERT_NOT_NULL(wifi_storage_kv_save_list_fake.arg1_val);
TEST_ASSERT_EQUAL_STRING("LegacyValid", wifi_storage_kv_save_list_fake.arg1_val[0].ap_info.ssid);
WIFI_STORAGE_FREE(&s_ctx, out_list);
}
void test_migrate_load_list_blob_eio_should_return_eio(void)
{
wifi_storage_blob_legacy_load_list_fake.return_val = -EIO;
wifi_storage_item_t *out_list = NULL;
uint32_t out_count = 0;
int ret = wifi_storage_migrate_load_list(&s_ctx, &out_list, &out_count);
TEST_ASSERT_EQUAL(-EIO, ret);
TEST_ASSERT_NULL(out_list);
TEST_ASSERT_EQUAL(0u, out_count);
}
/* ========== wifi_storage_migrate_delete_legacy tests ========== */
void test_migrate_delete_legacy_should_delete_blob_keys(void)
{
TEST_ASSERT_EQUAL(0, lisa_kv_set_int(WIFI_STORAGE_BLOB_COUNT_KEY, 2));
TEST_ASSERT_EQUAL(0, lisa_kv_set_blob(WIFI_STORAGE_BLOB_LIST_KEY, (uint8_t *)"test", 4));
int count_before = 0;
TEST_ASSERT_EQUAL(0, lisa_kv_get_int(WIFI_STORAGE_BLOB_COUNT_KEY, &count_before));
TEST_ASSERT_EQUAL(2, count_before);
wifi_storage_migrate_delete_legacy();
int count_after = 0;
TEST_ASSERT_NOT_EQUAL(0, lisa_kv_get_int(WIFI_STORAGE_BLOB_COUNT_KEY, &count_after));
uint8_t *blob = NULL;
int blob_len = 0;
TEST_ASSERT_NOT_EQUAL(0, lisa_kv_get_blob(WIFI_STORAGE_BLOB_LIST_KEY, &blob, &blob_len));
}
void test_migrate_delete_legacy_should_not_affect_kv_keys(void)
{
TEST_ASSERT_EQUAL(0, lisa_kv_set_int(WIFI_STORAGE_KV_COUNT_KEY, 3));
TEST_ASSERT_EQUAL(0, lisa_kv_set_int(WIFI_STORAGE_BLOB_COUNT_KEY, 1));
wifi_storage_migrate_delete_legacy();
int kv_count = 0;
TEST_ASSERT_EQUAL(0, lisa_kv_get_int(WIFI_STORAGE_KV_COUNT_KEY, &kv_count));
TEST_ASSERT_EQUAL(3, kv_count);
}
int main(void)
{
UNITY_BEGIN();
/* migrate_load_count tests */
RUN_TEST(test_migrate_load_count_null_param_should_return_einval);
RUN_TEST(test_migrate_load_count_kv_count_found_should_return_count);
RUN_TEST(test_migrate_load_count_blob_count_found_should_return_count);
RUN_TEST(test_migrate_load_count_kv_preferred_over_blob);
RUN_TEST(test_migrate_load_count_no_data_should_return_enoent);
RUN_TEST(test_migrate_load_count_negative_count_should_return_zero);
/* migrate_load_list tests */
RUN_TEST(test_migrate_load_list_null_params_should_free_list_and_succeed);
RUN_TEST(test_migrate_load_list_success_should_return_list_and_save_to_kv);
RUN_TEST(test_migrate_load_list_blob_load_fail_should_return_error);
RUN_TEST(test_migrate_load_list_kv_save_fail_should_free_list);
RUN_TEST(test_migrate_load_list_should_filter_empty_ssid);
RUN_TEST(test_migrate_load_list_blob_eio_should_return_eio);
/* migrate_delete_legacy tests */
RUN_TEST(test_migrate_delete_legacy_should_delete_blob_keys);
RUN_TEST(test_migrate_delete_legacy_should_not_affect_kv_keys);
return UNITY_END();
}

View File

@@ -0,0 +1,5 @@
tests:
test.wifi_manager_storage_migrate:
runner: linux
builder: cmake
log_analyzer: "unity"