blob: 32676ca3f1b89126dced8633d79519eca4753338 [file] [log] [blame]
Neels Hofmeyrd981efa2016-12-08 17:50:03 +01001/* Test Osmocom Authentication Protocol */
2/*
Harald Weltee08da972017-11-13 01:00:26 +09003 * (C) 2016 by sysmocom - s.f.m.c. GmbH
4 * Author: Neels Hofmeyr
Neels Hofmeyrd981efa2016-12-08 17:50:03 +01005 * All Rights Reserved
6 *
Neels Hofmeyrd981efa2016-12-08 17:50:03 +01007 *
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
Max2f0b0c92017-01-12 16:47:13 +010024#include <osmocom/core/application.h>
Neels Hofmeyrd981efa2016-12-08 17:50:03 +010025#include <osmocom/core/logging.h>
26#include <osmocom/core/utils.h>
27#include <osmocom/gsm/oap.h>
28
29#include <stdio.h>
30#include <string.h>
31#include <stdbool.h>
32
33static struct msgb *oap_encoded(const struct osmo_oap_message *oap_msg)
34{
35 struct msgb *msgb = msgb_alloc_headroom(1000, 64, __func__);
36 OSMO_ASSERT(msgb);
37 osmo_oap_encode(msgb, oap_msg);
38 return msgb;
39}
40
41static bool encode_decode_makes_same_msg(struct osmo_oap_message *oap_msg)
42{
43 struct osmo_oap_message oap_msg_decoded;
44 struct msgb *msgb;
45 int rc;
46 bool result;
47 memset(&oap_msg_decoded, 0, sizeof(oap_msg_decoded));
48
49 msgb = oap_encoded(oap_msg);
50 printf("encoded message:\n%s\n",
51 osmo_hexdump((void*)msgb_l2(msgb), msgb_l2len(msgb)));
52 rc = osmo_oap_decode(&oap_msg_decoded, msgb_l2(msgb), msgb_l2len(msgb));
53
54 if (rc) {
55 printf("osmo_oap_decode() returned error: %d\n", rc);
56 result = false;
57 goto free_msgb;
58 }
59
60 rc = memcmp(oap_msg, &oap_msg_decoded, sizeof(oap_msg_decoded));
61 if (rc) {
62 printf("decoded message mismatches encoded message\n");
63 printf("original:\n%s\n",
64 osmo_hexdump((void*)oap_msg, sizeof(*oap_msg)));
65 printf("en- and decoded:\n%s\n",
66 osmo_hexdump((void*)&oap_msg_decoded, sizeof(oap_msg_decoded)));
67 result = false;
68 goto free_msgb;
69 }
70
71 printf("ok\n");
72 result = true;
73
74free_msgb:
75 talloc_free(msgb);
76 return result;
77}
78
79static void test_oap_messages_dec_enc(void)
80{
81 printf("Testing OAP messages\n");
82
83 struct osmo_oap_message oap_msg;
84
85#define CLEAR() memset(&oap_msg, 0, sizeof(oap_msg))
86#define CHECK() OSMO_ASSERT(encode_decode_makes_same_msg(&oap_msg))
87
88 printf("- Register Request\n");
89 CLEAR();
90 oap_msg.message_type = OAP_MSGT_REGISTER_REQUEST;
91 oap_msg.client_id = 0x2342;
92 CHECK();
93
94 printf("- Register Error\n");
95 CLEAR();
96 oap_msg.message_type = OAP_MSGT_REGISTER_ERROR;
97 oap_msg.cause = GMM_CAUSE_PROTO_ERR_UNSPEC;
98 CHECK();
99
100 printf("- Register Result\n");
101 CLEAR();
102 oap_msg.message_type = OAP_MSGT_REGISTER_RESULT;
103 CHECK();
104
105 printf("- Challenge Request, no rand, no autn\n");
106 CLEAR();
107 oap_msg.message_type = OAP_MSGT_CHALLENGE_REQUEST;
108 oap_msg.rand_present = 0;
109 oap_msg.autn_present = 0;
110 CHECK();
111
112 printf("- Challenge Request, with rand, no autn\n");
113 CLEAR();
114 oap_msg.message_type = OAP_MSGT_CHALLENGE_REQUEST;
115 osmo_hexparse("0102030405060708090a0b0c0d0e0f10",
116 oap_msg.rand, 16);
117 oap_msg.rand_present = 1;
118 oap_msg.autn_present = 0;
119 CHECK();
120
121 printf("- Challenge Request, no rand, with autn\n");
122 CLEAR();
123 oap_msg.message_type = OAP_MSGT_CHALLENGE_REQUEST;
124 oap_msg.rand_present = 0;
125 osmo_hexparse("cec4e3848a33000086781158ca40f136",
126 oap_msg.autn, 16);
127 oap_msg.autn_present = 1;
128 CHECK();
129
130 printf("- Challenge Request, with rand, with autn\n");
131 CLEAR();
132 oap_msg.message_type = OAP_MSGT_CHALLENGE_REQUEST;
133 osmo_hexparse("0102030405060708090a0b0c0d0e0f10",
134 oap_msg.rand, 16);
135 oap_msg.rand_present = 1;
136 osmo_hexparse("cec4e3848a33000086781158ca40f136",
137 oap_msg.autn, 16);
138 oap_msg.autn_present = 1;
139 CHECK();
140
141 printf("- Challenge Error\n");
142 CLEAR();
143 oap_msg.message_type = OAP_MSGT_CHALLENGE_ERROR;
144 oap_msg.cause = GMM_CAUSE_GSM_AUTH_UNACCEPT;
145 CHECK();
146
147 printf("- Challenge Result\n");
148 CLEAR();
149 oap_msg.message_type = OAP_MSGT_CHALLENGE_RESULT;
150 osmo_hexparse("0102030405060708",
151 oap_msg.xres, 8);
152 oap_msg.xres_present = 1;
153 CHECK();
154
155 printf("- Sync Request\n");
156 CLEAR();
157 oap_msg.message_type = OAP_MSGT_SYNC_REQUEST;
Neels Hofmeyr8352d312017-02-02 20:05:14 +0100158 osmo_hexparse("102030405060708090a0b0c0d0e0",
159 oap_msg.auts, 14);
Neels Hofmeyrd981efa2016-12-08 17:50:03 +0100160 oap_msg.auts_present = 1;
161 CHECK();
162
163 /* Sync Error and Sync Result are not used in OAP */
164}
165
166const struct log_info_cat default_categories[] = {
167};
168
169static struct log_info info = {
170 .cat = default_categories,
171 .num_cat = ARRAY_SIZE(default_categories),
172};
173
174int main(int argc, char **argv)
175{
Neels Hofmeyra829b452018-04-05 03:02:35 +0200176 void *ctx = talloc_named_const(NULL, 0, "oap_test");
177 osmo_init_logging2(ctx, &info);
178 msgb_talloc_ctx_init(ctx, 0);
Neels Hofmeyrd981efa2016-12-08 17:50:03 +0100179
180 test_oap_messages_dec_enc();
181
182 printf("Done.\n");
183 return EXIT_SUCCESS;
184}