mirror of
https://gitee.com/openharmony/useriam_user_auth_framework
synced 2024-11-23 07:39:51 +00:00
first
Signed-off-by: LHY6968 <lihuayue1@huawei.com> Change-Id: Ibced5b5e82545da56bce745623df3be43bfaad74
This commit is contained in:
parent
2861f24053
commit
f05abcd9ed
@ -17,6 +17,7 @@
|
||||
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <endian.h>
|
||||
|
||||
#include "iam_logger.h"
|
||||
#include "securec.h"
|
||||
@ -128,15 +129,16 @@ Attributes::Impl::Impl(const std::vector<uint8_t> &raw)
|
||||
IAM_LOGE("type copy error");
|
||||
return;
|
||||
}
|
||||
curr += sizeof(uint32_t);
|
||||
|
||||
type = le32toh(type);
|
||||
curr += sizeof(uint32_t);
|
||||
uint32_t length;
|
||||
if (memcpy_s(&length, sizeof(uint32_t), curr, sizeof(uint32_t)) != EOK) {
|
||||
IAM_LOGE("length copy error");
|
||||
return;
|
||||
}
|
||||
length = le32toh(length);
|
||||
curr += sizeof(uint32_t);
|
||||
|
||||
if (!CheckAttributeLength(curr, end, length)) {
|
||||
IAM_LOGE("check attribute length error");
|
||||
return;
|
||||
@ -684,7 +686,8 @@ bool Attributes::Impl::EncodeBoolValue(bool src, std::vector<uint8_t> &dst)
|
||||
bool Attributes::Impl::EncodeUint64Value(uint64_t src, std::vector<uint8_t> &dst)
|
||||
{
|
||||
std::vector<uint8_t> out(sizeof(uint64_t) / sizeof(uint8_t));
|
||||
if (memcpy_s(out.data(), out.size(), &src, sizeof(src)) != EOK) {
|
||||
uint64_t srcLe64 = htole64(src);
|
||||
if (memcpy_s(out.data(), out.size(), &srcLe64, sizeof(srcLe64)) != EOK) {
|
||||
return false;
|
||||
}
|
||||
dst.swap(out);
|
||||
@ -694,7 +697,8 @@ bool Attributes::Impl::EncodeUint64Value(uint64_t src, std::vector<uint8_t> &dst
|
||||
bool Attributes::Impl::EncodeUint32Value(uint32_t src, std::vector<uint8_t> &dst)
|
||||
{
|
||||
std::vector<uint8_t> out(sizeof(uint32_t) / sizeof(uint8_t));
|
||||
if (memcpy_s(out.data(), out.size(), &src, sizeof(src)) != EOK) {
|
||||
uint32_t srcLe32 = htole32(src);
|
||||
if (memcpy_s(out.data(), out.size(), &srcLe32, sizeof(srcLe32)) != EOK) {
|
||||
return false;
|
||||
}
|
||||
dst.swap(out);
|
||||
@ -704,7 +708,8 @@ bool Attributes::Impl::EncodeUint32Value(uint32_t src, std::vector<uint8_t> &dst
|
||||
bool Attributes::Impl::EncodeUint16Value(uint16_t src, std::vector<uint8_t> &dst)
|
||||
{
|
||||
std::vector<uint8_t> out(sizeof(uint16_t) / sizeof(uint8_t));
|
||||
if (memcpy_s(out.data(), out.size(), &src, sizeof(src)) != EOK) {
|
||||
uint16_t srcLe16 = htole16(src);
|
||||
if (memcpy_s(out.data(), out.size(), &srcLe16, sizeof(srcLe16)) != EOK) {
|
||||
return false;
|
||||
}
|
||||
dst.swap(out);
|
||||
@ -722,7 +727,9 @@ bool Attributes::Impl::EncodeUint8Value(uint8_t src, std::vector<uint8_t> &dst)
|
||||
bool Attributes::Impl::EncodeInt32Value(int32_t src, std::vector<uint8_t> &dst)
|
||||
{
|
||||
std::vector<uint8_t> out(sizeof(int32_t) / sizeof(uint8_t));
|
||||
if (memcpy_s(out.data(), out.size(), &src, sizeof(src)) != EOK) {
|
||||
|
||||
uint32_t srcLe32 = htole32(static_cast<uint32_t>(src));
|
||||
if (memcpy_s(out.data(), out.size(), &srcLe32, sizeof(srcLe32)) != EOK) {
|
||||
return false;
|
||||
}
|
||||
dst.swap(out);
|
||||
@ -732,7 +739,9 @@ bool Attributes::Impl::EncodeInt32Value(int32_t src, std::vector<uint8_t> &dst)
|
||||
bool Attributes::Impl::EncodeInt64Value(int64_t src, std::vector<uint8_t> &dst)
|
||||
{
|
||||
std::vector<uint8_t> out(sizeof(int64_t) / sizeof(uint8_t));
|
||||
if (memcpy_s(out.data(), out.size(), &src, sizeof(src)) != EOK) {
|
||||
|
||||
uint64_t srcLe64 = htole64(static_cast<uint64_t>(src));
|
||||
if (memcpy_s(out.data(), out.size(), &srcLe64, sizeof(srcLe64)) != EOK) {
|
||||
return false;
|
||||
}
|
||||
dst.swap(out);
|
||||
@ -753,50 +762,60 @@ bool Attributes::Impl::EncodeStringValue(const std::string &src, std::vector<uin
|
||||
|
||||
bool Attributes::Impl::EncodeUint64ArrayValue(const std::vector<uint64_t> &src, std::vector<uint8_t> &dst)
|
||||
{
|
||||
auto size = src.size() * (sizeof(uint64_t) / sizeof(uint8_t));
|
||||
if (size > MAX_ATTR_LENGTH) {
|
||||
auto outSize = src.size() * (sizeof(uint64_t) / sizeof(uint8_t));
|
||||
if (outSize > MAX_ATTR_LENGTH) {
|
||||
return false;
|
||||
}
|
||||
|
||||
std::vector<uint8_t> out(size);
|
||||
|
||||
std::vector<uint8_t> out(outSize);
|
||||
std::vector<uint64_t> srcLe64(src.size());
|
||||
for (uint32_t i = 0; i < src.size();i++) {
|
||||
srcLe64[i] = htole64(src[i]);
|
||||
}
|
||||
if (!src.empty() &&
|
||||
memcpy_s(out.data(), out.size() * sizeof(uint8_t), src.data(), src.size() * sizeof(uint64_t)) != EOK) {
|
||||
memcpy_s(out.data(), out.size() * sizeof(uint8_t), srcLe64.data(), srcLe64.size() * sizeof(uint64_t)) != EOK) {
|
||||
return false;
|
||||
}
|
||||
|
||||
dst.swap(out);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Attributes::Impl::EncodeUint32ArrayValue(const std::vector<uint32_t> &src, std::vector<uint8_t> &dst)
|
||||
{
|
||||
auto size = src.size() * (sizeof(uint32_t) / sizeof(uint8_t));
|
||||
if (size > MAX_ATTR_LENGTH) {
|
||||
auto outSize = src.size() * (sizeof(uint32_t) / sizeof(uint8_t));
|
||||
if (outSize > MAX_ATTR_LENGTH) {
|
||||
return false;
|
||||
}
|
||||
|
||||
std::vector<uint8_t> out(size);
|
||||
std::vector<uint8_t> out(outSize);
|
||||
std::vector<uint32_t> srcLe32(src.size());
|
||||
for (uint32_t i = 0; i < src.size();i++) {
|
||||
srcLe32[i] = htole32(src[i]);
|
||||
}
|
||||
|
||||
if (!src.empty() &&
|
||||
memcpy_s(out.data(), out.size() * sizeof(uint8_t), src.data(), src.size() * sizeof(uint32_t)) != EOK) {
|
||||
memcpy_s(out.data(), out.size() * sizeof(uint8_t), srcLe32.data(), srcLe32.size() * sizeof(uint32_t)) != EOK) {
|
||||
return false;
|
||||
}
|
||||
|
||||
dst.swap(out);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Attributes::Impl::EncodeUint16ArrayValue(const std::vector<uint16_t> &src, std::vector<uint8_t> &dst)
|
||||
{
|
||||
auto size = src.size() * (sizeof(uint16_t) / sizeof(uint8_t));
|
||||
if (size > MAX_ATTR_LENGTH) {
|
||||
auto outSize = src.size() * (sizeof(uint16_t) / sizeof(uint8_t));
|
||||
if (outSize > MAX_ATTR_LENGTH) {
|
||||
return false;
|
||||
}
|
||||
|
||||
std::vector<uint8_t> out(size);
|
||||
std::vector<uint8_t> out(outSize);
|
||||
std::vector<uint16_t> srcLe16(src.size());
|
||||
for (uint32_t i = 0; i < src.size();i++) {
|
||||
srcLe16[i] = htole16(src[i]);
|
||||
}
|
||||
|
||||
if (!src.empty() &&
|
||||
memcpy_s(out.data(), out.size() * sizeof(uint8_t), src.data(), src.size() * sizeof(uint16_t)) != EOK) {
|
||||
memcpy_s(out.data(), out.size() * sizeof(uint8_t), srcLe16.data(), srcLe16.size() * sizeof(uint16_t)) != EOK) {
|
||||
return false;
|
||||
}
|
||||
dst.swap(out);
|
||||
@ -805,17 +824,22 @@ bool Attributes::Impl::EncodeUint16ArrayValue(const std::vector<uint16_t> &src,
|
||||
|
||||
bool Attributes::Impl::EncodeInt32ArrayValue(const std::vector<int32_t> &src, std::vector<uint8_t> &dst)
|
||||
{
|
||||
auto size = src.size() * (sizeof(int32_t) / sizeof(uint8_t));
|
||||
if (size > MAX_ATTR_LENGTH) {
|
||||
auto outSize = src.size() * (sizeof(int32_t) / sizeof(uint8_t));
|
||||
if (outSize > MAX_ATTR_LENGTH) {
|
||||
return false;
|
||||
}
|
||||
|
||||
std::vector<uint8_t> out(size);
|
||||
std::vector<uint8_t> out(outSize);
|
||||
std::vector<uint32_t> srcLe32(src.size());
|
||||
for (uint32_t i = 0; i < src.size();i++) {
|
||||
srcLe32[i] = htole32(static_cast<uint32_t>(src[i]));
|
||||
}
|
||||
|
||||
if (!src.empty() &&
|
||||
memcpy_s(out.data(), out.size() * sizeof(uint8_t), src.data(), src.size() * sizeof(int32_t)) != EOK) {
|
||||
memcpy_s(out.data(), out.size() * sizeof(uint8_t), srcLe32.data(), srcLe32.size() * sizeof(int32_t)) != EOK) {
|
||||
return false;
|
||||
}
|
||||
|
||||
dst.swap(out);
|
||||
return true;
|
||||
}
|
||||
@ -846,9 +870,11 @@ bool Attributes::Impl::DecodeUint64Value(const std::vector<uint8_t> &src, uint64
|
||||
return false;
|
||||
}
|
||||
|
||||
if (memcpy_s(&dst, sizeof(dst), src.data(), src.size() * sizeof(uint8_t)) != EOK) {
|
||||
uint64_t dstLe64;
|
||||
if (memcpy_s(&dstLe64, sizeof(dstLe64), src.data(), src.size() * sizeof(uint8_t)) != EOK) {
|
||||
return false;
|
||||
}
|
||||
dst = le64toh(dstLe64);
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -857,9 +883,13 @@ bool Attributes::Impl::DecodeUint32Value(const std::vector<uint8_t> &src, uint32
|
||||
if (src.size() * sizeof(uint8_t) != sizeof(uint32_t)) {
|
||||
return false;
|
||||
}
|
||||
if (memcpy_s(&dst, sizeof(dst), src.data(), src.size() * sizeof(uint8_t)) != EOK) {
|
||||
|
||||
uint32_t dstLe32;
|
||||
if (memcpy_s(&dstLe32, sizeof(dstLe32), src.data(), src.size() * sizeof(uint8_t)) != EOK) {
|
||||
return false;
|
||||
}
|
||||
dst = le32toh(dstLe32);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -868,9 +898,12 @@ bool Attributes::Impl::DecodeUint16Value(const std::vector<uint8_t> &src, uint16
|
||||
if (src.size() * sizeof(uint8_t) != sizeof(uint16_t)) {
|
||||
return false;
|
||||
}
|
||||
if (memcpy_s(&dst, sizeof(dst), src.data(), src.size() * sizeof(uint8_t)) != EOK) {
|
||||
|
||||
uint16_t dstLe16;
|
||||
if (memcpy_s(&dstLe16, sizeof(dstLe16), src.data(), src.size() * sizeof(uint8_t)) != EOK) {
|
||||
return false;
|
||||
}
|
||||
dst = le16toh(dstLe16);
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -888,9 +921,13 @@ bool Attributes::Impl::DecodeInt32Value(const std::vector<uint8_t> &src, int32_t
|
||||
if (src.size() * sizeof(uint8_t) != sizeof(int32_t)) {
|
||||
return false;
|
||||
}
|
||||
if (memcpy_s(&dst, sizeof(dst), src.data(), src.size() * sizeof(uint8_t)) != EOK) {
|
||||
|
||||
uint32_t dstLe32;
|
||||
if (memcpy_s(&dstLe32, sizeof(dstLe32), src.data(), src.size() * sizeof(uint8_t)) != EOK) {
|
||||
return false;
|
||||
}
|
||||
dst = static_cast<int32_t>(le32toh(dstLe32));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -899,9 +936,12 @@ bool Attributes::Impl::DecodeInt64Value(const std::vector<uint8_t> &src, int64_t
|
||||
if (src.size() * sizeof(uint8_t) != sizeof(int64_t)) {
|
||||
return false;
|
||||
}
|
||||
if (memcpy_s(&dst, sizeof(dst), src.data(), src.size() * sizeof(uint8_t)) != EOK) {
|
||||
|
||||
uint64_t dstLe64;
|
||||
if (memcpy_s(&dstLe64, sizeof(dstLe64), src.data(), src.size() * sizeof(uint8_t)) != EOK) {
|
||||
return false;
|
||||
}
|
||||
dst = static_cast<int64_t>(le64toh(dstLe64));
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -934,6 +974,10 @@ bool Attributes::Impl::DecodeUint64ArrayValue(const std::vector<uint8_t> &src, s
|
||||
return false;
|
||||
}
|
||||
|
||||
for (uint32_t i = 0; i < out.size(); i++) {
|
||||
out[i] = le64toh(out[i]);
|
||||
}
|
||||
|
||||
dst.swap(out);
|
||||
return true;
|
||||
}
|
||||
@ -951,6 +995,10 @@ bool Attributes::Impl::DecodeUint32ArrayValue(const std::vector<uint8_t> &src, s
|
||||
return false;
|
||||
}
|
||||
|
||||
for (uint32_t i = 0; i < out.size(); i++) {
|
||||
out[i] = le32toh(out[i]);
|
||||
}
|
||||
|
||||
dst.swap(out);
|
||||
return true;
|
||||
}
|
||||
@ -968,6 +1016,10 @@ bool Attributes::Impl::DecodeUint16ArrayValue(const std::vector<uint8_t> &src, s
|
||||
return false;
|
||||
}
|
||||
|
||||
for (uint32_t i = 0; i < out.size(); i++) {
|
||||
out[i] = le16toh(out[i]);
|
||||
}
|
||||
|
||||
dst.swap(out);
|
||||
return true;
|
||||
}
|
||||
@ -992,6 +1044,10 @@ bool Attributes::Impl::DecodeInt32ArrayValue(const std::vector<uint8_t> &src, st
|
||||
return false;
|
||||
}
|
||||
|
||||
for (uint32_t i = 0; i < out.size(); i++) {
|
||||
out[i] = static_cast<int32_t>(le32toh(static_cast<uint32_t>(out[i])));
|
||||
}
|
||||
|
||||
dst.swap(out);
|
||||
return true;
|
||||
}
|
||||
@ -1332,4 +1388,4 @@ std::vector<Attributes::AttributeKey> Attributes::GetKeys() const
|
||||
}
|
||||
} // namespace UserAuth
|
||||
} // namespace UserIam
|
||||
} // namespace OHOS
|
||||
} // namespace OHOS
|
@ -438,6 +438,387 @@ HWTEST_F(AttributesTest, AttributesSetAndGetAttributesArray01, TestSize.Level0)
|
||||
EXPECT_EQ(setAttrs.SetInt32ArrayValue(Attributes::ATTR_EXECUTOR_REGISTER_INFO_LIST, array1), true);
|
||||
EXPECT_EQ(setAttrs.GetInt32ArrayValue(Attributes::ATTR_EXECUTOR_REGISTER_INFO_LIST, array2), true);
|
||||
}
|
||||
|
||||
HWTEST_F(AttributesTest, AttributesEncodeAndDecodeUint64Value, TestSize.Level0)
|
||||
{
|
||||
Attributes attrs;
|
||||
uint64_t encode_val64 = UINT64_MAX;
|
||||
uint64_t encode_val32 = UINT32_MAX;
|
||||
|
||||
EXPECT_TRUE(attrs.SetUint64Value(Attributes::ATTR_SIGNATURE, encode_val64));
|
||||
EXPECT_TRUE(attrs.SetUint64Value(Attributes::ATTR_RESULT_CODE, encode_val32));
|
||||
|
||||
uint64_t decode_val64;
|
||||
uint64_t decode_val32;
|
||||
|
||||
EXPECT_TRUE(attrs.GetUint64Value(Attributes::ATTR_SIGNATURE, decode_val64));
|
||||
EXPECT_TRUE(attrs.GetUint64Value(Attributes::ATTR_RESULT_CODE, decode_val32));
|
||||
|
||||
EXPECT_EQ(encode_val64, decode_val64);
|
||||
EXPECT_EQ(encode_val32, decode_val32);
|
||||
}
|
||||
|
||||
HWTEST_F(AttributesTest, AttributesEncodeAndDecodeUint32Value, TestSize.Level0)
|
||||
{
|
||||
Attributes attrs;
|
||||
uint32_t encode_val32 = UINT32_MAX;
|
||||
uint32_t encode_val16 = UINT16_MAX;
|
||||
|
||||
EXPECT_TRUE(attrs.SetUint32Value(Attributes::ATTR_SIGNATURE, encode_val32));
|
||||
EXPECT_TRUE(attrs.SetUint32Value(Attributes::ATTR_RESULT_CODE, encode_val16));
|
||||
|
||||
uint32_t decode_val32;
|
||||
uint32_t decode_val16;
|
||||
|
||||
EXPECT_TRUE(attrs.GetUint32Value(Attributes::ATTR_SIGNATURE, decode_val32));
|
||||
EXPECT_TRUE(attrs.GetUint32Value(Attributes::ATTR_RESULT_CODE, decode_val16));
|
||||
|
||||
EXPECT_EQ(encode_val32, decode_val32);
|
||||
EXPECT_EQ(encode_val16, decode_val16);
|
||||
}
|
||||
|
||||
HWTEST_F(AttributesTest, AttributesEncodeAndDecodeUint16Value, TestSize.Level0)
|
||||
{
|
||||
Attributes attrs;
|
||||
uint16_t encode_val16 = UINT16_MAX;
|
||||
uint16_t encode_val8 = UINT8_MAX;
|
||||
|
||||
EXPECT_TRUE(attrs.SetUint16Value(Attributes::ATTR_SIGNATURE, encode_val16));
|
||||
EXPECT_TRUE(attrs.SetUint16Value(Attributes::ATTR_RESULT_CODE, encode_val8));
|
||||
|
||||
uint16_t decode_val16;
|
||||
uint16_t decode_val8;
|
||||
|
||||
EXPECT_TRUE(attrs.GetUint16Value(Attributes::ATTR_SIGNATURE, decode_val16));
|
||||
EXPECT_TRUE(attrs.GetUint16Value(Attributes::ATTR_RESULT_CODE, decode_val8));
|
||||
|
||||
EXPECT_EQ(encode_val16, decode_val16);
|
||||
EXPECT_EQ(encode_val8, decode_val8);
|
||||
}
|
||||
|
||||
HWTEST_F(AttributesTest, AttributesEncodeAndDecodeInt64Value, TestSize.Level0)
|
||||
{
|
||||
Attributes attrs;
|
||||
int64_t encode_val64 = INT64_MAX;
|
||||
int64_t encode_val32 = INT32_MAX;
|
||||
|
||||
EXPECT_TRUE(attrs.SetInt64Value(Attributes::ATTR_EXECUTOR_REGISTER_INFO_LIST, encode_val64));
|
||||
EXPECT_TRUE(attrs.SetInt64Value(Attributes::ATTR_PIN_EXPIRED_INFO, encode_val32));
|
||||
|
||||
int64_t decode_val64;
|
||||
int64_t decode_val32;
|
||||
|
||||
EXPECT_TRUE(attrs.GetInt64Value(Attributes::ATTR_EXECUTOR_REGISTER_INFO_LIST, decode_val64));
|
||||
EXPECT_TRUE(attrs.GetInt64Value(Attributes::ATTR_PIN_EXPIRED_INFO, decode_val32));
|
||||
|
||||
EXPECT_EQ(encode_val64, decode_val64);
|
||||
EXPECT_EQ(encode_val32, decode_val32);
|
||||
}
|
||||
|
||||
HWTEST_F(AttributesTest, AttributesEncodeAndDecodeInt32Value, TestSize.Level0)
|
||||
{
|
||||
Attributes attrs;
|
||||
int32_t encode_val32 = INT32_MAX;
|
||||
int32_t encode_val16 = INT16_MAX;
|
||||
|
||||
EXPECT_TRUE(attrs.SetInt32Value(Attributes::ATTR_REMAIN_TIMES, encode_val32));
|
||||
EXPECT_TRUE(attrs.SetInt32Value(Attributes::ATTR_TIP_INFO, encode_val16));
|
||||
|
||||
int32_t decode_val32;
|
||||
int32_t decode_val16;
|
||||
|
||||
EXPECT_TRUE(attrs.GetInt32Value(Attributes::ATTR_REMAIN_TIMES, decode_val32));
|
||||
EXPECT_TRUE(attrs.GetInt32Value(Attributes::ATTR_TIP_INFO, decode_val16));
|
||||
|
||||
EXPECT_EQ(encode_val32, decode_val32);
|
||||
EXPECT_EQ(encode_val16, decode_val16);
|
||||
}
|
||||
|
||||
HWTEST_F(AttributesTest, AttributesEncodeAndDecodeUint64Array, TestSize.Level0)
|
||||
{
|
||||
{
|
||||
Attributes attrs_outsize;
|
||||
constexpr int ENCODE_ARRAY_OUT_SIZE = 81921;
|
||||
std::vector<uint64_t> encode_outsize_array;
|
||||
encode_outsize_array.reserve(ENCODE_ARRAY_OUT_SIZE);
|
||||
for (int i = 0; i < ENCODE_ARRAY_OUT_SIZE; i++) {
|
||||
encode_outsize_array.push_back(UINT64_MAX - i);
|
||||
}
|
||||
EXPECT_FALSE(attrs_outsize.SetUint64ArrayValue(Attributes::ATTR_LOCKED_TEMPLATES, encode_outsize_array));
|
||||
}
|
||||
|
||||
{
|
||||
Attributes attrs_empty;
|
||||
std::vector<uint64_t> encode_empty_array;
|
||||
std::vector<uint64_t> decode_empty_array;
|
||||
EXPECT_TRUE(attrs_empty.SetUint64ArrayValue(Attributes::ATTR_LOCKED_TEMPLATES, encode_empty_array));
|
||||
EXPECT_TRUE(attrs_empty.GetUint64ArrayValue(Attributes::ATTR_LOCKED_TEMPLATES, decode_empty_array));
|
||||
EXPECT_THAT(encode_empty_array, decode_empty_array);
|
||||
}
|
||||
|
||||
{
|
||||
Attributes attrs;
|
||||
constexpr int ARRAY_SIZE = 1024;
|
||||
std::vector<uint64_t> encode_array;
|
||||
std::vector<uint64_t> decode_array;
|
||||
encode_array.reserve(ARRAY_SIZE);
|
||||
for (int i = 0; i < ARRAY_SIZE; i++) {
|
||||
encode_array.push_back(UINT64_MAX - i);
|
||||
}
|
||||
EXPECT_TRUE(attrs.SetUint64ArrayValue(Attributes::ATTR_LOCKED_TEMPLATES, encode_array));
|
||||
EXPECT_TRUE(attrs.GetUint64ArrayValue(Attributes::ATTR_LOCKED_TEMPLATES, decode_array));
|
||||
EXPECT_THAT(encode_array, decode_array);
|
||||
}
|
||||
}
|
||||
|
||||
HWTEST_F(AttributesTest, AttributesEncodeAndDecodeUint32Array, TestSize.Level0)
|
||||
{
|
||||
{
|
||||
Attributes attrs_outsize;
|
||||
constexpr int ENCODE_ARRAY_OUT_SIZE = 81921;
|
||||
std::vector<uint32_t> encode_outsize_array;
|
||||
encode_outsize_array.reserve(ENCODE_ARRAY_OUT_SIZE);
|
||||
for (int i = 0; i < ENCODE_ARRAY_OUT_SIZE; i++) {
|
||||
encode_outsize_array.push_back(UINT32_MAX - i);
|
||||
}
|
||||
EXPECT_FALSE(attrs_outsize.SetUint32ArrayValue(Attributes::ATTR_KEY_LIST, encode_outsize_array));
|
||||
}
|
||||
|
||||
{
|
||||
Attributes attrs_empty;
|
||||
std::vector<uint32_t> encode_empty_array;
|
||||
std::vector<uint32_t> decode_empty_array;
|
||||
EXPECT_TRUE(attrs_empty.SetUint32ArrayValue(Attributes::ATTR_KEY_LIST, encode_empty_array));
|
||||
EXPECT_TRUE(attrs_empty.GetUint32ArrayValue(Attributes::ATTR_KEY_LIST, decode_empty_array));
|
||||
EXPECT_THAT(encode_empty_array, decode_empty_array);
|
||||
}
|
||||
|
||||
{
|
||||
Attributes attrs;
|
||||
constexpr int ARRAY_SIZE = 1024;
|
||||
std::vector<uint32_t> encode_array;
|
||||
std::vector<uint32_t> decode_array;
|
||||
encode_array.reserve(ARRAY_SIZE);
|
||||
for (int i = 0; i < ARRAY_SIZE; i++) {
|
||||
encode_array.push_back(UINT32_MAX - i);
|
||||
}
|
||||
EXPECT_TRUE(attrs.SetUint32ArrayValue(Attributes::ATTR_KEY_LIST, encode_array));
|
||||
EXPECT_TRUE(attrs.GetUint32ArrayValue(Attributes::ATTR_KEY_LIST, decode_array));
|
||||
EXPECT_THAT(encode_array, decode_array);
|
||||
}
|
||||
}
|
||||
|
||||
HWTEST_F(AttributesTest, AttributesEncodeAndDecodeUint16Array, TestSize.Level0)
|
||||
{
|
||||
{
|
||||
Attributes attrs_outsize;
|
||||
constexpr int ENCODE_ARRAY_OUT_SIZE = 81921;
|
||||
std::vector<uint16_t> encode_outsize_array;
|
||||
encode_outsize_array.reserve(ENCODE_ARRAY_OUT_SIZE);
|
||||
for (int i = 0; i < ENCODE_ARRAY_OUT_SIZE; i++) {
|
||||
encode_outsize_array.push_back(UINT16_MAX - i);
|
||||
}
|
||||
EXPECT_FALSE(attrs_outsize.SetUint16ArrayValue(Attributes::ATTR_TEMPLATE_ID_LIST, encode_outsize_array));
|
||||
}
|
||||
|
||||
{
|
||||
Attributes attrs_empty;
|
||||
std::vector<uint16_t> encode_empty_array;
|
||||
std::vector<uint16_t> decode_empty_array;
|
||||
EXPECT_TRUE(attrs_empty.SetUint16ArrayValue(Attributes::ATTR_TEMPLATE_ID_LIST, encode_empty_array));
|
||||
EXPECT_TRUE(attrs_empty.GetUint16ArrayValue(Attributes::ATTR_TEMPLATE_ID_LIST, decode_empty_array));
|
||||
EXPECT_THAT(encode_empty_array, decode_empty_array);
|
||||
}
|
||||
|
||||
{
|
||||
Attributes attrs;
|
||||
constexpr int ARRAY_SIZE = 1024;
|
||||
std::vector<uint16_t> encode_array;
|
||||
std::vector<uint16_t> decode_array;
|
||||
encode_array.reserve(ARRAY_SIZE);
|
||||
for (int i = 0; i < ARRAY_SIZE; i++) {
|
||||
encode_array.push_back(UINT16_MAX - i);
|
||||
}
|
||||
EXPECT_TRUE(attrs.SetUint16ArrayValue(Attributes::ATTR_TEMPLATE_ID_LIST, encode_array));
|
||||
EXPECT_TRUE(attrs.GetUint16ArrayValue(Attributes::ATTR_TEMPLATE_ID_LIST, decode_array));
|
||||
EXPECT_THAT(encode_array, decode_array);
|
||||
}
|
||||
}
|
||||
|
||||
HWTEST_F(AttributesTest, AttributesEncodeAndDecodeInt32Array, TestSize.Level0)
|
||||
{
|
||||
{
|
||||
Attributes attrs_outsize;
|
||||
constexpr int ENCODE_ARRAY_OUT_SIZE = 81921;
|
||||
std::vector<int32_t> encode_outsize_array;
|
||||
encode_outsize_array.reserve(ENCODE_ARRAY_OUT_SIZE);
|
||||
for (int i = 0; i < ENCODE_ARRAY_OUT_SIZE; i++) {
|
||||
encode_outsize_array.push_back(INT32_MAX - i);
|
||||
}
|
||||
EXPECT_FALSE(attrs_outsize.SetInt32ArrayValue(Attributes::ATTR_AUTH_TYPES, encode_outsize_array));
|
||||
}
|
||||
|
||||
{
|
||||
Attributes attrs_empty;
|
||||
std::vector<int32_t> encode_empty_array;
|
||||
std::vector<int32_t> decode_empty_array;
|
||||
EXPECT_TRUE(attrs_empty.SetInt32ArrayValue(Attributes::ATTR_AUTH_TYPES, encode_empty_array));
|
||||
EXPECT_TRUE(attrs_empty.GetInt32ArrayValue(Attributes::ATTR_AUTH_TYPES, decode_empty_array));
|
||||
EXPECT_THAT(encode_empty_array, decode_empty_array);
|
||||
}
|
||||
|
||||
{
|
||||
Attributes attrs;
|
||||
constexpr int ARRAY_SIZE = 1024;
|
||||
std::vector<int32_t> encode_array;
|
||||
std::vector<int32_t> decode_array;
|
||||
encode_array.reserve(ARRAY_SIZE);
|
||||
for (int i = 0; i < ARRAY_SIZE; i++) {
|
||||
encode_array.push_back(INT32_MAX - i);
|
||||
}
|
||||
EXPECT_TRUE(attrs.SetInt32ArrayValue(Attributes::ATTR_AUTH_TYPES, encode_array));
|
||||
EXPECT_TRUE(attrs.GetInt32ArrayValue(Attributes::ATTR_AUTH_TYPES, decode_array));
|
||||
EXPECT_THAT(encode_array, decode_array);
|
||||
}
|
||||
}
|
||||
|
||||
HWTEST_F(AttributesTest, AttributesSerializeAndDeserialize01, TestSize.Level0)
|
||||
{
|
||||
Attributes attrs_serial;
|
||||
EXPECT_TRUE(attrs_serial.SetUint64Value(Attributes::ATTR_SCHEDULE_ID, UINT64_MAX));
|
||||
EXPECT_TRUE(attrs_serial.SetUint32Value(Attributes::ATTR_SCHEDULE_MODE, UINT32_MAX));
|
||||
EXPECT_TRUE(attrs_serial.SetUint16Value(Attributes::ATTR_CREDENTIAL_DIGEST, UINT16_MAX));
|
||||
EXPECT_TRUE(attrs_serial.SetUint64ArrayValue(Attributes::ATTR_FREEZING_TIME,
|
||||
{UINT64_MAX, UINT64_MAX, UINT64_MAX, UINT64_MAX, UINT64_MAX}));
|
||||
EXPECT_TRUE(attrs_serial.SetUint32ArrayValue(Attributes::ATTR_KEY_LIST,
|
||||
{UINT32_MAX, UINT32_MAX, UINT32_MAX, UINT32_MAX, UINT32_MAX}));
|
||||
EXPECT_TRUE(attrs_serial.SetInt32Value(Attributes::ATTR_REMAIN_TIMES, INT32_MAX));
|
||||
EXPECT_TRUE(attrs_serial.SetInt32ArrayValue(Attributes::ATTR_AUTH_TYPES,
|
||||
{INT32_MAX, INT32_MAX, INT32_MAX, INT32_MAX, INT32_MAX}));
|
||||
int64_t test_int64_val = 100;
|
||||
EXPECT_TRUE(attrs_serial.SetInt64Value(Attributes::ATTR_PIN_EXPIRED_INFO, test_int64_val));
|
||||
auto buffer = attrs_serial.Serialize();
|
||||
|
||||
Attributes attrs_deserial(buffer);
|
||||
uint64_t u64_value;
|
||||
EXPECT_TRUE(attrs_deserial.GetUint64Value(Attributes::ATTR_SCHEDULE_ID, u64_value));
|
||||
EXPECT_EQ(u64_value, UINT64_MAX);
|
||||
|
||||
uint32_t u32_value;
|
||||
EXPECT_TRUE(attrs_deserial.GetUint32Value(Attributes::ATTR_SCHEDULE_MODE, u32_value));
|
||||
EXPECT_EQ(u32_value, UINT32_MAX);
|
||||
|
||||
uint16_t u16_value;
|
||||
EXPECT_TRUE(attrs_deserial.GetUint16Value(Attributes::ATTR_CREDENTIAL_DIGEST, u16_value));
|
||||
EXPECT_EQ(u16_value, UINT16_MAX);
|
||||
|
||||
std::vector<uint64_t> u64_vector;
|
||||
EXPECT_TRUE(attrs_deserial.GetUint64ArrayValue(Attributes::ATTR_FREEZING_TIME, u64_vector));
|
||||
EXPECT_THAT(u64_vector, ElementsAre(UINT64_MAX, UINT64_MAX, UINT64_MAX, UINT64_MAX, UINT64_MAX));
|
||||
|
||||
std::vector<uint32_t> u32_vector;
|
||||
EXPECT_TRUE(attrs_deserial.GetUint32ArrayValue(Attributes::ATTR_KEY_LIST, u32_vector));
|
||||
EXPECT_THAT(u32_vector, ElementsAre(UINT32_MAX, UINT32_MAX, UINT32_MAX, UINT32_MAX, UINT32_MAX));
|
||||
|
||||
int32_t int32_value;
|
||||
EXPECT_TRUE(attrs_deserial.GetInt32Value(Attributes::ATTR_REMAIN_TIMES, int32_value));
|
||||
EXPECT_EQ(int32_value, INT32_MAX);
|
||||
|
||||
std::vector<int32_t> int32_vector;
|
||||
EXPECT_TRUE(attrs_deserial.GetInt32ArrayValue(Attributes::ATTR_AUTH_TYPES, int32_vector));
|
||||
EXPECT_THAT(int32_vector, ElementsAre(INT32_MAX, INT32_MAX, INT32_MAX, INT32_MAX, INT32_MAX));
|
||||
|
||||
int64_t int64_value;
|
||||
EXPECT_TRUE(attrs_deserial.GetInt64Value(Attributes::ATTR_PIN_EXPIRED_INFO, int64_value));
|
||||
EXPECT_EQ(int64_value, 100);
|
||||
}
|
||||
|
||||
HWTEST_F(AttributesTest, AttributesSerializeAndDeserialize02, TestSize.Level0)
|
||||
{
|
||||
Attributes attrs_serial;
|
||||
EXPECT_TRUE(attrs_serial.SetBoolValue(Attributes::ATTR_END_AFTER_FIRST_FAIL, true));
|
||||
EXPECT_TRUE(attrs_serial.SetBoolValue(Attributes::ATTR_MSG_ACK, false));
|
||||
EXPECT_TRUE(attrs_serial.SetUint32Value(Attributes::ATTR_SCHEDULE_MODE, UINT32_MAX));
|
||||
EXPECT_TRUE(attrs_serial.SetUint32ArrayValue(Attributes::ATTR_KEY_LIST,
|
||||
{UINT32_MAX, UINT32_MAX, UINT32_MAX, UINT32_MAX, UINT32_MAX}));
|
||||
EXPECT_TRUE(attrs_serial.SetStringValue(Attributes::ATTR_CALLER_NAME, "iam_unit_test"));
|
||||
EXPECT_TRUE(attrs_serial.SetUint8Value(Attributes::ATTR_ROOT, UINT8_MAX));
|
||||
EXPECT_TRUE(attrs_serial.SetUint8ArrayValue(Attributes::ATTR_DATA,
|
||||
{UINT8_MAX, UINT8_MAX, UINT8_MAX, UINT8_MAX, UINT8_MAX}));
|
||||
auto buffer = attrs_serial.Serialize();
|
||||
|
||||
Attributes attrs_deserial(buffer);
|
||||
bool bool_valt;
|
||||
EXPECT_TRUE(attrs_deserial.GetBoolValue(Attributes::ATTR_END_AFTER_FIRST_FAIL, bool_valt));
|
||||
EXPECT_EQ(bool_valt, true);
|
||||
|
||||
bool bool_valf;
|
||||
EXPECT_TRUE(attrs_deserial.GetBoolValue(Attributes::ATTR_MSG_ACK, bool_valf));
|
||||
EXPECT_EQ(bool_valf, false);
|
||||
|
||||
uint32_t u32_value;
|
||||
EXPECT_TRUE(attrs_deserial.GetUint32Value(Attributes::ATTR_SCHEDULE_MODE, u32_value));
|
||||
EXPECT_EQ(u32_value, UINT32_MAX);
|
||||
|
||||
std::vector<uint32_t> u32_vector;
|
||||
EXPECT_TRUE(attrs_deserial.GetUint32ArrayValue(Attributes::ATTR_KEY_LIST, u32_vector));
|
||||
EXPECT_THAT(u32_vector, ElementsAre(UINT32_MAX, UINT32_MAX, UINT32_MAX, UINT32_MAX, UINT32_MAX));
|
||||
|
||||
std::string str_value;
|
||||
EXPECT_TRUE(attrs_deserial.GetStringValue(Attributes::ATTR_CALLER_NAME, str_value));
|
||||
EXPECT_EQ(str_value, "iam_unit_test");
|
||||
|
||||
uint8_t u8_val;
|
||||
EXPECT_TRUE(attrs_deserial.GetUint8Value(Attributes::ATTR_ROOT, u8_val));
|
||||
EXPECT_EQ(u8_val, UINT8_MAX);
|
||||
|
||||
std::vector<uint8_t> u8_vector;
|
||||
EXPECT_TRUE(attrs_deserial.GetUint8ArrayValue(Attributes::ATTR_DATA, u8_vector));
|
||||
EXPECT_THAT(u8_vector, ElementsAre(UINT8_MAX, UINT8_MAX, UINT8_MAX, UINT8_MAX, UINT8_MAX));
|
||||
}
|
||||
|
||||
HWTEST_F(AttributesTest, AttributesRawSerializeTest01, TestSize.Level0)
|
||||
{
|
||||
std::vector<uint8_t> raw = {160, 134, 1, 0, 1, 0, 0, 0, 255, 175, 134,
|
||||
1, 0, 14, 0, 0, 0, 105, 97, 109, 95, 117, 110, 105, 116, 95, 116,
|
||||
101, 115, 116, 0, 180, 134, 1, 0, 5, 0, 0, 0, 255, 255,
|
||||
255, 255, 255, 182, 134, 1, 0, 4, 0, 0, 0, 255, 255, 255,
|
||||
255, 197, 134, 1, 0, 20, 0, 0, 0, 255, 255, 255, 255, 255,
|
||||
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
|
||||
255, 255, 198, 134, 1, 0, 1, 0, 0, 0, 1, 213, 134, 1, 0, 1,
|
||||
0, 0, 0, 0};
|
||||
|
||||
Attributes attrs(raw);
|
||||
std::vector<uint8_t> buffer = attrs.Serialize();
|
||||
for (int i = 0; i < buffer.size(); i++) {
|
||||
EXPECT_THAT(raw[i], buffer[i]);
|
||||
}
|
||||
}
|
||||
|
||||
HWTEST_F(AttributesTest, AttributesRawSerializeTest03, TestSize.Level0)
|
||||
{
|
||||
std::vector<uint8_t> raw = {169, 134, 1, 0, 4, 0, 0, 0, 255, 255, 255,
|
||||
127, 170, 134, 1, 0, 40, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255,
|
||||
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
|
||||
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
|
||||
255, 255, 255, 255, 255, 177, 134, 1, 0,
|
||||
8, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 182,
|
||||
134, 1, 0, 4, 0, 0, 0, 255, 255, 255, 255, 197,
|
||||
134, 1, 0, 20, 0, 0, 0, 255, 255, 255, 255, 255,
|
||||
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
|
||||
255, 255, 255, 255, 210, 134, 1, 0, 2, 0, 0, 0, 255,
|
||||
255, 234, 134, 1, 0, 20, 0, 0, 0, 255, 255, 255,
|
||||
127, 255, 255, 255, 127, 255, 255, 255,
|
||||
127, 255, 255, 255, 127, 255, 255, 255,
|
||||
127, 243, 134, 1, 0, 8, 0, 0, 0, 100, 0, 0, 0, 0, 0, 0, 0};
|
||||
|
||||
Attributes attrs(raw);
|
||||
std::vector<uint8_t> buffer = attrs.Serialize();
|
||||
for (int i = 0; i < buffer.size(); i++) {
|
||||
EXPECT_THAT(raw[i], buffer[i]);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace UserAuth
|
||||
} // namespace UserIam
|
||||
} // namespace OHOS
|
||||
|
Loading…
Reference in New Issue
Block a user