Commit c72db336 authored by Habib Virji's avatar Habib Virji

AMACL payload conversion from JSON to CBOR

Converts AMACL payload conversion from JSON to CBOR directly using tinycbor library.

- Amacl unit test is updated to match new changes.
- Data file are updated to handle new format.

Change-Id: I80e56f182f3f516025977e26fa5d719058832fd8
Signed-off-by: default avatarHabib Virji <habib.virji@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/5053Tested-by: default avatarjenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-on: https://gerrit.iotivity.org/gerrit/5777
parent 172aa5cd
......@@ -63,19 +63,22 @@ void DeInitAmaclResource();
OCStackResult AmaclGetAmsDeviceId(const char *resource, OicUuid_t *amsId);
/**
* This function converts Amacl data into JSON format.
* Caller needs to invoke 'free' when done using
* returned string.
* @param Amacl instance of OicSecAmacl_t structure.
* This function converts Amacl data into CBOR format.
* Caller needs to invoke 'free' when done using returned string.
*
* @retval pointer to Amacl in json format.
* @param amacl instance of @ref OicSecAmacl_t structure.
* @param cborPayload is the converted cbor value of @ref OicSecAmacl_t structure.
* @param cborSize is the size of the cbor payload. This value is the size of the
* cborPayload. It should not be NON-NULL value.
*
* @return ::OC_STACK_OK for Success. ::OC_STACK_INVALID in case of invalid parameters.
* ::OC_STACK_ERROR in case of error in converting to cbor.
*/
char* BinToAmaclJSON(const OicSecAmacl_t * amacl);
OCStackResult AmaclToCBORPayload(const OicSecAmacl_t *amacl, uint8_t **cborPayload,
size_t *cborSize);
#ifdef __cplusplus
}
#endif
#endif //IOTVT_SRM_AMACLR_H
......@@ -57,6 +57,17 @@ OCStackResult CreatePstatResource();
OCEntityHandlerResult PstatEntityHandler(OCEntityHandlerFlag flag,
OCEntityHandlerRequest * ehRequest);
/**
* Converts CBOR payload to AMACL.
*
* @param cborPayload is the amacl payload cbor value that neds to be converted.
* @param cborSize of the cborPayload. In case size is not known, it is 0.
* It should be NON-NULL.
* @param amacl is the value that is initialized. It is NULL in case of error.
*/
OCStackResult CBORPayloadToAmacl(const uint8_t *cborPayload, size_t cborSize,
OicSecAmacl_t **amacl);
#ifdef __cplusplus
}
#endif
......
This diff is collapsed.
......@@ -70,6 +70,7 @@ if not env.get('RELEASE'):
# Source files and Targets
######################################################################
unittest = srmtest_env.Program('unittest', ['aclresourcetest.cpp',
'amaclresourcetest.cpp',
'pstatresource.cpp',
'doxmresource.cpp',
'policyengine.cpp',
......
//******************************************************************
//
// Copyright 2015 Samsung Electronics All Rights Reserved.
//
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//
// 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 "gtest/gtest.h"
#include "cainterface.h"
#include "ocstack.h"
#include "ocpayload.h"
#include "oic_malloc.h"
#include "oic_string.h"
#include "payload_logging.h"
#include "psinterface.h"
#include "secureresourcemanager.h"
#include "securevirtualresourcetypes.h"
#include "srmresourcestrings.h"
#include "srmutility.h"
#include "amaclresource.h"
#include "security_internals.h"
using namespace std;
#define TAG "SRM-AMACL-UT"
TEST(AMACLResourceTest, CBORAMACLConversion)
{
OicSecAmacl_t *secAmacl = (OicSecAmacl_t *) OICCalloc(1, sizeof(*secAmacl));
ASSERT_TRUE(NULL != secAmacl);
uint8_t amss[] = {0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35,
0x35, 0x35, 0x35, 0x35, 0x35, 0x35};
uint8_t amss1[] = {0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
0x36, 0x36, 0x36, 0x36, 0x36, 0x36};
secAmacl->amssLen = 2;
secAmacl->amss = (OicUuid_t *)OICCalloc(secAmacl->amssLen, sizeof(*secAmacl->amss));
if (!secAmacl->amss)
{
OICFree(secAmacl);
}
ASSERT_TRUE(NULL != secAmacl->amss);
memcpy(secAmacl->amss[0].id, amss, sizeof(amss));
memcpy(secAmacl->amss[1].id, amss1, sizeof(amss1));
const char *rsrc[] = { "/a/led", "/a/fan"};
secAmacl->resourcesLen = 2;
secAmacl->resources = (char **)OICCalloc(secAmacl->resourcesLen,
sizeof(*secAmacl->resources));
if (!secAmacl->resources)
{
OICFree(secAmacl->amss);
OICFree(secAmacl);
}
ASSERT_TRUE(NULL != secAmacl->resources);
for (size_t i = 0 ; i < secAmacl->resourcesLen; i++)
{
secAmacl->resources[i] = OICStrdup(rsrc[i]);
ASSERT_TRUE(NULL != secAmacl->resources[i]);
}
secAmacl->ownersLen = 1;
uint8_t ownrs[] = {0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32,
0x32, 0x32, 0x32, 0x32, 0x32, 0x32};
secAmacl->owners = (OicUuid_t *)OICCalloc(1, sizeof(*secAmacl->owners));
if (!secAmacl->owners)
{
OICFree(secAmacl->resources);
OICFree(secAmacl->amss);
OICFree(secAmacl);
}
ASSERT_TRUE(NULL != secAmacl->owners);
memcpy(secAmacl->owners[0].id, ownrs, sizeof(ownrs));
OicSecAmacl_t *secAmacl1 = (OicSecAmacl_t *) OICCalloc(1, sizeof(*secAmacl1));
if (!secAmacl1)
{
OICFree(secAmacl->owners);
OICFree(secAmacl->resources);
OICFree(secAmacl->amss);
OICFree(secAmacl);
}
ASSERT_TRUE(NULL != secAmacl1);
secAmacl1->amssLen = 2;
secAmacl1->amss = (OicUuid_t *)OICCalloc(2, sizeof(*secAmacl1->amss));
if (!secAmacl1->amss)
{
OICFree(secAmacl->owners);
OICFree(secAmacl->resources);
OICFree(secAmacl->amss);
OICFree(secAmacl);
OICFree(secAmacl1);
}
ASSERT_TRUE(NULL != secAmacl1->amss);
memcpy(secAmacl1->amss[0].id, amss, sizeof(amss));
memcpy(secAmacl1->amss[1].id, amss1, sizeof(amss1));
const char *rsrc1[] = { "/b/led", "/b/fan"};
secAmacl1->resourcesLen = 2;
secAmacl1->resources = (char **)OICCalloc(secAmacl1->resourcesLen,
sizeof(*secAmacl1->resources));
if (!secAmacl1->resources)
{
OICFree(secAmacl->owners);
OICFree(secAmacl->resources);
OICFree(secAmacl->amss);
OICFree(secAmacl);
OICFree(secAmacl1->amss);
OICFree(secAmacl1);
}
ASSERT_TRUE(NULL != secAmacl1->resources);
for (size_t i = 0 ; i < secAmacl1->resourcesLen; i++)
{
secAmacl1->resources[i] = OICStrdup(rsrc1[i]);
ASSERT_TRUE(NULL != secAmacl1->resources[i]);
}
secAmacl1->ownersLen = 1;
secAmacl1->owners = (OicUuid_t *)OICCalloc(1, sizeof(*secAmacl1->owners));
if (!secAmacl1->owners)
{
OICFree(secAmacl->owners);
OICFree(secAmacl->resources);
OICFree(secAmacl->amss);
OICFree(secAmacl);
OICFree(secAmacl1->resources);
OICFree(secAmacl1->amss);
OICFree(secAmacl1);
}
ASSERT_TRUE(NULL != secAmacl1->owners);
memcpy(secAmacl1->owners[0].id, ownrs, sizeof(ownrs));
secAmacl1->next = NULL;
secAmacl->next = secAmacl1;
size_t size = 0;
uint8_t *psStorage = NULL;
EXPECT_EQ(OC_STACK_OK, AmaclToCBORPayload(secAmacl, &psStorage, &size));
if (!psStorage)
{
OICFree(secAmacl->owners);
OICFree(secAmacl->resources);
OICFree(secAmacl->amss);
OICFree(secAmacl);
OICFree(secAmacl1->owners);
OICFree(secAmacl1->resources);
OICFree(secAmacl1->amss);
OICFree(secAmacl1);
}
ASSERT_TRUE(NULL != psStorage);
OicSecAmacl_t *amacl = NULL;
EXPECT_EQ(OC_STACK_OK, CBORPayloadToAmacl(psStorage, size, &amacl));
if (!amacl)
{
OICFree(secAmacl->owners);
OICFree(secAmacl->resources);
OICFree(secAmacl->amss);
OICFree(secAmacl);
OICFree(secAmacl1->owners);
OICFree(secAmacl1->resources);
OICFree(secAmacl1->amss);
OICFree(secAmacl1);
OICFree(psStorage);
}
ASSERT_TRUE(NULL != amacl);
EXPECT_EQ(secAmacl->amssLen, amacl->amssLen);
EXPECT_EQ(sizeof(secAmacl->amss[0].id), sizeof(amacl->amss[0].id));
EXPECT_EQ(sizeof(secAmacl->amss[1].id), sizeof(amacl->amss[1].id));
EXPECT_STREQ(secAmacl->resources[0], amacl->resources[0]);
EXPECT_STREQ(secAmacl->resources[1], amacl->resources[1]);
EXPECT_EQ(secAmacl->resourcesLen, amacl->resourcesLen);
EXPECT_EQ(secAmacl->ownersLen, amacl->ownersLen);
EXPECT_EQ(*secAmacl->owners[0].id, *amacl->owners[0].id);
EXPECT_EQ(secAmacl->next->amssLen, amacl->next->amssLen);
EXPECT_EQ(sizeof(secAmacl->next->amss[0].id), sizeof(amacl->next->amss[0].id));
EXPECT_STREQ(secAmacl->next->resources[0], amacl->next->resources[0]);
EXPECT_STREQ(secAmacl->next->resources[1], amacl->next->resources[1]);
EXPECT_EQ(secAmacl->next->resourcesLen, amacl->next->resourcesLen);
EXPECT_EQ(secAmacl->next->ownersLen, amacl->next->ownersLen);
EXPECT_EQ(*secAmacl->next->owners[0].id, *amacl->next->owners[0].id);
OICFree(secAmacl->owners);
OICFree(secAmacl->resources);
OICFree(secAmacl->amss);
OICFree(secAmacl);
OICFree(secAmacl1->owners);
OICFree(secAmacl1->resources);
OICFree(secAmacl1->amss);
OICFree(secAmacl1);
OICFree(psStorage);
OICFree(amacl);
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment