blob: 70e3009979ab44b9a366c372efa8c6d43ef1b66e [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 {
Maxfcf81b52017-10-19 15:50:44 +020094 /* NULL input */
95 uint8_t input[] = { 0x1, 65 };
96 char *output = "LOL";
97 OSMO_ASSERT(osmo_apn_to_str(output, NULL, ARRAY_SIZE(input) - 1) == NULL);
98 }
99
100 {
Jacob Erlbeck81142942015-11-17 08:42:05 +0100101 uint8_t input[] = { 0x3, 65, 66, 67, 0x2, 90, 122 };
102 const char *output = "ABC.Zz";
103 char tmp[strlen(output) + 1];
104 apn_round_trip(input, ARRAY_SIZE(input), output);
105 OSMO_ASSERT(osmo_apn_to_str(tmp, input, ARRAY_SIZE(input) - 1) == NULL);
106 OSMO_ASSERT(osmo_apn_to_str(tmp, input, ARRAY_SIZE(input) - 2) == NULL);
107 OSMO_ASSERT(osmo_apn_to_str(tmp, input, ARRAY_SIZE(input) - 4) == NULL);
108 OSMO_ASSERT(osmo_apn_to_str(tmp, input, ARRAY_SIZE(input) - 5) == NULL);
109 OSMO_ASSERT(osmo_apn_to_str(tmp, input, ARRAY_SIZE(input) - 6) == NULL);
110 }
111}
112
113const struct log_info_cat default_categories[] = {
114};
115
116static struct log_info info = {
117 .cat = default_categories,
118 .num_cat = ARRAY_SIZE(default_categories),
119};
120
121int main(int argc, char **argv)
122{
Neels Hofmeyra829b452018-04-05 03:02:35 +0200123 void *ctx = talloc_named_const(NULL, 0, "gprs_test");
124 osmo_init_logging2(ctx, &info);
Jacob Erlbeck81142942015-11-17 08:42:05 +0100125
126 test_gsm_03_03_apn();
127
128 printf("Done.\n");
129 return EXIT_SUCCESS;
130}