summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHolger Hans Peter Freyther <zecke@selfish.org>2010-11-16 20:47:37 +0100
committerHolger Hans Peter Freyther <zecke@selfish.org>2010-11-16 20:56:08 +0100
commit13cd1199b92b98241d594c02873cd80a218a80ac (patch)
treed6f06e31bfc08b4e24f930edb936dfb1954f530b
parentec5409ca4f46c30813c7d213d15880a691e8e91f (diff)
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
-rw-r--r--src/sccp.c34
-rw-r--r--tests/sccp/sccp_test.c43
2 files changed, 64 insertions, 13 deletions
diff --git a/src/sccp.c b/src/sccp.c
index 1869631..e5b0e02 100644
--- a/src/sccp.c
+++ b/src/sccp.c
@@ -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;
}
/*
diff --git a/tests/sccp/sccp_test.c b/tests/sccp/sccp_test.c
index c5c6f4f..35b4cb7 100644
--- a/tests/sccp/sccp_test.c
+++ b/tests/sccp/sccp_test.c
@@ -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;
}