From 9818664315a514b187719792d77723ad9e18dcdf Mon Sep 17 00:00:00 2001 From: Max Date: Wed, 18 Oct 2017 13:14:49 +0200 Subject: Add tests for bitvec_write_field() This function is actively used by OsmoPCU but have not been covered by tests so far. The test code is based on Minh-Quang Nguyen submission with some modifications. The test's FIXME will be addressed in follow-up patches. Change-Id: I2ee544256b8675bc62a42493aab66a8eeee54f90 Related: OS#1526 --- tests/Makefile.am | 6 +- tests/bits/bitfield_test.c | 164 ++++++++++++++++++++++++++++++++++++++++++++ tests/bits/bitfield_test.ok | 36 ++++++++++ tests/testsuite.at | 6 ++ 4 files changed, 211 insertions(+), 1 deletion(-) create mode 100644 tests/bits/bitfield_test.c create mode 100644 tests/bits/bitfield_test.ok (limited to 'tests') diff --git a/tests/Makefile.am b/tests/Makefile.am index dbe349f5..7fb9a7bd 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -11,6 +11,7 @@ check_PROGRAMS = timer/timer_test sms/sms_test ussd/ussd_test \ loggingrb/loggingrb_test strrb/strrb_test \ comp128/comp128_test smscb/gsm0341_test \ bitvec/bitvec_test msgb/msgb_test bits/bitcomp_test \ + bits/bitfield_test \ tlv/tlv_test gsup/gsup_test oap/oap_test fsm/fsm_test \ write_queue/wqueue_test socket/socket_test \ coding/coding_test conv/conv_gsm0503_test \ @@ -77,6 +78,9 @@ bitvec_bitvec_test_LDADD = $(top_builddir)/src/libosmocore.la bits_bitcomp_test_SOURCES = bits/bitcomp_test.c bits_bitcomp_test_LDADD = $(top_builddir)/src/libosmocore.la +bits_bitfield_test_SOURCES = bits/bitfield_test.c +bits_bitfield_test_LDADD = $(top_builddir)/src/libosmocore.la + conv_conv_test_SOURCES = conv/conv_test.c conv/conv.c conv_conv_test_LDADD = $(top_builddir)/src/libosmocore.la $(top_builddir)/src/gsm/libgsmint.la @@ -232,7 +236,7 @@ EXTRA_DIST = testsuite.at $(srcdir)/package.m4 $(TESTSUITE) \ vty/ok_more_spaces.cfg \ vty/ok_tabs_and_spaces.cfg \ vty/ok_tabs.cfg \ - comp128/comp128_test.ok \ + comp128/comp128_test.ok bits/bitfield_test.ok \ utils/utils_test.ok stats/stats_test.ok \ bitvec/bitvec_test.ok msgb/msgb_test.ok bits/bitcomp_test.ok \ sim/sim_test.ok tlv/tlv_test.ok abis/abis_test.ok \ diff --git a/tests/bits/bitfield_test.c b/tests/bits/bitfield_test.c new file mode 100644 index 00000000..d6653e32 --- /dev/null +++ b/tests/bits/bitfield_test.c @@ -0,0 +1,164 @@ +#include +#include +#include +#include + +#include +#include +#include + +#define INTRO(p) printf("=== start %s(%u) ===\n", __func__, p) +#define OUTRO(p) printf("=== end %s(%u) ===\n\n", __func__, p) + +static void test_bitvec_ia_octet_encode_pkt_dl_ass(struct bitvec *dest, uint32_t ttli, + uint8_t tfi, uint8_t gamma, uint8_t ta_valid, uint8_t ws_enc, + bool use_lh) +{ + unsigned wp = 0; + + INTRO(use_lh); + + /* 3GPP TS 44.018 §10.5.2.16 IA Rest Octets */ + if (use_lh) /* FIXME: add function to encode LH properly */ + bitvec_write_field(dest, &wp, 3, 2); /* "HH" */ + else + bitvec_write_field(dest, &wp, 3, 2); /* "HH" */ + bitvec_write_field(dest, &wp, 1, 2); /* "01" Packet Downlink Assignment */ + bitvec_write_field(dest, &wp, ttli, 32); /* TLLI */ + bitvec_write_field(dest, &wp, 1, 1); /* switch TFI: on */ + bitvec_write_field(dest, &wp, tfi, 5); /* TFI */ + bitvec_write_field(dest, &wp, 0x0, 1); /* RLC acknowledged mode */ + bitvec_write_field(dest, &wp, 0x0, 1); /* ALPHA = not present */ + bitvec_write_field(dest, &wp, gamma, 5); /* GAMMA power control parameter */ + bitvec_write_field(dest, &wp, 0,1); /* Polling Bit: off */ + bitvec_write_field(dest, &wp, ta_valid, 1); /* N. B: NOT related to TAI! */ + bitvec_write_field(dest, &wp, 0, 1); /* No TIMING_ADVANCE_INDEX: */ + bitvec_write_field(dest, &wp, 0, 1); /* TBF Starting TIME present */ + bitvec_write_field(dest, &wp, 0, 1); /* P0 not present */ + if (use_lh) { /* FIXME: add function to encode LH properly */ + bitvec_write_field(dest, &wp, 1, 1); /* "H" - additional for R99 */ + } else + bitvec_write_field(dest, &wp, 1, 1); /* "H" - additional for R99 */ + bitvec_write_field(dest, &wp, ws_enc, 5); /* EGPRS Window Size */ + bitvec_write_field(dest, &wp, 0, 2); /* LINK_QUALITY_MEASUREMENT_MODE */ + bitvec_write_field(dest, &wp, 0, 1); /* BEP_PERIOD2 not present */ + + printf("Encoded PKT DL ASS IA Rest Octets: %s\n", osmo_hexdump(dest->data, dest->data_len)); + + OUTRO(use_lh); +} + +static void test_bitvec_ia_octet_encode_pkt_ul_ass(struct bitvec *dest, uint32_t fn, + uint8_t tfi, uint8_t gamma, uint8_t usf, bool tbf, bool use_lh) +{ + unsigned wp = 0; + + INTRO(use_lh); + + /* 3GPP TS 44.018 §10.5.2.37b 10.5.2.16 */ + if (use_lh) /* FIXME: add function to encode LH properly */ + bitvec_write_field(dest, &wp, 3, 2); /* "HH" */ + else + bitvec_write_field(dest, &wp, 3, 2); /* "HH" */ + bitvec_write_field(dest, &wp, 0, 2); /* "0" Packet Uplink Assignment */ + if (!tbf) { + bitvec_write_field(dest, &wp, 0, 1); /* Block Allocation: SBA */ + bitvec_write_field(dest, &wp, 0, 1); /* ALPHA = not present */ + bitvec_write_field(dest, &wp, gamma, 5); /* GAMMA power control parameter */ + bitvec_write_field(dest, &wp, 0, 1); /* No TIMING_ADVANCE_INDEX: */ + bitvec_write_field(dest, &wp, 1, 1); /* TBF_STARTING_TIME_FLAG */ + bitvec_write_field(dest, &wp, (fn / (26 * 51)) % 32, 5); /* T1' */ + bitvec_write_field(dest, &wp, fn % 51, 6); /* T3 */ + bitvec_write_field(dest, &wp, fn % 26, 5); /* T2 */ + } else { + bitvec_write_field(dest, &wp, 1, 1); /* Block Allocation: Not SBA */ + bitvec_write_field(dest, &wp, tfi, 5); /* TFI_ASSIGNMENT */ + bitvec_write_field(dest, &wp, 0, 1); /* POLLING = none */ + bitvec_write_field(dest, &wp, 0, 1); /* ALLOCATION_TYPE: dynamic */ + bitvec_write_field(dest, &wp, usf, 3); /* USF */ + bitvec_write_field(dest, &wp, 0, 1); /* USF_GRANULARITY */ + bitvec_write_field(dest, &wp, 0, 1); /* "0" power control: Not Present */ + bitvec_write_field(dest, &wp, 0, 2); /* CHANNEL_CODING_COMMAND */ + bitvec_write_field(dest, &wp, 1, 1); /* TLLI_BLOCK_CHANNEL_CODING */ + bitvec_write_field(dest, &wp, 0, 1); /* ALPHA = not present */ + bitvec_write_field(dest, &wp, gamma, 5); /* GAMMA power control parameter */ + /* note: there is no choise for TAI and no starting time */ + bitvec_write_field(dest, &wp, 0, 1); /* switch TIMING_ADVANCE_INDEX = off */ + bitvec_write_field(dest, &wp, 0, 1); /* TBF_STARTING_TIME_FLAG */ + } + + printf("Encoded PKT UL ASS IA Rest Octets: %s\n", osmo_hexdump(dest->data, dest->data_len)); + + OUTRO(use_lh); +} + +static void test_bitdiff(const struct bitvec *src1, const struct bitvec *src2, unsigned len) +{ + unsigned int bit_err = 0, i, j; + uint8_t byte_err = 0; + + INTRO(len); + + for (i = 0; i < len; i++) { + /* byte compare */ + byte_err = src1->data[i] ^ src2->data[i]; + if (byte_err) + for (j = 0; j < 8; j++) + bit_err += (byte_err >> j) & 0x01; /* count bits which differ */ + } + + + printf("=== total %u bits differ ===\n", bit_err); + + OUTRO(len); +} + +static inline void buf_init(struct bitvec *dest, struct bitvec *dest_lh) +{ + /* initialize buffer */ + bitvec_unhex(dest, "2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b"); + bitvec_unhex(dest_lh, "2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b"); +} + +static inline void test_enc_ul_ass(struct bitvec *dest, struct bitvec *dest_lh, uint32_t fn, + uint8_t tfi, uint8_t gamma, uint8_t usf, bool tbf) +{ + buf_init(dest, dest_lh); + + test_bitvec_ia_octet_encode_pkt_ul_ass(dest, fn, tfi, gamma, usf, tbf, false); + test_bitvec_ia_octet_encode_pkt_ul_ass(dest_lh, fn, tfi, gamma, usf, tbf, true); + + test_bitdiff(dest, dest_lh, 22); +} + +int main(int argc, char **argv) +{ + void *tall_pcu_ctx; + struct bitvec *dest, *dest_lh; + uint8_t gamma = 0, ta_valid = 1, ws_enc = 3, usf = 1, tfi = 0; /* Temporary Flow Identity */ + uint32_t ttli = 0xdeadbeef, fn = 1234; + + tall_pcu_ctx = talloc_named_const(NULL, 1, "bitvecTest context"); + if (!tall_pcu_ctx) + return EXIT_FAILURE; + + dest = bitvec_alloc(22, tall_pcu_ctx); + dest_lh = bitvec_alloc(22, tall_pcu_ctx); + + buf_init(dest, dest_lh); + + test_bitvec_ia_octet_encode_pkt_dl_ass(dest, ttli, tfi, gamma, ta_valid, ws_enc, false); + test_bitvec_ia_octet_encode_pkt_dl_ass(dest_lh, ttli, tfi, gamma, ta_valid, ws_enc, true); + + test_bitdiff(dest, dest_lh, 22); + + test_enc_ul_ass(dest, dest_lh, fn, tfi, gamma, usf, false); + test_enc_ul_ass(dest, dest_lh, fn, tfi, gamma, usf, true); + + bitvec_free(dest); + bitvec_free(dest_lh); + + talloc_free(tall_pcu_ctx); + + return EXIT_SUCCESS; +} diff --git a/tests/bits/bitfield_test.ok b/tests/bits/bitfield_test.ok new file mode 100644 index 00000000..5cb25002 --- /dev/null +++ b/tests/bits/bitfield_test.ok @@ -0,0 +1,36 @@ +=== start test_bitvec_ia_octet_encode_pkt_dl_ass(0) === +Encoded PKT DL ASS IA Rest Octets: dd ea db ee f8 00 22 31 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b +=== end test_bitvec_ia_octet_encode_pkt_dl_ass(0) === + +=== start test_bitvec_ia_octet_encode_pkt_dl_ass(1) === +Encoded PKT DL ASS IA Rest Octets: dd ea db ee f8 00 22 31 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b +=== end test_bitvec_ia_octet_encode_pkt_dl_ass(1) === + +=== start test_bitdiff(22) === +=== total 0 bits differ === +=== end test_bitdiff(22) === + +=== start test_bitvec_ia_octet_encode_pkt_ul_ass(0) === +Encoded PKT UL ASS IA Rest Octets: c0 08 0a 63 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b +=== end test_bitvec_ia_octet_encode_pkt_ul_ass(0) === + +=== start test_bitvec_ia_octet_encode_pkt_ul_ass(1) === +Encoded PKT UL ASS IA Rest Octets: c0 08 0a 63 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b +=== end test_bitvec_ia_octet_encode_pkt_ul_ass(1) === + +=== start test_bitdiff(22) === +=== total 0 bits differ === +=== end test_bitdiff(22) === + +=== start test_bitvec_ia_octet_encode_pkt_ul_ass(0) === +Encoded PKT UL ASS IA Rest Octets: c8 02 10 0b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b +=== end test_bitvec_ia_octet_encode_pkt_ul_ass(0) === + +=== start test_bitvec_ia_octet_encode_pkt_ul_ass(1) === +Encoded PKT UL ASS IA Rest Octets: c8 02 10 0b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b +=== end test_bitvec_ia_octet_encode_pkt_ul_ass(1) === + +=== start test_bitdiff(22) === +=== total 0 bits differ === +=== end test_bitdiff(22) === + diff --git a/tests/testsuite.at b/tests/testsuite.at index 483860fc..4a59b221 100644 --- a/tests/testsuite.at +++ b/tests/testsuite.at @@ -45,6 +45,12 @@ cat $abs_srcdir/bits/bitcomp_test.ok > expout AT_CHECK([$abs_top_builddir/tests/bits/bitcomp_test], [0], [expout]) AT_CLEANUP +AT_SETUP([bitfield]) +AT_KEYWORDS([bitfield]) +cat $abs_srcdir/bits/bitfield_test.ok > expout +AT_CHECK([$abs_top_builddir/tests/bits/bitfield_test], [0], [expout]) +AT_CLEANUP + AT_SETUP([conv]) AT_KEYWORDS([conv]) cat $abs_srcdir/conv/conv_test.ok > expout -- cgit v1.2.3