Holger Hans Peter Freyther | 23e8000 | 2012-11-22 14:59:46 +0100 | [diff] [blame] | 1 | /* |
| 2 | * (C) 2012 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 3 | * All Rights Reserved |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU Affero General Public License as published by |
| 7 | * the Free Software Foundation; either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU Affero General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | * |
| 18 | */ |
| 19 | |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | |
| 23 | #include <osmocom/core/application.h> |
| 24 | #include <osmocom/core/utils.h> |
| 25 | |
| 26 | #include <openbsc/gsm_data.h> |
| 27 | #include <openbsc/abis_nm.h> |
| 28 | #include <openbsc/debug.h> |
| 29 | |
| 30 | static const uint8_t simple_config[] = { |
| 31 | /*0, 13, */ |
| 32 | 66, 18, 0, 3, 1, 2, 3, 19, 0, 3, 3, 4, 5, |
| 33 | }; |
| 34 | |
| 35 | static const uint8_t dual_config[] = { |
| 36 | /*0, 26, */ |
| 37 | 66, 18, 0, 3, 1, 2, 3, 19, 0, 3, 3, 4, 5, |
| 38 | 66, 18, 0, 3, 9, 7, 5, 19, 0, 3, 6, 7, 8, |
| 39 | }; |
| 40 | |
Holger Hans Peter Freyther | 3cb4409 | 2012-11-22 19:04:10 +0100 | [diff] [blame] | 41 | static const uint8_t load_config[] = { |
| 42 | 0x42, 0x12, 0x00, 0x08, 0x31, 0x36, 0x38, 0x64, |
| 43 | 0x34, 0x37, 0x32, 0x00, 0x13, 0x00, 0x0b, 0x76, |
| 44 | 0x32, 0x30, 0x30, 0x62, 0x31, 0x34, 0x33, 0x64, |
| 45 | 0x30, 0x00, 0x42, 0x12, 0x00, 0x08, 0x31, 0x36, |
| 46 | 0x38, 0x64, 0x34, 0x37, 0x32, 0x00, 0x13, 0x00, |
| 47 | 0x0b, 0x76, 0x32, 0x30, 0x30, 0x62, 0x31, 0x34, |
| 48 | 0x33, 0x64, 0x31, 0x00 |
| 49 | }; |
| 50 | |
Holger Hans Peter Freyther | 23e8000 | 2012-11-22 14:59:46 +0100 | [diff] [blame] | 51 | static void test_simple_sw_config(void) |
| 52 | { |
| 53 | struct abis_nm_sw_descr descr[1]; |
| 54 | int rc; |
| 55 | |
| 56 | rc = abis_nm_parse_sw_config(simple_config, ARRAY_SIZE(simple_config), |
| 57 | &descr[0], ARRAY_SIZE(descr)); |
| 58 | if (rc != 1) { |
| 59 | printf("FAILED to parse the File Id/File version\n"); |
| 60 | abort(); |
| 61 | } |
| 62 | |
| 63 | if (descr[0].len != 13) { |
Holger Hans Peter Freyther | 844f4b8 | 2015-04-23 11:55:23 -0400 | [diff] [blame] | 64 | printf("WRONG SIZE: %zu\n", descr[0].len); |
Holger Hans Peter Freyther | 23e8000 | 2012-11-22 14:59:46 +0100 | [diff] [blame] | 65 | abort(); |
| 66 | } |
| 67 | |
Alexander Huemer | e6e4f0d | 2015-11-07 14:00:52 +0100 | [diff] [blame] | 68 | printf("Start: %td len: %zu\n", descr[0].start - simple_config, descr[0].len); |
Holger Hans Peter Freyther | 23e8000 | 2012-11-22 14:59:46 +0100 | [diff] [blame] | 69 | printf("file_id: %s\n", osmo_hexdump(descr[0].file_id, descr[0].file_id_len)); |
| 70 | printf("file_ver: %s\n", osmo_hexdump(descr[0].file_ver, descr[0].file_ver_len)); |
| 71 | } |
| 72 | |
| 73 | static void test_simple_sw_short(void) |
| 74 | { |
| 75 | struct abis_nm_sw_descr descr[1]; |
| 76 | int i; |
| 77 | |
| 78 | for (i = 1; i < ARRAY_SIZE(simple_config); ++i) { |
| 79 | int rc = abis_nm_parse_sw_config(simple_config, |
| 80 | ARRAY_SIZE(simple_config) - i, &descr[0], |
| 81 | ARRAY_SIZE(descr)); |
| 82 | if (rc >= 1) { |
| 83 | printf("SHOULD not have parsed: %d\n", rc); |
| 84 | abort(); |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | static void test_dual_sw_config(void) |
| 90 | { |
| 91 | struct abis_nm_sw_descr descr[2]; |
| 92 | int rc; |
| 93 | |
| 94 | rc = abis_nm_parse_sw_config(dual_config, ARRAY_SIZE(dual_config), |
| 95 | &descr[0], ARRAY_SIZE(descr)); |
| 96 | if (rc != 2) { |
| 97 | printf("FAILED to parse the File Id/File version\n"); |
| 98 | abort(); |
| 99 | } |
| 100 | |
| 101 | if (descr[0].len != 13) { |
Holger Hans Peter Freyther | 844f4b8 | 2015-04-23 11:55:23 -0400 | [diff] [blame] | 102 | printf("WRONG SIZE0: %zu\n", descr[0].len); |
Holger Hans Peter Freyther | 23e8000 | 2012-11-22 14:59:46 +0100 | [diff] [blame] | 103 | abort(); |
| 104 | } |
| 105 | |
| 106 | if (descr[1].len != 13) { |
Holger Hans Peter Freyther | 844f4b8 | 2015-04-23 11:55:23 -0400 | [diff] [blame] | 107 | printf("WRONG SIZE1: %zu\n", descr[1].len); |
Holger Hans Peter Freyther | 23e8000 | 2012-11-22 14:59:46 +0100 | [diff] [blame] | 108 | abort(); |
| 109 | } |
| 110 | |
Alexander Huemer | e6e4f0d | 2015-11-07 14:00:52 +0100 | [diff] [blame] | 111 | printf("Start: %td len: %zu\n", descr[0].start - dual_config, descr[0].len); |
Holger Hans Peter Freyther | 23e8000 | 2012-11-22 14:59:46 +0100 | [diff] [blame] | 112 | printf("file_id: %s\n", osmo_hexdump(descr[0].file_id, descr[0].file_id_len)); |
| 113 | printf("file_ver: %s\n", osmo_hexdump(descr[0].file_ver, descr[0].file_ver_len)); |
| 114 | |
Alexander Huemer | e6e4f0d | 2015-11-07 14:00:52 +0100 | [diff] [blame] | 115 | printf("Start: %td len: %zu\n", descr[1].start - dual_config, descr[1].len); |
Holger Hans Peter Freyther | 23e8000 | 2012-11-22 14:59:46 +0100 | [diff] [blame] | 116 | printf("file_id: %s\n", osmo_hexdump(descr[1].file_id, descr[1].file_id_len)); |
| 117 | printf("file_ver: %s\n", osmo_hexdump(descr[1].file_ver, descr[1].file_ver_len)); |
| 118 | } |
| 119 | |
Holger Hans Peter Freyther | 3cb4409 | 2012-11-22 19:04:10 +0100 | [diff] [blame] | 120 | static void test_sw_selection(void) |
| 121 | { |
| 122 | struct abis_nm_sw_descr descr[8], tmp; |
| 123 | int rc, pos; |
| 124 | |
| 125 | rc = abis_nm_parse_sw_config(load_config, ARRAY_SIZE(load_config), |
| 126 | &descr[0], ARRAY_SIZE(descr)); |
| 127 | if (rc != 2) { |
| 128 | printf("FAILED to parse the File Id/File version\n"); |
| 129 | abort(); |
| 130 | } |
| 131 | |
Alexander Huemer | e6e4f0d | 2015-11-07 14:00:52 +0100 | [diff] [blame] | 132 | printf("Start: %td len: %zu\n", descr[0].start - load_config, descr[0].len); |
Holger Hans Peter Freyther | 3cb4409 | 2012-11-22 19:04:10 +0100 | [diff] [blame] | 133 | printf("file_id: %s\n", osmo_hexdump(descr[0].file_id, descr[0].file_id_len)); |
| 134 | printf("file_ver: %s\n", osmo_hexdump(descr[0].file_ver, descr[0].file_ver_len)); |
| 135 | |
Alexander Huemer | e6e4f0d | 2015-11-07 14:00:52 +0100 | [diff] [blame] | 136 | printf("Start: %td len: %zu\n", descr[1].start - load_config, descr[1].len); |
Holger Hans Peter Freyther | 3cb4409 | 2012-11-22 19:04:10 +0100 | [diff] [blame] | 137 | printf("file_id: %s\n", osmo_hexdump(descr[1].file_id, descr[1].file_id_len)); |
| 138 | printf("file_ver: %s\n", osmo_hexdump(descr[1].file_ver, descr[1].file_ver_len)); |
| 139 | |
| 140 | /* start */ |
| 141 | pos = abis_nm_select_newest_sw(descr, rc); |
| 142 | if (pos != 1) { |
| 143 | printf("Selected the wrong version: %d\n", pos); |
| 144 | abort(); |
| 145 | } |
| 146 | printf("SELECTED: %d\n", pos); |
| 147 | |
| 148 | /* shuffle */ |
| 149 | tmp = descr[0]; |
| 150 | descr[0] = descr[1]; |
| 151 | descr[1] = tmp; |
| 152 | pos = abis_nm_select_newest_sw(descr, rc); |
| 153 | if (pos != 0) { |
| 154 | printf("Selected the wrong version: %d\n", pos); |
| 155 | abort(); |
| 156 | } |
| 157 | printf("SELECTED: %d\n", pos); |
| 158 | } |
| 159 | |
Holger Hans Peter Freyther | 23e8000 | 2012-11-22 14:59:46 +0100 | [diff] [blame] | 160 | int main(int argc, char **argv) |
| 161 | { |
| 162 | osmo_init_logging(&log_info); |
| 163 | test_simple_sw_config(); |
| 164 | test_simple_sw_short(); |
| 165 | test_dual_sw_config(); |
Holger Hans Peter Freyther | 3cb4409 | 2012-11-22 19:04:10 +0100 | [diff] [blame] | 166 | test_sw_selection(); |
Holger Hans Peter Freyther | 23e8000 | 2012-11-22 14:59:46 +0100 | [diff] [blame] | 167 | |
| 168 | return EXIT_SUCCESS; |
| 169 | } |