Jacob Erlbeck | 8114294 | 2015-11-17 08:42:05 +0100 | [diff] [blame] | 1 | #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 | |
| 11 | static 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 | |
| 36 | static 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 | { |
Max | fcf81b5 | 2017-10-19 15:50:44 +0200 | [diff] [blame] | 94 | /* 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 Erlbeck | 8114294 | 2015-11-17 08:42:05 +0100 | [diff] [blame] | 101 | 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 | |
| 113 | const struct log_info_cat default_categories[] = { |
| 114 | }; |
| 115 | |
| 116 | static struct log_info info = { |
| 117 | .cat = default_categories, |
| 118 | .num_cat = ARRAY_SIZE(default_categories), |
| 119 | }; |
| 120 | |
| 121 | int main(int argc, char **argv) |
| 122 | { |
| 123 | osmo_init_logging(&info); |
| 124 | |
| 125 | test_gsm_03_03_apn(); |
| 126 | |
| 127 | printf("Done.\n"); |
| 128 | return EXIT_SUCCESS; |
| 129 | } |