summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorHolger Hans Peter Freyther <zecke@selfish.org>2010-07-20 02:46:56 +0800
committerHolger Hans Peter Freyther <zecke@selfish.org>2010-07-20 02:48:17 +0800
commit31e97ea34ef77388ad27fa1ee2a5084ae238234f (patch)
tree1e862cf2749ca5243e37d411dd6fd98a081425c7 /tests
parent28de05336b01aa72d7f191b33467e6c57be178a4 (diff)
tests: Separate encoding/decoding test to verify them independly
Use the current test result as test data for future regression testing. The encode function appears to add too many zeros to the text.
Diffstat (limited to 'tests')
-rw-r--r--tests/sms/sms_test.c95
1 files changed, 78 insertions, 17 deletions
diff --git a/tests/sms/sms_test.c b/tests/sms/sms_test.c
index 4daf003a..3742dd88 100644
--- a/tests/sms/sms_test.c
+++ b/tests/sms/sms_test.c
@@ -25,6 +25,57 @@
#include <sys/types.h>
#include <osmocore/msgb.h>
#include <osmocore/gsm_utils.h>
+#include <osmocore/utils.h>
+
+struct test_case {
+ const uint8_t *input;
+ const uint16_t input_length;
+
+ const uint8_t *expected;
+ const uint16_t expected_length;
+};
+
+static const char simple_text[] = "test text";
+static const uint8_t simple_enc[] = {
+ 0xf4, 0xf2, 0x9c, 0x0e, 0xa2, 0x97, 0xf1, 0x74, 0x00,
+};
+
+static const char escape_text[] = "!$ a more#^- complicated test@@?_\%! case";
+static const uint8_t escape_enc[] = {
+ 0x21, 0x01, 0x28, 0x0c, 0x6a, 0xbf, 0xe5, 0xe5, 0xd1,
+ 0x86, 0xd2, 0x02, 0x8d, 0xdf, 0x6d, 0x38, 0x3b, 0x3d,
+ 0x0e, 0xd3, 0xcb, 0x64, 0x10, 0xbd, 0x3c, 0xa7, 0x03,
+ 0x00, 0xbf, 0x48, 0x29, 0x04, 0x1a, 0x87, 0xe7, 0x65,
+ 0x00, 0x00, 0x00, 0x00, 0x00
+};
+
+static const struct test_case test_encode[] =
+{
+ {
+ .input = simple_text,
+ .expected = simple_enc,
+ .expected_length = 9,
+ },
+ {
+ .input = escape_text,
+ .expected = escape_enc,
+ .expected_length = 41,
+ },
+};
+
+static const struct test_case test_decode[] =
+{
+ {
+ .input = simple_enc,
+ .input_length = 9,
+ .expected = simple_text,
+ },
+ {
+ .input = escape_enc,
+ .input_length = 41,
+ .expected = escape_text,
+ },
+};
int main(int argc, char** argv)
{
@@ -33,30 +84,40 @@ int main(int argc, char** argv)
uint8_t *sms;
uint8_t i;
- /* test 7-bit coding/decoding */
- const char *input = "test text";
uint8_t length;
uint8_t coded[256];
char result[256];
- length = gsm_7bit_encode(coded, input);
- gsm_7bit_decode(result, coded, length);
- if (strcmp(result, input) != 0) {
- printf("7 Bit coding failed... life sucks\n");
- printf("Wanted: '%s' got '%s'\n", input, result);
- return -1;
+ /* test 7-bit encoding */
+ for (i = 0; i < ARRAY_SIZE(test_encode); ++i) {
+ memset(coded, 0, sizeof(coded));
+ length = gsm_7bit_encode(coded, test_encode[i].input);
+
+ if (length != test_encode[i].expected_length) {
+ fprintf(stderr, "Failed to encode case %d. Got %d\n",
+ i, length);
+ return -1;
+ }
+
+ if (memcmp(coded, test_encode[i].expected, length) != 0) {
+ fprintf(stderr, "Encoded content does not match for %d\n",
+ i);
+ return -1;
+ }
}
- memset(coded, 0, sizeof(coded));
- memset(result, 0, sizeof(coded));
- input = strdup("!$ a more#^- complicated test@@?_\%! case");
- length = gsm_7bit_encode(coded, input);
- gsm_7bit_decode(result, coded, length);
- if (strcmp(result, input) != 0) {
- printf("7 Bit coding failed... life sucks\n");
- printf("Wanted: '%s' got '%s'\n", input, result);
- return -2;
+ /* test 7-bit decoding */
+ for (i = 0; i < ARRAY_SIZE(test_decode); ++i) {
+ memset(result, 0, sizeof(coded));
+ gsm_7bit_decode(result, test_decode[i].input,
+ test_decode[i].input_length);
+
+ if (strcmp(result, test_decode[i].expected) != 0) {
+ fprintf(stderr, "Test case %d failed to decode.\n", i);
+ return -1;
+ }
}
+ printf("OK\n");
return 0;
}