diff --git a/common_lib/impl/src/clib_types.c b/common_lib/impl/src/clib_types.c index 7b3c2af..c6ca169 100644 --- a/common_lib/impl/src/clib_types.c +++ b/common_lib/impl/src/clib_types.c @@ -14,7 +14,7 @@ */ #include "clib_types.h" -#include +#include "securec.h" void *ClibMalloc(uint32_t size, char val) { diff --git a/common_lib/impl/src/hc_parcel.c b/common_lib/impl/src/hc_parcel.c index 247acc5..1c9fb59 100644 --- a/common_lib/impl/src/hc_parcel.c +++ b/common_lib/impl/src/hc_parcel.c @@ -29,7 +29,7 @@ HcParcel CreateParcel(uint32_t size, uint32_t allocUnit) parcel.allocUnit = PARCEL_DEFAULT_INCREASE_STEP; } if (size > 0) { - parcel.data = (char*)ClibMalloc(size, 0); + parcel.data = (char *)ClibMalloc(size, 0); if (parcel.data != NULL) { parcel.length = size; } diff --git a/common_lib/impl/src/json_utils.c b/common_lib/impl/src/json_utils.c index 3c44500..7c7d806 100644 --- a/common_lib/impl/src/json_utils.c +++ b/common_lib/impl/src/json_utils.c @@ -16,7 +16,7 @@ #include "json_utils.h" #include #include -#include +#include "securec.h" #include "clib_error.h" #include "clib_types.h" #include "string_util.h" @@ -203,12 +203,7 @@ int32_t GetByteFromJson(const CJson *jsonObj, const char *key, uint8_t *byte, ui if (len < strlen(valueStr) / BYTE_TO_HEX_OPER_LENGTH) { return CLIB_ERR_INVALID_LEN; } - int32_t ret = HexStringToByte(valueStr, byte, len); - if (ret != CLIB_SUCCESS) { - return ret; - } - - return ret; + return HexStringToByte(valueStr, byte, len); } int32_t GetIntFromJson(const CJson *jsonObj, const char *key, int32_t *value) diff --git a/common_lib/impl/src/string_util.c b/common_lib/impl/src/string_util.c index 3eb8565..b08e51b 100644 --- a/common_lib/impl/src/string_util.c +++ b/common_lib/impl/src/string_util.c @@ -18,7 +18,7 @@ #include #include #include -#include +#include "securec.h" #include "clib_error.h" #include "clib_types.h" @@ -228,8 +228,7 @@ int32_t ByteToBase64String(const uint8_t *byte, uint32_t byteLen, char *base64St if (byteLen % 3 == 1) { base64Str[i - 2] = '='; base64Str[i - 1] = '='; - } - if (byteLen % 3 == 2) { + } else if (byteLen % 3 == 2) { base64Str[i - 1] = '='; } base64Str[len] = '\0'; diff --git a/deps_adapter/key_management_adapter/impl/src/small/mbedtls_hash_to_point.c b/deps_adapter/key_management_adapter/impl/src/small/mbedtls_hash_to_point.c index f617841..42e457d 100644 --- a/deps_adapter/key_management_adapter/impl/src/small/mbedtls_hash_to_point.c +++ b/deps_adapter/key_management_adapter/impl/src/small/mbedtls_hash_to_point.c @@ -16,7 +16,7 @@ #include "mbedtls_hash_to_point.h" #include #include -#include +#include "securec.h" #include "alg_defs.h" #include "hal_error.h" #include "hc_log.h" diff --git a/deps_adapter/os_adapter/impl/src/hc_log.c b/deps_adapter/os_adapter/impl/src/hc_log.c index 569d7ed..d973f6b 100644 --- a/deps_adapter/os_adapter/impl/src/hc_log.c +++ b/deps_adapter/os_adapter/impl/src/hc_log.c @@ -14,7 +14,7 @@ */ #include "hc_log.h" -#include +#include "securec.h" #define LOG_PRINT_MAX_LEN 256 diff --git a/deps_adapter/os_adapter/impl/src/linux/hc_file.c b/deps_adapter/os_adapter/impl/src/linux/hc_file.c index 1e7ffa3..4c0791a 100644 --- a/deps_adapter/os_adapter/impl/src/linux/hc_file.c +++ b/deps_adapter/os_adapter/impl/src/linux/hc_file.c @@ -19,9 +19,9 @@ #include #include #include -#include "securec.h" #include "hc_log.h" #include "hc_types.h" +#include "securec.h" #ifdef __cplusplus extern "C" { @@ -123,8 +123,8 @@ int HcFileRead(FileHandle file, void *dst, int dstSize) int total = 0; while (total < dstSize) { int readCount = fread(dstBuffer + total, 1, dstSize - total, fp); - if ((readCount < 0) || (readCount > (dstSize - total))) { - return -1; + if (ferror(fp) != 0) { + LOGE("read file error!"); } if (readCount == 0) { return total; @@ -146,8 +146,8 @@ int HcFileWrite(FileHandle file, const void *src, int srcSize) int total = 0; while (total < srcSize) { int writeCount = fwrite(srcBuffer + total, 1, srcSize - total, fp); - if (writeCount < 0 || writeCount > (srcSize - total)) { - return -1; + if (ferror(fp) != 0) { + LOGE("write file error!"); } total += writeCount; } @@ -196,6 +196,9 @@ void HcFileGetSubFileName(const char *path, StringVector *nameVec) DeleteString(&subFileName); } } + if (closedir(dir) < 0) { + LOGE("Failed to close file"); + } } #ifdef __cplusplus diff --git a/deps_adapter/os_adapter/impl/src/liteos/mini/hc_file.c b/deps_adapter/os_adapter/impl/src/liteos/mini/hc_file.c index d40b680..eced8f8 100644 --- a/deps_adapter/os_adapter/impl/src/liteos/mini/hc_file.c +++ b/deps_adapter/os_adapter/impl/src/liteos/mini/hc_file.c @@ -17,11 +17,11 @@ #include #include #include -#include #include #include #include "hal_error.h" #include "hc_log.h" +#include "securec.h" #include "utils_file.h" #define MAX_FOLDER_NAME_SIZE 128 @@ -136,8 +136,11 @@ void HcFileGetSubFileName(const char *path, StringVector *nameVec) continue; } if (nameVec->pushBackT(nameVec, subFileName) == NULL) { - LOGE("Failed to push path to pathVec!"); + LOGE("Failed to push name!"); DeleteString(&subFileName); } } + if (closedir(dir) < 0) { + LOGE("Failed to close file"); + } } \ No newline at end of file diff --git a/frameworks/deviceauth_lite/source/auth_info/add_auth_info.c b/frameworks/deviceauth_lite/source/auth_info/add_auth_info.c index 5354457..bba55a7 100644 --- a/frameworks/deviceauth_lite/source/auth_info/add_auth_info.c +++ b/frameworks/deviceauth_lite/source/auth_info/add_auth_info.c @@ -15,7 +15,7 @@ #include "add_auth_info.h" #include -#include +#include "securec.h" #include "log.h" #include "base.h" #include "mem_stat.h" @@ -192,7 +192,7 @@ int32_t import_signed_auth_info_hilink(const struct hichain *hichain, const stru uint32_t len = (data->length / BYTE_TO_HEX_OPER_LENGTH) + 1; uint8_t *receive_data = (uint8_t *)MALLOC(len); if (receive_data == NULL) { - LOGE("malloc inport signed auth info receive data failed"); + LOGE("malloc import signed auth info receive data failed"); return HC_MALLOC_FAILED; } (void)memset_s(receive_data, len, 0, len); diff --git a/frameworks/deviceauth_lite/source/auth_info/add_auth_info_client.c b/frameworks/deviceauth_lite/source/auth_info/add_auth_info_client.c index 900847a..19ee103 100755 --- a/frameworks/deviceauth_lite/source/auth_info/add_auth_info_client.c +++ b/frameworks/deviceauth_lite/source/auth_info/add_auth_info_client.c @@ -14,7 +14,7 @@ */ #include "add_auth_info_client.h" -#include +#include "securec.h" #include "huks_adapter.h" #include "log.h" #include "mem_stat.h" diff --git a/frameworks/deviceauth_lite/source/auth_info/auth_info.c b/frameworks/deviceauth_lite/source/auth_info/auth_info.c index 6064360..10e3ae3 100755 --- a/frameworks/deviceauth_lite/source/auth_info/auth_info.c +++ b/frameworks/deviceauth_lite/source/auth_info/auth_info.c @@ -14,7 +14,7 @@ */ #include "auth_info.h" -#include +#include "securec.h" #include "log.h" #include "mem_stat.h" #include "huks_adapter.h" @@ -78,7 +78,7 @@ int32_t encrypt_payload(const struct var_buffer *key, const struct uint8_buff *p struct aes_aad aes_aad; (void)memset_s(&aes_aad, sizeof(aes_aad), 0, sizeof(aes_aad)); if (strcpy_s((char *)aes_aad.aad, sizeof(aes_aad.aad), aad) != EOK) { - LOGE("What happened was that the probability was zero"); /* caller perceives memery error, no return */ + LOGE("What happened was that the probability was zero"); /* caller perceives memory error, no return */ } aes_aad.length = strlen(aad); int32_t ret = aes_gcm_encrypt((struct var_buffer *)key, plain, &aes_aad, payload); @@ -104,7 +104,7 @@ int32_t decrypt_payload(const struct var_buffer *key, const struct uint8_buff *p struct aes_aad aes_aad; (void)memset_s(&aes_aad, sizeof(aes_aad), 0, sizeof(aes_aad)); if (strcpy_s((char *)aes_aad.aad, sizeof(aes_aad.aad), aad) != EOK) { - LOGE("What happened was that the probability was zero"); /* caller perceives memery error, no return */ + LOGE("What happened was that the probability was zero"); /* caller perceives memory error, no return */ } aes_aad.length = strlen(aad); int32_t ret = aes_gcm_decrypt((struct var_buffer *)key, payload, &aes_aad, plain); diff --git a/frameworks/deviceauth_lite/source/auth_info/exchange_auth_info.c b/frameworks/deviceauth_lite/source/auth_info/exchange_auth_info.c index d57685c..21c460b 100755 --- a/frameworks/deviceauth_lite/source/auth_info/exchange_auth_info.c +++ b/frameworks/deviceauth_lite/source/auth_info/exchange_auth_info.c @@ -16,7 +16,7 @@ #if !(defined(_CUT_PAKE_) || defined(_CUT_PAKE_SERVER_) || defined(_CUT_EXCHANGE_) || defined(_CUT_EXCHANGE_SERVER_)) #include "exchange_auth_info.h" -#include +#include "securec.h" #include "log.h" #include "base.h" #include "mem_stat.h" diff --git a/frameworks/deviceauth_lite/source/auth_info/exchange_auth_info_client.c b/frameworks/deviceauth_lite/source/auth_info/exchange_auth_info_client.c index 90ce0e6..12c3c89 100755 --- a/frameworks/deviceauth_lite/source/auth_info/exchange_auth_info_client.c +++ b/frameworks/deviceauth_lite/source/auth_info/exchange_auth_info_client.c @@ -15,7 +15,7 @@ #if !(defined(_CUT_PAKE_) || defined(_CUT_PAKE_CLIENT_) || defined(_CUT_EXCHANGE_) || defined(_CUT_EXCHANGE_CLIENT_)) #include "exchange_auth_info_client.h" -#include +#include "securec.h" #include "log.h" #include "base.h" #include "mem_stat.h" diff --git a/frameworks/deviceauth_lite/source/auth_info/remove_auth_info_client.c b/frameworks/deviceauth_lite/source/auth_info/remove_auth_info_client.c index 60547a3..666707d 100755 --- a/frameworks/deviceauth_lite/source/auth_info/remove_auth_info_client.c +++ b/frameworks/deviceauth_lite/source/auth_info/remove_auth_info_client.c @@ -16,7 +16,7 @@ #include "remove_auth_info_client.h" #include #include -#include +#include "securec.h" #include "log.h" #include "base.h" #include "mem_stat.h" diff --git a/frameworks/deviceauth_lite/source/hichain.c b/frameworks/deviceauth_lite/source/hichain.c index 261aca7..ff7b638 100755 --- a/frameworks/deviceauth_lite/source/hichain.c +++ b/frameworks/deviceauth_lite/source/hichain.c @@ -13,7 +13,7 @@ * limitations under the License. */ -#include +#include "securec.h" #include "commonutil.h" #include "distribution.h" #include "log.h" @@ -242,7 +242,7 @@ int32_t init_center(const struct hc_package_name *package_name, const struct hc_ LOGE("Generate derived dek failed"); return ret; } - if (memcpy_s(dek, sizeof(struct hc_key_alias), &dek_alias, sizeof(struct hc_key_alias)) != HC_OK) { + if (memcpy_s(dek, sizeof(struct hc_key_alias), &dek_alias, sizeof(struct hc_key_alias)) != EOK) { return HC_INNER_ERROR; } @@ -1175,7 +1175,7 @@ static int32_t delete_public_key(hc_handle handle, struct service_id service_id, (void)memset_s(auth_id_list, length, 0, length); uint32_t peers_num = list_trust_peers(handle, user_type, NULL, &auth_id_list); - LOGI("peers_num %d", peers_num); + LOGI("peers_num %u", peers_num); for (uint32_t loop = 0; loop < peers_num; loop++) { struct hc_key_alias key_alias = generate_key_alias(&service_id, &auth_id_list[loop], user_type); if (key_alias.length == 0) { diff --git a/frameworks/deviceauth_lite/source/json/commonutil.c b/frameworks/deviceauth_lite/source/json/commonutil.c index 14bff63..6d04558 100755 --- a/frameworks/deviceauth_lite/source/json/commonutil.c +++ b/frameworks/deviceauth_lite/source/json/commonutil.c @@ -14,7 +14,7 @@ */ #include "commonutil.h" -#include +#include "securec.h" #include "base.h" #include "log.h" #include "mem_stat.h" @@ -22,7 +22,8 @@ char hex_to_char(uint8_t hex) { - return (hex > 9) ? hex + 0x37 : hex + 0x30; /* Convert to the corresponding character */ + /* Convert to the corresponding character, hex + 0x37 -> A-F, hex + 0x30 -> 0-9 */ + return (hex > 9) ? hex + 0x37 : hex + 0x30; } void byte_to_hex_string(const uint8_t *hex, int32_t hex_len, uint8_t *buf, int32_t buf_len) @@ -117,22 +118,6 @@ int32_t string_convert(json_pobject obj, const char *field, uint8_t *str, uint32 return HC_OK; } -void hex_string_convert(uint32_t length, const uint8_t *hex, const char *field, json_pobject parent) -{ - int32_t org_cha_len = length; - int32_t tmp_cha_data_hex_len = org_cha_len * BYTE_TO_HEX_OPER_LENGTH + 1; - uint8_t *tmp_cha_data_hex = (uint8_t *)MALLOC(tmp_cha_data_hex_len); - - if (tmp_cha_data_hex == NULL) { - return; - } - (void)memset_s(tmp_cha_data_hex, tmp_cha_data_hex_len, 0, tmp_cha_data_hex_len); - byte_to_hex_string(hex, org_cha_len, tmp_cha_data_hex, org_cha_len * BYTE_TO_HEX_OPER_LENGTH); - add_string_to_object(parent, field, (char *)tmp_cha_data_hex); - FREE(tmp_cha_data_hex); - tmp_cha_data_hex = NULL; -} - int32_t memory_copy_error(const char *fun, unsigned int line) { (void)fun; diff --git a/frameworks/deviceauth_lite/source/json/commonutil.h b/frameworks/deviceauth_lite/source/json/commonutil.h index fc88d0c..9356cea 100755 --- a/frameworks/deviceauth_lite/source/json/commonutil.h +++ b/frameworks/deviceauth_lite/source/json/commonutil.h @@ -28,7 +28,6 @@ void print_bytes(uint8_t *buf, int32_t buf_len); int32_t byte_convert(json_pobject obj, const char *field, uint8_t *hex, uint32_t *length, uint32_t max_len); int32_t string_convert(json_pobject obj, const char *field, uint8_t *str, uint32_t *length, uint32_t max_len); -void hex_string_convert(uint32_t length, const uint8_t *hex, const char *field, json_pobject parent); int32_t memory_copy_error(const char *fun, unsigned int line); diff --git a/frameworks/deviceauth_lite/source/json/jsonutil.c b/frameworks/deviceauth_lite/source/json/jsonutil.c index 9da8f34..1627507 100755 --- a/frameworks/deviceauth_lite/source/json/jsonutil.c +++ b/frameworks/deviceauth_lite/source/json/jsonutil.c @@ -27,10 +27,10 @@ json_handle parse_json(const char *data) return (void *)root; } -void free_json(json_handle hanlde) +void free_json(json_handle handle) { - if (hanlde != NULL) { - cJSON_Delete((cJSON *)hanlde); + if (handle != NULL) { + cJSON_Delete((cJSON *)handle); } } diff --git a/frameworks/deviceauth_lite/source/json/jsonutil.h b/frameworks/deviceauth_lite/source/json/jsonutil.h index 2ba362e..cf5fb9e 100755 --- a/frameworks/deviceauth_lite/source/json/jsonutil.h +++ b/frameworks/deviceauth_lite/source/json/jsonutil.h @@ -22,7 +22,7 @@ typedef void *json_handle; typedef void *json_pobject; json_handle parse_json(const char *data); -void free_json(json_handle hanlde); +void free_json(json_handle handle); json_pobject get_json_obj(json_pobject parent, const char *field); int32_t get_json_int(json_pobject obj, const char *field); diff --git a/frameworks/deviceauth_lite/source/key_agreement/key_agreement_server.c b/frameworks/deviceauth_lite/source/key_agreement/key_agreement_server.c index aff4f94..440eed6 100755 --- a/frameworks/deviceauth_lite/source/key_agreement/key_agreement_server.c +++ b/frameworks/deviceauth_lite/source/key_agreement/key_agreement_server.c @@ -62,7 +62,7 @@ int32_t send_start_response(void *handle, void *receive_data, void *send_data) } set_state(base, START_RESPONSE); set_last_time_sec(base); - DBG_OUT("Object %u receive start request data send start response data succcess", base->sn); + DBG_OUT("Object %u receive start request data send start response data success", base->sn); return HC_OK; } @@ -94,7 +94,7 @@ int32_t send_end_response(void *handle, void *receive_data, void *send_data) } set_state(base, PROTOCOL_FINISH); set_last_time_sec(base); - DBG_OUT("Object %u receive end request data send end response data succcess", base->sn); + DBG_OUT("Object %u receive end request data send end response data success", base->sn); return HC_OK; } #endif /* DESC */ diff --git a/frameworks/deviceauth_lite/source/key_agreement/pake_client.c b/frameworks/deviceauth_lite/source/key_agreement/pake_client.c index 62ba235..8a902e5 100644 --- a/frameworks/deviceauth_lite/source/key_agreement/pake_client.c +++ b/frameworks/deviceauth_lite/source/key_agreement/pake_client.c @@ -20,7 +20,7 @@ #if !(defined(_CUT_PAKE_) || defined(_CUT_PAKE_CLIENT_)) #include -#include +#include "securec.h" #include "huks_adapter.h" #include "commonutil.h" diff --git a/frameworks/deviceauth_lite/source/key_agreement/pake_server.c b/frameworks/deviceauth_lite/source/key_agreement/pake_server.c index cd163c3..54e398d 100644 --- a/frameworks/deviceauth_lite/source/key_agreement/pake_server.c +++ b/frameworks/deviceauth_lite/source/key_agreement/pake_server.c @@ -18,7 +18,7 @@ #if !(defined(_CUT_PAKE_) || defined(_CUT_PAKE_SERVER_)) -#include +#include "securec.h" #include "huks_adapter.h" #include "mem_stat.h" diff --git a/frameworks/deviceauth_lite/source/key_agreement/sec_clone_server.c b/frameworks/deviceauth_lite/source/key_agreement/sec_clone_server.c index dfe76cb..5d57b0f 100755 --- a/frameworks/deviceauth_lite/source/key_agreement/sec_clone_server.c +++ b/frameworks/deviceauth_lite/source/key_agreement/sec_clone_server.c @@ -18,7 +18,7 @@ #include "sec_clone_server.h" #include #include -#include +#include "securec.h" #include "huks_adapter.h" #include "log.h" #include "commonutil.h" diff --git a/frameworks/deviceauth_lite/source/key_agreement/sts_client.c b/frameworks/deviceauth_lite/source/key_agreement/sts_client.c index f2515f7..4d52074 100755 --- a/frameworks/deviceauth_lite/source/key_agreement/sts_client.c +++ b/frameworks/deviceauth_lite/source/key_agreement/sts_client.c @@ -17,7 +17,7 @@ #if !(defined(_CUT_STS_) || defined(_CUT_STS_CLIENT_)) #include -#include +#include "securec.h" #include "huks_adapter.h" #include "log.h" #include "commonutil.h" diff --git a/frameworks/deviceauth_lite/source/key_agreement/sts_server.c b/frameworks/deviceauth_lite/source/key_agreement/sts_server.c index cb5a90b..c5e4cf6 100755 --- a/frameworks/deviceauth_lite/source/key_agreement/sts_server.c +++ b/frameworks/deviceauth_lite/source/key_agreement/sts_server.c @@ -18,7 +18,7 @@ #if !(defined(_CUT_STS_) || defined(_CUT_STS_SERVER_)) -#include +#include "securec.h" #include "commonutil.h" #include "distribution.h" #include "mem_stat.h" diff --git a/frameworks/deviceauth_lite/source/log/log.c b/frameworks/deviceauth_lite/source/log/log.c index 34caa67..5df4cd5 100755 --- a/frameworks/deviceauth_lite/source/log/log.c +++ b/frameworks/deviceauth_lite/source/log/log.c @@ -16,7 +16,7 @@ #ifndef _HC_DEBUG_ #include "log.h" -#include +#include "securec.h" #include "base.h" #if defined(_WINDOWS) diff --git a/frameworks/deviceauth_lite/source/schedule/build_object.c b/frameworks/deviceauth_lite/source/schedule/build_object.c index 3c2794a..5db441a 100755 --- a/frameworks/deviceauth_lite/source/schedule/build_object.c +++ b/frameworks/deviceauth_lite/source/schedule/build_object.c @@ -14,7 +14,7 @@ */ #include "build_object.h" -#include +#include "securec.h" #include "log.h" #include "auth_info.h" diff --git a/frameworks/deviceauth_lite/source/schedule/distribution.c b/frameworks/deviceauth_lite/source/schedule/distribution.c index 380fa04..5ab7f95 100644 --- a/frameworks/deviceauth_lite/source/schedule/distribution.c +++ b/frameworks/deviceauth_lite/source/schedule/distribution.c @@ -14,7 +14,7 @@ */ #include "distribution.h" -#include +#include "securec.h" #include "log.h" #include "pake_client.h" #include "pake_server.h" diff --git a/frameworks/deviceauth_lite/source/struct/add_auth_info_data.c b/frameworks/deviceauth_lite/source/struct/add_auth_info_data.c index 245ebec..2ca7db6 100755 --- a/frameworks/deviceauth_lite/source/struct/add_auth_info_data.c +++ b/frameworks/deviceauth_lite/source/struct/add_auth_info_data.c @@ -15,7 +15,7 @@ #if !(defined(_CUT_STS_) || defined(_CUT_STS_SERVER_) || defined(_CUT_ADD_) || defined(_CUT_ADD_SERVER_)) -#include +#include "securec.h" #include "log.h" #include "mem_stat.h" #include "jsonutil.h" diff --git a/frameworks/deviceauth_lite/source/struct/add_auth_info_request.c b/frameworks/deviceauth_lite/source/struct/add_auth_info_request.c index 6dab0f2..7f7fb56 100755 --- a/frameworks/deviceauth_lite/source/struct/add_auth_info_request.c +++ b/frameworks/deviceauth_lite/source/struct/add_auth_info_request.c @@ -15,7 +15,7 @@ #if !(defined(_CUT_STS_) || defined(_CUT_STS_SERVER_) || defined(_CUT_ADD_) || defined(_CUT_ADD_SERVER_)) -#include +#include "securec.h" #include "log.h" #include "mem_stat.h" #include "jsonutil.h" diff --git a/frameworks/deviceauth_lite/source/struct/add_auth_info_response.c b/frameworks/deviceauth_lite/source/struct/add_auth_info_response.c index 34328f4..8bbb85a 100755 --- a/frameworks/deviceauth_lite/source/struct/add_auth_info_response.c +++ b/frameworks/deviceauth_lite/source/struct/add_auth_info_response.c @@ -15,7 +15,7 @@ #if !(defined(_CUT_STS_) || defined(_CUT_STS_SERVER_) || defined(_CUT_ADD_) || defined(_CUT_ADD_SERVER_)) -#include +#include "securec.h" #include "log.h" #include "mem_stat.h" #include "jsonutil.h" diff --git a/frameworks/deviceauth_lite/source/struct/auth_ack_request.c b/frameworks/deviceauth_lite/source/struct/auth_ack_request.c index 14a4d18..3d52f1d 100755 --- a/frameworks/deviceauth_lite/source/struct/auth_ack_request.c +++ b/frameworks/deviceauth_lite/source/struct/auth_ack_request.c @@ -15,7 +15,7 @@ #if !(defined(_CUT_STS_) || defined(_CUT_STS_SERVER_)) -#include +#include "securec.h" #include "log.h" #include "mem_stat.h" #include "jsonutil.h" diff --git a/frameworks/deviceauth_lite/source/struct/auth_ack_response.c b/frameworks/deviceauth_lite/source/struct/auth_ack_response.c index a62fada..ed04366 100755 --- a/frameworks/deviceauth_lite/source/struct/auth_ack_response.c +++ b/frameworks/deviceauth_lite/source/struct/auth_ack_response.c @@ -15,7 +15,7 @@ #if !(defined(_CUT_STS_) || defined(_CUT_STS_SERVER_)) -#include +#include "securec.h" #include "log.h" #include "mem_stat.h" #include "jsonutil.h" diff --git a/frameworks/deviceauth_lite/source/struct/auth_start_request.c b/frameworks/deviceauth_lite/source/struct/auth_start_request.c index b34b371..dd749ae 100755 --- a/frameworks/deviceauth_lite/source/struct/auth_start_request.c +++ b/frameworks/deviceauth_lite/source/struct/auth_start_request.c @@ -15,7 +15,7 @@ #if !(defined(_CUT_STS_) || defined(_CUT_STS_SERVER_)) -#include +#include "securec.h" #include "log.h" #include "mem_stat.h" #include "jsonutil.h" @@ -90,8 +90,8 @@ void free_auth_start_request(void *obj) } } -static char *make_requst_json_str(struct sts_start_request_data *auth_start_request, - struct sts_start_request_data_hex tmp_hex) +static char *make_request_json_str(struct sts_start_request_data *auth_start_request, + struct sts_start_request_data_hex tmp_hex) { char *tmp_str = (char *)MALLOC(RET_STR_LENGTH); if (tmp_str == NULL) { @@ -156,7 +156,7 @@ char *make_auth_start_request(void *data) FREE(tmp_hex.tmp_cha_data_hex); return NULL; } - char *ret_str = make_requst_json_str(auth_start_request, tmp_hex); + char *ret_str = make_request_json_str(auth_start_request, tmp_hex); FREE(tmp_hex.tmp_epk_data_hex); FREE(tmp_hex.tmp_cha_data_hex); FREE(tmp_hex.tmp_type_data_hex); diff --git a/frameworks/deviceauth_lite/source/struct/auth_start_response.c b/frameworks/deviceauth_lite/source/struct/auth_start_response.c index d84c64c..f977082 100755 --- a/frameworks/deviceauth_lite/source/struct/auth_start_response.c +++ b/frameworks/deviceauth_lite/source/struct/auth_start_response.c @@ -15,7 +15,7 @@ #if !(defined(_CUT_STS_) || defined(_CUT_STS_SERVER_)) -#include +#include "securec.h" #include "log.h" #include "mem_stat.h" #include "jsonutil.h" diff --git a/frameworks/deviceauth_lite/source/struct/exchange_auth_data.c b/frameworks/deviceauth_lite/source/struct/exchange_auth_data.c index dfc4167..9cc3efc 100755 --- a/frameworks/deviceauth_lite/source/struct/exchange_auth_data.c +++ b/frameworks/deviceauth_lite/source/struct/exchange_auth_data.c @@ -15,7 +15,7 @@ #if !(defined(_CUT_PAKE_) || defined(_CUT_PAKE_SERVER_) || defined(_CUT_EXCHANGE_) || defined(_CUT_EXCHANGE_SERVER_)) -#include +#include "securec.h" #include "log.h" #include "mem_stat.h" #include "jsonutil.h" diff --git a/frameworks/deviceauth_lite/source/struct/exchange_request.c b/frameworks/deviceauth_lite/source/struct/exchange_request.c index c1c8cbd..58c9dd5 100755 --- a/frameworks/deviceauth_lite/source/struct/exchange_request.c +++ b/frameworks/deviceauth_lite/source/struct/exchange_request.c @@ -15,7 +15,7 @@ #if !(defined(_CUT_PAKE_) || defined(_CUT_PAKE_SERVER_) || defined(_CUT_EXCHANGE_) || defined(_CUT_EXCHANGE_SERVER_)) -#include +#include "securec.h" #include "log.h" #include "mem_stat.h" #include "jsonutil.h" diff --git a/frameworks/deviceauth_lite/source/struct/exchange_response.c b/frameworks/deviceauth_lite/source/struct/exchange_response.c index 575b1c9..d85747e 100755 --- a/frameworks/deviceauth_lite/source/struct/exchange_response.c +++ b/frameworks/deviceauth_lite/source/struct/exchange_response.c @@ -15,7 +15,7 @@ #if !(defined(_CUT_PAKE_) || defined(_CUT_PAKE_SERVER_) || defined(_CUT_EXCHANGE_) || defined(_CUT_EXCHANGE_SERVER_)) -#include +#include "securec.h" #include "log.h" #include "mem_stat.h" #include "jsonutil.h" diff --git a/frameworks/deviceauth_lite/source/struct/import_add_auth_data.c b/frameworks/deviceauth_lite/source/struct/import_add_auth_data.c index 73c44d3..a319d57 100755 --- a/frameworks/deviceauth_lite/source/struct/import_add_auth_data.c +++ b/frameworks/deviceauth_lite/source/struct/import_add_auth_data.c @@ -13,7 +13,7 @@ * limitations under the License. */ -#include +#include "securec.h" #include "log.h" #include "mem_stat.h" #include "jsonutil.h" diff --git a/frameworks/deviceauth_lite/source/struct/inform_message.c b/frameworks/deviceauth_lite/source/struct/inform_message.c index b183231..6af939a 100755 --- a/frameworks/deviceauth_lite/source/struct/inform_message.c +++ b/frameworks/deviceauth_lite/source/struct/inform_message.c @@ -13,7 +13,7 @@ * limitations under the License. */ -#include +#include "securec.h" #include "log.h" #include "mem_stat.h" #include "jsonutil.h" diff --git a/frameworks/deviceauth_lite/source/struct/key_agreement_version.c b/frameworks/deviceauth_lite/source/struct/key_agreement_version.c index bb58849..bea483e 100755 --- a/frameworks/deviceauth_lite/source/struct/key_agreement_version.c +++ b/frameworks/deviceauth_lite/source/struct/key_agreement_version.c @@ -14,7 +14,7 @@ */ #include "key_agreement_version.h" -#include +#include "securec.h" #include "log.h" #include "mem_stat.h" #include "jsonutil.h" diff --git a/frameworks/deviceauth_lite/source/struct/pake_client_confirm.c b/frameworks/deviceauth_lite/source/struct/pake_client_confirm.c index 7eefddf..d91e424 100755 --- a/frameworks/deviceauth_lite/source/struct/pake_client_confirm.c +++ b/frameworks/deviceauth_lite/source/struct/pake_client_confirm.c @@ -16,7 +16,7 @@ #if !(defined(_CUT_PAKE_) || defined(_CUT_PAKE_SERVER_)) #include -#include +#include "securec.h" #include "mem_stat.h" #include "jsonutil.h" #include "commonutil.h" diff --git a/frameworks/deviceauth_lite/source/struct/pake_request.c b/frameworks/deviceauth_lite/source/struct/pake_request.c index 16ae8f1..feb5269 100755 --- a/frameworks/deviceauth_lite/source/struct/pake_request.c +++ b/frameworks/deviceauth_lite/source/struct/pake_request.c @@ -15,7 +15,7 @@ #if !(defined(_CUT_PAKE_) || defined(_CUT_PAKE_SERVER_)) -#include +#include "securec.h" #include "log.h" #include "mem_stat.h" #include "jsonutil.h" diff --git a/frameworks/deviceauth_lite/source/struct/pake_response.c b/frameworks/deviceauth_lite/source/struct/pake_response.c index e70e039..fa828f1 100755 --- a/frameworks/deviceauth_lite/source/struct/pake_response.c +++ b/frameworks/deviceauth_lite/source/struct/pake_response.c @@ -15,7 +15,7 @@ #if !(defined(_CUT_PAKE_) || defined(_CUT_PAKE_SERVER_)) -#include +#include "securec.h" #include "log.h" #include "mem_stat.h" #include "jsonutil.h" diff --git a/frameworks/deviceauth_lite/source/struct/pake_server_confirm.c b/frameworks/deviceauth_lite/source/struct/pake_server_confirm.c index 42e5c3e..1a89df6 100755 --- a/frameworks/deviceauth_lite/source/struct/pake_server_confirm.c +++ b/frameworks/deviceauth_lite/source/struct/pake_server_confirm.c @@ -15,7 +15,7 @@ #if !(defined(_CUT_PAKE_) || defined(_CUT_PAKE_SERVER_)) -#include +#include "securec.h" #include "log.h" #include "mem_stat.h" #include "jsonutil.h" diff --git a/frameworks/deviceauth_lite/source/struct/parsedata.c b/frameworks/deviceauth_lite/source/struct/parsedata.c index 11ee46f..d8ef534 100755 --- a/frameworks/deviceauth_lite/source/struct/parsedata.c +++ b/frameworks/deviceauth_lite/source/struct/parsedata.c @@ -14,7 +14,7 @@ */ #include "parsedata.h" -#include +#include "securec.h" #include "mem_stat.h" #include "jsonutil.h" #include "log.h" diff --git a/frameworks/deviceauth_lite/source/struct/rmv_auth_info_data.c b/frameworks/deviceauth_lite/source/struct/rmv_auth_info_data.c index a9b8789..4769ea9 100755 --- a/frameworks/deviceauth_lite/source/struct/rmv_auth_info_data.c +++ b/frameworks/deviceauth_lite/source/struct/rmv_auth_info_data.c @@ -15,7 +15,7 @@ #if !(defined(_CUT_STS_) || defined(_CUT_STS_SERVER_) || defined(_CUT_REMOVE_) || defined(_CUT_REMOVE_SERVER_)) -#include +#include "securec.h" #include "log.h" #include "mem_stat.h" #include "jsonutil.h" diff --git a/frameworks/deviceauth_lite/source/struct/rmv_auth_info_request.c b/frameworks/deviceauth_lite/source/struct/rmv_auth_info_request.c index 5a9af29..eb73f3c 100755 --- a/frameworks/deviceauth_lite/source/struct/rmv_auth_info_request.c +++ b/frameworks/deviceauth_lite/source/struct/rmv_auth_info_request.c @@ -15,7 +15,7 @@ #if !(defined(_CUT_STS_) || defined(_CUT_STS_SERVER_) || defined(_CUT_REMOVE_) || defined(_CUT_REMOVE_SERVER_)) -#include +#include "securec.h" #include "log.h" #include "mem_stat.h" #include "jsonutil.h" diff --git a/frameworks/deviceauth_lite/source/struct/rmv_auth_info_response.c b/frameworks/deviceauth_lite/source/struct/rmv_auth_info_response.c index 36bace0..ef43b37 100755 --- a/frameworks/deviceauth_lite/source/struct/rmv_auth_info_response.c +++ b/frameworks/deviceauth_lite/source/struct/rmv_auth_info_response.c @@ -15,7 +15,7 @@ #if !(defined(_CUT_STS_) || defined(_CUT_STS_SERVER_) || defined(_CUT_REMOVE_) || defined(_CUT_REMOVE_SERVER_)) -#include +#include "securec.h" #include "log.h" #include "mem_stat.h" #include "jsonutil.h" diff --git a/frameworks/deviceauth_lite/source/struct/sec_clone_data.c b/frameworks/deviceauth_lite/source/struct/sec_clone_data.c index 4dbfd5d..15ce1c3 100755 --- a/frameworks/deviceauth_lite/source/struct/sec_clone_data.c +++ b/frameworks/deviceauth_lite/source/struct/sec_clone_data.c @@ -16,7 +16,7 @@ #if (defined(_SUPPORT_SEC_CLONE_) || defined(_SUPPORT_SEC_CLONE_SERVER_)) #include -#include +#include "securec.h" #include "jsonutil.h" #include "commonutil.h" #include "exchange_auth_info.h" diff --git a/frameworks/deviceauth_lite/unittest/deviceauth_test.cpp b/frameworks/deviceauth_lite/unittest/deviceauth_test.cpp index 81002a9..f42b3e6 100644 --- a/frameworks/deviceauth_lite/unittest/deviceauth_test.cpp +++ b/frameworks/deviceauth_lite/unittest/deviceauth_test.cpp @@ -15,7 +15,7 @@ #include "deviceauth_test.h" #include -#include +#include "securec.h" #include "hichain.h" #define LOG(format, ...) (printf(format"\n", ##__VA_ARGS__)) @@ -54,7 +54,7 @@ static void Transmit(const struct session_identity *identity, const void *data, LOG("--------Transmit--------"); LOG("identity session_id[%d] package_name[%s]", identity->session_id, identity->package_name.name); LOG("data[%s]", (char *)data); - LOG("length[%d]", length); + LOG("length[%u]", length); LOG("--------Transmit--------"); } diff --git a/frameworks/inc/lite/ipc_callback_stub.h b/frameworks/inc/lite/ipc_callback_stub.h index 76a438b..5fbdaeb 100755 --- a/frameworks/inc/lite/ipc_callback_stub.h +++ b/frameworks/inc/lite/ipc_callback_stub.h @@ -25,7 +25,7 @@ extern "C" { typedef struct { SvcIdentity stubIdentity; - bool registed; + bool registered; } StubDevAuthCb; int32_t CbStubOnRemoteRequest(const IpcContext *ctx, void *ipcMsg, IpcIo *data, void *arg); diff --git a/frameworks/src/lite/ipc_adapt.c b/frameworks/src/lite/ipc_adapt.c index 6ca50d8..3b873b1 100644 --- a/frameworks/src/lite/ipc_adapt.c +++ b/frameworks/src/lite/ipc_adapt.c @@ -1392,8 +1392,8 @@ void SetCbCtxToDataCtx(uintptr_t callCtx, int32_t cbIdx) ProxyDevAuthData *dataCache = NULL; const SvcIdentity *stubInfo = &g_sdkCbStub.stubIdentity; (void)cbIdx; - if (!g_sdkCbStub.registed) { - LOGW("SDK callback stub un-registed"); + if (!g_sdkCbStub.registered) { + LOGW("SDK callback stub un-registered"); return; } ShowIpcSvcInfo(stubInfo); @@ -1661,7 +1661,7 @@ int32_t InitProxyAdapt(void) LOGI("get proxy instance success"); } - if (!g_sdkCbStub.registed) { + if (!g_sdkCbStub.registered) { ret = RegisterIpcCallback(CbStubOnRemoteRequest, 0, IPC_WAIT_FOREVER, &(g_sdkCbStub.stubIdentity), NULL); if (ret != 0) { LOGE("register ipc cb failed"); @@ -1669,7 +1669,7 @@ int32_t InitProxyAdapt(void) } ShowIpcSvcInfo(&(g_sdkCbStub.stubIdentity)); LOGI("register ipc cb success"); - g_sdkCbStub.registed = true; + g_sdkCbStub.registered = true; } return HC_SUCCESS; } @@ -1680,7 +1680,7 @@ void UnInitProxyAdapt(void) if (UnregisterIpcCallback(g_sdkCbStub.stubIdentity)) { LOGW("un-register ipc cb failed"); } - g_sdkCbStub.registed = false; + g_sdkCbStub.registered = false; return; } diff --git a/frameworks/src/lite/ipc_dev_auth_stub.c b/frameworks/src/lite/ipc_dev_auth_stub.c index 995e01e..1157a40 100644 --- a/frameworks/src/lite/ipc_dev_auth_stub.c +++ b/frameworks/src/lite/ipc_dev_auth_stub.c @@ -66,7 +66,7 @@ static int32_t BinderLiteProcess(SvcIdentity **svc, int32_t procType) (void)BinderRelease((*svc)->ipcContext, (*svc)->handle); break; default: - LOGW("internal error: unknow processing type"); + LOGW("internal error: unknown processing type"); } #endif return (ret == 0) ? HC_SUCCESS : HC_ERROR; diff --git a/frameworks/src/standard/ipc_adapt.cpp b/frameworks/src/standard/ipc_adapt.cpp index 56950ce..3dd4ec8 100644 --- a/frameworks/src/standard/ipc_adapt.cpp +++ b/frameworks/src/standard/ipc_adapt.cpp @@ -1349,6 +1349,7 @@ int32_t AddDevAuthServiceToManager(uintptr_t *serviceCtx) ret = sysMgr->AddSystemAbility(DEVICE_AUTH_SERVICE_ID, sPtr); if (ret != ERR_OK) { LOGE("add service failed"); + delete sPtr; return HC_ERROR; } *serviceCtx = reinterpret_cast(sPtr); diff --git a/interfaces/innerkits/device_auth_defines.h b/interfaces/innerkits/device_auth_defines.h index cbb677c..538b50a 100644 --- a/interfaces/innerkits/device_auth_defines.h +++ b/interfaces/innerkits/device_auth_defines.h @@ -140,7 +140,7 @@ enum { BAD_PAYLOAD = 0xF000000B, ALGORITHM_UNSUPPORTED = 0xF000000C, PROOF_MISMATCH = 0xF000000D, - UNKOWN = 0xF0000000, + UNKNOWN = 0xF0000000, }; #endif diff --git a/services/authenticators/src/account_unrelated/das_module.c b/services/authenticators/src/account_unrelated/das_module.c index ec1ddca..8487003 100644 --- a/services/authenticators/src/account_unrelated/das_module.c +++ b/services/authenticators/src/account_unrelated/das_module.c @@ -61,7 +61,7 @@ bool IsDasMsgNeedIgnore(const CJson *in) return false; } - LOGI("The message needs to ignore, message: %u.", message); + LOGI("The message needs to ignore, message: %d.", message); return true; } diff --git a/services/authenticators/src/account_unrelated/das_task_common.c b/services/authenticators/src/account_unrelated/das_task_common.c index b9e4b1c..e4dcb2d 100644 --- a/services/authenticators/src/account_unrelated/das_task_common.c +++ b/services/authenticators/src/account_unrelated/das_task_common.c @@ -402,12 +402,12 @@ int32_t GetAndCheckAuthIdPeer(const CJson *in, const Uint8Buff *authIdSelf, cons return HC_ERR_CONVERT_FAILED; } if ((authIdSelf->length == authIdPeer->length) && - memcmp(authIdSelf->val, authIdPeer->val, authIdSelf->length) == 0) { + memcmp(authIdSelf->val, authIdPeer->val, authIdSelf->length) == EOK) { LOGE("Peer id can not be equal to self id."); HcFree(authIdPeerTmp); return HC_ERR_INVALID_PARAMS; } - if (memcmp(authIdPeer->val, authIdPeerTmp, authIdPeer->length) != 0) { + if (memcmp(authIdPeer->val, authIdPeerTmp, authIdPeer->length) != EOK) { LOGE("Peer authId does not match."); HcFree(authIdPeerTmp); return HC_ERR_INVALID_PARAMS; diff --git a/services/authenticators/src/account_unrelated/pake_task/standard_exchange_task/standard_client_bind_exchange_task.c b/services/authenticators/src/account_unrelated/pake_task/standard_exchange_task/standard_client_bind_exchange_task.c index 1f28d49..edab8b5 100644 --- a/services/authenticators/src/account_unrelated/pake_task/standard_exchange_task/standard_client_bind_exchange_task.c +++ b/services/authenticators/src/account_unrelated/pake_task/standard_exchange_task/standard_client_bind_exchange_task.c @@ -58,11 +58,7 @@ static int ExchangeRequest(AsyBaseCurTask *task, PakeParams *params, const CJson } // execute - res = ClientRequestStandardBindExchange(params, &(realTask->params)); - if (res != HC_SUCCESS) { - LOGE("ClientRequestStandardBindExchange failed"); - return res; - } + GOTO_ERR_AND_SET_RET(ClientRequestStandardBindExchange(params, &(realTask->params)), res); // package message GOTO_ERR_AND_SET_RET(AddIntToJson(sendToPeer, FIELD_MESSAGE, PAKE_BIND_EXCHANGE_REQUEST), res); diff --git a/services/data_manager/src/data_manager.c b/services/data_manager/src/data_manager.c index 186ba74..6c15b3f 100644 --- a/services/data_manager/src/data_manager.c +++ b/services/data_manager/src/data_manager.c @@ -127,10 +127,7 @@ static bool EndWithZero(HcParcel *parcel) if (p == NULL) { return false; } - if (*p == '\0') { - return true; - } - return false; + return (*p == '\0'); } static bool LoadStringVectorFromParcel(StringVector *vec, HcParcel *parcel) @@ -201,7 +198,7 @@ static bool GetOsAccountInfoPath(int32_t osAccountId, char *infoPath, uint32_t p { const char *beginPath = GetStorageDirPath(); if (beginPath == NULL) { - LOGE("[DB]: Failed to get the stroage path dir!"); + LOGE("[DB]: Failed to get the storage path dir!"); return false; } int32_t ret; @@ -424,13 +421,13 @@ static bool ReadParcelFromFile(int32_t osAccountId, HcParcel *parcel) } int fileSize = HcFileSize(file); if (fileSize <= 0) { - LOGE("[DB]: The databse file size is invalid!"); + LOGE("[DB]: The database file size is invalid!"); HcFileClose(file); return false; } char *fileData = (char *)HcMalloc(fileSize, 0); if (fileData == NULL) { - LOGE("[DB]: Failed to allocate fileData memroy!"); + LOGE("[DB]: Failed to allocate fileData memory!"); HcFileClose(file); return false; } @@ -697,16 +694,25 @@ static TrustedDeviceEntry **QueryDeviceEntryPtrIfMatch(const DeviceEntryVec *vec static void PostGroupCreatedMsg(const TrustedGroupEntry *groupEntry) { + if (!IsBroadcastSupported()) { + return; + } GetBroadcaster()->postOnGroupCreated(groupEntry); } static void PostGroupDeletedMsg(const TrustedGroupEntry *groupEntry) { + if (!IsBroadcastSupported()) { + return; + } GetBroadcaster()->postOnGroupDeleted(groupEntry); } static void PostDeviceBoundMsg(OsAccountTrustedInfo *info, const TrustedDeviceEntry *deviceEntry) { + if (!IsBroadcastSupported()) { + return; + } QueryGroupParams groupParams = InitQueryGroupParams(); groupParams.groupId = StringGet(&deviceEntry->groupId); TrustedGroupEntry **groupEntryPtr = QueryGroupEntryPtrIfMatch(&info->groups, &groupParams); @@ -717,11 +723,21 @@ static void PostDeviceBoundMsg(OsAccountTrustedInfo *info, const TrustedDeviceEn static void PostDeviceUnBoundMsg(OsAccountTrustedInfo *info, const TrustedDeviceEntry *deviceEntry) { + if (!IsBroadcastSupported()) { + return; + } + const char *groupId = StringGet(&deviceEntry->groupId); + const char *udid = StringGet(&deviceEntry->udid); QueryGroupParams groupParams = InitQueryGroupParams(); - groupParams.groupId = StringGet(&deviceEntry->groupId); + groupParams.groupId = groupId; TrustedGroupEntry **groupEntryPtr = QueryGroupEntryPtrIfMatch(&info->groups, &groupParams); if (groupEntryPtr != NULL) { - GetBroadcaster()->postOnDeviceUnBound(StringGet(&deviceEntry->udid), *groupEntryPtr); + GetBroadcaster()->postOnDeviceUnBound(udid, *groupEntryPtr); + } + QueryDeviceParams deviceParams = InitQueryDeviceParams(); + deviceParams.udid = udid; + if (QueryDeviceEntryPtrIfMatch(&info->devices, &deviceParams) == NULL) { + GetBroadcaster()->postOnDeviceNotTrusted(udid); } } @@ -1021,7 +1037,7 @@ int32_t QueryGroups(int32_t osAccountId, const QueryGroupParams *params, GroupEn continue; } if (vec->pushBackT(vec, newEntry) == NULL) { - LOGE("[DB]: Faild to push entry to vec!"); + LOGE("[DB]: Failed to push entry to vec!"); DestroyGroupEntry(newEntry); } } @@ -1053,7 +1069,7 @@ int32_t QueryDevices(int32_t osAccountId, const QueryDeviceParams *params, Devic continue; } if (vec->pushBackT(vec, newEntry) == NULL) { - LOGE("[DB]: Faild to push entry to vec!"); + LOGE("[DB]: Failed to push entry to vec!"); DestroyDeviceEntry(newEntry); } } diff --git a/services/data_manager/src/database_manager.c b/services/data_manager/src/database_manager.c deleted file mode 100644 index a00c85b..0000000 --- a/services/data_manager/src/database_manager.c +++ /dev/null @@ -1,1626 +0,0 @@ -/* - * Copyright (C) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "data_manager.h" - -#include "database_util.h" -#include "device_auth.h" -#include "hc_dev_info.h" -#include "hc_file.h" -#include "hc_log.h" -#include "hc_mutex.h" -#include "hc_types.h" -#include "securec.h" - -DEFINE_TLV_FIX_LENGTH_TYPE(TlvDevAuthFixedLenInfo, NO_REVERT) - -BEGIN_TLV_STRUCT_DEFINE(TlvGroupElement, 0x0001) - TLV_MEMBER(TlvString, name, 0x4001) - TLV_MEMBER(TlvString, id, 0x4002) - TLV_MEMBER(TlvUint32, type, 0x4003) - TLV_MEMBER(TlvInt32, visibility, 0x4004) - TLV_MEMBER(TlvInt32, expireTime, 0x4005) - TLV_MEMBER(TlvString, userIdHash, 0x4006) - TLV_MEMBER(TlvBuffer, sharedUserIdHashVec, 0x4007) - TLV_MEMBER(TlvBuffer, managers, 0x4008) - TLV_MEMBER(TlvBuffer, friends, 0x4009) -END_TLV_STRUCT_DEFINE() -IMPLEMENT_TLV_VECTOR(TlvGroupVec, TlvGroupElement, 1) - -BEGIN_TLV_STRUCT_DEFINE(TlvDevAuthElement, 0x0002) - TLV_MEMBER(TlvString, groupId, 0x4101) - TLV_MEMBER(TlvString, udid, 0x4102) - TLV_MEMBER(TlvString, authId, 0x4103) - TLV_MEMBER(TlvString, userIdHash, 0x4107) - TLV_MEMBER(TlvString, serviceType, 0x4104) - TLV_MEMBER(TlvBuffer, ext, 0x4105) - TLV_MEMBER(TlvDevAuthFixedLenInfo, info, 0x4106) -END_TLV_STRUCT_DEFINE() -IMPLEMENT_TLV_VECTOR(TlvDevAuthVec, TlvDevAuthElement, 2) - -BEGIN_TLV_STRUCT_DEFINE(HCDataBaseV1, 0x0001) - TLV_MEMBER(TlvInt32, version, 0x6001) - TLV_MEMBER(TlvGroupVec, groups, 0x6002) - TLV_MEMBER(TlvDevAuthVec, devices, 0x6003) -END_TLV_STRUCT_DEFINE() - -IMPLEMENT_HC_VECTOR(TrustedGroupTable, TrustedGroupEntry *, 1) -IMPLEMENT_HC_VECTOR(TrustedDeviceTable, TrustedDeviceEntry, 2) -IMPLEMENT_HC_VECTOR(GroupInfoVec, void *, 1) -IMPLEMENT_HC_VECTOR(DeviceInfoVec, void *, 2) - -static HcMutex *g_databaseMutex = NULL; -static char g_localUdid[INPUT_UDID_LEN] = { 0 }; -static TrustedGroupTable g_trustedGroupTable; -static TrustedDeviceTable g_trustedDeviceTable; - -static void DestroyGroupTable(void) -{ - uint32_t groupIndex; - TrustedGroupEntry **entry = NULL; - FOR_EACH_HC_VECTOR(g_trustedGroupTable, groupIndex, entry) { - DestroyGroupEntryStruct(*entry); - HcFree(*entry); - } - DESTROY_HC_VECTOR(TrustedGroupTable, &g_trustedGroupTable); -} - -static void DestroyTrustDeviceTable(void) -{ - uint32_t devIndex; - TrustedDeviceEntry *deviceEntry = NULL; - FOR_EACH_HC_VECTOR(g_trustedDeviceTable, devIndex, deviceEntry) { - DestroyDeviceEntryStruct(deviceEntry); - } - DESTROY_HC_VECTOR(TrustedDeviceTable, &g_trustedDeviceTable); -} - -static TrustedGroupEntry *GetGroupEntryById(const char *groupId) -{ - uint32_t groupIndex; - TrustedGroupEntry **entry = NULL; - FOR_EACH_HC_VECTOR(g_trustedGroupTable, groupIndex, entry) { - if ((entry != NULL) && (*entry != NULL) && (IsGroupIdEquals(*entry, groupId))) { - return *entry; - } - } - return NULL; -} - -static TrustedDeviceEntry *GetTrustedDeviceEntryById(const char *deviceId, bool isUdid, const char *groupId) -{ - uint32_t index; - TrustedDeviceEntry *deviceEntry = NULL; - FOR_EACH_HC_VECTOR(g_trustedDeviceTable, index, deviceEntry) { - if (CompareDevIdInDeviceEntryOrNull(deviceEntry, deviceId, isUdid)) { - if (CompareGroupIdInDeviceEntryOrNull(deviceEntry, groupId)) { - return deviceEntry; - } - } - } - return NULL; -} - -static bool HasDevEntryInSuchTypeGroup(const char *udid, int32_t groupType) -{ - uint32_t devIndex; - TrustedDeviceEntry *deviceEntry = NULL; - FOR_EACH_HC_VECTOR(g_trustedDeviceTable, devIndex, deviceEntry) { - if ((deviceEntry != NULL) && (deviceEntry->groupEntry != NULL)) { - if (CompareDevIdInDeviceEntryOrNull(deviceEntry, udid, true) && - CompareGroupTypeInGroupEntryOrAll(deviceEntry->groupEntry, groupType)) { - return true; - } - } - } - return false; -} - -/* - * Currently, this interface does not return the actual number of trusted devices. - * If at least one trusted device exists, return 1. Otherwise, return 0. - */ -static int GetTrustedDeviceNum(void) -{ - return (g_trustedDeviceTable.size(&g_trustedDeviceTable) > 0) ? 1 : 0; -} - -static bool GenerateGroupEntryFromTlv(TrustedGroupEntry *entry, TlvGroupElement *group) -{ - entry->name = CreateString(); - entry->id = CreateString(); - entry->userIdHash = CreateString(); - entry->managers = CreateStrVector(); - entry->friends = CreateStrVector(); - entry->sharedUserIdHashVec = CreateStrVector(); - if (!LoadStringVectorFromParcel(&entry->managers, &group->managers.data)) { - return false; - } - if (!LoadStringVectorFromParcel(&entry->friends, &group->friends.data)) { - return false; - } - if (!LoadStringVectorFromParcel(&entry->sharedUserIdHashVec, &group->sharedUserIdHashVec.data)) { - return false; - } - if (!StringSet(&entry->name, group->name.data)) { - return false; - } - if (!StringSet(&entry->id, group->id.data)) { - return false; - } - if (!StringSet(&entry->userIdHash, group->userIdHash.data)) { - return false; - } - entry->type = group->type.data; - entry->visibility = group->visibility.data; - entry->expireTime = group->expireTime.data; - return true; -} - -static bool LoadGroupDb(HCDataBaseV1 *db) -{ - uint32_t index; - TlvGroupElement *group = NULL; - FOR_EACH_HC_VECTOR(db->groups.data, index, group) { - TrustedGroupEntry *entry = HcMalloc(sizeof(TrustedGroupEntry), 0); - if (entry == NULL) { - return false; - } - if (!GenerateGroupEntryFromTlv(entry, group)) { - DestroyGroupEntryStruct(entry); - HcFree(entry); - return false; - } - if (g_trustedGroupTable.pushBack(&g_trustedGroupTable, (const TrustedGroupEntry **)&entry) == NULL) { - DestroyGroupEntryStruct(entry); - HcFree(entry); - return false; - } - } - return true; -} - -static bool GenerateDeviceEntryFromTlv(TrustedDeviceEntry *deviceEntry, TlvDevAuthElement *devAuth) -{ - deviceEntry->udid = CreateString(); - deviceEntry->authId = CreateString(); - deviceEntry->serviceType = CreateString(); - deviceEntry->userIdHash = CreateString(); - deviceEntry->ext = CreateParcel(0, 0); - const char *groupId = StringGet(&devAuth->groupId.data); - if (groupId == NULL) { - LOGE("[DB]: Failed to get groupId from devAuth!"); - return false; - } - deviceEntry->groupEntry = GetGroupEntryById(groupId); - if (deviceEntry->groupEntry == NULL) { - LOGE("[DB]: Failed to find groupEntry by groupId!"); - return false; - } - if (!StringSet(&deviceEntry->udid, devAuth->udid.data)) { - return false; - } - if (!StringSet(&deviceEntry->authId, devAuth->authId.data)) { - return false; - } - if (!StringSet(&deviceEntry->serviceType, devAuth->serviceType.data)) { - return false; - } - if (!StringSet(&deviceEntry->userIdHash, devAuth->userIdHash.data)) { - return false; - } - if (!ParcelCopy(&devAuth->ext.data, &deviceEntry->ext)) { - return false; - } - deviceEntry->credential = devAuth->info.data.credential; - deviceEntry->devType = devAuth->info.data.devType; - deviceEntry->lastTm = devAuth->info.data.lastTm; - return true; -} - -static bool LoadDeviceDb(HCDataBaseV1 *db) -{ - uint32_t index; - TlvDevAuthElement *devAuth = NULL; - FOR_EACH_HC_VECTOR(db->devices.data, index, devAuth) { - TrustedDeviceEntry authInfo; - if (!GenerateDeviceEntryFromTlv(&authInfo, devAuth)) { - DestroyDeviceEntryStruct(&authInfo); - return false; - } - if (g_trustedDeviceTable.pushBack(&g_trustedDeviceTable, &authInfo) == NULL) { - DestroyDeviceEntryStruct(&authInfo); - return false; - } - } - return true; -} - -static bool LoadDbFromParcel(HcParcel *parcelIn) -{ - bool ret = false; - HCDataBaseV1 dbv1; - TLV_INIT(HCDataBaseV1, &dbv1); - if (DecodeTlvMessage((TlvBase *)&dbv1, parcelIn, false)) { - if (!LoadGroupDb(&dbv1)) { - TLV_DEINIT(dbv1); - return false; - } - if (!LoadDeviceDb(&dbv1)) { - TLV_DEINIT(dbv1); - return false; - } - ret = true; - } else { - LOGE("[DB]: Decode Tlv Message Failed!"); - } - TLV_DEINIT(dbv1); - return ret; -} - -static bool SaveGroupDb(HCDataBaseV1 *db) -{ - uint32_t index; - TrustedGroupEntry **entry; - FOR_EACH_HC_VECTOR(g_trustedGroupTable, index, entry) { - TlvGroupElement tmp; - TlvGroupElement *element = db->groups.data.pushBack(&db->groups.data, &tmp); - if (element == NULL) { - return false; - } - TLV_INIT(TlvGroupElement, element); - if (!SetGroupElement(element, entry)) { - TLV_DEINIT((*element)); - return false; - } - } - return true; -} - -static bool SaveDeviceDb(HCDataBaseV1 *db) -{ - uint32_t index; - TrustedDeviceEntry *entry = NULL; - FOR_EACH_HC_VECTOR(g_trustedDeviceTable, index, entry) { - TlvDevAuthElement tmp; - TlvDevAuthElement *element = db->devices.data.pushBack(&db->devices.data, &tmp); - if (element == NULL) { - return false; - } - TLV_INIT(TlvDevAuthElement, element); - if (!SetDeviceElement(element, entry)) { - TLV_DEINIT((*element)); - return false; - } - } - return true; -} - -static bool SaveDBToParcel(HcParcel *parcelOut) -{ - int ret; - HCDataBaseV1 dbv1; - TLV_INIT(HCDataBaseV1, &dbv1); - dbv1.version.data = 1; - do { - if (!SaveGroupDb(&dbv1)) { - ret = false; - break; - } - if (!SaveDeviceDb(&dbv1)) { - ret = false; - break; - } - if (!EncodeTlvMessage((TlvBase *)&dbv1, parcelOut)) { - LOGE("[DB]: Encode Tlv Message failed!"); - ret = false; - break; - } - ret = true; - } while (0); - TLV_DEINIT(dbv1); - return ret; -} - -static bool LoadDb(void) -{ - FileHandle file; - int ret = HcFileOpen(FILE_ID_GROUP, MODE_FILE_READ, &file); - if (ret != 0) { - return false; - } - int fileSize = HcFileSize(file); - if (fileSize <= 0) { - HcFileClose(file); - return false; - } - char *fileData = (char *)HcMalloc(fileSize, 0); - if (fileData == NULL) { - HcFileClose(file); - return false; - } - if (HcFileRead(file, fileData, fileSize) != fileSize) { - HcFileClose(file); - HcFree(fileData); - return false; - } - HcFileClose(file); - HcParcel parcel = CreateParcel(0, 0); - if (!ParcelWrite(&parcel, fileData, fileSize)) { - HcFree(fileData); - DeleteParcel(&parcel); - return false; - } - ret = LoadDbFromParcel(&parcel); - HcFree(fileData); - DeleteParcel(&parcel); - return ret; -} - -static bool SaveDB(void) -{ - HcParcel parcel = CreateParcel(0, 0); - if (!SaveDBToParcel(&parcel)) { - DeleteParcel(&parcel); - return false; - } - FileHandle file; - int ret = HcFileOpen(FILE_ID_GROUP, MODE_FILE_WRITE, &file); - if (ret != 0) { - DeleteParcel(&parcel); - return false; - } - int fileSize = (int)GetParcelDataSize(&parcel); - const char *fileData = GetParcelData(&parcel); - if (HcFileWrite(file, fileData, fileSize) == fileSize) { - ret = true; - } else { - ret = false; - } - DeleteParcel(&parcel); - HcFileClose(file); - return ret; -} - -static void CheckAndNotifyAfterDelDevice(const TrustedDeviceEntry *deviceEntry) -{ - const char *udid = StringGet(&deviceEntry->udid); - if (udid == NULL) { - return; - } - NotifyDeviceUnBound(deviceEntry); - if (!HasDevEntryInSuchTypeGroup(udid, deviceEntry->groupEntry->type)) { - NotifyLastGroupDeleted(udid, deviceEntry->groupEntry->type); - } - if (GetTrustedDeviceEntryById(udid, true, NULL) == NULL) { - NotifyDeviceNotTrusted(udid); - NotifyTrustedDeviceNumChanged(GetTrustedDeviceNum()); - } -} - -static int32_t GenerateGroupEntryByInfo(const GroupInfo *groupInfo, TrustedGroupEntry *entry) -{ - if (!StringSet(&entry->name, groupInfo->name)) { - LOGE("[DB]: Failed to copy groupName!"); - return HC_ERR_MEMORY_COPY; - } - if (!StringSet(&entry->id, groupInfo->id)) { - LOGE("[DB]: Failed to copy groupId!"); - return HC_ERR_MEMORY_COPY; - } - if (!StringSet(&entry->userIdHash, groupInfo->userIdHash)) { - LOGE("[DB]: Failed to copy userIdHash!"); - return HC_ERR_MEMORY_COPY; - } - entry->type = groupInfo->type; - entry->visibility = groupInfo->visibility; - entry->expireTime = groupInfo->expireTime; - HcString ownerName = CreateString(); - if (!StringSet(&ownerName, groupInfo->ownerName)) { - LOGE("[DB]: Failed to copy groupOwner!"); - DeleteString(&ownerName); - return HC_ERR_ALLOC_MEMORY; - } - if (entry->managers.pushBack(&entry->managers, &ownerName) == NULL) { - LOGE("[DB]: Failed to push groupOwner to managers!"); - DeleteString(&ownerName); - return HC_ERR_MEMORY_COPY; - } - return HC_SUCCESS; -} - -static int32_t GenerateDeviceEntryByInfo(const TrustedDeviceEntry *deviceInfo, const Uint8Buff *ext, - TrustedDeviceEntry *deviceEntry) -{ - deviceEntry->udid = CreateString(); - deviceEntry->authId = CreateString(); - deviceEntry->serviceType = CreateString(); - deviceEntry->userIdHash = CreateString(); - /* reserved field */ - deviceEntry->ext = CreateParcel(0, 0); - const char *groupId = StringGet(&deviceInfo->groupId); - if (groupId == NULL) { - LOGE("Failed to get groupId from from deviceInfo!"); - return HC_ERR_NULL_PTR; - } - deviceEntry->groupEntry = GetGroupEntryById(groupId); - if (deviceEntry->groupEntry == NULL) { - LOGE("[DB]: The group corresponding to groupId cannot be found!"); - return HC_ERR_GROUP_NOT_EXIST; - } - if (!StringSet(&deviceEntry->udid, deviceInfo->udid)) { - LOGE("[DB]: Failed to copy udid!"); - return HC_ERR_MEMORY_COPY; - } - if (!StringSet(&deviceEntry->authId, deviceInfo->authId)) { - LOGE("[DB]: Failed to copy authId!"); - return HC_ERR_MEMORY_COPY; - } - if (!StringSet(&deviceEntry->serviceType, deviceInfo->serviceType)) { - LOGE("[DB]: Failed to copy serviceType!"); - return HC_ERR_MEMORY_COPY; - } - if (!StringSet(&deviceEntry->userIdHash, deviceInfo->userIdHash)) { - LOGE("[DB]: Failed to copy userIdHash!"); - return HC_ERR_MEMORY_COPY; - } - deviceEntry->credential = deviceInfo->credential; - deviceEntry->devType = deviceInfo->devType; - deviceEntry->lastTm = 0; - if (ext != NULL && ext->val != NULL) { - if (!ParcelWrite(&deviceEntry->ext, ext->val, ext->length)) { - LOGE("[DB]: Failed to copy extern data!"); - return HC_ERR_MEMORY_COPY; - } - } - return HC_SUCCESS; -} - -static bool IsSatisfyGroup(const TrustedGroupEntry *entry, const GroupQueryParams *params) -{ - return (SatisfyType(entry->type, params->type) && SatisfyVisibility(entry->visibility, params->visibility)); -} - -static bool IsSatisfyUdidAndAuthId(const TrustedDeviceEntry *deviceEntry, const GroupQueryParams *params) -{ - if (params->udid != NULL) { - const char *udid = StringGet(&deviceEntry->udid); - if ((udid == NULL) || (strcmp(udid, params->udid) != 0)) { - return false; - } - } - if (params->authId != NULL) { - const char *authId = StringGet(&deviceEntry->authId); - if ((authId == NULL) || (strcmp(authId, params->authId) != 0)) { - return false; - } - } - return true; -} - -static int32_t GetGroupInfoIfDevExistInner(const char *groupId, const char *udid, GroupInfo *returnGroupInfo) -{ - uint32_t index; - TrustedGroupEntry **entry = NULL; - FOR_EACH_HC_VECTOR(g_trustedGroupTable, index, entry) { - if ((entry == NULL) || (*entry == NULL)) { - continue; - } - if (IsGroupIdEquals(*entry, groupId)) { - if ((udid != NULL) && (GetTrustedDeviceEntryById(udid, true, groupId) == NULL)) { - LOGE("[DB]: The trusted device does not exist in the group!"); - return HC_ERR_DEVICE_NOT_EXIST; - } else { - return GenerateGroupInfoByEntry(*entry, groupId, NULL, returnGroupInfo); - } - } - } - LOGE("[DB]: The group does not exist!"); - return HC_ERR_GROUP_NOT_EXIST; -} - -static void DelDeviceEntryByGroupId(const char *groupId) -{ - uint32_t devIndex = 0; - TrustedDeviceEntry *deviceEntry = NULL; - while (devIndex < g_trustedDeviceTable.size(&g_trustedDeviceTable)) { - deviceEntry = g_trustedDeviceTable.getp(&g_trustedDeviceTable, devIndex); - if ((deviceEntry == NULL) || (deviceEntry->groupEntry == NULL)) { - devIndex++; - continue; - } - const char *entryGroupId = StringGet(&deviceEntry->groupEntry->id); - if ((entryGroupId == NULL) || (strcmp(entryGroupId, groupId) != 0)) { - devIndex++; - continue; - } - TrustedDeviceEntry tmpDeviceEntry; - HC_VECTOR_POPELEMENT(&g_trustedDeviceTable, &tmpDeviceEntry, devIndex); - CheckAndNotifyAfterDelDevice(&tmpDeviceEntry); - DestroyDeviceEntryStruct(&tmpDeviceEntry); - } -} - -static void DelGroupEntryByGroupId(const char *groupId) -{ - TrustedGroupEntry **groupEntry = NULL; - uint32_t groupIndex = 0; - while (groupIndex < g_trustedGroupTable.size(&g_trustedGroupTable)) { - groupEntry = g_trustedGroupTable.getp(&g_trustedGroupTable, groupIndex); - if ((groupEntry == NULL) || (*groupEntry == NULL) || (!IsGroupIdEquals(*groupEntry, groupId))) { - groupIndex++; - continue; - } - TrustedGroupEntry *tmpEntry = NULL; - HC_VECTOR_POPELEMENT(&g_trustedGroupTable, &tmpEntry, groupIndex); - if (tmpEntry->type != ACROSS_ACCOUNT_AUTHORIZE_GROUP) { - NotifyGroupDeleted(tmpEntry, NULL); - } else { - const char *sharedUserIdHash = NULL; - if (GetSharedUserIdFromVecByGroupId(tmpEntry, groupId, &sharedUserIdHash) == HC_SUCCESS) { - NotifyGroupDeleted(tmpEntry, sharedUserIdHash); - } - } - DestroyGroupEntryStruct(tmpEntry); - HcFree(tmpEntry); - LOGI("[DB]: Delete a group from database successfully!"); - return; - } - LOGI("[DB]: The group does not exist!"); -} - -static void DeleteUserIdExpiredDeviceEntry(const char *curUserIdHash) -{ - uint32_t devIndex = 0; - TrustedDeviceEntry *deviceEntry = NULL; - while (devIndex < g_trustedDeviceTable.size(&g_trustedDeviceTable)) { - deviceEntry = g_trustedDeviceTable.getp(&g_trustedDeviceTable, devIndex); - if ((deviceEntry == NULL) || (deviceEntry->groupEntry == NULL)) { - devIndex++; - continue; - } - if (deviceEntry->groupEntry->type != ACROSS_ACCOUNT_AUTHORIZE_GROUP) { - devIndex++; - continue; - } - const char *userIdHash = StringGet(&deviceEntry->groupEntry->userIdHash); - if ((userIdHash == NULL) || (strcmp(userIdHash, curUserIdHash) == 0)) { - devIndex++; - continue; - } - TrustedDeviceEntry tmpDeviceEntry; - HC_VECTOR_POPELEMENT(&g_trustedDeviceTable, &tmpDeviceEntry, devIndex); - CheckAndNotifyAfterDelDevice(&tmpDeviceEntry); - DestroyDeviceEntryStruct(&tmpDeviceEntry); - } -} - -static void DeleteUserIdExpiredGroupEntry(const char *curUserIdHash) -{ - TrustedGroupEntry **groupEntry = NULL; - uint32_t groupIndex = 0; - while (groupIndex < g_trustedGroupTable.size(&g_trustedGroupTable)) { - groupEntry = g_trustedGroupTable.getp(&g_trustedGroupTable, groupIndex); - if ((groupEntry == NULL) || (*groupEntry == NULL)) { - groupIndex++; - continue; - } - if ((*groupEntry)->type != ACROSS_ACCOUNT_AUTHORIZE_GROUP) { - groupIndex++; - continue; - } - const char *userIdHash = StringGet(&(*groupEntry)->userIdHash); - if ((userIdHash == NULL) || (strcmp(userIdHash, curUserIdHash) == 0)) { - groupIndex++; - continue; - } - TrustedGroupEntry *tmpEntry = NULL; - HC_VECTOR_POPELEMENT(&g_trustedGroupTable, &tmpEntry, groupIndex); - uint32_t tmpIndex; - HcString *sharedUserIdHash = NULL; - FOR_EACH_HC_VECTOR(tmpEntry->sharedUserIdHashVec, tmpIndex, sharedUserIdHash) { - NotifyGroupDeleted(tmpEntry, StringGet(sharedUserIdHash)); - } - DestroyGroupEntryStruct(tmpEntry); - HcFree(tmpEntry); - } -} - -static void DeleteAccountDeviceEntry(void) -{ - uint32_t devIndex = 0; - TrustedDeviceEntry *deviceEntry = NULL; - while (devIndex < g_trustedDeviceTable.size(&g_trustedDeviceTable)) { - deviceEntry = g_trustedDeviceTable.getp(&g_trustedDeviceTable, devIndex); - if (deviceEntry == NULL) { - devIndex++; - continue; - } - if ((deviceEntry->groupEntry->type != IDENTICAL_ACCOUNT_GROUP) && - (deviceEntry->groupEntry->type != ACROSS_ACCOUNT_AUTHORIZE_GROUP)) { - devIndex++; - continue; - } - TrustedDeviceEntry tmpDeviceEntry; - HC_VECTOR_POPELEMENT(&g_trustedDeviceTable, &tmpDeviceEntry, devIndex); - CheckAndNotifyAfterDelDevice(&tmpDeviceEntry); - DestroyDeviceEntryStruct(&tmpDeviceEntry); - } -} - -static void DeleteAccountGroupEntry(void) -{ - TrustedGroupEntry **groupEntry = NULL; - uint32_t groupIndex = 0; - while (groupIndex < g_trustedGroupTable.size(&g_trustedGroupTable)) { - groupEntry = g_trustedGroupTable.getp(&g_trustedGroupTable, groupIndex); - if ((groupEntry == NULL) || (*groupEntry == NULL)) { - groupIndex++; - continue; - } - if (((*groupEntry)->type != IDENTICAL_ACCOUNT_GROUP) && - ((*groupEntry)->type != ACROSS_ACCOUNT_AUTHORIZE_GROUP)) { - groupIndex++; - continue; - } - TrustedGroupEntry *tmpEntry = NULL; - HC_VECTOR_POPELEMENT(&g_trustedGroupTable, &tmpEntry, groupIndex); - if (tmpEntry->type == IDENTICAL_ACCOUNT_GROUP) { - NotifyGroupDeleted(tmpEntry, NULL); - } else { - uint32_t tmpIndex; - HcString *sharedUserIdHash = NULL; - FOR_EACH_HC_VECTOR(tmpEntry->sharedUserIdHashVec, tmpIndex, sharedUserIdHash) { - NotifyGroupDeleted(tmpEntry, StringGet(sharedUserIdHash)); - } - } - DestroyGroupEntryStruct(tmpEntry); - HcFree(tmpEntry); - } -} - -static int32_t PushAcrossAccountGroupsToVec(const TrustedGroupEntry *entry, GroupInfoVec *groupInfoVec) -{ - uint32_t index; - HcString *tmpSharedUserIdHash = NULL; - FOR_EACH_HC_VECTOR(entry->sharedUserIdHashVec, index, tmpSharedUserIdHash) { - const char *tmpSharedUserIdHashPtr = StringGet(tmpSharedUserIdHash); - if (tmpSharedUserIdHashPtr == NULL) { - LOGE("[DB]: Failed to get sharedUserIdHash from sharedUserIdHashVec!"); - return HC_ERR_LOST_DATA; - } - GroupInfo *groupInfo = CreateGroupInfoStruct(); - if (groupInfo == NULL) { - LOGE("[DB]: Failed to allocate groupInfo memory!"); - return HC_ERR_ALLOC_MEMORY; - } - int32_t result = GenerateGroupInfoByEntry(entry, NULL, tmpSharedUserIdHashPtr, groupInfo); - if (result != HC_SUCCESS) { - DestroyGroupInfoStruct(groupInfo); - return result; - } - if (groupInfoVec->pushBackT(groupInfoVec, groupInfo) == NULL) { - LOGE("[DB]: Failed to push groupInfo to groupInfoVec!"); - DestroyGroupInfoStruct(groupInfo); - return HC_ERR_MEMORY_COPY; - } - } - return HC_SUCCESS; -} - -static int32_t PushGroupInfoToVec(const TrustedGroupEntry *entry, const char *groupId, - const char *sharedUserIdHash, GroupInfoVec *groupInfoVec) -{ - int32_t result; - if ((entry->type == ACROSS_ACCOUNT_AUTHORIZE_GROUP) && (groupId == NULL) && (sharedUserIdHash == NULL)) { - return PushAcrossAccountGroupsToVec(entry, groupInfoVec); - } - GroupInfo *groupInfo = CreateGroupInfoStruct(); - if (groupInfo == NULL) { - LOGE("[DB]: Failed to allocate groupInfo memory!"); - return HC_ERR_ALLOC_MEMORY; - } - result = GenerateGroupInfoByEntry(entry, groupId, sharedUserIdHash, groupInfo); - if (result != HC_SUCCESS) { - DestroyGroupInfoStruct(groupInfo); - return result; - } - if (groupInfoVec->pushBackT(groupInfoVec, groupInfo) == NULL) { - LOGE("[DB]: Failed to push groupInfo to groupInfoVec!"); - DestroyGroupInfoStruct(groupInfo); - return HC_ERR_MEMORY_COPY; - } - return HC_SUCCESS; -} - -static int32_t PushDevInfoToVec(const TrustedDeviceEntry *entry, const char *groupId, DeviceInfoVec *deviceInfoVec) -{ - TrustedDeviceEntry *deviceInfo = CreateDeviceInfoStruct(); - if (deviceInfo == NULL) { - LOGE("[DB]: Failed to allocate deviceInfo memory!"); - return HC_ERR_ALLOC_MEMORY; - } - int32_t result = GenerateDeviceInfoByEntry(entry, groupId, deviceInfo); - if (result != HC_SUCCESS) { - DestroyDeviceInfoStruct(deviceInfo); - return result; - } - if (deviceInfoVec->pushBackT(deviceInfoVec, deviceInfo) == NULL) { - LOGE("[DB]: Failed to push deviceInfo to deviceInfoVec!"); - DestroyDeviceInfoStruct(deviceInfo); - return HC_ERR_MEMORY_COPY; - } - return HC_SUCCESS; -} - -int32_t AddGroup(const GroupInfo *groupInfo) -{ - LOGI("[DB]: Start to add a group to database!"); - if (groupInfo == NULL) { - LOGE("[DB]: The input groupInfo is NULL!"); - return HC_ERR_INVALID_PARAMS; - } - const char *groupId = StringGet(&groupInfo->id); - if (groupId == NULL) { - LOGE("[DB]: Failed to get groupId from groupInfo!"); - return HC_ERR_NULL_PTR; - } - g_databaseMutex->lock(g_databaseMutex); - if (GetGroupEntryById(groupId) != NULL) { - LOGE("[DB]: The group corresponding to the groupId already exists!"); - g_databaseMutex->unlock(g_databaseMutex); - return HC_ERR_GROUP_DUPLICATE; - } - TrustedGroupEntry *entry = CreateGroupEntryStruct(); - if (entry == NULL) { - LOGE("[DB]: Failed to allocate groupEntry memory!"); - g_databaseMutex->unlock(g_databaseMutex); - return HC_ERR_ALLOC_MEMORY; - } - int32_t result = GenerateGroupEntryByInfo(groupInfo, entry); - if (result != HC_SUCCESS) { - DestroyGroupEntryStruct(entry); - HcFree(entry); - g_databaseMutex->unlock(g_databaseMutex); - return result; - } - if (g_trustedGroupTable.pushBack(&g_trustedGroupTable, (const TrustedGroupEntry **)&entry) == NULL) { - LOGE("[DB]: Failed to push groupEntry to groupTable!"); - DestroyGroupEntryStruct(entry); - HcFree(entry); - g_databaseMutex->unlock(g_databaseMutex); - return HC_ERR_MEMORY_COPY; - } - if (!SaveDB()) { - LOGE("[DB]: Failed to save database!"); - g_databaseMutex->unlock(g_databaseMutex); - return HC_ERR_SAVE_DB_FAILED; - } - if (entry->type != ACROSS_ACCOUNT_AUTHORIZE_GROUP) { - NotifyGroupCreated(entry, NULL); - } - LOGI("[DB]: Add a group to database successfully! [GroupType]: %d", groupInfo->type); - g_databaseMutex->unlock(g_databaseMutex); - return HC_SUCCESS; -} - -int32_t DelGroupByGroupId(const char *groupId) -{ - LOGI("[DB]: Start to delete a group from database!"); - if (groupId == NULL) { - LOGE("[DB]: The input groupId is NULL!"); - return HC_ERR_INVALID_PARAMS; - } - if (strcmp(groupId, "") == 0) { - LOGE("[DB]: The input groupId is empty string!"); - return HC_ERR_INVALID_PARAMS; - } - g_databaseMutex->lock(g_databaseMutex); - DelDeviceEntryByGroupId(groupId); - DelGroupEntryByGroupId(groupId); - if (!SaveDB()) { - LOGE("[DB]: Failed to save database!"); - g_databaseMutex->unlock(g_databaseMutex); - return HC_ERR_SAVE_DB_FAILED; - } - g_databaseMutex->unlock(g_databaseMutex); - return HC_SUCCESS; -} - -int32_t AddTrustedDevice(const TrustedDeviceEntry *deviceInfo, const Uint8Buff *ext) -{ - LOGI("[DB]: Start to add a trusted device to database!"); - if (deviceInfo == NULL) { - LOGE("[DB]: The input deviceInfo is NULL!"); - return HC_ERR_INVALID_PARAMS; - } - const char *udid = StringGet(&(deviceInfo->udid)); - const char *groupId = StringGet(&(deviceInfo->groupId)); - if ((udid == NULL) || (groupId == NULL)) { - LOGE("[DB]: The input udid or groupId is NULL!"); - return HC_ERR_NULL_PTR; - } - bool isTrustedDeviceNumChanged = false; - g_databaseMutex->lock(g_databaseMutex); - if (GetTrustedDeviceEntryById(udid, true, NULL) == NULL) { - isTrustedDeviceNumChanged = true; - } - if (GetTrustedDeviceEntryById(udid, true, groupId) != NULL) { - LOGE("[DB]: The device already exists in the group!"); - g_databaseMutex->unlock(g_databaseMutex); - return HC_ERR_DEVICE_DUPLICATE; - } - TrustedDeviceEntry deviceEntry; - int32_t result = GenerateDeviceEntryByInfo(deviceInfo, ext, &deviceEntry); - if (result != HC_SUCCESS) { - DestroyDeviceEntryStruct(&deviceEntry); - g_databaseMutex->unlock(g_databaseMutex); - return result; - } - if (g_trustedDeviceTable.pushBack(&g_trustedDeviceTable, &deviceEntry) == NULL) { - LOGE("[DB]: Failed to push deviceEntry to deviceTable!"); - DestroyDeviceEntryStruct(&deviceEntry); - g_databaseMutex->unlock(g_databaseMutex); - return HC_ERR_MEMORY_COPY; - } - if (!SaveDB()) { - LOGE("[DB]: Failed to save database!"); - g_databaseMutex->unlock(g_databaseMutex); - return HC_ERR_SAVE_DB_FAILED; - } - if (isTrustedDeviceNumChanged) { - NotifyTrustedDeviceNumChanged(GetTrustedDeviceNum()); - } - NotifyDeviceBound(&deviceEntry); - g_databaseMutex->unlock(g_databaseMutex); - LOGI("[DB]: Add a trusted device to database successfully!"); - return HC_SUCCESS; -} - -int32_t DelTrustedDevice(const char *deviceId, bool isUdid, const char *groupId) -{ - LOGI("[DB]: Start to delete a trusted device from database!"); - if ((deviceId == NULL) || (groupId == NULL)) { - LOGE("[DB]: The input udid or groupId is NULL!"); - return HC_ERR_INVALID_PARAMS; - } - uint32_t devIndex; - TrustedDeviceEntry *deviceEntry = NULL; - g_databaseMutex->lock(g_databaseMutex); - FOR_EACH_HC_VECTOR(g_trustedDeviceTable, devIndex, deviceEntry) { - if ((deviceEntry == NULL) || (deviceEntry->groupEntry == NULL)) { - continue; - } - const char *entryDevId = isUdid ? StringGet(&deviceEntry->udid) : StringGet(&deviceEntry->authId); - if ((entryDevId != NULL) && (strcmp(entryDevId, deviceId) == 0) && - (CompareGroupIdInDeviceEntryOrNull(deviceEntry, groupId))) { - TrustedDeviceEntry tmpDeviceEntry; - HC_VECTOR_POPELEMENT(&g_trustedDeviceTable, &tmpDeviceEntry, devIndex); - if (!SaveDB()) { - DestroyDeviceEntryStruct(&tmpDeviceEntry); - LOGE("[DB]: Failed to save database!"); - g_databaseMutex->unlock(g_databaseMutex); - return HC_ERR_SAVE_DB_FAILED; - } - CheckAndNotifyAfterDelDevice(&tmpDeviceEntry); - DestroyDeviceEntryStruct(&tmpDeviceEntry); - LOGI("[DB]: Delete a trusted device from database successfully!"); - g_databaseMutex->unlock(g_databaseMutex); - return HC_SUCCESS; - } - } - LOGE("[DB]: The trusted device is not found!"); - g_databaseMutex->unlock(g_databaseMutex); - return HC_ERR_DEVICE_NOT_EXIST; -} - -int32_t AddGroupRole(const char *groupId, GroupRole roleType, const char *appId) -{ - if ((groupId == NULL) || (appId == NULL)) { - LOGE("[DB]: The input groupId or appId is NULL!"); - return HC_ERR_INVALID_PARAMS; - } - LOGI("[DB]: Start to add a role to the group! [AppId]: %s, [Role]: %d", appId, roleType); - g_databaseMutex->lock(g_databaseMutex); - TrustedGroupEntry *groupEntry = GetGroupEntryById(groupId); - if (groupEntry == NULL) { - LOGE("[DB]: The group does not exist!"); - g_databaseMutex->unlock(g_databaseMutex); - return HC_ERR_GROUP_NOT_EXIST; - } - HcString roleStr = CreateString(); - if (!StringSetPointer(&roleStr, appId)) { - LOGE("[DB]: Failed to copy roleStr!"); - DeleteString(&roleStr); - g_databaseMutex->unlock(g_databaseMutex); - return HC_ERR_MEMORY_COPY; - } - bool isOk = false; - if (roleType == GROUP_MANAGER) { - isOk = (groupEntry->managers.pushBackT(&groupEntry->managers, roleStr) == NULL) ? true : false; - } else if (roleType == GROUP_FRIEND) { - isOk = (groupEntry->friends.pushBackT(&groupEntry->friends, roleStr) == NULL) ? true : false; - } - if (!isOk) { - LOGE("[DB]: Failed to copy roleStr!"); - DeleteString(&roleStr); - g_databaseMutex->unlock(g_databaseMutex); - return HC_ERR_MEMORY_COPY; - } - if (!SaveDB()) { - LOGE("[DB]: Failed to save database!"); - g_databaseMutex->unlock(g_databaseMutex); - return HC_ERR_SAVE_DB_FAILED; - } - LOGI("[DB]: Add a role to the group successfully!"); - g_databaseMutex->unlock(g_databaseMutex); - return HC_SUCCESS; -} - -int32_t RemoveGroupRole(const char *groupId, GroupRole roleType, const char *appId) -{ - if ((groupId == NULL) || (appId == NULL)) { - LOGE("[DB]: The input groupId or appId is NULL!"); - return HC_ERR_INVALID_PARAMS; - } - LOGI("[DB]: Start to delete a role from the group! [AppId]: %s, [Role]: %d", appId, roleType); - g_databaseMutex->lock(g_databaseMutex); - TrustedGroupEntry *groupEntry = GetGroupEntryById(groupId); - if (groupEntry == NULL) { - LOGE("[DB]: The group does not exist!"); - g_databaseMutex->unlock(g_databaseMutex); - return HC_ERR_GROUP_NOT_EXIST; - } - StringVector roleVec = (roleType == GROUP_MANAGER) ? groupEntry->managers : groupEntry->friends; - HcString *roleEntry = NULL; - uint32_t roleIndex; - FOR_EACH_HC_VECTOR(roleVec, roleIndex, roleEntry) { - if ((roleEntry == NULL) || ((roleType == GROUP_MANAGER) && (roleIndex == 0))) { - continue; - } - const char *roleStr = StringGet(roleEntry); - if ((roleStr == NULL) || (strcmp(roleStr, appId) != 0)) { - continue; - } - HcString tmpRole; - HC_VECTOR_POPELEMENT(&(roleVec), &tmpRole, roleIndex); - DeleteString(&tmpRole); - if (!SaveDB()) { - LOGE("[DB]: Failed to save database!"); - g_databaseMutex->unlock(g_databaseMutex); - return HC_ERR_SAVE_DB_FAILED; - } - LOGI("[DB]: Delete a role from the group successfully!"); - g_databaseMutex->unlock(g_databaseMutex); - return HC_SUCCESS; - } - LOGE("[DB]: The role does not exist in the group!"); - g_databaseMutex->unlock(g_databaseMutex); - return HC_ERR_ROLE_NOT_EXIST; -} - -int32_t GetGroupRoles(const char *groupId, GroupRole roleType, CJson *returnRoles) -{ - if ((groupId == NULL) || (returnRoles == NULL)) { - LOGE("[DB]: The input groupId or returnRoles is NULL!"); - return HC_ERR_INVALID_PARAMS; - } - LOGI("[DB]: Start to get roles from the group! [Role]: %d", roleType); - g_databaseMutex->lock(g_databaseMutex); - TrustedGroupEntry *groupEntry = GetGroupEntryById(groupId); - if (groupEntry == NULL) { - LOGE("[DB]: The group does not exist!"); - g_databaseMutex->unlock(g_databaseMutex); - return HC_ERR_GROUP_NOT_EXIST; - } - StringVector roleVec = (roleType == GROUP_MANAGER) ? groupEntry->managers : groupEntry->friends; - HcString *roleEntry = NULL; - uint32_t roleIndex; - FOR_EACH_HC_VECTOR(roleVec, roleIndex, roleEntry) { - if (AddStringToArray(returnRoles, StringGet(roleEntry)) != HC_SUCCESS) { - LOGD("[DB]: Failed to add role to returnRoles!"); - } - } - LOGI("[DB]: Get group roles from the group successfully!"); - g_databaseMutex->unlock(g_databaseMutex); - return HC_SUCCESS; -} - -int32_t CompareVisibility(const char *groupId, int groupVisibility) -{ - if (groupId == NULL) { - LOGE("[DB]: The input groupId is NULL!"); - return HC_ERR_INVALID_PARAMS; - } - g_databaseMutex->lock(g_databaseMutex); - TrustedGroupEntry *groupEntry = GetGroupEntryById(groupId); - if (groupEntry == NULL) { - LOGE("[DB]: The group does not exist!"); - g_databaseMutex->unlock(g_databaseMutex); - return HC_ERR_GROUP_NOT_EXIST; - } - int32_t result = (((uint32_t)(groupEntry->visibility) & (uint32_t)groupVisibility) == 0) ? HC_ERROR : HC_SUCCESS; - g_databaseMutex->unlock(g_databaseMutex); - return result; -} - -bool IsTrustedDeviceExist(const char *udid) -{ - if (udid == NULL) { - LOGE("[DB]: The input udid is NULL!"); - return HC_ERR_INVALID_PARAMS; - } - g_databaseMutex->lock(g_databaseMutex); - bool isExists = (GetTrustedDeviceEntryById(udid, true, NULL) != NULL) ? true : false; - g_databaseMutex->unlock(g_databaseMutex); - return isExists; -} - -int32_t GetTrustedDevNumber(void) -{ - g_databaseMutex->lock(g_databaseMutex); - int num = GetTrustedDeviceNum(); - g_databaseMutex->unlock(g_databaseMutex); - return num; -} - -int32_t DeleteUserIdExpiredGroups(const char *curUserIdHash) -{ - if (curUserIdHash == NULL) { - LOGE("[DB]: The input curUserIdHash is NULL!"); - return HC_ERR_INVALID_PARAMS; - } - LOGI("[DB]: Start to delete all across account groups with expired userId!"); - g_databaseMutex->lock(g_databaseMutex); - DeleteUserIdExpiredDeviceEntry(curUserIdHash); - DeleteUserIdExpiredGroupEntry(curUserIdHash); - if (!SaveDB()) { - g_databaseMutex->unlock(g_databaseMutex); - LOGE("[DB]: Failed to save database!"); - return HC_ERR_SAVE_DB_FAILED; - } - g_databaseMutex->unlock(g_databaseMutex); - LOGI("[DB]: Delete all across account groups with expired userId successfully!"); - return HC_SUCCESS; -} - -int32_t DeleteAllAccountGroup(void) -{ - LOGI("[DB]: Start to delete all account-related groups!"); - g_databaseMutex->lock(g_databaseMutex); - DeleteAccountDeviceEntry(); - DeleteAccountGroupEntry(); - if (!SaveDB()) { - g_databaseMutex->unlock(g_databaseMutex); - LOGE("[DB]: Failed to save database!"); - return HC_ERR_SAVE_DB_FAILED; - } - g_databaseMutex->unlock(g_databaseMutex); - LOGI("[DB]: Delete all account-related groups successfully!"); - return HC_SUCCESS; -} - -int32_t OnlyAddSharedUserIdVec(const StringVector *sharedUserIdHashVec, CJson *groupIdList) -{ - if ((sharedUserIdHashVec == NULL) || (groupIdList == NULL)) { - LOGE("[DB]: The input parameter contains NULL value!"); - return HC_ERR_INVALID_PARAMS; - } - LOGI("[DB]: Start to add shared userId list!"); - TrustedGroupEntry **entry = NULL; - uint32_t index; - g_databaseMutex->lock(g_databaseMutex); - FOR_EACH_HC_VECTOR(g_trustedGroupTable, index, entry) { - if ((entry != NULL) && (*entry != NULL) && ((*entry)->type == ACROSS_ACCOUNT_AUTHORIZE_GROUP)) { - AddNewSharedUserId(sharedUserIdHashVec, *entry, groupIdList); - LOGI("[DB]: Add new userIds successfully!"); - if (!SaveDB()) { - g_databaseMutex->unlock(g_databaseMutex); - LOGE("[DB]: Failed to save database!"); - return HC_ERR_SAVE_DB_FAILED; - } - g_databaseMutex->unlock(g_databaseMutex); - LOGI("[DB]: Only add shared userId list successfully!"); - return HC_SUCCESS; - } - } - g_databaseMutex->unlock(g_databaseMutex); - LOGE("[DB]: The across account group does not exist!"); - return HC_ERR_GROUP_NOT_EXIST; -} - -int32_t ChangeSharedUserIdVec(const StringVector *sharedUserIdHashVec) -{ - if (sharedUserIdHashVec == NULL) { - LOGE("[DB]: The input sharedUserIdHashVec is NULL!"); - return HC_ERR_INVALID_PARAMS; - } - LOGI("[DB]: Start to change shared userId list!"); - TrustedGroupEntry **entry = NULL; - uint32_t index; - g_databaseMutex->lock(g_databaseMutex); - FOR_EACH_HC_VECTOR(g_trustedGroupTable, index, entry) { - if ((entry != NULL) && (*entry != NULL) && ((*entry)->type == ACROSS_ACCOUNT_AUTHORIZE_GROUP)) { - DeleteExpiredSharedUserId(sharedUserIdHashVec, *entry); - LOGI("[DB]: Delete expired local userIds successfully!"); - AddNewSharedUserId(sharedUserIdHashVec, *entry, NULL); - LOGI("[DB]: Add new userIds successfully!"); - if (!SaveDB()) { - g_databaseMutex->unlock(g_databaseMutex); - LOGE("[DB]: Failed to save database!"); - return HC_ERR_SAVE_DB_FAILED; - } - g_databaseMutex->unlock(g_databaseMutex); - LOGI("[DB]: Change shared userId list successfully!"); - return HC_SUCCESS; - } - } - g_databaseMutex->unlock(g_databaseMutex); - LOGE("[DB]: The across account group does not exist!"); - return HC_ERR_GROUP_NOT_EXIST; -} - -bool IsTrustedDeviceInGroup(const char *groupId, const char *deviceId, bool isUdid) -{ - if ((groupId == NULL) || (deviceId == NULL)) { - LOGE("[DB]: The input groupId or deviceId is NULL!"); - return false; - } - g_databaseMutex->lock(g_databaseMutex); - bool isExists = (GetTrustedDeviceEntryById(deviceId, isUdid, groupId) != NULL) ? true : false; - g_databaseMutex->unlock(g_databaseMutex); - return isExists; -} - -bool IsSameNameGroupExist(const char *ownerName, const char *groupName) -{ - if ((ownerName == NULL) || (groupName == NULL)) { - LOGE("[DB]: The input ownerName or groupName is NULL!"); - return false; - } - uint32_t index; - TrustedGroupEntry **entry = NULL; - g_databaseMutex->lock(g_databaseMutex); - FOR_EACH_HC_VECTOR(g_trustedGroupTable, index, entry) { - if ((entry == NULL) || (*entry == NULL) || - (strcmp(StringGet(&(*entry)->name), groupName) != 0)) { - continue; - } - if (HC_VECTOR_SIZE(&(*entry)->managers) > 0) { - HcString entryOwner = HC_VECTOR_GET(&(*entry)->managers, 0); - const char *entryOwnerName = StringGet(&entryOwner); - if ((entryOwnerName != NULL) && (strcmp(entryOwnerName, ownerName) == 0)) { - g_databaseMutex->unlock(g_databaseMutex); - return true; - } - } - } - g_databaseMutex->unlock(g_databaseMutex); - return false; -} - -bool IsIdenticalGroupExist(void) -{ - uint32_t index; - TrustedGroupEntry **entry = NULL; - g_databaseMutex->lock(g_databaseMutex); - FOR_EACH_HC_VECTOR(g_trustedGroupTable, index, entry) { - if ((entry != NULL) && (*entry != NULL) && ((*entry)->type == IDENTICAL_ACCOUNT_GROUP)) { - g_databaseMutex->unlock(g_databaseMutex); - return true; - } - } - g_databaseMutex->unlock(g_databaseMutex); - return false; -} - -bool IsAcrossAccountGroupExist(void) -{ - uint32_t index; - TrustedGroupEntry **entry = NULL; - g_databaseMutex->lock(g_databaseMutex); - FOR_EACH_HC_VECTOR(g_trustedGroupTable, index, entry) { - if ((entry != NULL) && (*entry != NULL) && ((*entry)->type == ACROSS_ACCOUNT_AUTHORIZE_GROUP)) { - g_databaseMutex->unlock(g_databaseMutex); - return true; - } - } - g_databaseMutex->unlock(g_databaseMutex); - return false; -} - -bool IsGroupExistByGroupId(const char *groupId) -{ - if (groupId == NULL) { - LOGE("[DB]: The input groupId is NULL!"); - return false; - } - g_databaseMutex->lock(g_databaseMutex); - bool isExists = (GetGroupEntryById(groupId) == NULL) ? false : true; - g_databaseMutex->unlock(g_databaseMutex); - return isExists; -} - -int32_t GetTrustedDevInfoById(const char *deviceId, bool isUdid, const char *groupId, TrustedDeviceEntry *deviceInfo) -{ - if ((deviceId == NULL) || (groupId == NULL) || (deviceInfo == NULL)) { - LOGE("[DB]: The input parameters contains NULL value!"); - return HC_ERR_INVALID_PARAMS; - } - LOGI("[DB]: Start to get device information of a specified group!"); - g_databaseMutex->lock(g_databaseMutex); - TrustedDeviceEntry *deviceEntry = GetTrustedDeviceEntryById(deviceId, isUdid, groupId); - if (deviceEntry == NULL) { - LOGE("[DB]: The trusted device is not found!"); - g_databaseMutex->unlock(g_databaseMutex); - return HC_ERR_DEVICE_NOT_EXIST; - } - int32_t result = GenerateDeviceInfoByEntry(deviceEntry, groupId, deviceInfo); - g_databaseMutex->unlock(g_databaseMutex); - return result; -} - -int32_t GetGroupNumByOwner(const char *ownerName) -{ - if (ownerName == NULL) { - LOGE("[DB]: The input ownerName is NULL!"); - return 0; - } - int count = 0; - uint32_t index; - TrustedGroupEntry **entry = NULL; - g_databaseMutex->lock(g_databaseMutex); - FOR_EACH_HC_VECTOR(g_trustedGroupTable, index, entry) { - if ((entry != NULL) && (*entry != NULL) && (HC_VECTOR_SIZE(&(*entry)->managers) > 0)) { - HcString entryOwner = HC_VECTOR_GET(&(*entry)->managers, 0); - const char *entryOwnerName = StringGet(&entryOwner); - if ((entryOwnerName != NULL) && (strcmp(entryOwnerName, ownerName) == 0)) { - count++; - } - } - } - g_databaseMutex->unlock(g_databaseMutex); - return count; -} - -int32_t GetCurDeviceNumByGroupId(const char *groupId) -{ - if (groupId == NULL) { - LOGE("[DB]: The input groupId is NULL!"); - return 0; - } - int count = 0; - uint32_t index; - TrustedDeviceEntry *deviceEntry = NULL; - g_databaseMutex->lock(g_databaseMutex); - FOR_EACH_HC_VECTOR(g_trustedDeviceTable, index, deviceEntry) { - if ((deviceEntry != NULL) && (CompareGroupIdInDeviceEntryOrNull(deviceEntry, groupId))) { - count++; - } - } - g_databaseMutex->unlock(g_databaseMutex); - return count; -} - -int32_t GetGroupInfoIfDevExist(const char *groupId, const char *udid, GroupInfo *returnGroupInfo) -{ - if ((groupId == NULL) || (udid == NULL) || (returnGroupInfo == NULL)) { - LOGE("[DB]: The input parameters contains NULL value!"); - return HC_ERR_INVALID_PARAMS; - } - g_databaseMutex->lock(g_databaseMutex); - int32_t res = GetGroupInfoIfDevExistInner(groupId, udid, returnGroupInfo); - g_databaseMutex->unlock(g_databaseMutex); - return res; -} - -int32_t GetGroupInfoById(const char *groupId, GroupInfo *returnGroupInfo) -{ - if ((groupId == NULL) || (returnGroupInfo == NULL)) { - LOGE("[DB]: The input groupId or returnGroupInfo is NULL!"); - return HC_ERR_INVALID_PARAMS; - } - g_databaseMutex->lock(g_databaseMutex); - int32_t res = GetGroupInfoIfDevExistInner(groupId, NULL, returnGroupInfo); - g_databaseMutex->unlock(g_databaseMutex); - return res; -} - -void CreateGroupInfoVecStruct(GroupInfoVec *vec) -{ - if (vec == NULL) { - return; - } - *vec = CREATE_HC_VECTOR(GroupInfoVec); -} - -void DestroyGroupInfoVecStruct(GroupInfoVec *vec) -{ - uint32_t index; - void **entry = NULL; - FOR_EACH_HC_VECTOR(*vec, index, entry) { - if ((entry != NULL) && (*entry != NULL)) { - DestroyGroupInfoStruct(*entry); - } - } - DESTROY_HC_VECTOR(GroupInfoVec, vec); -} - -void CreateDeviceInfoVecStruct(DeviceInfoVec *vec) -{ - if (vec == NULL) { - return; - } - *vec = CREATE_HC_VECTOR(DeviceInfoVec); -} - -void DestroyDeviceInfoVecStruct(DeviceInfoVec *vec) -{ - uint32_t index; - void **entry = NULL; - FOR_EACH_HC_VECTOR(*vec, index, entry) { - if ((entry != NULL) && (*entry != NULL)) { - DestroyDeviceInfoStruct(*entry); - } - } - DESTROY_HC_VECTOR(DeviceInfoVec, vec); -} - -int32_t GetJoinedGroupInfoVecByDevId(const GroupQueryParams *params, GroupInfoVec *vec) -{ - if ((params == NULL) || (vec == NULL)) { - LOGE("[DB]: The input params or vec is NULL!"); - return HC_ERR_INVALID_PARAMS; - } - if ((params->udid == NULL) && (params->authId == NULL)) { - LOGE("[DB]: The input udid and authId cannot be NULL at the same time!"); - return HC_ERR_INVALID_PARAMS; - } - uint32_t index; - TrustedDeviceEntry *entry = NULL; - g_databaseMutex->lock(g_databaseMutex); - FOR_EACH_HC_VECTOR(g_trustedDeviceTable, index, entry) { - if ((entry == NULL) || - (!IsSatisfyGroup(entry->groupEntry, params)) || - (!IsSatisfyUdidAndAuthId(entry, params))) { - continue; - } - int32_t result = PushGroupInfoToVec(entry->groupEntry, StringGet(&entry->serviceType), NULL, vec); - if (result != HC_SUCCESS) { - g_databaseMutex->unlock(g_databaseMutex); - return result; - } - } - if (vec->size(vec) == 0) { - g_databaseMutex->unlock(g_databaseMutex); - return HC_ERR_GROUP_NOT_EXIST; - } - g_databaseMutex->unlock(g_databaseMutex); - return HC_SUCCESS; -} - -bool IsGroupOwner(const char *groupId, const char *appId) -{ - if ((groupId == NULL) || (appId == NULL)) { - LOGE("[DB]: The input parameter contains NULL value!"); - return false; - } - g_databaseMutex->lock(g_databaseMutex); - TrustedGroupEntry *entry = GetGroupEntryById(groupId); - if (entry == NULL) { - g_databaseMutex->unlock(g_databaseMutex); - LOGE("[DB]: The group cannot be found!"); - return false; - } - if (HC_VECTOR_SIZE(&(entry->managers)) <= 0) { - g_databaseMutex->unlock(g_databaseMutex); - LOGE("[DB]: The group does not have manager and owner!"); - return false; - } - HcString entryOwner = HC_VECTOR_GET(&(entry->managers), 0); - bool isOwner = (strcmp(StringGet(&entryOwner), appId) == 0) ? true : false; - g_databaseMutex->unlock(g_databaseMutex); - return isOwner; -} - -bool IsGroupAccessible(const char *groupId, const char *appId) -{ - if ((groupId == NULL) || (appId == NULL)) { - LOGE("[DB]: The input groupId or appId is NULL!"); - return false; - } - g_databaseMutex->lock(g_databaseMutex); - TrustedGroupEntry *entry = GetGroupEntryById(groupId); - if (entry == NULL) { - g_databaseMutex->unlock(g_databaseMutex); - LOGE("[DB]: The group cannot be found!"); - return false; - } - if ((entry->visibility == GROUP_VISIBILITY_PUBLIC) || - (IsGroupManager(appId, entry)) || - (IsGroupFriend(appId, entry))) { - g_databaseMutex->unlock(g_databaseMutex); - return true; - } - g_databaseMutex->unlock(g_databaseMutex); - return false; -} - -bool IsGroupEditAllowed(const char *groupId, const char *appId) -{ - if ((groupId == NULL) || (appId == NULL)) { - LOGE("[DB]: The input groupId or appId is NULL!"); - return false; - } - g_databaseMutex->lock(g_databaseMutex); - TrustedGroupEntry *entry = GetGroupEntryById(groupId); - if (entry == NULL) { - g_databaseMutex->unlock(g_databaseMutex); - LOGE("[DB]: The group cannot be found!"); - return false; - } - bool isManager = IsGroupManager(appId, entry) ? true : false; - g_databaseMutex->unlock(g_databaseMutex); - return isManager; -} - -const char *GetLocalDevUdid(void) -{ - if (strcmp(g_localUdid, "") == 0) { - int32_t res = HcGetUdid((uint8_t *)g_localUdid, INPUT_UDID_LEN); - if (res != HC_SUCCESS) { - LOGE("[DB]: Failed to get local udid! res: %d", res); - return NULL; - } - } - return g_localUdid; -} - -int32_t GetGroupInfo(int groupType, const char *groupId, const char *groupName, const char *groupOwner, - GroupInfoVec *groupInfoVec) -{ - /* Fuzzy query interfaces, so some parameters can be NULL. */ - if (groupInfoVec == NULL) { - LOGE("[DB]: The input groupInfoVec is NULL!"); - return HC_ERR_INVALID_PARAMS; - } - int32_t result; - uint32_t index; - TrustedGroupEntry **entry = NULL; - g_databaseMutex->lock(g_databaseMutex); - FOR_EACH_HC_VECTOR(g_trustedGroupTable, index, entry) { - if ((entry == NULL) || (*entry == NULL) || (!CompareSearchParams(groupType, groupId, groupName, - groupOwner, *entry))) { - continue; - } - if (((*entry)->type != ACROSS_ACCOUNT_AUTHORIZE_GROUP) || ((groupId == NULL) && (groupName == NULL))) { - result = PushGroupInfoToVec(*entry, NULL, NULL, groupInfoVec); - } else { - result = PushGroupInfoToVec(*entry, ((groupName != NULL) ? groupName : groupId), NULL, groupInfoVec); - } - if (result != HC_SUCCESS) { - g_databaseMutex->unlock(g_databaseMutex); - return result; - } - } - g_databaseMutex->unlock(g_databaseMutex); - return HC_SUCCESS; -} - -int32_t GetJoinedGroups(int groupType, GroupInfoVec *groupInfoVec) -{ - int32_t result; - uint32_t index; - TrustedGroupEntry **entry = NULL; - g_databaseMutex->lock(g_databaseMutex); - FOR_EACH_HC_VECTOR(g_trustedGroupTable, index, entry) { - if ((entry != NULL) && (*entry != NULL) && ((*entry)->type == groupType)) { - result = PushGroupInfoToVec(*entry, NULL, NULL, groupInfoVec); - if (result != HC_SUCCESS) { - g_databaseMutex->unlock(g_databaseMutex); - return result; - } - } - } - g_databaseMutex->unlock(g_databaseMutex); - return HC_SUCCESS; -} - -int32_t GetRelatedGroups(const char *peerDeviceId, bool isUdid, GroupInfoVec *groupInfoVec) -{ - int32_t result; - uint32_t index; - TrustedDeviceEntry *entry = NULL; - g_databaseMutex->lock(g_databaseMutex); - FOR_EACH_HC_VECTOR(g_trustedDeviceTable, index, entry) { - if (entry == NULL) { - continue; - } - const char *entryDevId = isUdid ? StringGet(&entry->udid) : StringGet(&entry->authId); - const char *serviceType = StringGet(&entry->serviceType); - if ((entryDevId == NULL) || (serviceType == NULL) || (strcmp(entryDevId, peerDeviceId) != 0)) { - continue; - } - if (strcmp(serviceType, "") == 0) { - result = PushGroupInfoToVec(entry->groupEntry, NULL, NULL, groupInfoVec); - } else { - result = PushGroupInfoToVec(entry->groupEntry, serviceType, NULL, groupInfoVec); - } - if (result != HC_SUCCESS) { - g_databaseMutex->unlock(g_databaseMutex); - return result; - } - } - g_databaseMutex->unlock(g_databaseMutex); - return HC_SUCCESS; -} - -int32_t GetTrustedDevices(const char *groupId, DeviceInfoVec *deviceInfoVec) -{ - int32_t result; - uint32_t index; - TrustedDeviceEntry *entry = NULL; - g_databaseMutex->lock(g_databaseMutex); - FOR_EACH_HC_VECTOR(g_trustedDeviceTable, index, entry) { - if ((entry != NULL) && (entry->groupEntry != NULL) && (CompareGroupIdInDeviceEntryOrNull(entry, groupId))) { - result = PushDevInfoToVec(entry, groupId, deviceInfoVec); - if (result != HC_SUCCESS) { - g_databaseMutex->unlock(g_databaseMutex); - return result; - } - } - } - g_databaseMutex->unlock(g_databaseMutex); - return HC_SUCCESS; -} - -int32_t InitDatabase(void) -{ - if (g_databaseMutex == NULL) { - g_databaseMutex = (HcMutex *)HcMalloc(sizeof(HcMutex), 0); - if (g_databaseMutex == NULL) { - LOGE("[DB]: Alloc databaseMutex failed"); - return HC_ERR_ALLOC_MEMORY; - } - if (InitHcMutex(g_databaseMutex) != HC_SUCCESS) { - LOGE("[DB]: Init mutex failed"); - HcFree(g_databaseMutex); - g_databaseMutex = NULL; - return HC_ERROR; - } - } - g_trustedGroupTable = CREATE_HC_VECTOR(TrustedGroupTable); - g_trustedDeviceTable = CREATE_HC_VECTOR(TrustedDeviceTable); - SetFilePath(FILE_ID_GROUP, GetStoragePath()); - if (!LoadDb()) { - LOGI("[DB]: Failed to load database, it may be the first time the database is read!"); - } else { - LOGI("[DB]: Load database successfully!"); - } - return HC_SUCCESS; -} - -void DestroyDatabase(void) -{ - g_databaseMutex->lock(g_databaseMutex); - DestroyTrustDeviceTable(); - DestroyGroupTable(); - g_databaseMutex->unlock(g_databaseMutex); - if (g_databaseMutex != NULL) { - DestroyHcMutex(g_databaseMutex); - HcFree(g_databaseMutex); - g_databaseMutex = NULL; - } -} \ No newline at end of file diff --git a/services/data_manager/src/database_util.c b/services/data_manager/src/database_util.c deleted file mode 100644 index 898944b..0000000 --- a/services/data_manager/src/database_util.c +++ /dev/null @@ -1,855 +0,0 @@ -/* - * Copyright (C) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "database_util.h" - -#include "broadcast_manager.h" -#include "device_auth.h" -#include "hc_dev_info.h" -#include "hc_log.h" -#include "hc_types.h" -#include "securec.h" - -IMPLEMENT_HC_VECTOR(StringVector, HcString, 1) - -/* cache across account groupId func */ -static GenGroupIdFunc g_generateIdFunc = NULL; - -static bool EndWithZero(HcParcel *parcel) -{ - const char *p = GetParcelLastChar(parcel); - if (p == NULL) { - return false; - } - if (*p == '\0') { - return true; - } - return false; -} - -static int32_t GenerateGroupInfoCommonByEntry(const TrustedGroupEntry *groupEntry, GroupInfo *returnGroupInfo) -{ - if (HC_VECTOR_SIZE(&(groupEntry->managers)) == 0) { - LOGE("[DB]: The group owner is lost!"); - return HC_ERR_LOST_DATA; - } - HcString entryOwner = HC_VECTOR_GET(&groupEntry->managers, 0); - if (!StringSet(&(returnGroupInfo->ownerName), entryOwner)) { - LOGE("[DB]: Failed to copy groupOwner!"); - return HC_ERR_MEMORY_COPY; - } - if (!StringSet(&(returnGroupInfo->userIdHash), groupEntry->userIdHash)) { - LOGE("[DB]: Failed to copy userIdHash!"); - return HC_ERR_MEMORY_COPY; - } - returnGroupInfo->type = groupEntry->type; - returnGroupInfo->visibility = groupEntry->visibility; - returnGroupInfo->expireTime = groupEntry->expireTime; - return HC_SUCCESS; -} - -static int32_t GenerateGroupInfoIdAndName(const char *groupId, const char *groupName, GroupInfo *returnGroupInfo) -{ - if (!StringSetPointer(&(returnGroupInfo->id), groupId)) { - LOGE("[DB]: Failed to copy groupId!"); - return HC_ERR_MEMORY_COPY; - } - if (!StringSetPointer(&(returnGroupInfo->name), groupName)) { - LOGE("[DB]: Failed to copy groupName!"); - return HC_ERR_MEMORY_COPY; - } - return HC_SUCCESS; -} - -static int32_t GenerateGroupInfoSharedUserIdHash(const char *sharedUserIdHash, GroupInfo *returnGroupInfo) -{ - if (!StringSetPointer(&(returnGroupInfo->sharedUserIdHash), sharedUserIdHash)) { - LOGE("[DB]: Failed to copy sharedUserIdHash!"); - return HC_ERR_MEMORY_COPY; - } - return HC_SUCCESS; -} - -static int32_t GenerateAcrossAccountGroupInfoById(const TrustedGroupEntry *groupEntry, const char *groupId, - GroupInfo *returnGroupInfo) -{ - int32_t result = GenerateGroupInfoIdAndName(groupId, groupId, returnGroupInfo); - if (result != HC_SUCCESS) { - return result; - } - const char *targetSharedUserIdHash = NULL; - result = GetSharedUserIdFromVecByGroupId(groupEntry, groupId, &targetSharedUserIdHash); - if (result != HC_SUCCESS) { - return result; - } - return GenerateGroupInfoSharedUserIdHash(targetSharedUserIdHash, returnGroupInfo); -} - -static int32_t GenerateAcrossAccountGroupInfoByUserIdHash(const TrustedGroupEntry *groupEntry, - const char *sharedUserIdHash, GroupInfo *returnGroupInfo) -{ - if (g_generateIdFunc == NULL) { - LOGE("[DB]: Generate groupId function is NULL!"); - return HC_ERR_NOT_SUPPORT; - } - char *tempGroupId = NULL; - const char *userIdHash = StringGet(&groupEntry->userIdHash); - if (userIdHash == NULL) { - LOGE("[DB]: Failed to get userIdHash from groupEntry!"); - return HC_ERR_NULL_PTR; - } - int32_t result = g_generateIdFunc(userIdHash, sharedUserIdHash, &tempGroupId); - if (result != HC_SUCCESS) { - LOGE("[DB]: Failed to generate temp groupId!"); - return result; - } - result = GenerateGroupInfoIdAndName(tempGroupId, tempGroupId, returnGroupInfo); - HcFree(tempGroupId); - if (result != HC_SUCCESS) { - return result; - } - return GenerateGroupInfoSharedUserIdHash(sharedUserIdHash, returnGroupInfo); -} - -static int32_t GenerateDeviceInfoCommonByEntry(const TrustedDeviceEntry *entry, TrustedDeviceEntry *returnDeviceInfo) -{ - if (!StringSet(&returnDeviceInfo->udid, entry->udid)) { - LOGE("[DB]: Failed to copy udid!"); - return HC_ERR_MEMORY_COPY; - } - if (!StringSet(&returnDeviceInfo->authId, entry->authId)) { - LOGE("[DB]: Failed to copy authId!"); - return HC_ERR_MEMORY_COPY; - } - if (!StringSet(&returnDeviceInfo->serviceType, entry->serviceType)) { - LOGE("[DB]: Failed to copy serviceType!"); - return HC_ERR_MEMORY_COPY; - } - if (!StringSet(&returnDeviceInfo->userIdHash, entry->userIdHash)) { - LOGE("[DB]: Failed to copy userIdHash!"); - return HC_ERR_MEMORY_COPY; - } - returnDeviceInfo->credential = entry->credential; - returnDeviceInfo->devType = entry->devType; - return HC_SUCCESS; -} - -static int32_t GenerateDeviceInfoId(const char *groupId, TrustedDeviceEntry *returnDeviceInfo) -{ - if (!StringSetPointer(&(returnDeviceInfo->groupId), groupId)) { - LOGE("[DB]: Failed to copy groupId!"); - return HC_ERR_MEMORY_COPY; - } - return HC_SUCCESS; -} - -static int32_t GenerateGroupInfoByDevEntry(const TrustedDeviceEntry *deviceEntry, GroupInfo *groupInfo) -{ - const TrustedGroupEntry *groupEntry = deviceEntry->groupEntry; - if (groupEntry == NULL) { - LOGE("[DB]: The groupEntry is NULL!"); - return HC_ERR_NULL_PTR; - } - const char *groupId = StringGet(&deviceEntry->serviceType); - if (groupId == NULL) { - LOGE("[DB]: The groupId is NULL!"); - return HC_ERR_NULL_PTR; - } - return GenerateGroupInfoByEntry(groupEntry, groupId, NULL, groupInfo); -} - -static int32_t AddGroupIdToList(const char *userIdHash, const char *sharedUserIdHash, CJson *groupIdList) -{ - char *groupId = NULL; - int32_t result = g_generateIdFunc(userIdHash, sharedUserIdHash, &groupId); - if (result != HC_SUCCESS) { - LOGE("[DB]: Failed to generate groupId!"); - return result; - } - if (AddStringToArray(groupIdList, groupId) != HC_SUCCESS) { - LOGE("[DB]: Failed to add groupId to groupIdList!"); - HcFree(groupId); - return HC_ERR_JSON_ADD; - } - HcFree(groupId); - return HC_SUCCESS; -} - -bool LoadStringVectorFromParcel(StringVector *vec, HcParcel *parcel) -{ - uint32_t strLen = 0; - do { - if (!ParcelReadUint32(parcel, &strLen)) { - return true; - } - if ((strLen == 0) || (strLen > MAX_STRING_LEN)) { - return false; - } - HcString str = CreateString(); - ClearParcel(&str.parcel); - if (!ParcelReadParcel(parcel, &str.parcel, strLen, false) || - !EndWithZero(&str.parcel)) { - DeleteString(&str); - return false; - } else { - if (vec->pushBack(vec, &str) == NULL) { - DeleteString(&str); - return false; - } - } - } while (1); -} - -bool SaveStringVectorToParcel(const StringVector *vec, HcParcel *parcel) -{ - uint32_t index; - HcString *str = NULL; - FOR_EACH_HC_VECTOR(*vec, index, str) { - uint32_t len = StringLength(str) + sizeof(char); - if (!ParcelWriteUint32(parcel, len)) { - return false; - } - if (!ParcelWrite(parcel, GetParcelData(&str->parcel), GetParcelDataSize(&str->parcel))) { - return false; - } - } - return true; -} - -StringVector CreateStrVector(void) -{ - return CreateStringVector(); -} - -void DestroyStrVector(StringVector *vec) -{ - uint32_t index; - HcString *strItemPtr = NULL; - FOR_EACH_HC_VECTOR(*vec, index, strItemPtr) { - DeleteString(strItemPtr); - } - DESTROY_HC_VECTOR(StringVector, vec); -} - -GroupInfo *CreateGroupInfoStruct(void) -{ - GroupInfo *ptr = (GroupInfo *)HcMalloc(sizeof(GroupInfo), 0); - if (ptr == NULL) { - LOGE("[DB]: Failed to allocate groupInfo memory!"); - return NULL; - } - ptr->name = CreateString(); - ptr->id = CreateString(); - ptr->ownerName = CreateString(); - ptr->userIdHash = CreateString(); - ptr->sharedUserIdHash = CreateString(); - return ptr; -} - -void DestroyGroupInfoStruct(GroupInfo *groupInfo) -{ - if (groupInfo == NULL) { - return; - } - DeleteString(&(groupInfo->name)); - DeleteString(&(groupInfo->id)); - DeleteString(&(groupInfo->ownerName)); - DeleteString(&(groupInfo->userIdHash)); - DeleteString(&(groupInfo->sharedUserIdHash)); - HcFree(groupInfo); -} - -TrustedDeviceEntry *CreateDeviceInfoStruct(void) -{ - TrustedDeviceEntry *deviceInfo = (TrustedDeviceEntry *)HcMalloc(sizeof(DeviceInfo), 0); - if (deviceInfo == NULL) { - LOGE("[DB]: Failed to allocate deviceInfo memory!"); - return NULL; - } - deviceInfo->authId = CreateString(); - deviceInfo->udid = CreateString(); - deviceInfo->serviceType = CreateString(); - deviceInfo->userIdHash = CreateString(); - deviceInfo->groupId = CreateString(); - return deviceInfo; -} - -void DestroyDeviceInfoStruct(TrustedDeviceEntry *deviceInfo) -{ - if (deviceInfo == NULL) { - return; - } - DeleteString(&(deviceInfo->authId)); - DeleteString(&(deviceInfo->udid)); - DeleteString(&(deviceInfo->serviceType)); - DeleteString(&(deviceInfo->userIdHash)); - DeleteString(&(deviceInfo->groupId)); - HcFree(deviceInfo); -} - -TrustedGroupEntry *CreateGroupEntryStruct(void) -{ - TrustedGroupEntry *ptr = (TrustedGroupEntry *)HcMalloc(sizeof(TrustedGroupEntry), 0); - if (ptr == NULL) { - LOGE("[DB]: Failed to allocate groupEntry memory!"); - return NULL; - } - ptr->name = CreateString(); - ptr->id = CreateString(); - ptr->userIdHash = CreateString(); - ptr->sharedUserIdHashVec = CREATE_HC_VECTOR(StringVector); - ptr->managers = CREATE_HC_VECTOR(StringVector); - ptr->friends = CREATE_HC_VECTOR(StringVector); - return ptr; -} - -void DestroyGroupEntryStruct(TrustedGroupEntry *groupEntry) -{ - DeleteString(&groupEntry->name); - DeleteString(&groupEntry->id); - DeleteString(&groupEntry->userIdHash); - DestroyStrVector(&groupEntry->managers); - DestroyStrVector(&groupEntry->friends); - DestroyStrVector(&groupEntry->sharedUserIdHashVec); -} - -void DestroyDeviceEntryStruct(TrustedDeviceEntry *deviceEntry) -{ - DeleteString(&deviceEntry->udid); - DeleteString(&deviceEntry->authId); - DeleteString(&deviceEntry->serviceType); - DeleteString(&deviceEntry->userIdHash); - DeleteParcel(&deviceEntry->ext); -} - -int32_t GenerateGroupInfoByEntry(const TrustedGroupEntry *groupEntry, const char *groupId, - const char *sharedUserIdHash, GroupInfo *returnGroupInfo) -{ - if ((groupEntry == NULL) || (returnGroupInfo == NULL)) { - LOGE("[DB]: The input parameters contains NULL value!"); - return HC_ERR_NULL_PTR; - } - int32_t result = GenerateGroupInfoCommonByEntry(groupEntry, returnGroupInfo); - if (result != HC_SUCCESS) { - return result; - } - if (groupEntry->type != ACROSS_ACCOUNT_AUTHORIZE_GROUP) { - return GenerateGroupInfoIdAndName(StringGet(&groupEntry->id), StringGet(&groupEntry->name), returnGroupInfo); - } - if (groupId != NULL) { - return GenerateAcrossAccountGroupInfoById(groupEntry, groupId, returnGroupInfo); - } else if (sharedUserIdHash != NULL) { - return GenerateAcrossAccountGroupInfoByUserIdHash(groupEntry, sharedUserIdHash, returnGroupInfo); - } else { - return HC_ERR_INVALID_PARAMS; - } -} - -int32_t GenerateDeviceInfoByEntry(const TrustedDeviceEntry *deviceEntry, const char *groupId, - TrustedDeviceEntry *returnDeviceInfo) -{ - int32_t result = GenerateDeviceInfoCommonByEntry(deviceEntry, returnDeviceInfo); - if (result != HC_SUCCESS) { - return result; - } - if (groupId != NULL) { - return GenerateDeviceInfoId(groupId, returnDeviceInfo); - } - if (deviceEntry->groupEntry->type != ACROSS_ACCOUNT_AUTHORIZE_GROUP) { - return GenerateDeviceInfoId(StringGet(&deviceEntry->groupEntry->id), returnDeviceInfo); - } - return GenerateDeviceInfoId(StringGet(&deviceEntry->serviceType), returnDeviceInfo); -} - -int32_t GetSharedUserIdFromVecByGroupId(const TrustedGroupEntry *groupEntry, const char *groupId, - const char **returnUserIdHash) -{ - if ((groupEntry == NULL) || (groupId == NULL) || (returnUserIdHash == NULL)) { - LOGE("[DB]: The input parameters contains NULL value!"); - return HC_ERR_INVALID_PARAMS; - } - if ((groupEntry->type != ACROSS_ACCOUNT_AUTHORIZE_GROUP) || (g_generateIdFunc == NULL)) { - return HC_ERR_NOT_SUPPORT; - } - if (strcmp(groupId, "") == 0) { - return HC_ERR_INVALID_PARAMS; - } - const char *userIdHash = StringGet(&groupEntry->userIdHash); - if (userIdHash == NULL) { - LOGE("[DB]: Failed to get userIdHash from groupEntry!"); - return HC_ERR_NULL_PTR; - } - /* The groupId of the across account group needs to be generated temporarily due to device resource problems. */ - uint32_t index; - HcString *sharedUserIdHash = NULL; - FOR_EACH_HC_VECTOR(groupEntry->sharedUserIdHashVec, index, sharedUserIdHash) { - const char *sharedUserIdHashPtr = StringGet(sharedUserIdHash); - if (sharedUserIdHashPtr == NULL) { - LOGW("[DB]: Failed to get sharedUserIdHash from sharedUserIdHashVec!"); - continue; - } - char *tmpGroupId = NULL; - int32_t result = g_generateIdFunc(userIdHash, sharedUserIdHashPtr, &tmpGroupId); - if (result != HC_SUCCESS) { - LOGE("[DB]: Failed to generate temp groupId!"); - return result; - } - bool isEquals = (strcmp(tmpGroupId, groupId) == 0) ? true : false; - HcFree(tmpGroupId); - if (isEquals) { - *returnUserIdHash = sharedUserIdHashPtr; - return HC_SUCCESS; - } - } - return HC_ERR_GROUP_NOT_EXIST; -} - -void AddNewSharedUserId(const StringVector *sharedUserIdHashList, TrustedGroupEntry *entry, CJson *groupIdList) -{ - if ((sharedUserIdHashList == NULL) || (entry == NULL) || (groupIdList == NULL)) { - LOGE("[DB]: The input parameters contains NULL value!"); - return; - } - HcString *sharedUserIdHash = NULL; - uint32_t sharedUserIdIndex = 0; - while (sharedUserIdIndex < sharedUserIdHashList->size(sharedUserIdHashList)) { - sharedUserIdHash = sharedUserIdHashList->getp(sharedUserIdHashList, sharedUserIdIndex); - sharedUserIdIndex++; - const char *sharedUserIdHashPtr = StringGet(sharedUserIdHash); - if (sharedUserIdHashPtr == NULL) { - continue; - } - bool isNeedAdd = true; - uint32_t tmpIndex; - HcString *tmpSharedUserIdHash = NULL; - FOR_EACH_HC_VECTOR(entry->sharedUserIdHashVec, tmpIndex, tmpSharedUserIdHash) { - const char *tmpSharedUserIdHashPtr = StringGet(tmpSharedUserIdHash); - if ((tmpSharedUserIdHashPtr != NULL) && (strcmp(sharedUserIdHashPtr, tmpSharedUserIdHashPtr) == 0)) { - isNeedAdd = false; - break; - } - } - if (!isNeedAdd) { - continue; - } - HcString newSharedUserIdHash = CreateString(); - if (!StringSet(&newSharedUserIdHash, *sharedUserIdHash)) { - LOGE("Failed to copy sharedUserIdHash!"); - DeleteString(&newSharedUserIdHash); - continue; - } - HC_VECTOR_PUSHBACK(&entry->sharedUserIdHashVec, &newSharedUserIdHash); - NotifyGroupCreated(entry, StringGet(&newSharedUserIdHash)); - if (groupIdList != NULL) { - (void)AddGroupIdToList(StringGet(&entry->userIdHash), StringGet(&newSharedUserIdHash), groupIdList); - } - LOGI("[DB]: Add a across account group to database successfully!"); - } -} - -void DeleteExpiredSharedUserId(const StringVector *sharedUserIdHashList, TrustedGroupEntry *entry) -{ - if ((sharedUserIdHashList == NULL) || (entry == NULL)) { - LOGE("[DB]: The input parameters contains NULL value!"); - return; - } - HcString *sharedUserIdHash = NULL; - uint32_t sharedUserIdIndex = 0; - while (sharedUserIdIndex < entry->sharedUserIdHashVec.size(&entry->sharedUserIdHashVec)) { - sharedUserIdHash = entry->sharedUserIdHashVec.getp(&entry->sharedUserIdHashVec, sharedUserIdIndex); - const char *sharedUserIdHashPtr = StringGet(sharedUserIdHash); - if (sharedUserIdHash == NULL) { - sharedUserIdIndex++; - continue; - } - bool isNeedRemove = true; - uint32_t tmpIndex; - HcString *tmpSharedUserIdHash = NULL; - FOR_EACH_HC_VECTOR(*sharedUserIdHashList, tmpIndex, tmpSharedUserIdHash) { - const char *tmpSharedUserIdHashPtr = StringGet(tmpSharedUserIdHash); - if ((tmpSharedUserIdHashPtr != NULL) && (strcmp(sharedUserIdHashPtr, tmpSharedUserIdHashPtr) == 0)) { - isNeedRemove = false; - break; - } - } - if (!isNeedRemove) { - sharedUserIdIndex++; - continue; - } - HcString popSharedUserIdHash; - HC_VECTOR_POPELEMENT(&entry->sharedUserIdHashVec, &popSharedUserIdHash, sharedUserIdIndex); - NotifyGroupDeleted(entry, StringGet(&popSharedUserIdHash)); - DeleteString(&popSharedUserIdHash); - LOGI("[DB]: Delete a across account group from database successfully!"); - } -} - -bool CompareGroupTypeInGroupEntryOrAll(const TrustedGroupEntry *groupEntry, int32_t groupType) -{ - if (groupType == ALL_GROUP) { - return true; - } else { - return (groupType == groupEntry->type); - } -} - -bool CompareDevIdInDeviceEntryOrNull(const TrustedDeviceEntry *deviceEntry, const char *devId, bool isUdid) -{ - if (devId == NULL) { - return true; - } else { - const char *entryDevId = isUdid ? StringGet(&deviceEntry->udid) : StringGet(&deviceEntry->authId); - if (entryDevId == NULL) { - return false; - } - return (strcmp(entryDevId, devId) == 0); - } -} - -bool CompareGroupIdInDeviceEntryOrNull(const TrustedDeviceEntry *deviceEntry, const char *groupId) -{ - if (groupId == NULL) { - return true; - } else { - const char *tmpGroupId = (deviceEntry->groupEntry->type != ACROSS_ACCOUNT_AUTHORIZE_GROUP) ? - (StringGet(&deviceEntry->groupEntry->id)) : (StringGet(&deviceEntry->serviceType)); - if (tmpGroupId == NULL) { - return false; - } - /* Check whether the device is the local device entry in the across account group. */ - if (strcmp(tmpGroupId, "") != 0) { - return (strcmp(tmpGroupId, groupId) == 0); - } - const char *sharedUserIdHash = NULL; - if (GetSharedUserIdFromVecByGroupId(deviceEntry->groupEntry, groupId, &sharedUserIdHash) == HC_SUCCESS) { - return true; - } else { - return false; - } - } -} - -bool CompareSearchParams(int32_t groupType, const char *groupId, const char *groupName, const char *groupOwner, - const TrustedGroupEntry *entry) -{ - if ((groupType != ALL_GROUP) && (entry->type != groupType)) { - return false; - } - if ((groupId != NULL) && (!IsGroupIdEquals(entry, groupId))) { - return false; - } - if ((groupName != NULL) && (!IsGroupNameEquals(entry, groupName))) { - return false; - } - if (HC_VECTOR_SIZE(&(entry->managers)) == 0) { - LOGE("[DB]: The group owner is lost!"); - return false; - } - if (groupOwner != NULL) { - HcString entryOwnerStr = HC_VECTOR_GET(&(entry->managers), 0); - const char *entryOwner = StringGet(&entryOwnerStr); - if ((entryOwner == NULL) || (strcmp(entryOwner, groupOwner) != 0)) { - return false; - } - } - return true; -} - -bool IsGroupIdEquals(const TrustedGroupEntry *groupEntry, const char *groupId) -{ - if ((groupEntry == NULL) || (groupId == NULL)) { - return false; - } - if (groupEntry->type != ACROSS_ACCOUNT_AUTHORIZE_GROUP) { - const char *entryGroupId = StringGet(&groupEntry->id); - if ((entryGroupId != NULL) && (strcmp(entryGroupId, groupId) == 0)) { - return true; - } - return false; - } - const char *sharedUserIdHash = NULL; - if (GetSharedUserIdFromVecByGroupId(groupEntry, groupId, &sharedUserIdHash) == HC_SUCCESS) { - return true; - } - return false; -} - -bool IsGroupNameEquals(const TrustedGroupEntry *groupEntry, const char *groupName) -{ - if ((groupEntry == NULL) || (groupName == NULL)) { - return false; - } - if (groupEntry->type != ACROSS_ACCOUNT_AUTHORIZE_GROUP) { - const char *entryGroupName = StringGet(&groupEntry->name); - if ((entryGroupName != NULL) && (strcmp(entryGroupName, groupName) == 0)) { - return true; - } - return false; - } - const char *sharedUserIdHash = NULL; - if (GetSharedUserIdFromVecByGroupId(groupEntry, groupName, &sharedUserIdHash) == HC_SUCCESS) { - return true; - } - return false; -} - -bool IsGroupManager(const char *appId, const TrustedGroupEntry *entry) -{ - uint32_t index; - HcString *manager = NULL; - FOR_EACH_HC_VECTOR(entry->managers, index, manager) { - if (strcmp(StringGet(manager), appId) == 0) { - return true; - } - } - return false; -} - -bool IsGroupFriend(const char *appId, const TrustedGroupEntry *entry) -{ - uint32_t index; - HcString *trustedFriend = NULL; - FOR_EACH_HC_VECTOR(entry->friends, index, trustedFriend) { - if (strcmp(StringGet(trustedFriend), appId) == 0) { - return true; - } - } - return false; -} - -bool SetGroupElement(TlvGroupElement *element, TrustedGroupEntry **entry) -{ - if (!StringSet(&element->name.data, (*entry)->name)) { - return false; - } - if (!StringSet(&element->id.data, (*entry)->id)) { - return false; - } - if (!StringSet(&element->userIdHash.data, (*entry)->userIdHash)) { - return false; - } - element->type.data = (*entry)->type; - element->visibility.data = (*entry)->visibility; - element->expireTime.data = (*entry)->expireTime; - if (!SaveStringVectorToParcel(&(*entry)->managers, &element->managers.data)) { - return false; - } - if (!SaveStringVectorToParcel(&(*entry)->friends, &element->friends.data)) { - return false; - } - if (!SaveStringVectorToParcel(&(*entry)->sharedUserIdHashVec, &element->sharedUserIdHashVec.data)) { - return false; - } - return true; -} - -bool SetDeviceElement(TlvDevAuthElement *element, TrustedDeviceEntry *entry) -{ - if (!StringSet(&element->groupId.data, entry->groupEntry->id)) { - return false; - } - if (!StringSet(&element->udid.data, entry->udid)) { - return false; - } - if (!StringSet(&element->authId.data, entry->authId)) { - return false; - } - if (!StringSet(&element->serviceType.data, entry->serviceType)) { - return false; - } - if (!StringSet(&element->userIdHash.data, entry->userIdHash)) { - return false; - } - if (!ParcelCopy(&element->ext.data, &entry->ext)) { - return false; - } - element->info.data.credential = entry->credential; - element->info.data.devType = entry->devType; - element->info.data.lastTm = entry->lastTm; - return true; -} - -bool SatisfyType(int32_t type, int32_t standardType) -{ - return ((standardType == ALL_GROUP) || (type == standardType)); -} - -bool SatisfyVisibility(int32_t visibility, int32_t standardVisibility) -{ - return ((standardVisibility == ALL_GROUP_VISIBILITY) || (visibility == standardVisibility)); -} - -void RegGenerateGroupIdFunc(GenGroupIdFunc func) -{ - if (func == NULL) { - LOGE("[DB]: The input func is NULL!"); - return; - } - g_generateIdFunc = func; -} - -void DeregGenerateGroupIdFunc(void) -{ - g_generateIdFunc = NULL; -} - -void NotifyGroupCreated(const TrustedGroupEntry *groupEntry, const char *sharedUserIdHash) -{ - if (!IsBroadcastSupported()) { - return; - } - if (groupEntry == NULL) { - return; - } - Broadcaster *broadcaster = GetBroadcaster(); - if ((broadcaster == NULL) || (broadcaster->postOnGroupCreated == NULL)) { - LOGE("[DB]: Failed to get broadcaster!"); - return; - } - GroupInfo *groupInfo = CreateGroupInfoStruct(); - if (groupInfo == NULL) { - LOGE("[DB]: Failed to allocate groupInfo memory!"); - return; - } - if (GenerateGroupInfoByEntry(groupEntry, NULL, sharedUserIdHash, groupInfo) != HC_SUCCESS) { - DestroyGroupInfoStruct(groupInfo); - return; - } - broadcaster->postOnGroupCreated(groupInfo); - DestroyGroupInfoStruct(groupInfo); -} - -void NotifyGroupDeleted(const TrustedGroupEntry *groupEntry, const char *sharedUserIdHash) -{ - if (!IsBroadcastSupported()) { - return; - } - if (groupEntry == NULL) { - return; - } - Broadcaster *broadcaster = GetBroadcaster(); - if ((broadcaster == NULL) || (broadcaster->postOnGroupDeleted == NULL)) { - LOGE("[DB]: Failed to get broadcaster!"); - return; - } - GroupInfo *groupInfo = CreateGroupInfoStruct(); - if (groupInfo == NULL) { - LOGE("[DB]: Failed to allocate groupInfo memory!"); - return; - } - if (GenerateGroupInfoByEntry(groupEntry, NULL, sharedUserIdHash, groupInfo) != HC_SUCCESS) { - DestroyGroupInfoStruct(groupInfo); - return; - } - broadcaster->postOnGroupDeleted(groupInfo); - DestroyGroupInfoStruct(groupInfo); -} - -void NotifyDeviceBound(const TrustedDeviceEntry *deviceEntry) -{ - if (!IsBroadcastSupported()) { - return; - } - if (deviceEntry == NULL) { - return; - } - Broadcaster *broadcaster = GetBroadcaster(); - if ((broadcaster == NULL) || (broadcaster->postOnDeviceBound == NULL)) { - LOGE("[DB]: Failed to get broadcaster!"); - return; - } - GroupInfo *groupInfo = CreateGroupInfoStruct(); - if (groupInfo == NULL) { - LOGE("[DB]: Failed to allocate groupInfo memory!"); - return; - } - if (GenerateGroupInfoByDevEntry(deviceEntry, groupInfo) != HC_SUCCESS) { - DestroyGroupInfoStruct(groupInfo); - return; - } - broadcaster->postOnDeviceBound(StringGet(&deviceEntry->udid), groupInfo); - DestroyGroupInfoStruct(groupInfo); -} - -void NotifyDeviceUnBound(const TrustedDeviceEntry *deviceEntry) -{ - if (!IsBroadcastSupported()) { - return; - } - if (deviceEntry == NULL) { - return; - } - Broadcaster *broadcaster = GetBroadcaster(); - if ((broadcaster == NULL) || (broadcaster->postOnDeviceUnBound == NULL)) { - LOGE("[DB]: Failed to get broadcaster!"); - return; - } - GroupInfo *groupInfo = CreateGroupInfoStruct(); - if (groupInfo == NULL) { - LOGE("[DB]: Failed to allocate groupInfo memory!"); - return; - } - if (GenerateGroupInfoByDevEntry(deviceEntry, groupInfo) != HC_SUCCESS) { - DestroyGroupInfoStruct(groupInfo); - return; - } - broadcaster->postOnDeviceUnBound(StringGet(&deviceEntry->udid), groupInfo); - DestroyGroupInfoStruct(groupInfo); -} - -void NotifyDeviceNotTrusted(const char *peerUdid) -{ - if (!IsBroadcastSupported()) { - return; - } - if (peerUdid == NULL) { - return; - } - Broadcaster *broadcaster = GetBroadcaster(); - if ((broadcaster == NULL) || (broadcaster->postOnDeviceNotTrusted == NULL)) { - LOGE("[DB]: Failed to get broadcaster!"); - return; - } - broadcaster->postOnDeviceNotTrusted(peerUdid); -} - -void NotifyLastGroupDeleted(const char *peerUdid, int groupType) -{ - if (!IsBroadcastSupported()) { - return; - } - if (peerUdid == NULL) { - return; - } - Broadcaster *broadcaster = GetBroadcaster(); - if ((broadcaster == NULL) || (broadcaster->postOnLastGroupDeleted == NULL)) { - LOGE("[DB]: Failed to get broadcaster!"); - return; - } - broadcaster->postOnLastGroupDeleted(peerUdid, groupType); -} - -void NotifyTrustedDeviceNumChanged(int trustedDeviceNum) -{ - if (!IsBroadcastSupported()) { - return; - } - Broadcaster *broadcaster = GetBroadcaster(); - if ((broadcaster == NULL) || (broadcaster->postOnTrustedDeviceNumChanged == NULL)) { - LOGE("[DB]: Failed to get broadcaster!"); - return; - } - broadcaster->postOnTrustedDeviceNumChanged(trustedDeviceNum); -} \ No newline at end of file diff --git a/services/group_auth/src/session/auth_session/auth_session_util.c b/services/group_auth/src/session/auth_session/auth_session_util.c index 3122111..b4a1c97 100644 --- a/services/group_auth/src/session/auth_session/auth_session_util.c +++ b/services/group_auth/src/session/auth_session/auth_session_util.c @@ -90,7 +90,7 @@ int32_t GetInfoHash(const uint8_t *info, uint32_t infoLen, char *str, uint32_t s } int32_t res; do { - if (memcpy_s(message.val, message.length, info, infoLen) != HC_SUCCESS) { + if (memcpy_s(message.val, message.length, info, infoLen) != EOK) { LOGE("Failed to copy data!"); res = HC_ERR_ALLOC_MEMORY; break; diff --git a/services/group_manager/src/channel_manager/soft_bus_channel/soft_bus_channel.c b/services/group_manager/src/channel_manager/soft_bus_channel/soft_bus_channel.c index d260de9..e4aae3e 100644 --- a/services/group_manager/src/channel_manager/soft_bus_channel/soft_bus_channel.c +++ b/services/group_manager/src/channel_manager/soft_bus_channel/soft_bus_channel.c @@ -62,7 +62,7 @@ static int32_t AddChannelEntry(int64_t requestId, int64_t channelId) { int64_t tmpReqId = DEFAULT_REQUEST_ID; if (GetReqIdByChannelId(channelId, &tmpReqId) == HC_SUCCESS) { - LOGE("A request to use the channel alreay exists!"); + LOGE("A request to use the channel already exists!"); return HC_ERR_REQUEST_EXIST; } ChannelEntry entry = { @@ -149,7 +149,7 @@ static char *GenRecvData(int64_t channelId, const void *data, uint32_t dataLen, static bool IsServer(int sessionId) { - return (GetSessionSide(sessionId) == 0) ? true : false; + return GetSessionSide(sessionId) == 0; } static int OnChannelOpenedCb(int sessionId, int result) diff --git a/services/group_manager/src/group_operation/group_operation_common.c b/services/group_manager/src/group_operation/group_operation_common.c index 117f124..491c843 100644 --- a/services/group_manager/src/group_operation/group_operation_common.c +++ b/services/group_manager/src/group_operation/group_operation_common.c @@ -998,7 +998,7 @@ int32_t ProcessKeyPair(int action, const CJson *jsonParams, const char *groupId) LOGE("Failed to allocate authIdBuff memory!"); return HC_ERR_ALLOC_MEMORY; } - if (memcpy_s(authIdBuff.val, authIdBuff.length, authId, authIdBuff.length) != HC_SUCCESS) { + if (memcpy_s(authIdBuff.val, authIdBuff.length, authId, authIdBuff.length) != EOK) { LOGE("Failed to copy authId!"); HcFree(authIdBuff.val); return HC_ERR_MEMORY_COPY; diff --git a/test/unittest/deviceauth/BUILD.gn b/test/unittest/deviceauth/BUILD.gn index 731c22f..a24d719 100644 --- a/test/unittest/deviceauth/BUILD.gn +++ b/test/unittest/deviceauth/BUILD.gn @@ -30,6 +30,7 @@ ohos_unittest("deviceauth_llt") { "//foundation/communication/dsoftbus/interfaces/kits/common", "//foundation/communication/dsoftbus/interfaces/kits/transport", "//foundation/communication/dsoftbus/interfaces/inner_kits/transport", + "//base/startup/syspara_lite/interfaces/innerkits/native/syspara/include", ] sources = [ @@ -47,19 +48,18 @@ ohos_unittest("deviceauth_llt") { "${os_adapter_path}/impl/src/hc_task_thread.c", "${os_adapter_path}/impl/src/hc_time.c", "${os_adapter_path}/impl/src/linux/hc_condition.c", + "${os_adapter_path}/impl/src/linux/hc_dev_info.c", "${os_adapter_path}/impl/src/linux/hc_file.c", "${os_adapter_path}/impl/src/linux/hc_init_protection.c", "${os_adapter_path}/impl/src/linux/hc_thread.c", "${os_adapter_path}/impl/src/linux/hc_types.c", ] sources += deviceauth_files - sources += [ - "source/deviceauth_standard_test.cpp", - "source/deviceauth_test_mock.cpp", - ] + sources += [ "source/deviceauth_standard_test.cpp" ] deps = [ "//base/security/huks/interfaces/innerkits/huks_standard/main:libhukssdk", + "//base/startup/syspara_lite/interfaces/innerkits/native/syspara:syspara", "//third_party/cJSON:cjson_static", "//third_party/googletest:gmock_main", "//third_party/googletest:gtest_main", diff --git a/test/unittest/deviceauth/include/deviceauth_test_mock.h b/test/unittest/deviceauth/include/deviceauth_test_mock.h deleted file mode 100644 index 8fa96f9..0000000 --- a/test/unittest/deviceauth/include/deviceauth_test_mock.h +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright (C) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef DEVICEAUTH_STANDARD_MOCK_H -#define DEVICEAUTH_STANDARD_MOCK_H - -#include -#include -#include "hc_dev_info.h" - -void SetClient(bool tag); -bool GetClient(); - -const char *GetStorageDirPath(void); - -#endif diff --git a/test/unittest/deviceauth/source/deviceauth_standard_test.cpp b/test/unittest/deviceauth/source/deviceauth_standard_test.cpp index 46bbe4a..e55b612 100644 --- a/test/unittest/deviceauth/source/deviceauth_standard_test.cpp +++ b/test/unittest/deviceauth/source/deviceauth_standard_test.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2021 Huawei Device Co., Ltd. + * Copyright (C) 2021-2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -14,44 +14,730 @@ */ #include "deviceauth_standard_test.h" -#include -#include +#include #include "common_defs.h" -#include "deviceauth_test_mock.h" #include "device_auth.h" #include "device_auth_defines.h" -#include "gtest/gtest.h" -#include "gmock/gmock.h" #include "json_utils.h" +#include "securec.h" using namespace std; using namespace testing::ext; -/* test suit - GET_INSTANCE */ -class GET_INSTANCE : public testing::Test { +#define TEST_REQ_ID 123 +#define TEST_APP_ID "TestAppId" + +class InitDeviceAuthServiceTest : public testing::Test { public: - static void SetUpTestCase() {}; - static void TearDownTestCase() {}; - virtual void SetUp() - { - int32_t ret = InitDeviceAuthService(); - EXPECT_EQ(ret == HC_SUCCESS, true); - } - virtual void TearDown() - { - DestroyDeviceAuthService(); - } + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); }; -/* start cases */ -TEST_F(GET_INSTANCE, TC_GET_GM_INSTANCE) +void InitDeviceAuthServiceTest::SetUpTestCase() {} +void InitDeviceAuthServiceTest::TearDownTestCase() {} +void InitDeviceAuthServiceTest::SetUp() {} +void InitDeviceAuthServiceTest::TearDown() {} + +HWTEST_F(InitDeviceAuthServiceTest, InitDeviceAuthServiceTest001, TestSize.Level0) +{ + int ret = InitDeviceAuthService(); + EXPECT_EQ(ret, HC_SUCCESS); +} + +class DestroyDeviceAuthServiceTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void DestroyDeviceAuthServiceTest::SetUpTestCase() {} +void DestroyDeviceAuthServiceTest::TearDownTestCase() {} +void DestroyDeviceAuthServiceTest::SetUp() {} +void DestroyDeviceAuthServiceTest::TearDown() {} + +HWTEST_F(DestroyDeviceAuthServiceTest, DestroyDeviceAuthServiceTest001, TestSize.Level0) +{ + int ret = InitDeviceAuthService(); + EXPECT_EQ(ret, HC_SUCCESS); + DestroyDeviceAuthService(); +} + +class GetGmInstanceTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void GetGmInstanceTest::SetUpTestCase() {} +void GetGmInstanceTest::TearDownTestCase() {} + +void GetGmInstanceTest::SetUp() +{ + int ret = InitDeviceAuthService(); + EXPECT_EQ(ret, HC_SUCCESS); +} + +void GetGmInstanceTest::TearDown() +{ + DestroyDeviceAuthService(); +} + +HWTEST_F(GetGmInstanceTest, GetGmInstanceTest001, TestSize.Level0) { const DeviceGroupManager *gm = GetGmInstance(); EXPECT_NE(gm, nullptr); } -TEST_F(GET_INSTANCE, TC_GET_GA_INSTANCE) +class GetGaInstanceTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void GetGaInstanceTest::SetUpTestCase() {} +void GetGaInstanceTest::TearDownTestCase() {} + +void GetGaInstanceTest::SetUp() +{ + int ret = InitDeviceAuthService(); + EXPECT_EQ(ret, HC_SUCCESS); +} + +void GetGaInstanceTest::TearDown() +{ + DestroyDeviceAuthService(); +} + +HWTEST_F(GetGaInstanceTest, GetGaInstanceTest001, TestSize.Level0) { const GroupAuthManager *ga = GetGaInstance(); EXPECT_NE(ga, nullptr); } + +class GmRegCallbackTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void GmRegCallbackTest::SetUpTestCase() {} +void GmRegCallbackTest::TearDownTestCase() {} + +void GmRegCallbackTest::SetUp() +{ + int ret = InitDeviceAuthService(); + EXPECT_EQ(ret, HC_SUCCESS); +} + +void GmRegCallbackTest::TearDown() +{ + DestroyDeviceAuthService(); +} + +HWTEST_F(GmRegCallbackTest, GmRegCallbackTest001, TestSize.Level0) +{ + const DeviceGroupManager *gm = GetGmInstance(); + EXPECT_NE(gm, nullptr); + DeviceAuthCallback callback; + int32_t ret = gm->regCallback(TEST_APP_ID, &callback); + EXPECT_EQ(ret, HC_SUCCESS); +} + +class GmUnRegCallbackTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void GmUnRegCallbackTest::SetUpTestCase() {} +void GmUnRegCallbackTest::TearDownTestCase() {} + +void GmUnRegCallbackTest::SetUp() +{ + int ret = InitDeviceAuthService(); + EXPECT_EQ(ret, HC_SUCCESS); +} + +void GmUnRegCallbackTest::TearDown() +{ + DestroyDeviceAuthService(); +} + +HWTEST_F(GmUnRegCallbackTest, GmUnRegCallbackTest001, TestSize.Level0) +{ + const DeviceGroupManager *gm = GetGmInstance(); + EXPECT_NE(gm, nullptr); + DeviceAuthCallback callback; + int32_t ret = gm->regCallback(TEST_APP_ID, &callback); + EXPECT_EQ(ret, HC_SUCCESS); + ret = gm->unRegCallback(TEST_APP_ID); + EXPECT_EQ(ret, HC_SUCCESS); +} + +class GmRegDataChangeListenerTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void GmRegDataChangeListenerTest::SetUpTestCase() {} +void GmRegDataChangeListenerTest::TearDownTestCase() {} + +void GmRegDataChangeListenerTest::SetUp() +{ + int ret = InitDeviceAuthService(); + EXPECT_EQ(ret, HC_SUCCESS); +} + +void GmRegDataChangeListenerTest::TearDown() +{ + DestroyDeviceAuthService(); +} + +HWTEST_F(GmRegDataChangeListenerTest, GmRegDataChangeListenerTest001, TestSize.Level0) +{ + const DeviceGroupManager *gm = GetGmInstance(); + EXPECT_NE(gm, nullptr); + DataChangeListener listener; + int32_t ret = gm->regDataChangeListener(TEST_APP_ID, &listener); + EXPECT_EQ(ret, HC_SUCCESS); +} + +class GmUnRegDataChangeListenerTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void GmUnRegDataChangeListenerTest::SetUpTestCase() {} +void GmUnRegDataChangeListenerTest::TearDownTestCase() {} + +void GmUnRegDataChangeListenerTest::SetUp() +{ + int ret = InitDeviceAuthService(); + EXPECT_EQ(ret, HC_SUCCESS); +} + +void GmUnRegDataChangeListenerTest::TearDown() +{ + DestroyDeviceAuthService(); +} + +HWTEST_F(GmUnRegDataChangeListenerTest, GmUnRegDataChangeListenerTest001, TestSize.Level0) +{ + const DeviceGroupManager *gm = GetGmInstance(); + EXPECT_NE(gm, nullptr); + DataChangeListener listener; + int32_t ret = gm->regDataChangeListener(TEST_APP_ID, &listener); + EXPECT_EQ(ret, HC_SUCCESS); + ret = gm->unRegDataChangeListener(TEST_APP_ID); + EXPECT_EQ(ret, HC_SUCCESS); +} + +class GmCreateGroupTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void GmCreateGroupTest::SetUpTestCase() {} +void GmCreateGroupTest::TearDownTestCase() {} + +void GmCreateGroupTest::SetUp() +{ + int ret = InitDeviceAuthService(); + EXPECT_EQ(ret, HC_SUCCESS); +} + +void GmCreateGroupTest::TearDown() +{ + DestroyDeviceAuthService(); +} + +HWTEST_F(GmCreateGroupTest, GmCreateGroupTest002, TestSize.Level0) +{ + const DeviceGroupManager *gm = GetGmInstance(); + EXPECT_NE(gm, nullptr); + int32_t ret = gm->createGroup(DEFAULT_OS_ACCOUNT, TEST_REQ_ID, TEST_APP_ID, NULL); + EXPECT_NE(ret, HC_SUCCESS); +} + +class GmDeleteGroupTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void GmDeleteGroupTest::SetUpTestCase() {} +void GmDeleteGroupTest::TearDownTestCase() {} + +void GmDeleteGroupTest::SetUp() +{ + int ret = InitDeviceAuthService(); + EXPECT_EQ(ret, HC_SUCCESS); +} + +void GmDeleteGroupTest::TearDown() +{ + DestroyDeviceAuthService(); +} + +HWTEST_F(GmDeleteGroupTest, GmDeleteGroupTest002, TestSize.Level0) +{ + const DeviceGroupManager *gm = GetGmInstance(); + EXPECT_NE(gm, nullptr); + int32_t ret = gm->deleteGroup(DEFAULT_OS_ACCOUNT, TEST_REQ_ID, TEST_APP_ID, NULL); + EXPECT_NE(ret, HC_SUCCESS); +} + +class GmAddMemberToGroupTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void GmAddMemberToGroupTest::SetUpTestCase() {} +void GmAddMemberToGroupTest::TearDownTestCase() {} + +void GmAddMemberToGroupTest::SetUp() +{ + int ret = InitDeviceAuthService(); + EXPECT_EQ(ret, HC_SUCCESS); +} + +void GmAddMemberToGroupTest::TearDown() +{ + DestroyDeviceAuthService(); +} + +HWTEST_F(GmAddMemberToGroupTest, GmAddMemberToGroupTest002, TestSize.Level0) +{ + const DeviceGroupManager *gm = GetGmInstance(); + EXPECT_NE(gm, nullptr); + int32_t ret = gm->addMemberToGroup(DEFAULT_OS_ACCOUNT, TEST_REQ_ID, TEST_APP_ID, NULL); + EXPECT_NE(ret, HC_SUCCESS); +} + +class GmDeleteMemberFromGroupTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void GmDeleteMemberFromGroupTest::SetUpTestCase() {} +void GmDeleteMemberFromGroupTest::TearDownTestCase() {} + +void GmDeleteMemberFromGroupTest::SetUp() +{ + int ret = InitDeviceAuthService(); + EXPECT_EQ(ret, HC_SUCCESS); +} + +void GmDeleteMemberFromGroupTest::TearDown() +{ + DestroyDeviceAuthService(); +} + +HWTEST_F(GmDeleteMemberFromGroupTest, GmDeleteMemberFromGroupTest002, TestSize.Level0) +{ + const DeviceGroupManager *gm = GetGmInstance(); + EXPECT_NE(gm, nullptr); + int32_t ret = gm->deleteMemberFromGroup(DEFAULT_OS_ACCOUNT, TEST_REQ_ID, TEST_APP_ID, NULL); + EXPECT_NE(ret, HC_SUCCESS); +} + +class GmProcessDataTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void GmProcessDataTest::SetUpTestCase() {} +void GmProcessDataTest::TearDownTestCase() {} + +void GmProcessDataTest::SetUp() +{ + int ret = InitDeviceAuthService(); + EXPECT_EQ(ret, HC_SUCCESS); +} + +void GmProcessDataTest::TearDown() +{ + DestroyDeviceAuthService(); +} + +HWTEST_F(GmProcessDataTest, GmProcessDataTest002, TestSize.Level0) +{ + const DeviceGroupManager *gm = GetGmInstance(); + EXPECT_NE(gm, nullptr); + int32_t ret = gm->processData(TEST_REQ_ID, NULL, 0); + EXPECT_NE(ret, HC_SUCCESS); +} + +class GmCheckAccessToGroupTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void GmCheckAccessToGroupTest::SetUpTestCase() {} +void GmCheckAccessToGroupTest::TearDownTestCase() {} + +void GmCheckAccessToGroupTest::SetUp() +{ + int ret = InitDeviceAuthService(); + EXPECT_EQ(ret, HC_SUCCESS); +} + +void GmCheckAccessToGroupTest::TearDown() +{ + DestroyDeviceAuthService(); +} + +HWTEST_F(GmCheckAccessToGroupTest, GmCheckAccessToGroupTest002, TestSize.Level0) +{ + const DeviceGroupManager *gm = GetGmInstance(); + EXPECT_NE(gm, nullptr); + int32_t ret = gm->checkAccessToGroup(TEST_REQ_ID, TEST_APP_ID, NULL); + EXPECT_NE(ret, HC_SUCCESS); +} + +class GmGetPkInfoListTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void GmGetPkInfoListTest::SetUpTestCase() {} +void GmGetPkInfoListTest::TearDownTestCase() {} + +void GmGetPkInfoListTest::SetUp() +{ + int ret = InitDeviceAuthService(); + EXPECT_EQ(ret, HC_SUCCESS); +} + +void GmGetPkInfoListTest::TearDown() +{ + DestroyDeviceAuthService(); +} + +HWTEST_F(GmGetPkInfoListTest, GmGetPkInfoListTest002, TestSize.Level0) +{ + const DeviceGroupManager *gm = GetGmInstance(); + EXPECT_NE(gm, nullptr); + char *returnData = NULL; + uint32_t returnNum = 0; + int32_t ret = gm->getPkInfoList(TEST_REQ_ID, TEST_APP_ID, NULL, &returnData, &returnNum); + EXPECT_NE(ret, HC_SUCCESS); +} + +class GmGetGroupInfoByIdTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void GmGetGroupInfoByIdTest::SetUpTestCase() {} +void GmGetGroupInfoByIdTest::TearDownTestCase() {} + +void GmGetGroupInfoByIdTest::SetUp() +{ + int ret = InitDeviceAuthService(); + EXPECT_EQ(ret, HC_SUCCESS); +} + +void GmGetGroupInfoByIdTest::TearDown() +{ + DestroyDeviceAuthService(); +} + +HWTEST_F(GmGetGroupInfoByIdTest, GmGetGroupInfoByIdTest002, TestSize.Level0) +{ + const DeviceGroupManager *gm = GetGmInstance(); + EXPECT_NE(gm, nullptr); + char *returnData = NULL; + int32_t ret = gm->getGroupInfoById(TEST_REQ_ID, TEST_APP_ID, NULL, &returnData); + EXPECT_NE(ret, HC_SUCCESS); +} + +class GmGetGroupInfoTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void GmGetGroupInfoTest::SetUpTestCase() {} +void GmGetGroupInfoTest::TearDownTestCase() {} + +void GmGetGroupInfoTest::SetUp() +{ + int ret = InitDeviceAuthService(); + EXPECT_EQ(ret, HC_SUCCESS); +} + +void GmGetGroupInfoTest::TearDown() +{ + DestroyDeviceAuthService(); +} + +HWTEST_F(GmGetGroupInfoTest, GmGetGroupInfoTest002, TestSize.Level0) +{ + const DeviceGroupManager *gm = GetGmInstance(); + EXPECT_NE(gm, nullptr); + char *returnData = NULL; + uint32_t returnNum = 0; + int32_t ret = gm->getGroupInfo(TEST_REQ_ID, TEST_APP_ID, NULL, &returnData, &returnNum); + EXPECT_NE(ret, HC_SUCCESS); +} + +class GmGetJoinedGroupsTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void GmGetJoinedGroupsTest::SetUpTestCase() {} +void GmGetJoinedGroupsTest::TearDownTestCase() {} + +void GmGetJoinedGroupsTest::SetUp() +{ + int ret = InitDeviceAuthService(); + EXPECT_EQ(ret, HC_SUCCESS); +} + +void GmGetJoinedGroupsTest::TearDown() +{ + DestroyDeviceAuthService(); +} + +HWTEST_F(GmGetJoinedGroupsTest, GmGetJoinedGroupsTest002, TestSize.Level0) +{ + const DeviceGroupManager *gm = GetGmInstance(); + EXPECT_NE(gm, nullptr); + char *returnData = NULL; + uint32_t returnNum = 0; + int32_t ret = gm->getJoinedGroups(TEST_REQ_ID, TEST_APP_ID, IDENTICAL_ACCOUNT_GROUP, &returnData, &returnNum); + EXPECT_NE(ret, HC_SUCCESS); +} + +class GmGetRelatedGroupsTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void GmGetRelatedGroupsTest::SetUpTestCase() {} +void GmGetRelatedGroupsTest::TearDownTestCase() {} + +void GmGetRelatedGroupsTest::SetUp() +{ + int ret = InitDeviceAuthService(); + EXPECT_EQ(ret, HC_SUCCESS); +} + +void GmGetRelatedGroupsTest::TearDown() +{ + DestroyDeviceAuthService(); +} + +HWTEST_F(GmGetRelatedGroupsTest, GmGetRelatedGroupsTest002, TestSize.Level0) +{ + const DeviceGroupManager *gm = GetGmInstance(); + EXPECT_NE(gm, nullptr); + char *returnData = NULL; + uint32_t returnNum = 0; + int32_t ret = gm->getRelatedGroups(TEST_REQ_ID, TEST_APP_ID, NULL, &returnData, &returnNum); + EXPECT_NE(ret, HC_SUCCESS); +} + +class GmGetDeviceInfoByIdTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void GmGetDeviceInfoByIdTest::SetUpTestCase() {} +void GmGetDeviceInfoByIdTest::TearDownTestCase() {} + +void GmGetDeviceInfoByIdTest::SetUp() +{ + int ret = InitDeviceAuthService(); + EXPECT_EQ(ret, HC_SUCCESS); +} + +void GmGetDeviceInfoByIdTest::TearDown() +{ + DestroyDeviceAuthService(); +} + +HWTEST_F(GmGetDeviceInfoByIdTest, GmGetDeviceInfoByIdTest002, TestSize.Level0) +{ + const DeviceGroupManager *gm = GetGmInstance(); + EXPECT_NE(gm, nullptr); + char *returnData = NULL; + int32_t ret = gm->getDeviceInfoById(TEST_REQ_ID, TEST_APP_ID, NULL, NULL, &returnData); + EXPECT_NE(ret, HC_SUCCESS); +} + +class GmGetTrustedDevicesTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void GmGetTrustedDevicesTest::SetUpTestCase() {} +void GmGetTrustedDevicesTest::TearDownTestCase() {} + +void GmGetTrustedDevicesTest::SetUp() +{ + int ret = InitDeviceAuthService(); + EXPECT_EQ(ret, HC_SUCCESS); +} + +void GmGetTrustedDevicesTest::TearDown() +{ + DestroyDeviceAuthService(); +} + +HWTEST_F(GmGetTrustedDevicesTest, GmGetTrustedDevicesTest002, TestSize.Level0) +{ + const DeviceGroupManager *gm = GetGmInstance(); + EXPECT_NE(gm, nullptr); + char *returnData = NULL; + uint32_t returnNum = 0; + int32_t ret = gm->getTrustedDevices(TEST_REQ_ID, TEST_APP_ID, NULL, &returnData, &returnNum); + EXPECT_NE(ret, HC_SUCCESS); +} + +class GmIsDeviceInGroupTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void GmIsDeviceInGroupTest::SetUpTestCase() {} +void GmIsDeviceInGroupTest::TearDownTestCase() {} + +void GmIsDeviceInGroupTest::SetUp() +{ + int ret = InitDeviceAuthService(); + EXPECT_EQ(ret, HC_SUCCESS); +} + +void GmIsDeviceInGroupTest::TearDown() +{ + DestroyDeviceAuthService(); +} + +HWTEST_F(GmIsDeviceInGroupTest, GmIsDeviceInGroupTest002, TestSize.Level0) +{ + const DeviceGroupManager *gm = GetGmInstance(); + EXPECT_NE(gm, nullptr); + bool ret = gm->isDeviceInGroup(TEST_REQ_ID, TEST_APP_ID, NULL, NULL); + EXPECT_NE(ret, true); +} + +class GaAuthDeviceTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void GaAuthDeviceTest::SetUpTestCase() {} +void GaAuthDeviceTest::TearDownTestCase() {} + +void GaAuthDeviceTest::SetUp() +{ + int ret = InitDeviceAuthService(); + EXPECT_EQ(ret, HC_SUCCESS); +} + +void GaAuthDeviceTest::TearDown() +{ + DestroyDeviceAuthService(); +} + +HWTEST_F(GaAuthDeviceTest, GaAuthDeviceTest002, TestSize.Level0) +{ + const GroupAuthManager *ga = GetGaInstance(); + EXPECT_NE(ga, nullptr); + int32_t ret = ga->authDevice(DEFAULT_OS_ACCOUNT, TEST_REQ_ID, NULL, NULL); + EXPECT_NE(ret, HC_SUCCESS); +} + +class GaProcessDataTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void GaProcessDataTest::SetUpTestCase() {} +void GaProcessDataTest::TearDownTestCase() {} + +void GaProcessDataTest::SetUp() +{ + int ret = InitDeviceAuthService(); + EXPECT_EQ(ret, HC_SUCCESS); +} + +void GaProcessDataTest::TearDown() +{ + DestroyDeviceAuthService(); +} + +HWTEST_F(GaProcessDataTest, GaProcessDataTest002, TestSize.Level0) +{ + const GroupAuthManager *ga = GetGaInstance(); + EXPECT_NE(ga, nullptr); + int32_t ret = ga->processData(TEST_REQ_ID, NULL, 0, NULL); + EXPECT_NE(ret, HC_SUCCESS); +} \ No newline at end of file diff --git a/test/unittest/deviceauth/source/deviceauth_test_mock.cpp b/test/unittest/deviceauth/source/deviceauth_test_mock.cpp deleted file mode 100644 index b5e42b6..0000000 --- a/test/unittest/deviceauth/source/deviceauth_test_mock.cpp +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Copyright (C) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "deviceauth_test_mock.h" -#include "iostream" -#include -#include "securec.h" - -using namespace std; - -static bool g_testForClient = false; - -void SetClient(bool tag) -{ - g_testForClient = tag; -} - -bool GetClient() -{ - return g_testForClient; -} - -int32_t HcGetUdid(uint8_t *udid, int32_t udidLen) -{ - const char *clientUdid = "D6350E39AD8F11963C181BEEDC11AC85158E04466B68F1F4E6D895237E0FE81C"; - const char *serverUdid = "ABCDEF00ABCDEF00ABCDEF00ABCDEF00ABCDEF00ABCDEF00ABCDEF00ABCDEF00"; - if (g_testForClient) { - (void)memcpy_s(udid, udidLen - 1, clientUdid, strlen(clientUdid)); - return 0; - } - (void)memcpy_s(udid, udidLen - 1, serverUdid, strlen(serverUdid)); - return 0; -} - -const char *GetStoragePath(void) -{ - return "/data/data/deviceauth/hcgroup.dat"; -} - -const char *GetStorageDirPath(void) -{ -#ifndef LITE_DEVICE - const char *storageFile = "/data/data/deviceauth"; -#else - const char *storageFile = "/storage/deviceauth"; -#endif - return storageFile; -} \ No newline at end of file