1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
|
/*
* (C) 2012 by Harald Welte <laforge@gnumonks.org>
* All Rights Reserved
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <osmocom/gsm/protocol/gsm_04_08.h>
#include <osmocom/gsm/gsm48_ie.h>
#include <osmocom/gsm/gsm48.h>
#include <osmocom/gsm/gsm48_arfcn_range_encode.h>
#include <osmocom/gsm/gsm_utils.h>
#include <osmocom/gsm/mncc.h>
#include <osmocom/core/backtrace.h>
#include <osmocom/core/utils.h>
#include <osmocom/core/msgb.h>
static const uint8_t csd_9600_v110_lv[] = { 0x07, 0xa1, 0xb8, 0x89, 0x21, 0x15, 0x63, 0x80 };
static const struct gsm_mncc_bearer_cap bcap_csd_9600_v110 = {
.transfer = GSM48_BCAP_ITCAP_UNR_DIG_INF,
.mode = GSM48_BCAP_TMOD_CIRCUIT,
.coding = GSM48_BCAP_CODING_GSM_STD,
.radio = GSM48_BCAP_RRQ_FR_ONLY,
.speech_ver[0]= -1,
.data = {
.rate_adaption = GSM48_BCAP_RA_V110_X30,
.sig_access = GSM48_BCAP_SA_I440_I450,
.async = 1,
.nr_stop_bits = 1,
.nr_data_bits = 8,
.user_rate = GSM48_BCAP_UR_9600,
.parity = GSM48_BCAP_PAR_NONE,
.interm_rate = GSM48_BCAP_IR_16k,
.transp = GSM48_BCAP_TR_TRANSP,
.modem_type = GSM48_BCAP_MT_NONE,
},
};
static const uint8_t speech_all_lv[] = { 0x06, 0x60, 0x04, 0x02, 0x00, 0x05, 0x81 };
static const struct gsm_mncc_bearer_cap bcap_speech_all = {
.transfer = GSM48_BCAP_ITCAP_SPEECH,
.mode = GSM48_BCAP_TMOD_CIRCUIT,
.coding = GSM48_BCAP_CODING_GSM_STD,
.radio = GSM48_BCAP_RRQ_DUAL_FR,
.speech_ver = {
4, 2, 0, 5, 1, -1,
},
};
struct bcap_test {
const uint8_t *lv;
const struct gsm_mncc_bearer_cap *bc;
const char *name;
};
static const struct bcap_test bcap_tests[] = {
{ csd_9600_v110_lv, &bcap_csd_9600_v110, "CSD 9600/V.110/transparent" },
{ speech_all_lv, &bcap_speech_all, "Speech, all codecs" },
};
static int test_bearer_cap()
{
struct gsm_mncc_bearer_cap bc;
int i, rc;
for (i = 0; i < ARRAY_SIZE(bcap_tests); i++) {
struct msgb *msg = msgb_alloc(100, "test");
int lv_len;
memset(&bc, 0, sizeof(bc));
/* test decoding */
rc = gsm48_decode_bearer_cap(&bc, bcap_tests[i].lv);
if (rc < 0) {
fprintf(stderr, "Error decoding %s\n",
bcap_tests[i].name);
return rc;
}
if (memcmp(&bc, bcap_tests[i].bc, sizeof(bc))) {
fprintf(stderr, "Incorrect decoded result of %s:\n",
bcap_tests[i].name);
fprintf(stderr, " should: %s\n",
osmo_hexdump((uint8_t *) bcap_tests[i].bc, sizeof(bc)));
fprintf(stderr, " is: %s\n",
osmo_hexdump((uint8_t *) &bc, sizeof(bc)));
return -1;
}
/* also test re-encode? */
rc = gsm48_encode_bearer_cap(msg, 1, &bc);
if (rc < 0) {
fprintf(stderr, "Error encoding %s\n",
bcap_tests[i].name);
return rc;
}
lv_len = bcap_tests[i].lv[0]+1;
if (memcmp(msg->data, bcap_tests[i].lv, lv_len)) {
fprintf(stderr, "Incorrect encoded result of %s:\n",
bcap_tests[i].name);
fprintf(stderr, " should: %s\n",
osmo_hexdump(bcap_tests[i].lv, lv_len));
fprintf(stderr, " is: %s\n",
osmo_hexdump(msg->data, msg->len));
return -1;
}
printf("Test `%s' passed\n", bcap_tests[i].name);
msgb_free(msg);
}
return 0;
}
static inline void dump_ra(const struct gprs_ra_id *raid)
{
printf("%s%s\n", osmo_rai_name(raid), raid->mnc_3_digits ? " (3-digit MNC)" : "");
}
static inline void check_ra(const struct gprs_ra_id *raid)
{
struct gsm48_ra_id ra;
struct gprs_ra_id raid0 = {
.mnc = 0,
.mcc = 0,
.lac = 0,
.rac = 0,
};
gsm48_encode_ra(&ra, raid);
printf("Constructed RA:\n");
gsm48_parse_ra(&raid0, (const uint8_t *)&ra);
dump_ra(raid);
printf("MCC+MNC in BCD: %s\n", osmo_hexdump(ra.digits, sizeof(ra.digits)));
dump_ra(&raid0);
printf("RA test...");
if (raid->mnc != raid0.mnc || raid->mcc != raid0.mcc || raid->lac != raid0.lac || raid->rac != raid0.rac
|| (raid->mnc_3_digits || raid->mnc > 99) != raid0.mnc_3_digits)
printf("FAIL\n");
else
printf("passed\n");
}
static inline void check_lai(const struct gprs_ra_id *raid)
{
int rc;
struct gsm48_loc_area_id lai = {};
struct gprs_ra_id decoded = {};
struct gprs_ra_id _laid = *raid;
struct gprs_ra_id *laid = &_laid;
laid->rac = 0;
printf("- gsm48_generate_lai() from "); dump_ra(laid);
gsm48_generate_lai(&lai, laid->mcc, laid->mnc, laid->lac);
printf(" Encoded %s\n", osmo_hexdump((unsigned char*)&lai, sizeof(lai)));
rc = gsm48_decode_lai(&lai, &decoded.mcc, &decoded.mnc, &decoded.lac);
if (rc) {
printf(" gsm48_decode_lai() returned %d --> FAIL\n", rc);
return;
}
printf(" gsm48_decode_lai() gives "); dump_ra(&decoded);
if (decoded.mcc == laid->mcc
&& decoded.mnc == laid->mnc
&& decoded.lac == laid->lac)
printf(" passed\n");
else
printf(" FAIL\n");
}
static inline void dump_lai(const struct osmo_location_area_id *lai)
{
printf("%s%s\n", osmo_lai_name(lai), lai->plmn.mnc_3_digits ? " (3-digit MNC)" : "");
}
static inline void check_lai2(const struct gprs_ra_id *raid)
{
struct gsm48_loc_area_id lai = {};
struct osmo_location_area_id decoded = {};
struct osmo_location_area_id laid = {
.plmn = {
.mcc = raid->mcc,
.mnc = raid->mnc,
.mnc_3_digits = raid->mnc_3_digits,
},
.lac = raid->lac,
};
printf("- gsm48_generate_lai2() from "); dump_lai(&laid);
gsm48_generate_lai2(&lai, &laid);
printf(" Encoded %s\n", osmo_hexdump((unsigned char*)&lai, sizeof(lai)));
gsm48_decode_lai2(&lai, &decoded);
printf(" gsm48_decode_lai2() gives "); dump_lai(&decoded);
if (decoded.plmn.mcc == laid.plmn.mcc
&& decoded.plmn.mnc == laid.plmn.mnc
&& decoded.lac == laid.lac
&& decoded.plmn.mnc_3_digits == (laid.plmn.mnc_3_digits || laid.plmn.mnc > 99))
printf(" passed\n");
else
printf(" FAIL\n");
}
static struct gprs_ra_id test_ra_cap_items[] = {
{
.mcc = 77,
.mnc = 121,
.lac = 666,
.rac = 5,
},
{
.mcc = 84,
.mnc = 98,
.lac = 11,
.rac = 89,
},
{
.mcc = 0,
.mnc = 0,
.lac = 0,
.rac = 0,
.mnc_3_digits = false,
/* expecting 000-00, BCD = 00 f0 00 */
},
{
.mcc = 0,
.mnc = 0,
.lac = 0,
.rac = 0,
.mnc_3_digits = true,
/* expecting 000-000, BCD = 00 00 00 */
},
{
.mcc = 999,
.mnc = 999,
.lac = 65535,
.rac = 255,
},
{
.mcc = 1,
.mnc = 2,
.lac = 23,
.rac = 42,
.mnc_3_digits = false,
/* expecting 001-02, BCD = 00 f1 20 */
},
{
.mcc = 1,
.mnc = 2,
.lac = 23,
.rac = 42,
.mnc_3_digits = true,
/* expecting 001-002, BCD = 00 21 00 */
},
{
.mcc = 12,
.mnc = 34,
.lac = 56,
.rac = 78,
.mnc_3_digits = false,
/* expecting 012-34, BCD = 10 f2 43 */
},
{
.mcc = 12,
.mnc = 34,
.lac = 23,
.rac = 42,
.mnc_3_digits = true,
/* expecting 012-034, BCD = 10 42 30 */
},
{
.mcc = 123,
.mnc = 456,
.lac = 23,
.rac = 42,
.mnc_3_digits = false,
/* expecting 123-456, BCD = 21 63 54 (false flag has no effect) */
},
{
.mcc = 123,
.mnc = 456,
.lac = 23,
.rac = 42,
.mnc_3_digits = true,
/* expecting 123-456, BCD = 21 63 54 (same) */
},
};
static void test_ra_cap(void)
{
int i;
for (i = 0; i < ARRAY_SIZE(test_ra_cap_items); i++)
check_ra(&test_ra_cap_items[i]);
}
static void test_lai_encode_decode(void)
{
int i;
for (i = 0; i < ARRAY_SIZE(test_ra_cap_items); i++) {
check_lai(&test_ra_cap_items[i]);
check_lai2(&test_ra_cap_items[i]);
}
}
static void test_mid_from_tmsi(void)
{
static const uint8_t res[] = { 0x17, 0x05, 0xf4, 0xaa, 0xbb, 0xcc, 0xdd };
uint32_t tmsi = 0xAABBCCDD;
uint8_t buf[3 + sizeof(uint32_t)];
printf("Simple TMSI encoding test....");
memset(&buf, 0xFE, sizeof(buf));
gsm48_generate_mid_from_tmsi(buf, tmsi);
OSMO_ASSERT(memcmp(buf, res, sizeof(res)) == 0);
printf("passed\n");
}
static void test_mid_from_imsi(void)
{
char *imsi = "901700000004620";
uint8_t buf[10], len;
printf("Simple IMSI encoding test....");
len = gsm48_generate_mid_from_imsi(buf, imsi);
printf("passed: [%u] %s\n", len, osmo_hexdump(buf, len));
}
struct test_mid_encode_decode_test {
uint8_t mi_type;
const char *mi_str;
const char *mi_name;
size_t str_size;
const char *expect_mi_tlv_hex;
const char *expect_str;
int expect_rc;
};
static const struct test_mid_encode_decode_test test_mid_encode_decode_tests[] = {
{
.mi_type = GSM_MI_TYPE_IMSI,
.mi_str = "123456789012345",
.mi_name = "IMSI-123456789012345",
.expect_mi_tlv_hex = "17081932547698103254",
},
{
.mi_type = GSM_MI_TYPE_IMSI,
.mi_str = "12345678901234",
.mi_name = "IMSI-12345678901234",
.expect_mi_tlv_hex = "170811325476981032f4",
},
{
.mi_type = GSM_MI_TYPE_IMSI,
.mi_str = "423423",
.mi_name = "IMSI-423423",
.expect_mi_tlv_hex = "1704413224f3",
},
{
.mi_type = GSM_MI_TYPE_IMSI | GSM_MI_ODD,
.mi_str = "423423",
.mi_name = "IMSI-423423",
.expect_mi_tlv_hex = "1704413224f3",
},
{
.mi_type = GSM_MI_TYPE_IMSI,
.mi_str = "4234235",
.mi_name = "IMSI-4234235",
.expect_mi_tlv_hex = "170449322453",
},
{
.mi_type = GSM_MI_TYPE_IMSI,
.mi_str = "4234235",
.mi_name = "IMSI-4234235",
.expect_mi_tlv_hex = "170449322453",
.str_size = 4,
.expect_str = "423",
},
{
.mi_type = GSM_MI_TYPE_IMEI,
.mi_str = "123456789012345",
.mi_name = "IMEI-123456789012345",
.expect_mi_tlv_hex = "17081a32547698103254",
},
{
.mi_type = GSM_MI_TYPE_IMEI,
.mi_str = "98765432109876",
.mi_name = "IMEI-98765432109876",
.expect_mi_tlv_hex = "170892785634129078f6",
},
{
.mi_type = GSM_MI_TYPE_IMEI,
.mi_str = "987654321098765",
.mi_name = "IMEI-987654321098765",
.expect_mi_tlv_hex = "17089a78563412907856",
},
{
.mi_type = GSM_MI_TYPE_IMEISV,
.mi_str = "9876543210987654",
.mi_name = "IMEI-SV-9876543210987654",
.expect_mi_tlv_hex = "17099378563412907856f4",
},
{
.mi_type = GSM_MI_TYPE_IMEISV,
.mi_str = "9876543210987654",
.mi_name = "IMEI-SV-9876543210987654",
.expect_mi_tlv_hex = "17099378563412907856f4",
.str_size = 17,
.expect_str = "9876543210987654",
},
{
/* gsm48 treats TMSI as decimal string */
.mi_type = GSM_MI_TYPE_TMSI,
.mi_str = "305419896", /* 0x12345678 as decimal */
.mi_name = "TMSI-0x12345678",
.expect_mi_tlv_hex = "1705f412345678",
.expect_rc = 9, /* exception: gsm48_mi_to_string() for TMSI returns strlen(), not bytes! */
},
{
.mi_type = GSM_MI_TYPE_TMSI,
.mi_str = "12648430", /* 0xc0ffee as decimal */
.mi_name = "TMSI-0x00C0FFEE",
.expect_mi_tlv_hex = "1705f400c0ffee",
.expect_rc = 8, /* exception: gsm48_mi_to_string() for TMSI returns strlen(), not bytes! */
},
{
.mi_type = GSM_MI_TYPE_TMSI,
.mi_str = "0",
.mi_name = "TMSI-0x00000000",
.expect_mi_tlv_hex = "1705f400000000",
.expect_rc = 1, /* exception: gsm48_mi_to_string() for TMSI returns strlen(), not bytes! */
},
{
/* gsm48 treats TMSI as decimal string */
.mi_type = GSM_MI_TYPE_TMSI,
.mi_str = "305419896", /* 0x12345678 as decimal */
.mi_name = "TMSI-0x12345678",
.expect_mi_tlv_hex = "1705f412345678",
.str_size = 5,
.expect_str = "3054",
.expect_rc = 9, /* exception: gsm48_mi_to_string() for TMSI returns would-be strlen() like snprintf()! */
},
{
.mi_type = GSM_MI_TYPE_NONE,
.mi_str = "123",
.mi_name = "unknown",
.expect_mi_tlv_hex = "17021832", /* encoding invalid MI type */
.expect_str = "",
},
{
.mi_type = GSM_MI_TYPE_NONE,
.mi_str = "1234",
.mi_name = "unknown",
.expect_mi_tlv_hex = "17031032f4", /* encoding invalid MI type */
.expect_str = "",
},
{
.mi_type = GSM_MI_ODD,
.mi_str = "1234",
.mi_name = "unknown",
.expect_mi_tlv_hex = "17031032f4", /* encoding invalid MI type */
.expect_str = "",
},
};
static void test_mid_encode_decode(void)
{
int i;
printf("\nTesting Mobile Identity conversions\n");
for (i = 0; i < ARRAY_SIZE(test_mid_encode_decode_tests); i++) {
const struct test_mid_encode_decode_test *t = &test_mid_encode_decode_tests[i];
uint8_t tlv_buf[64];
uint8_t *mi_buf;
int tlv_len;
int mi_len;
const char *tlv_hex;
char str[64] = {};
size_t str_size = t->str_size ? : sizeof(str);
const char *expect_str = t->expect_str ? : t->mi_str;
int expect_rc = t->expect_rc ? : strlen(expect_str)+1;
int rc;
int str_len;
printf("- %s %s\n", gsm48_mi_type_name(t->mi_type), t->mi_str);
if (t->mi_type == GSM_MI_TYPE_TMSI)
tlv_len = gsm48_generate_mid_from_tmsi(tlv_buf, (uint32_t)atoll(t->mi_str));
else
tlv_len = gsm48_generate_mid(tlv_buf, t->mi_str, t->mi_type);
tlv_hex = osmo_hexdump_nospc(tlv_buf, tlv_len);
printf(" -> MI-TLV-hex='%s'\n", tlv_hex);
if (t->expect_mi_tlv_hex && strcmp(tlv_hex, t->expect_mi_tlv_hex)) {
printf(" ERROR: expected '%s'\n", t->expect_mi_tlv_hex);
}
/* skip the GSM48_IE_MOBILE_ID tag and length */
mi_buf = tlv_buf + 2;
mi_len = tlv_len - 2;
rc = gsm48_mi_to_string(str, str_size, mi_buf, mi_len);
printf(" -> MI-str=%s rc=%d\n", osmo_quote_str(str, -1), rc);
if (strcmp(str, expect_str))
printf(" ERROR: expected MI-str=%s\n", osmo_quote_str(expect_str, -1));
if (rc != expect_rc)
printf(" ERROR: expected rc=%d\n", expect_rc);
if (t->mi_name) {
const char *mi_name = osmo_mi_name(mi_buf, mi_len);
printf(" -> MI-name=%s\n", osmo_quote_str(mi_name, -1));
if (strcmp(mi_name, t->mi_name))
printf(" ERROR: expected MI-name=%s\n", osmo_quote_str(t->mi_name, -1));
}
/* Now make sure the resulting string is always '\0' terminated.
* The above started out with a zeroed buffer, now repeat with a tainted one. */
str_len = strlen(str);
str[str_len] = '!';
gsm48_mi_to_string(str, str_size, mi_buf, mi_len);
if (strlen(str) != str_len)
printf(" ERROR: resulting string is not explicitly nul terminated\n");
}
}
static const uint8_t test_mid_decode_zero_length_types[] = { GSM_MI_TYPE_IMSI, GSM_MI_TYPE_TMSI, GSM_MI_TYPE_NONE };
static void test_mid_decode_zero_length(void)
{
int odd;
uint8_t valid_mi[64];
int valid_mi_len;
printf("\nDecoding zero length Mobile Identities\n");
/* IMSI = 123456789012345 */
valid_mi_len = osmo_hexparse("1932547698103254", valid_mi, sizeof(valid_mi));
for (odd = 0; odd <= 1; odd++) {
int i;
for (i = 0; i < ARRAY_SIZE(test_mid_decode_zero_length_types); i++) {
uint8_t mi_type = test_mid_decode_zero_length_types[i] | (odd ? GSM_MI_ODD : 0);
char str[8] = {};
int rc;
printf("- MI type: %s%s\n", gsm48_mi_type_name(mi_type & GSM_MI_TYPE_MASK),
odd ? " | GSM_MI_ODD":"");
valid_mi[0] = (valid_mi[0] & 0xf0) | mi_type;
printf(" - writing to zero-length string:\n");
memset(str, '!', sizeof(str) - 1);
rc = gsm48_mi_to_string(str, 0, valid_mi, valid_mi_len);
printf(" rc=%d\n", rc);
if (str[0] == '!')
printf(" nothing written\n");
else
printf(" ERROR: Wrote to invalid memory!\n");
printf(" - writing to 1-byte-length string:\n");
memset(str, '!', sizeof(str) - 1);
rc = gsm48_mi_to_string(str, 1, valid_mi, valid_mi_len);
printf(" rc=%d\n", rc);
if (str[0] == '\0')
printf(" returned empty string\n");
else if (str[0] == '!')
printf(" ERROR: nothing written, expected nul-terminated empty string\n");
else
printf(" ERROR: Wrote unexpected string %s\n", osmo_quote_str(str, 5));
if (str[1] != '!')
printf(" ERROR: Wrote to invalid memory!\n");
printf(" - decode zero-length mi:\n");
memset(str, '!', sizeof(str) - 1);
rc = gsm48_mi_to_string(str, sizeof(str), valid_mi, 0);
printf(" rc=%d\n", rc);
if (str[0] == '\0')
printf(" returned empty string\n");
else if (str[0] == '!')
printf(" ERROR: nothing written, expected nul-terminated empty string\n");
else
printf(" ERROR: expected empty string, got output string: %s\n", osmo_quote_str(str, -1));
}
}
printf("\n");
}
static const struct bcd_number_test {
/* Human-readable test name */
const char *test_name;
/* To be encoded number in ASCII */
const char *enc_ascii;
/* Expected encoding result in HEX */
const char *enc_hex;
/* Optional header length (LHV) */
uint8_t enc_h_len;
/* Expected return code */
int enc_rc;
/* To be decoded buffer in HEX */
const char *dec_hex;
/* Expected decoding result in ASCII */
const char *dec_ascii;
/* Optional header length (LHV) */
uint8_t dec_h_len;
/* Expected return code */
int dec_rc;
/* Encoding buffer limit (0 means unlimited) */
size_t enc_buf_lim;
/* Decoding buffer limit (0 means unlimited) */
size_t dec_buf_lim;
} bcd_number_test_set[] = {
{
.test_name = "regular 9-digit MSISDN",
/* Encoding test */
.enc_ascii = "123456789",
.enc_hex = "0521436587f9",
.enc_rc = 6,
/* Decoding test */
.dec_hex = "0521436587f9",
.dec_ascii = "123456789",
},
{
.test_name = "regular 6-digit MSISDN with optional header (LHV)",
/* Encoding test */
.enc_ascii = "123456",
.enc_hex = "07ffffffff214365",
.enc_h_len = 4, /* LHV */
.enc_rc = 4 + 4,
/* Decoding test */
.dec_hex = "07deadbeef214365",
.dec_ascii = "123456",
.dec_h_len = 4, /* LHV */
},
{
.test_name = "long 15-digit (maximum) MSISDN",
/* Encoding test */
.enc_ascii = "123456789012345",
.enc_hex = "0821436587092143f5",
.enc_rc = 9,
/* Decoding test */
.dec_hex = "0821436587092143f5",
.dec_ascii = "123456789012345",
},
{
.test_name = "long 15-digit (maximum) MSISDN, limited buffer",
/* Encoding test */
.enc_ascii = "123456789012345",
.enc_hex = "0821436587092143f5",
.enc_rc = 9,
/* Decoding test */
.dec_hex = "0821436587092143f5",
.dec_ascii = "123456789012345",
/* Buffer length limitations */
.dec_buf_lim = 15 + 1,
.enc_buf_lim = 9,
},
{
.test_name = "to be truncated 20-digit MSISDN",
/* Encoding test (not enough room in buffer) */
.enc_ascii = "12345678901234567890",
.enc_hex = "", /* nothing */
.enc_rc = -EIO,
/* Decoding test (one 5 digits do not fit) */
.dec_hex = "0a21436587092143658709",
.dec_ascii = "123456789012345",
.dec_rc = -ENOSPC,
/* Buffer length limitations */
.dec_buf_lim = 15 + 1, /* 5 digits less */
.enc_buf_lim = 9,
},
{
.test_name = "LV incorrect length",
.dec_hex = "05214365", /* should be 0x03 */
.dec_ascii = "(none)",
.dec_rc = -EINVAL,
},
{
.test_name = "empty input buffer",
/* Encoding test */
.enc_ascii = "",
.enc_hex = "00",
.enc_rc = 1,
/* Decoding test */
.dec_hex = "",
.dec_ascii = "(none)",
.dec_rc = -EIO,
},
{
.test_name = "decoding buffer is one byte too small (OS#4049)",
/* Decoding test */
.dec_hex = "022143", /* "1234" */
.dec_ascii = "123", /* '4' was truncated */
.dec_rc = -ENOSPC,
/* Buffer length limitations */
.dec_buf_lim = 4,
},
};
static void test_bcd_number_encode_decode()
{
const struct bcd_number_test *test;
uint8_t buf_enc[0xff] = { 0xff };
char buf_dec[0xff] = { 0xff };
size_t buf_len, i;
int rc;
printf("BSD number encoding / decoding test\n");
for (i = 0; i < ARRAY_SIZE(bcd_number_test_set); i++) {
test = &bcd_number_test_set[i];
printf("- Running test: %s\n", test->test_name);
if (test->enc_ascii) {
if (test->enc_buf_lim)
buf_len = test->enc_buf_lim;
else
buf_len = sizeof(buf_enc);
printf(" - Encoding ASCII (buffer limit=%zu) '%s'...\n",
test->enc_buf_lim, test->enc_ascii);
rc = gsm48_encode_bcd_number(buf_enc, buf_len,
test->enc_h_len, test->enc_ascii);
printf(" - Expected: (rc=%d) '%s'\n",
test->enc_rc, test->enc_hex);
printf(" - Actual: (rc=%d) '%s'\n",
rc, osmo_hexdump_nospc(buf_enc, rc >= 0 ? rc : 0));
}
if (test->dec_hex) {
/* Parse a HEX string */
rc = osmo_hexparse(test->dec_hex, buf_enc, sizeof(buf_enc));
OSMO_ASSERT(rc >= 0);
if (test->dec_buf_lim)
buf_len = test->dec_buf_lim;
else
buf_len = sizeof(buf_dec);
printf(" - Decoding HEX (buffer limit=%zu) '%s'...\n",
test->dec_buf_lim, test->dec_hex);
rc = gsm48_decode_bcd_number2(buf_dec, buf_len,
buf_enc, rc, test->dec_h_len);
printf(" - Expected: (rc=%d) '%s'\n",
test->dec_rc, test->dec_ascii);
printf(" - Actual: (rc=%d) '%s'\n",
rc, (rc == 0 || rc == -ENOSPC) ? buf_dec : "(none)");
}
/* Poison buffers between the test iterations */
memset(buf_enc, 0xff, sizeof(buf_enc));
memset(buf_dec, 0xff, sizeof(buf_dec));
}
printf("\n");
}
struct {
int range;
int arfcns_num;
int arfcns[OSMO_GSM48_RANGE_ENC_MAX_ARFCNS];
} arfcn_test_ranges[] = {
{OSMO_GSM48_ARFCN_RANGE_512, 12,
{ 1, 12, 31, 51, 57, 91, 97, 98, 113, 117, 120, 125 }},
{OSMO_GSM48_ARFCN_RANGE_512, 17,
{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17 }},
{OSMO_GSM48_ARFCN_RANGE_512, 18,
{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 }},
{OSMO_GSM48_ARFCN_RANGE_512, 18,
{ 1,
|