blob: be80e5c0de4c4d0a437b29553cea826be47bf04b [file] [log] [blame]
Jacob Erlbeck81142942015-11-17 08:42:05 +01001#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4#include <inttypes.h>
5
6#include <osmocom/core/application.h>
7#include <osmocom/core/utils.h>
8#include <osmocom/core/logging.h>
9#include <osmocom/gsm/apn.h>
10
11static void apn_round_trip(const uint8_t *input, size_t len, const char *wanted_output)
12{
13 char output[len ? len : 1];
14 uint8_t encoded[len + 50];
15 char *out_str;
16 int enc_len;
17
18 /* decode and verify we have what we want */
19 out_str = osmo_apn_to_str(output, input, len);
20 OSMO_ASSERT(out_str);
21 OSMO_ASSERT(out_str == &output[0]);
22 OSMO_ASSERT(strlen(out_str) == strlen(wanted_output));
23 OSMO_ASSERT(strcmp(out_str, wanted_output) == 0);
24
25 /* encode and verify it */
26 if (len != 0) {
27 enc_len = osmo_apn_from_str(encoded, ARRAY_SIZE(encoded), wanted_output);
28 OSMO_ASSERT(enc_len == len);
29 OSMO_ASSERT(memcmp(encoded, input, enc_len) == 0);
30 } else {
31 enc_len = osmo_apn_from_str(encoded, 0, wanted_output);
32 OSMO_ASSERT(enc_len == -1);
33 }
34}
35
36static void test_gsm_03_03_apn(void)
37{
38
39 {
40 /* test invalid writes */
41 const uint8_t ref[10] = { 0xAB, 0xAC, 0xAD, 0xAE, 0xAF, 0xAB, 0xAC, 0xAD, 0xAE, 0xAF };
42 uint8_t output[10];
43 int enc_len;
44
45 memcpy(output, ref, ARRAY_SIZE(output));
46 enc_len = osmo_apn_from_str(output, 0, "");
47 OSMO_ASSERT(enc_len == -1);
48 OSMO_ASSERT(memcmp(ref, output, ARRAY_SIZE(ref)) == 0);
49
50 memcpy(output, ref, ARRAY_SIZE(output));
51 enc_len = osmo_apn_from_str(output, 0, "foo");
52 OSMO_ASSERT(enc_len == -1);
53 OSMO_ASSERT(memcmp(ref, output, ARRAY_SIZE(ref)) == 0);
54
55 memcpy(output, ref, ARRAY_SIZE(output));
56 enc_len = osmo_apn_from_str(output, 1, "foo");
57 OSMO_ASSERT(enc_len == -1);
58 OSMO_ASSERT(memcmp(ref + 1, output + 1, ARRAY_SIZE(ref) - 1) == 0);
59
60 memcpy(output, ref, ARRAY_SIZE(output));
61 enc_len = osmo_apn_from_str(output, 2, "foo");
62 OSMO_ASSERT(enc_len == -1);
63 OSMO_ASSERT(memcmp(ref + 2, output + 2, ARRAY_SIZE(ref) - 2) == 0);
64
65 memcpy(output, ref, ARRAY_SIZE(output));
66 enc_len = osmo_apn_from_str(output, 3, "foo");
67 OSMO_ASSERT(enc_len == -1);
68 OSMO_ASSERT(memcmp(ref + 3, output + 3, ARRAY_SIZE(ref) - 3) == 0);
69 }
70
71 {
72 /* single empty label */
73 uint8_t input[] = { 0x0 };
74 const char *output = "";
75 apn_round_trip(input, ARRAY_SIZE(input), output);
76 }
77
78 {
79 /* no label */
80 uint8_t input[] = { };
81 const char *output = "";
82 apn_round_trip(input, ARRAY_SIZE(input), output);
83 }
84
85 {
86 /* single label with A */
87 uint8_t input[] = { 0x1, 65 };
88 const char *output = "A";
89 apn_round_trip(input, ARRAY_SIZE(input), output);
90 OSMO_ASSERT(osmo_apn_to_str(NULL, input, ARRAY_SIZE(input) - 1) == NULL);
91 }
92
93 {
94 uint8_t input[] = { 0x3, 65, 66, 67, 0x2, 90, 122 };
95 const char *output = "ABC.Zz";
96 char tmp[strlen(output) + 1];
97 apn_round_trip(input, ARRAY_SIZE(input), output);
98 OSMO_ASSERT(osmo_apn_to_str(tmp, input, ARRAY_SIZE(input) - 1) == NULL);
99 OSMO_ASSERT(osmo_apn_to_str(tmp, input, ARRAY_SIZE(input) - 2) == NULL);
100 OSMO_ASSERT(osmo_apn_to_str(tmp, input, ARRAY_SIZE(input) - 4) == NULL);
101 OSMO_ASSERT(osmo_apn_to_str(tmp, input, ARRAY_SIZE(input) - 5) == NULL);
102 OSMO_ASSERT(osmo_apn_to_str(tmp, input, ARRAY_SIZE(input) - 6) == NULL);
103 }
104}
105
106const struct log_info_cat default_categories[] = {
107};
108
109static struct log_info info = {
110 .cat = default_categories,
111 .num_cat = ARRAY_SIZE(default_categories),
112};
113
114int main(int argc, char **argv)
115{
116 osmo_init_logging(&info);
117
118 test_gsm_03_03_apn();
119
120 printf("Done.\n");
121 return EXIT_SUCCESS;
122}