sccp: Rewrite the sccp_test_addr to set the size correctly

Fix the code to set the number of consumed bytes correctly
and return the number of bytes consumed for for the address.

Add a simple but expandable test case to test the SCCP address
This commit is contained in:
Holger Hans Peter Freyther 2010-11-16 20:47:37 +01:00
parent ec5409ca4f
commit 13cd1199b9
2 changed files with 64 additions and 13 deletions

View File

@ -26,6 +26,7 @@
#include <osmocore/msgb.h>
#include <osmocore/talloc.h>
#include <osmocore/tlv.h>
#include <osmocore/logging.h>
#include <sccp/sccp.h>
@ -483,30 +484,37 @@ static int _sccp_parse_err(struct msgb *msgb, struct sccp_parse_result *result)
int sccp_create_sccp_addr(struct msgb *msg, const struct sockaddr_sccp *sock)
{
int pos = 2;
uint8_t *data;
uint8_t *len, *ai, *gti;
len = msgb_put(msg, 1);
ai = msgb_put(msg, 1);
data = msgb_put(msg, 1 + 2 + sock->gti_len);
data[0] = 2;
if (sock->gti)
data[1] = 0 << 6 | (sock->gti_ind & 0x0f) << 2;
ai[0] = 0 << 6 | (sock->gti_ind & 0x0f) << 2;
else
data[1] = 1 << 6 | 1 << 1;
ai[0] = 1 << 6 | 1 << 1;
/* store a point code */
if (sock->use_poi) {
msgb_put(msg, 2);
data[1] |= 0x01;
data[pos++] = sock->poi[0];
data[pos++] = sock->poi[1];
uint8_t *poi;
ai[0] |= 0x01;
poi = msgb_put(msg, 2);
poi[0] = sock->poi[0];
poi[1] = sock->poi[1];
}
data[pos++] = sock->sccp_ssn;
/* copy the SSN */
msgb_v_put(msg, sock->sccp_ssn);
/* copy the gti if it is present */
memcpy(&data[pos++], sock->gti, sock->gti_len);
gti = msgb_put(msg, sock->gti_len);
memcpy(gti, sock->gti, sock->gti_len);
/* update the length now */
len[0] = msg->tail - len - 1;
return len[0] + 1;
}
/*

View File

@ -887,6 +887,48 @@ static void test_sccp_parsing(void)
}
}
/*
* Test the creation of SCCP addresses
*/
int sccp_create_sccp_addr(struct msgb *msg, const struct sockaddr_sccp *sock);
struct sccp_addr_tst {
const struct sockaddr_sccp *addr;
const uint8_t *output;
const int output_len;
};
static uint8_t ssn_out[] = {
0x02, 0x42, 0xfe,
};
static struct sccp_addr_tst sccp_addr_tst[] = {
{
.addr = &sccp_ssn_bssap,
.output = ssn_out,
.output_len = ARRAY_SIZE(ssn_out),
},
};
static void test_sccp_address()
{
int i, ret;
struct msgb *msg = msgb_alloc(128, "sccp-addr");
for (i = 0; i < ARRAY_SIZE(sccp_addr_tst); ++i) {
msgb_reset(msg);
ret = sccp_create_sccp_addr(msg, sccp_addr_tst[i].addr);
if (ret != sccp_addr_tst[i].output_len) {
FAIL("Length is from for %d\n", i);
}
if (memcmp(msg->data, sccp_addr_tst[i].output, ret) != 0) {
FAIL("Unexpected data for %d\n", i);
}
}
}
static const struct log_info_cat default_categories[] = {
[0] = {
.name = "DSCCP",
@ -921,6 +963,7 @@ int main(int argc, char **argv)
test_sccp_connection();
test_sccp_system_crash();
test_sccp_parsing();
test_sccp_address();
return 0;
}