blob: f7fe0b7830137ec8323e15c8290d648aa3cb79b9 [file] [log] [blame]
Neels Hofmeyrd981efa2016-12-08 17:50:03 +01001/* Test Osmocom Authentication Protocol */
2/*
3 * (C) 2016 by sysmocom s.f.m.c. GmbH
4 * All Rights Reserved
5 *
6 * Author: Neels Hofmeyr
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 */
23
24#include <osmocom/core/logging.h>
25#include <osmocom/core/utils.h>
26#include <osmocom/gsm/oap.h>
27
28#include <stdio.h>
29#include <string.h>
30#include <stdbool.h>
31
32static struct msgb *oap_encoded(const struct osmo_oap_message *oap_msg)
33{
34 struct msgb *msgb = msgb_alloc_headroom(1000, 64, __func__);
35 OSMO_ASSERT(msgb);
36 osmo_oap_encode(msgb, oap_msg);
37 return msgb;
38}
39
40static bool encode_decode_makes_same_msg(struct osmo_oap_message *oap_msg)
41{
42 struct osmo_oap_message oap_msg_decoded;
43 struct msgb *msgb;
44 int rc;
45 bool result;
46 memset(&oap_msg_decoded, 0, sizeof(oap_msg_decoded));
47
48 msgb = oap_encoded(oap_msg);
49 printf("encoded message:\n%s\n",
50 osmo_hexdump((void*)msgb_l2(msgb), msgb_l2len(msgb)));
51 rc = osmo_oap_decode(&oap_msg_decoded, msgb_l2(msgb), msgb_l2len(msgb));
52
53 if (rc) {
54 printf("osmo_oap_decode() returned error: %d\n", rc);
55 result = false;
56 goto free_msgb;
57 }
58
59 rc = memcmp(oap_msg, &oap_msg_decoded, sizeof(oap_msg_decoded));
60 if (rc) {
61 printf("decoded message mismatches encoded message\n");
62 printf("original:\n%s\n",
63 osmo_hexdump((void*)oap_msg, sizeof(*oap_msg)));
64 printf("en- and decoded:\n%s\n",
65 osmo_hexdump((void*)&oap_msg_decoded, sizeof(oap_msg_decoded)));
66 result = false;
67 goto free_msgb;
68 }
69
70 printf("ok\n");
71 result = true;
72
73free_msgb:
74 talloc_free(msgb);
75 return result;
76}
77
78static void test_oap_messages_dec_enc(void)
79{
80 printf("Testing OAP messages\n");
81
82 struct osmo_oap_message oap_msg;
83
84#define CLEAR() memset(&oap_msg, 0, sizeof(oap_msg))
85#define CHECK() OSMO_ASSERT(encode_decode_makes_same_msg(&oap_msg))
86
87 printf("- Register Request\n");
88 CLEAR();
89 oap_msg.message_type = OAP_MSGT_REGISTER_REQUEST;
90 oap_msg.client_id = 0x2342;
91 CHECK();
92
93 printf("- Register Error\n");
94 CLEAR();
95 oap_msg.message_type = OAP_MSGT_REGISTER_ERROR;
96 oap_msg.cause = GMM_CAUSE_PROTO_ERR_UNSPEC;
97 CHECK();
98
99 printf("- Register Result\n");
100 CLEAR();
101 oap_msg.message_type = OAP_MSGT_REGISTER_RESULT;
102 CHECK();
103
104 printf("- Challenge Request, no rand, no autn\n");
105 CLEAR();
106 oap_msg.message_type = OAP_MSGT_CHALLENGE_REQUEST;
107 oap_msg.rand_present = 0;
108 oap_msg.autn_present = 0;
109 CHECK();
110
111 printf("- Challenge Request, with rand, no autn\n");
112 CLEAR();
113 oap_msg.message_type = OAP_MSGT_CHALLENGE_REQUEST;
114 osmo_hexparse("0102030405060708090a0b0c0d0e0f10",
115 oap_msg.rand, 16);
116 oap_msg.rand_present = 1;
117 oap_msg.autn_present = 0;
118 CHECK();
119
120 printf("- Challenge Request, no rand, with autn\n");
121 CLEAR();
122 oap_msg.message_type = OAP_MSGT_CHALLENGE_REQUEST;
123 oap_msg.rand_present = 0;
124 osmo_hexparse("cec4e3848a33000086781158ca40f136",
125 oap_msg.autn, 16);
126 oap_msg.autn_present = 1;
127 CHECK();
128
129 printf("- Challenge Request, with rand, with autn\n");
130 CLEAR();
131 oap_msg.message_type = OAP_MSGT_CHALLENGE_REQUEST;
132 osmo_hexparse("0102030405060708090a0b0c0d0e0f10",
133 oap_msg.rand, 16);
134 oap_msg.rand_present = 1;
135 osmo_hexparse("cec4e3848a33000086781158ca40f136",
136 oap_msg.autn, 16);
137 oap_msg.autn_present = 1;
138 CHECK();
139
140 printf("- Challenge Error\n");
141 CLEAR();
142 oap_msg.message_type = OAP_MSGT_CHALLENGE_ERROR;
143 oap_msg.cause = GMM_CAUSE_GSM_AUTH_UNACCEPT;
144 CHECK();
145
146 printf("- Challenge Result\n");
147 CLEAR();
148 oap_msg.message_type = OAP_MSGT_CHALLENGE_RESULT;
149 osmo_hexparse("0102030405060708",
150 oap_msg.xres, 8);
151 oap_msg.xres_present = 1;
152 CHECK();
153
154 printf("- Sync Request\n");
155 CLEAR();
156 oap_msg.message_type = OAP_MSGT_SYNC_REQUEST;
157 osmo_hexparse("102030405060708090a0b0c0d0e0f001",
158 oap_msg.auts, 16);
159 oap_msg.auts_present = 1;
160 CHECK();
161
162 /* Sync Error and Sync Result are not used in OAP */
163}
164
165const struct log_info_cat default_categories[] = {
166};
167
168static struct log_info info = {
169 .cat = default_categories,
170 .num_cat = ARRAY_SIZE(default_categories),
171};
172
173int main(int argc, char **argv)
174{
175 osmo_init_logging(&info);
176
177 test_oap_messages_dec_enc();
178
179 printf("Done.\n");
180 return EXIT_SUCCESS;
181}