blob: 30e900f6193395ed53515c6924aed79ebbd3f4ff [file] [log] [blame]
Holger Hans Peter Freytherfaf1f642011-06-23 17:53:27 -04001#include <stdio.h>
2#include <stdlib.h>
3#include <inttypes.h>
4
5#include <openbsc/gprs_llc.h>
Holger Hans Peter Freytherce1b22e2014-08-04 14:22:13 +02006#include <openbsc/gprs_utils.h>
Holger Hans Peter Freytherfaf1f642011-06-23 17:53:27 -04007
8#define ASSERT_FALSE(x) if (x) { printf("Should have returned false.\n"); abort(); }
9#define ASSERT_TRUE(x) if (!x) { printf("Should have returned true.\n"); abort(); }
10
11/**
12 * GSM 04.64 8.4.2 Receipt of unacknowledged information
13 */
14static int nu_is_retransmission(uint16_t nu, uint16_t vur)
15{
16 int ret = gprs_llc_is_retransmit(nu, vur);
17 printf("N(U) = %d, V(UR) = %d => %s\n", nu, vur,
18 ret == 1 ? "retransmit" : "new");
19 return ret;
20}
21
22static void test_8_4_2()
23{
24 printf("Testing gprs_llc_is_retransmit.\n");
25
26 ASSERT_FALSE(nu_is_retransmission(0, 0));
27 ASSERT_TRUE (nu_is_retransmission(0, 1));
28
29 /* expect 1... check for retransmissions */
30 ASSERT_TRUE (nu_is_retransmission(0, 1));
31 ASSERT_TRUE (nu_is_retransmission(511, 1));
32 ASSERT_TRUE (nu_is_retransmission(483, 1));
33 ASSERT_TRUE (nu_is_retransmission(482, 1));
34 ASSERT_FALSE(nu_is_retransmission(481, 1));
35
36 /* expect 511... check for retransmissions */
37 ASSERT_FALSE(nu_is_retransmission(0, 240)); // ahead
38 ASSERT_FALSE(nu_is_retransmission(0, 511)); // ahead
39 ASSERT_FALSE(nu_is_retransmission(1, 511)); // ahead
40 ASSERT_FALSE(nu_is_retransmission(511, 511)); // same
41 ASSERT_TRUE (nu_is_retransmission(510, 511)); // behind
42 ASSERT_TRUE (nu_is_retransmission(481, 511)); // behind
43 ASSERT_FALSE(nu_is_retransmission(479, 511)); // wrapped
44}
45
Holger Hans Peter Freytherce1b22e2014-08-04 14:22:13 +020046static void apn_round_trip(const uint8_t *input, size_t len, const char *wanted_output)
47{
48 char output[len ? len : 1];
49 uint8_t encoded[len + 50];
50 char *out_str;
51 int enc_len;
52
53 /* decode and verify we have what we want */
54 out_str = gprs_apn_to_str(output, input, len);
55 OSMO_ASSERT(out_str);
56 OSMO_ASSERT(out_str == &output[0]);
57 OSMO_ASSERT(strlen(out_str) == strlen(wanted_output));
58 OSMO_ASSERT(strcmp(out_str, wanted_output) == 0);
59
60 /* encode and verify it */
61 if (len != 0) {
62 enc_len = gprs_str_to_apn(encoded, ARRAY_SIZE(encoded), wanted_output);
63 OSMO_ASSERT(enc_len == len);
64 OSMO_ASSERT(memcmp(encoded, input, enc_len) == 0);
65 } else {
66 enc_len = gprs_str_to_apn(encoded, 0, wanted_output);
67 OSMO_ASSERT(enc_len == -1);
68 }
69}
70
71static void test_gsm_03_03_apn(void)
72{
73
74 {
75 /* test invalid writes */
76 const uint8_t ref[10] = { 0xAB, 0xAC, 0xAD, 0xAE, 0xAF, 0xAB, 0xAC, 0xAD, 0xAE, 0xAF };
77 uint8_t output[10];
78 int enc_len;
79
80 memcpy(output, ref, ARRAY_SIZE(output));
81 enc_len = gprs_str_to_apn(output, 0, "");
82 OSMO_ASSERT(enc_len == -1);
83 OSMO_ASSERT(memcmp(ref, output, ARRAY_SIZE(ref)) == 0);
84
85 memcpy(output, ref, ARRAY_SIZE(output));
86 enc_len = gprs_str_to_apn(output, 0, "foo");
87 OSMO_ASSERT(enc_len == -1);
88 OSMO_ASSERT(memcmp(ref, output, ARRAY_SIZE(ref)) == 0);
89
90 memcpy(output, ref, ARRAY_SIZE(output));
91 enc_len = gprs_str_to_apn(output, 1, "foo");
92 OSMO_ASSERT(enc_len == -1);
93 OSMO_ASSERT(memcmp(ref + 1, output + 1, ARRAY_SIZE(ref) - 1) == 0);
94
95 memcpy(output, ref, ARRAY_SIZE(output));
96 enc_len = gprs_str_to_apn(output, 2, "foo");
97 OSMO_ASSERT(enc_len == -1);
98 OSMO_ASSERT(memcmp(ref + 2, output + 2, ARRAY_SIZE(ref) - 2) == 0);
99
100 memcpy(output, ref, ARRAY_SIZE(output));
101 enc_len = gprs_str_to_apn(output, 3, "foo");
102 OSMO_ASSERT(enc_len == -1);
103 OSMO_ASSERT(memcmp(ref + 3, output + 3, ARRAY_SIZE(ref) - 3) == 0);
104 }
105
106 {
107 /* single empty label */
108 uint8_t input[] = { 0x0 };
109 const char *output = "";
110 apn_round_trip(input, ARRAY_SIZE(input), output);
111 }
112
113 {
114 /* no label */
115 uint8_t input[] = { };
116 const char *output = "";
117 apn_round_trip(input, ARRAY_SIZE(input), output);
118 }
119
120 {
121 /* single label with A */
122 uint8_t input[] = { 0x1, 65 };
123 const char *output = "A";
124 apn_round_trip(input, ARRAY_SIZE(input), output);
125 OSMO_ASSERT(gprs_apn_to_str(NULL, input, ARRAY_SIZE(input) - 1) == NULL);
126 }
127
128 {
129 uint8_t input[] = { 0x3, 65, 66, 67, 0x2, 90, 122 };
130 const char *output = "ABC.Zz";
131 char tmp[strlen(output) + 1];
132 apn_round_trip(input, ARRAY_SIZE(input), output);
133 OSMO_ASSERT(gprs_apn_to_str(tmp, input, ARRAY_SIZE(input) - 1) == NULL);
134 OSMO_ASSERT(gprs_apn_to_str(tmp, input, ARRAY_SIZE(input) - 2) == NULL);
135 OSMO_ASSERT(gprs_apn_to_str(tmp, input, ARRAY_SIZE(input) - 4) == NULL);
136 OSMO_ASSERT(gprs_apn_to_str(tmp, input, ARRAY_SIZE(input) - 5) == NULL);
137 OSMO_ASSERT(gprs_apn_to_str(tmp, input, ARRAY_SIZE(input) - 6) == NULL);
138 }
139}
140
Holger Hans Peter Freytherfaf1f642011-06-23 17:53:27 -0400141int main(int argc, char **argv)
142{
143 test_8_4_2();
Holger Hans Peter Freytherce1b22e2014-08-04 14:22:13 +0200144 test_gsm_03_03_apn();
Holger Hans Peter Freytherfaf1f642011-06-23 17:53:27 -0400145
146 printf("Done.\n");
147 return EXIT_SUCCESS;
148}