Skip to content

Commit e580132

Browse files
committed
Separated block sorting from encoding, use block number as key
1 parent 6fb1036 commit e580132

8 files changed

Lines changed: 72 additions & 43 deletions

File tree

src/mock_bpa/agent.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,7 @@ static void *MockBPA_Agent_work_over_rx(void *arg)
725725
break;
726726
}
727727
BSL_LOG_INFO("over_rx item");
728-
mock_bpa_decode(&item);
728+
mock_bpa_ctr_decode(&item);
729729

730730
MockBPA_Bundle_t *bundle = item.bundle_ref.data;
731731
if (MockBPA_Agent_process(agent, &agent->appin, BSL_POLICYLOCATION_APPIN, bundle))
@@ -764,7 +764,7 @@ static void *MockBPA_Agent_work_under_rx(void *arg)
764764
}
765765

766766
BSL_LOG_INFO("under_rx item");
767-
if (mock_bpa_decode(&item))
767+
if (mock_bpa_ctr_decode(&item))
768768
{
769769
BSL_LOG_ERR("failed to decode bundle");
770770
mock_bpa_ctr_deinit(&item);
@@ -822,7 +822,7 @@ static void *MockBPA_Agent_work_deliver(void *arg)
822822
continue;
823823
}
824824

825-
mock_bpa_encode(&item);
825+
mock_bpa_ctr_encode(&item);
826826
MockBPA_data_queue_push(agent->over_tx, item);
827827
{
828828
uint8_t buf = 0;
@@ -867,7 +867,7 @@ static void *MockBPA_Agent_work_forward(void *arg)
867867
continue;
868868
}
869869

870-
mock_bpa_encode(&item);
870+
mock_bpa_ctr_encode(&item);
871871
MockBPA_data_queue_push(agent->under_tx, item);
872872
{
873873
uint8_t buf = 0;

src/mock_bpa/bundle.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,23 @@
2121
*/
2222
#include "bundle.h"
2323

24+
int MockBPA_CanonicalBlock_cmp(const MockBPA_CanonicalBlock_t *block_a, const MockBPA_CanonicalBlock_t *block_b)
25+
{
26+
return M_CMP_DEFAULT(block_b->blk_num, block_a->blk_num);
27+
}
28+
2429
int MockBPA_Bundle_Init(MockBPA_Bundle_t *bundle)
2530
{
2631
ASSERT_ARG_NONNULL(bundle);
2732
memset(bundle, 0, sizeof(*bundle));
2833

2934
bundle->retain = true;
3035

36+
BSL_Data_Init(&bundle->primary_block.encoded);
37+
BSL_HostEID_Init(&bundle->primary_block.src_node_id);
38+
BSL_HostEID_Init(&bundle->primary_block.dest_eid);
39+
BSL_HostEID_Init(&bundle->primary_block.report_to_eid);
40+
3141
MockBPA_BlockList_init(bundle->blocks);
3242
MockBPA_BlockByNum_init(bundle->blocks_num);
3343

src/mock_bpa/bundle.h

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
#include "eid.h"
3131

32+
#include <m-algo.h>
3233
#include <m-deque.h>
3334
#include <m-bptree.h>
3435

@@ -68,17 +69,28 @@ typedef struct
6869
*/
6970
typedef struct
7071
{
72+
/// Block type code
7173
uint64_t blk_type;
74+
/// Unique block number, with 0 reserved and 1 for payload
7275
uint64_t blk_num;
76+
/// Block processing control flags
7377
uint64_t flags;
78+
/// CRC type code
7479
uint64_t crc_type;
7580

76-
/// Pointer to memory managed by the BPA
81+
/// Pointer to memory managed by the BPA outside this struct
7782
void *btsd;
7883
/// Known length of the #btsd
7984
size_t btsd_len;
8085
} MockBPA_CanonicalBlock_t;
8186

87+
/** Block comparison which will order by block number in descending order.
88+
*/
89+
int MockBPA_CanonicalBlock_cmp(const MockBPA_CanonicalBlock_t *block_a, const MockBPA_CanonicalBlock_t *block_b);
90+
91+
/// M*LIB OPLIST for ::MockBPA_CanonicalBlock_t
92+
#define M_OPL_MockBPA_CanonicalBlock_t() M_OPEXTEND(M_POD_OPLIST, CMP(API_6(MockBPA_CanonicalBlock_cmp)))
93+
8294
/** @struct MockBPA_BlockList_t
8395
* An ordered list of ::MockBPA_CanonicalBlock_t storage
8496
* with fast size access.
@@ -89,15 +101,20 @@ typedef struct
89101
*/
90102
/// @cond Doxygen_Suppress
91103
// GCOV_EXCL_START
92-
M_DEQUE_DEF(MockBPA_BlockList, MockBPA_CanonicalBlock_t, M_POD_OPLIST)
104+
M_DEQUE_DEF(MockBPA_BlockList, MockBPA_CanonicalBlock_t, M_OPL_MockBPA_CanonicalBlock_t())
105+
M_ALGO_DEF(MockBPA_BlockList, M_DEQUE_OPLIST(MockBPA_BlockList, M_OPL_MockBPA_CanonicalBlock_t()))
93106
M_BPTREE_DEF2(MockBPA_BlockByNum, 4, uint64_t, M_BASIC_OPLIST, MockBPA_CanonicalBlock_t *, M_PTR_OPLIST)
94107
// GCOV_EXCL_STOP
95108
/// @endcond
96109

97110
typedef struct MockBPA_Bundle_s
98111
{
99-
uint64_t id;
100-
bool retain;
112+
/** Explicit retention constraint.
113+
* When true, indicates to retain this bundle after forwarding or delivery.
114+
* This defaults to true.
115+
*/
116+
bool retain;
117+
/// Primary block contents
101118
MockBPA_PrimaryBlock_t primary_block;
102119

103120
/// Storage for blocks in this bundle

src/mock_bpa/ctr.c

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
* Container structs for BPv7 data.
2525
*/
2626
#include <BPSecLib_Private.h>
27-
#include <m-algo.h>
2827

2928
#include "ctr.h"
3029
#include "decode.h"
@@ -68,7 +67,14 @@ void mock_bpa_ctr_deinit(mock_bpa_ctr_t *ctr)
6867
}
6968
}
7069

71-
int mock_bpa_decode(mock_bpa_ctr_t *ctr)
70+
void mock_bpa_ctr_sort_blocks(mock_bpa_ctr_t *ctr)
71+
{
72+
BSL_CHKVOID(ctr);
73+
// normalize block list by block number in descending order
74+
MockBPA_BlockList_sort(ctr->bundle->blocks);
75+
}
76+
77+
int mock_bpa_ctr_decode(mock_bpa_ctr_t *ctr)
7278
{
7379
BSL_CHKERR1(ctr);
7480
MockBPA_Bundle_t *bundle = ctr->bundle_ref.data;
@@ -93,34 +99,12 @@ int mock_bpa_decode(mock_bpa_ctr_t *ctr)
9399
return 0;
94100
}
95101

96-
// TODO this is not really defined by BPSec or BPv7
97-
static int block_cmp(const MockBPA_CanonicalBlock_t *block_a, const MockBPA_CanonicalBlock_t *block_b)
98-
{
99-
if (block_b->blk_type > block_a->blk_type)
100-
{
101-
return 1;
102-
}
103-
else if (block_b->blk_type < block_a->blk_type)
104-
{
105-
return -1;
106-
}
107-
return 0;
108-
}
109-
110-
// Add comparison by block type to sort just before encoding
111-
// GCOV_EXCL_START
112-
M_ALGO_DEF(MockBPA_BlockList, M_DEQUE_OPLIST(MockBPA_BlockList, M_OPEXTEND(M_POD_OPLIST, CMP(API_6(block_cmp)))))
113-
// GCOV_EXCL_STOP
114-
115-
int mock_bpa_encode(mock_bpa_ctr_t *ctr)
102+
int mock_bpa_ctr_encode(mock_bpa_ctr_t *ctr)
116103
{
117104
BSL_CHKERR1(ctr);
118-
MockBPA_Bundle_t *bundle = ctr->bundle_ref.data;
105+
const MockBPA_Bundle_t *bundle = ctr->bundle_ref.data;
119106
BSL_CHKERR1(bundle);
120107

121-
// TODO this is not really defined by BPSec or BPv7
122-
MockBPA_BlockList_sort(bundle->blocks);
123-
124108
QCBOREncodeContext encoder;
125109
// first round of encoding is to get the full size
126110
QCBOREncode_Init(&encoder, SizeCalculateUsefulBuf);

src/mock_bpa/ctr.h

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,25 @@ void mock_bpa_ctr_init_move(mock_bpa_ctr_t *ctr, mock_bpa_ctr_t *src);
4949

5050
void mock_bpa_ctr_deinit(mock_bpa_ctr_t *ctr);
5151

52-
int mock_bpa_decode(mock_bpa_ctr_t *ctr);
52+
/** Sort canonical blocks in a bundle by descending block number.
53+
* This ensures the primary block is the last block.
54+
* @param[in,out] ctr The container to read and decode PDU data from.
55+
*/
56+
void mock_bpa_ctr_sort_blocks(mock_bpa_ctr_t *ctr);
57+
58+
/** Decode a bundle PDU into a container.
59+
* @param[in,out] ctr The container to read and decode PDU data from.
60+
* @return Zero if successful.
61+
*/
62+
int mock_bpa_ctr_decode(mock_bpa_ctr_t *ctr);
5363

54-
int mock_bpa_encode(mock_bpa_ctr_t *ctr);
64+
/** Encode to a bundle PDU in a container.
65+
* @param[in,out] ctr The container to encode and write PDU data into.
66+
* @return Zero if successful.
67+
*/
68+
int mock_bpa_ctr_encode(mock_bpa_ctr_t *ctr);
5569

70+
/// M*LIB OPLIST for ::mock_bpa_ctr_t
5671
#define M_OPL_mock_bpa_ctr_t() \
5772
(INIT(API_2(mock_bpa_ctr_init)), INIT_MOVE(API_6(mock_bpa_ctr_init_move)), CLEAR(API_2(mock_bpa_ctr_deinit)))
5873

test/bsl_test_utils.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ int BSL_TestUtils_LoadBundleFromCBOR(BSL_TestContext_t *test_ctx, const char *cb
343343
MockBPA_Bundle_t *bundle = test_ctx->mock_bpa_ctr.bundle_ref.data;
344344
assert(bundle != NULL);
345345

346-
int decode_status = mock_bpa_decode(&(test_ctx->mock_bpa_ctr));
346+
int decode_status = mock_bpa_ctr_decode(&(test_ctx->mock_bpa_ctr));
347347
assert(bundle->primary_block.version == 7);
348348
assert(bundle->primary_block.timestamp.seq_num > 0);
349349
assert(bundle->primary_block.lifetime > 0);

test/test_BackendSecurityContext.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@ void test_SecurityContext_BIB_Source(void)
108108
(BSL_Data_t) { .len = target_block->btsd_len, .ptr = target_block->btsd });
109109
TEST_ASSERT_TRUE(x);
110110

111-
TEST_ASSERT_EQUAL(0, mock_bpa_encode(mock_bpa_ctr));
111+
mock_bpa_ctr_sort_blocks(mock_bpa_ctr);
112+
TEST_ASSERT_EQUAL(0, mock_bpa_ctr_encode(mock_bpa_ctr));
112113
bool is_expected =
113114
(BSL_TestUtils_IsB16StrEqualTo(RFC9173_TestVectors_AppendixA1.cbor_bundle_bib, mock_bpa_ctr->encoded));
114115

@@ -149,7 +150,7 @@ void test_SecurityContext_BIB_Verifier(void)
149150

150151
TEST_ASSERT_EQUAL(0, BSL_SecCtx_ExecutePolicyActionSet(&LocalTestCtx.bsl, malloced_responseset,
151152
&mock_bpa_ctr->bundle_ref, malloced_actionset));
152-
TEST_ASSERT_EQUAL(0, mock_bpa_encode(mock_bpa_ctr));
153+
TEST_ASSERT_EQUAL(0, mock_bpa_ctr_encode(mock_bpa_ctr));
153154
bool is_match =
154155
(BSL_TestUtils_IsB16StrEqualTo(RFC9173_TestVectors_AppendixA1.cbor_bundle_bib, mock_bpa_ctr->encoded));
155156

@@ -243,7 +244,7 @@ void test_SecurityContext_BIB_Acceptor(void)
243244
if (sec_context_result != 0)
244245
goto cleanup;
245246

246-
encode_result = mock_bpa_encode(mock_bpa_ctr);
247+
encode_result = mock_bpa_ctr_encode(mock_bpa_ctr);
247248
if (encode_result != 0)
248249
goto cleanup;
249250

@@ -528,7 +529,7 @@ void test_RFC9173_AppendixA_Example4_Acceptor(void)
528529
const char *expected_processed_bundle = ("9f88070000820282010282028202018202820201820018281a000f424085010100"
529530
"005823526561647920746f2067656e657261746520612033322d6279746520706179"
530531
"6c6f6164ff");
531-
TEST_ASSERT_EQUAL(0, mock_bpa_encode(mock_bpa_ctr));
532+
TEST_ASSERT_EQUAL(0, mock_bpa_ctr_encode(mock_bpa_ctr));
532533
TEST_ASSERT_TRUE(BSL_TestUtils_IsB16StrEqualTo(expected_processed_bundle, mock_bpa_ctr->encoded));
533534

534535
BSL_SecurityAction_Deinit(malloced_action);

test/test_mock_bpa_ctr.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,12 @@ void test_mock_bpa_ctr_loopback_decode_encode(const char *hexdata)
7777
mock_bpa_ctr_init(&ctr);
7878
TEST_ASSERT_EQUAL_INT(0, BSL_Data_CopyFrom(&ctr.encoded, in_data.len, in_data.ptr));
7979

80-
int res = mock_bpa_decode(&ctr);
80+
int res = mock_bpa_ctr_decode(&ctr);
8181
TEST_ASSERT_EQUAL_INT_MESSAGE(0, res, "mock_bpa_decode() failed");
8282

83-
res = mock_bpa_encode(&ctr);
83+
mock_bpa_ctr_sort_blocks(&ctr);
84+
85+
res = mock_bpa_ctr_encode(&ctr);
8486
TEST_ASSERT_EQUAL_INT_MESSAGE(0, res, "mock_bpa_encode() failed");
8587

8688
TEST_ASSERT_EQUAL_INT(in_data.len, ctr.encoded.len);

0 commit comments

Comments
 (0)