Benny Prijono | fb9d987 | 2007-03-21 22:05:58 +0000 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
| 3 | * Copyright (C) 2003-2007 Benny Prijono <benny@prijono.org> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 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 General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | */ |
| 19 | #include "test.h" |
| 20 | |
Benny Prijono | 7e0d52c | 2007-03-26 13:25:07 +0000 | [diff] [blame] | 21 | #define THIS_FILE "ice_test.c" |
Benny Prijono | fb9d987 | 2007-03-21 22:05:58 +0000 | [diff] [blame] | 22 | |
| 23 | |
| 24 | struct ice_data |
| 25 | { |
Benny Prijono | 2532b9d | 2007-03-22 01:16:37 +0000 | [diff] [blame] | 26 | const char *obj_name; |
Benny Prijono | fb9d987 | 2007-03-21 22:05:58 +0000 | [diff] [blame] | 27 | pj_bool_t complete; |
| 28 | pj_status_t err_code; |
| 29 | unsigned rx_rtp_cnt; |
| 30 | unsigned rx_rtcp_cnt; |
Benny Prijono | 2532b9d | 2007-03-22 01:16:37 +0000 | [diff] [blame] | 31 | |
Benny Prijono | fd7f147 | 2007-03-22 11:59:03 +0000 | [diff] [blame] | 32 | unsigned rx_rtp_count; |
| 33 | char last_rx_rtp_data[32]; |
| 34 | unsigned rx_rtcp_count; |
| 35 | char last_rx_rtcp_data[32]; |
Benny Prijono | fb9d987 | 2007-03-21 22:05:58 +0000 | [diff] [blame] | 36 | }; |
| 37 | |
| 38 | static pj_stun_config stun_cfg; |
| 39 | |
Benny Prijono | a6bd758 | 2007-03-28 15:49:48 +0000 | [diff] [blame^] | 40 | static void on_ice_complete(pj_ice_strans *icest, |
Benny Prijono | fb9d987 | 2007-03-21 22:05:58 +0000 | [diff] [blame] | 41 | pj_status_t status) |
| 42 | { |
Benny Prijono | ff49ab7 | 2007-03-22 21:00:53 +0000 | [diff] [blame] | 43 | struct ice_data *id = (struct ice_data*) icest->user_data; |
Benny Prijono | fb9d987 | 2007-03-21 22:05:58 +0000 | [diff] [blame] | 44 | id->complete = PJ_TRUE; |
| 45 | id->err_code = status; |
Benny Prijono | 7e0d52c | 2007-03-26 13:25:07 +0000 | [diff] [blame] | 46 | PJ_LOG(3,(THIS_FILE, " ICE %s complete %s", id->obj_name, |
Benny Prijono | 2532b9d | 2007-03-22 01:16:37 +0000 | [diff] [blame] | 47 | (status==PJ_SUCCESS ? "successfully" : "with failure"))); |
Benny Prijono | fb9d987 | 2007-03-21 22:05:58 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | |
Benny Prijono | a6bd758 | 2007-03-28 15:49:48 +0000 | [diff] [blame^] | 51 | static void on_rx_data(pj_ice_strans *icest, unsigned comp_id, |
Benny Prijono | fb9d987 | 2007-03-21 22:05:58 +0000 | [diff] [blame] | 52 | void *pkt, pj_size_t size, |
| 53 | const pj_sockaddr_t *src_addr, |
| 54 | unsigned src_addr_len) |
| 55 | { |
Benny Prijono | ff49ab7 | 2007-03-22 21:00:53 +0000 | [diff] [blame] | 56 | struct ice_data *id = (struct ice_data*) icest->user_data; |
Benny Prijono | 2532b9d | 2007-03-22 01:16:37 +0000 | [diff] [blame] | 57 | |
Benny Prijono | ff49ab7 | 2007-03-22 21:00:53 +0000 | [diff] [blame] | 58 | if (comp_id == 1) { |
| 59 | id->rx_rtp_cnt++; |
| 60 | pj_memcpy(id->last_rx_rtp_data, pkt, size); |
| 61 | id->last_rx_rtp_data[size] = '\0'; |
| 62 | } else if (comp_id == 2) { |
| 63 | id->rx_rtcp_cnt++; |
| 64 | pj_memcpy(id->last_rx_rtcp_data, pkt, size); |
| 65 | id->last_rx_rtcp_data[size] = '\0'; |
| 66 | } else { |
| 67 | pj_assert(!"Invalid component ID"); |
| 68 | } |
Benny Prijono | 2532b9d | 2007-03-22 01:16:37 +0000 | [diff] [blame] | 69 | |
| 70 | PJ_UNUSED_ARG(src_addr); |
| 71 | PJ_UNUSED_ARG(src_addr_len); |
Benny Prijono | fb9d987 | 2007-03-21 22:05:58 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | |
| 75 | static void handle_events(unsigned msec_timeout) |
| 76 | { |
| 77 | pj_time_val delay; |
| 78 | |
| 79 | pj_timer_heap_poll(stun_cfg.timer_heap, NULL); |
| 80 | |
| 81 | delay.sec = 0; |
| 82 | delay.msec = msec_timeout; |
| 83 | pj_time_val_normalize(&delay); |
| 84 | |
| 85 | pj_ioqueue_poll(stun_cfg.ioqueue, &delay); |
| 86 | } |
| 87 | |
| 88 | |
| 89 | /* Basic create and destroy test */ |
| 90 | static int ice_basic_create_destroy_test() |
| 91 | { |
Benny Prijono | a6bd758 | 2007-03-28 15:49:48 +0000 | [diff] [blame^] | 92 | pj_ice_strans *im; |
| 93 | pj_ice_strans_cb icest_cb; |
Benny Prijono | fb9d987 | 2007-03-21 22:05:58 +0000 | [diff] [blame] | 94 | pj_status_t status; |
| 95 | |
| 96 | PJ_LOG(3,(THIS_FILE, "...basic create/destroy")); |
| 97 | |
Benny Prijono | ff49ab7 | 2007-03-22 21:00:53 +0000 | [diff] [blame] | 98 | pj_bzero(&icest_cb, sizeof(icest_cb)); |
| 99 | icest_cb.on_ice_complete = &on_ice_complete; |
| 100 | icest_cb.on_rx_data = &on_rx_data; |
Benny Prijono | fb9d987 | 2007-03-21 22:05:58 +0000 | [diff] [blame] | 101 | |
Benny Prijono | a6bd758 | 2007-03-28 15:49:48 +0000 | [diff] [blame^] | 102 | status = pj_ice_strans_create(&stun_cfg, "icetest", 2, NULL, &icest_cb, &im); |
Benny Prijono | fb9d987 | 2007-03-21 22:05:58 +0000 | [diff] [blame] | 103 | if (status != PJ_SUCCESS) |
| 104 | return -10; |
| 105 | |
Benny Prijono | a6bd758 | 2007-03-28 15:49:48 +0000 | [diff] [blame^] | 106 | pj_ice_strans_destroy(im); |
Benny Prijono | fb9d987 | 2007-03-21 22:05:58 +0000 | [diff] [blame] | 107 | |
| 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | |
Benny Prijono | a6bd758 | 2007-03-28 15:49:48 +0000 | [diff] [blame^] | 112 | static pj_status_t start_ice(pj_ice_strans *ist, pj_ice_strans *remote) |
Benny Prijono | fb9d987 | 2007-03-21 22:05:58 +0000 | [diff] [blame] | 113 | { |
Benny Prijono | ff49ab7 | 2007-03-22 21:00:53 +0000 | [diff] [blame] | 114 | unsigned count; |
Benny Prijono | a6bd758 | 2007-03-28 15:49:48 +0000 | [diff] [blame^] | 115 | pj_ice_sess_cand cand[PJ_ICE_MAX_CAND]; |
Benny Prijono | fb9d987 | 2007-03-21 22:05:58 +0000 | [diff] [blame] | 116 | pj_status_t status; |
| 117 | |
Benny Prijono | ff49ab7 | 2007-03-22 21:00:53 +0000 | [diff] [blame] | 118 | count = PJ_ARRAY_SIZE(cand); |
Benny Prijono | a6bd758 | 2007-03-28 15:49:48 +0000 | [diff] [blame^] | 119 | status = pj_ice_strans_enum_cands(remote, &count, cand); |
Benny Prijono | fb9d987 | 2007-03-21 22:05:58 +0000 | [diff] [blame] | 120 | if (status != PJ_SUCCESS) |
| 121 | return status; |
| 122 | |
Benny Prijono | a6bd758 | 2007-03-28 15:49:48 +0000 | [diff] [blame^] | 123 | return pj_ice_strans_start_ice(ist, &remote->ice->rx_ufrag, &remote->ice->rx_pass, |
Benny Prijono | ff49ab7 | 2007-03-22 21:00:53 +0000 | [diff] [blame] | 124 | count, cand); |
Benny Prijono | fb9d987 | 2007-03-21 22:05:58 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | |
Benny Prijono | 7e0d52c | 2007-03-26 13:25:07 +0000 | [diff] [blame] | 128 | struct dummy_cand |
| 129 | { |
| 130 | unsigned comp_id; |
| 131 | pj_ice_cand_type type; |
| 132 | const char *addr; |
| 133 | unsigned port; |
| 134 | }; |
| 135 | |
Benny Prijono | a6bd758 | 2007-03-28 15:49:48 +0000 | [diff] [blame^] | 136 | static int init_ice_st(pj_ice_strans *ice_st, |
Benny Prijono | 7e0d52c | 2007-03-26 13:25:07 +0000 | [diff] [blame] | 137 | pj_bool_t add_valid_comp, |
| 138 | unsigned dummy_cnt, |
| 139 | struct dummy_cand cand[]) |
| 140 | { |
| 141 | pj_str_t a; |
| 142 | pj_status_t status; |
| 143 | unsigned i; |
| 144 | |
| 145 | /* Create components */ |
| 146 | for (i=0; i<ice_st->comp_cnt; ++i) { |
Benny Prijono | a6bd758 | 2007-03-28 15:49:48 +0000 | [diff] [blame^] | 147 | status = pj_ice_strans_create_comp(ice_st, i+1, PJ_ICE_ST_OPT_DONT_ADD_CAND, NULL); |
Benny Prijono | 7e0d52c | 2007-03-26 13:25:07 +0000 | [diff] [blame] | 148 | if (status != PJ_SUCCESS) |
| 149 | return -21; |
| 150 | } |
| 151 | |
| 152 | /* Add dummy candidates */ |
| 153 | for (i=0; i<dummy_cnt; ++i) { |
| 154 | pj_sockaddr_in addr; |
| 155 | |
| 156 | pj_sockaddr_in_init(&addr, pj_cstr(&a, cand[i].addr), (pj_uint16_t)cand[i].port); |
Benny Prijono | a6bd758 | 2007-03-28 15:49:48 +0000 | [diff] [blame^] | 157 | status = pj_ice_strans_add_cand(ice_st, cand[i].comp_id, cand[i].type, |
Benny Prijono | 7e0d52c | 2007-03-26 13:25:07 +0000 | [diff] [blame] | 158 | 65535, &addr, PJ_FALSE); |
| 159 | if (status != PJ_SUCCESS) |
| 160 | return -22; |
| 161 | } |
| 162 | |
| 163 | /* Add the real candidate */ |
| 164 | if (add_valid_comp) { |
| 165 | for (i=0; i<ice_st->comp_cnt; ++i) { |
Benny Prijono | a6bd758 | 2007-03-28 15:49:48 +0000 | [diff] [blame^] | 166 | status = pj_ice_strans_add_cand(ice_st, i+1, PJ_ICE_CAND_TYPE_HOST, 65535, |
Benny Prijono | 7e0d52c | 2007-03-26 13:25:07 +0000 | [diff] [blame] | 167 | &ice_st->comp[i]->local_addr.ipv4, PJ_TRUE); |
| 168 | if (status != PJ_SUCCESS) |
| 169 | return -23; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | return 0; |
| 174 | } |
| 175 | |
| 176 | |
| 177 | /* When ICE completes, both agents should agree on the same candidate pair. |
| 178 | * Check that the remote address selected by agent1 is equal to the |
| 179 | * local address of selected by agent 2. |
| 180 | */ |
Benny Prijono | a6bd758 | 2007-03-28 15:49:48 +0000 | [diff] [blame^] | 181 | static int verify_address(pj_ice_strans *agent1, pj_ice_strans *agent2, |
Benny Prijono | 7e0d52c | 2007-03-26 13:25:07 +0000 | [diff] [blame] | 182 | unsigned comp_id) |
| 183 | { |
Benny Prijono | a6bd758 | 2007-03-28 15:49:48 +0000 | [diff] [blame^] | 184 | pj_ice_sess_cand *rcand, *lcand; |
Benny Prijono | 7e0d52c | 2007-03-26 13:25:07 +0000 | [diff] [blame] | 185 | int lcand_id; |
| 186 | |
| 187 | if (agent1->ice->comp[comp_id-1].valid_check == NULL) { |
| 188 | PJ_LOG(3,(THIS_FILE, "....error: valid_check not set for comp_id %d", comp_id)); |
| 189 | return -60; |
| 190 | } |
| 191 | |
| 192 | /* Get default remote candidate of agent 1 */ |
| 193 | rcand = agent1->ice->comp[comp_id-1].valid_check->rcand; |
| 194 | |
| 195 | /* Get default local candidate of agent 2 */ |
Benny Prijono | a6bd758 | 2007-03-28 15:49:48 +0000 | [diff] [blame^] | 196 | pj_ice_sess_find_default_cand(agent2->ice, comp_id, &lcand_id); |
Benny Prijono | 7e0d52c | 2007-03-26 13:25:07 +0000 | [diff] [blame] | 197 | if (lcand_id < 0) |
| 198 | return -62; |
| 199 | |
| 200 | lcand = &agent2->ice->lcand[lcand_id]; |
| 201 | |
| 202 | if (pj_memcmp(&rcand->addr, &lcand->addr, sizeof(pj_sockaddr_in))!=0) { |
| 203 | PJ_LOG(3,(THIS_FILE, "....error: the selected addresses are incorrect for comp_id %d", comp_id)); |
| 204 | return -64; |
| 205 | } |
| 206 | |
| 207 | return 0; |
| 208 | } |
| 209 | |
Benny Prijono | ff49ab7 | 2007-03-22 21:00:53 +0000 | [diff] [blame] | 210 | |
Benny Prijono | 2532b9d | 2007-03-22 01:16:37 +0000 | [diff] [blame] | 211 | /* Perform ICE test with the following parameters: |
| 212 | * |
| 213 | * - title: The title of the test |
| 214 | * - ocand_cnt, |
| 215 | * ocand Additional candidates to be added to offerer |
| 216 | * - acand_cnt, |
| 217 | * acand Additional candidates to be added to answerer |
| 218 | * |
Benny Prijono | fd7f147 | 2007-03-22 11:59:03 +0000 | [diff] [blame] | 219 | * The additional candidates are normally invalid candidates, meaning |
| 220 | * they won't be reachable by the agents. They are used to "confuse" |
Benny Prijono | 2532b9d | 2007-03-22 01:16:37 +0000 | [diff] [blame] | 221 | * ICE processing. |
| 222 | */ |
| 223 | static int perform_ice_test(const char *title, |
Benny Prijono | 7e0d52c | 2007-03-26 13:25:07 +0000 | [diff] [blame] | 224 | pj_bool_t expected_success, |
| 225 | unsigned comp_cnt, |
| 226 | pj_bool_t add_valid_comp, |
Benny Prijono | fd7f147 | 2007-03-22 11:59:03 +0000 | [diff] [blame] | 227 | unsigned wait_before_send, |
Benny Prijono | 7e0d52c | 2007-03-26 13:25:07 +0000 | [diff] [blame] | 228 | unsigned max_total_time, |
| 229 | unsigned ocand_cnt, |
| 230 | struct dummy_cand ocand[], |
| 231 | unsigned acand_cnt, |
| 232 | struct dummy_cand acand[]) |
Benny Prijono | fb9d987 | 2007-03-21 22:05:58 +0000 | [diff] [blame] | 233 | { |
Benny Prijono | a6bd758 | 2007-03-28 15:49:48 +0000 | [diff] [blame^] | 234 | pj_ice_strans *im1, *im2; |
| 235 | pj_ice_strans_cb icest_cb; |
Benny Prijono | fb9d987 | 2007-03-21 22:05:58 +0000 | [diff] [blame] | 236 | struct ice_data *id1, *id2; |
Benny Prijono | 2532b9d | 2007-03-22 01:16:37 +0000 | [diff] [blame] | 237 | pj_timestamp t_start, t_end; |
Benny Prijono | 7e0d52c | 2007-03-26 13:25:07 +0000 | [diff] [blame] | 238 | unsigned i; |
Benny Prijono | fd7f147 | 2007-03-22 11:59:03 +0000 | [diff] [blame] | 239 | pj_str_t data_from_offerer, data_from_answerer; |
Benny Prijono | fb9d987 | 2007-03-21 22:05:58 +0000 | [diff] [blame] | 240 | pj_status_t status; |
| 241 | |
Benny Prijono | 7e0d52c | 2007-03-26 13:25:07 +0000 | [diff] [blame] | 242 | #define CHECK_COMPLETE() if (id1->complete && id2->complete) { \ |
| 243 | if (t_end.u32.lo==0) pj_get_timestamp(&t_end); \ |
| 244 | } else {} |
| 245 | |
Benny Prijono | 2532b9d | 2007-03-22 01:16:37 +0000 | [diff] [blame] | 246 | PJ_LOG(3,(THIS_FILE, "...%s", title)); |
Benny Prijono | fb9d987 | 2007-03-21 22:05:58 +0000 | [diff] [blame] | 247 | |
Benny Prijono | 7e0d52c | 2007-03-26 13:25:07 +0000 | [diff] [blame] | 248 | pj_bzero(&t_end, sizeof(t_end)); |
| 249 | |
Benny Prijono | ff49ab7 | 2007-03-22 21:00:53 +0000 | [diff] [blame] | 250 | pj_bzero(&icest_cb, sizeof(icest_cb)); |
| 251 | icest_cb.on_ice_complete = &on_ice_complete; |
| 252 | icest_cb.on_rx_data = &on_rx_data; |
Benny Prijono | fb9d987 | 2007-03-21 22:05:58 +0000 | [diff] [blame] | 253 | |
| 254 | /* Create first ICE */ |
Benny Prijono | a6bd758 | 2007-03-28 15:49:48 +0000 | [diff] [blame^] | 255 | status = pj_ice_strans_create(&stun_cfg, "offerer", comp_cnt, NULL, &icest_cb, &im1); |
Benny Prijono | fb9d987 | 2007-03-21 22:05:58 +0000 | [diff] [blame] | 256 | if (status != PJ_SUCCESS) |
| 257 | return -20; |
| 258 | |
| 259 | id1 = PJ_POOL_ZALLOC_T(im1->pool, struct ice_data); |
Benny Prijono | 2532b9d | 2007-03-22 01:16:37 +0000 | [diff] [blame] | 260 | id1->obj_name = "offerer"; |
Benny Prijono | fb9d987 | 2007-03-21 22:05:58 +0000 | [diff] [blame] | 261 | im1->user_data = id1; |
| 262 | |
Benny Prijono | 7e0d52c | 2007-03-26 13:25:07 +0000 | [diff] [blame] | 263 | /* Init components */ |
| 264 | status = init_ice_st(im1, add_valid_comp, ocand_cnt, ocand); |
| 265 | if (status != 0) |
| 266 | return status; |
Benny Prijono | 2532b9d | 2007-03-22 01:16:37 +0000 | [diff] [blame] | 267 | |
Benny Prijono | fb9d987 | 2007-03-21 22:05:58 +0000 | [diff] [blame] | 268 | /* Create second ICE */ |
Benny Prijono | a6bd758 | 2007-03-28 15:49:48 +0000 | [diff] [blame^] | 269 | status = pj_ice_strans_create(&stun_cfg, "answerer", comp_cnt, NULL, &icest_cb, &im2); |
Benny Prijono | fb9d987 | 2007-03-21 22:05:58 +0000 | [diff] [blame] | 270 | if (status != PJ_SUCCESS) |
| 271 | return -25; |
| 272 | |
| 273 | id2 = PJ_POOL_ZALLOC_T(im2->pool, struct ice_data); |
Benny Prijono | 2532b9d | 2007-03-22 01:16:37 +0000 | [diff] [blame] | 274 | id2->obj_name = "answerer"; |
Benny Prijono | fb9d987 | 2007-03-21 22:05:58 +0000 | [diff] [blame] | 275 | im2->user_data = id2; |
| 276 | |
Benny Prijono | 7e0d52c | 2007-03-26 13:25:07 +0000 | [diff] [blame] | 277 | /* Init components */ |
| 278 | status = init_ice_st(im2, add_valid_comp, acand_cnt, acand); |
| 279 | if (status != 0) |
| 280 | return status; |
Benny Prijono | 2532b9d | 2007-03-22 01:16:37 +0000 | [diff] [blame] | 281 | |
Benny Prijono | fb9d987 | 2007-03-21 22:05:58 +0000 | [diff] [blame] | 282 | |
Benny Prijono | ff49ab7 | 2007-03-22 21:00:53 +0000 | [diff] [blame] | 283 | /* Init ICE on im1 */ |
Benny Prijono | a6bd758 | 2007-03-28 15:49:48 +0000 | [diff] [blame^] | 284 | status = pj_ice_strans_init_ice(im1, PJ_ICE_SESS_ROLE_CONTROLLING, NULL, NULL); |
Benny Prijono | ff49ab7 | 2007-03-22 21:00:53 +0000 | [diff] [blame] | 285 | if (status != PJ_SUCCESS) |
| 286 | return -29; |
Benny Prijono | fb9d987 | 2007-03-21 22:05:58 +0000 | [diff] [blame] | 287 | |
Benny Prijono | ff49ab7 | 2007-03-22 21:00:53 +0000 | [diff] [blame] | 288 | /* Init ICE on im2 */ |
Benny Prijono | a6bd758 | 2007-03-28 15:49:48 +0000 | [diff] [blame^] | 289 | status = pj_ice_strans_init_ice(im2, PJ_ICE_SESS_ROLE_CONTROLLED, NULL, NULL); |
Benny Prijono | ff49ab7 | 2007-03-22 21:00:53 +0000 | [diff] [blame] | 290 | if (status != PJ_SUCCESS) |
| 291 | return -29; |
| 292 | |
| 293 | /* Start ICE on im2 */ |
| 294 | status = start_ice(im2, im1); |
Benny Prijono | fb9d987 | 2007-03-21 22:05:58 +0000 | [diff] [blame] | 295 | if (status != PJ_SUCCESS) |
| 296 | return -30; |
| 297 | |
Benny Prijono | ff49ab7 | 2007-03-22 21:00:53 +0000 | [diff] [blame] | 298 | /* Start ICE on im1 */ |
| 299 | status = start_ice(im1, im2); |
Benny Prijono | fb9d987 | 2007-03-21 22:05:58 +0000 | [diff] [blame] | 300 | if (status != PJ_SUCCESS) |
| 301 | return -35; |
| 302 | |
Benny Prijono | 7e0d52c | 2007-03-26 13:25:07 +0000 | [diff] [blame] | 303 | /* Apply delay to let other checks commence */ |
| 304 | pj_thread_sleep(40); |
| 305 | |
Benny Prijono | 2532b9d | 2007-03-22 01:16:37 +0000 | [diff] [blame] | 306 | /* Mark start time */ |
| 307 | pj_get_timestamp(&t_start); |
| 308 | |
Benny Prijono | fd7f147 | 2007-03-22 11:59:03 +0000 | [diff] [blame] | 309 | /* Poll for wait_before_send msecs before we send the first data */ |
Benny Prijono | 7e0d52c | 2007-03-26 13:25:07 +0000 | [diff] [blame] | 310 | if (expected_success) { |
| 311 | for (;;) { |
| 312 | pj_timestamp t_now; |
Benny Prijono | fd7f147 | 2007-03-22 11:59:03 +0000 | [diff] [blame] | 313 | |
Benny Prijono | 7e0d52c | 2007-03-26 13:25:07 +0000 | [diff] [blame] | 314 | handle_events(1); |
Benny Prijono | fd7f147 | 2007-03-22 11:59:03 +0000 | [diff] [blame] | 315 | |
Benny Prijono | 7e0d52c | 2007-03-26 13:25:07 +0000 | [diff] [blame] | 316 | CHECK_COMPLETE(); |
| 317 | |
| 318 | pj_get_timestamp(&t_now); |
| 319 | if (pj_elapsed_msec(&t_start, &t_now) >= wait_before_send) |
| 320 | break; |
| 321 | } |
| 322 | |
| 323 | /* Send data. It must be successful! */ |
| 324 | data_from_offerer = pj_str("from offerer"); |
Benny Prijono | a6bd758 | 2007-03-28 15:49:48 +0000 | [diff] [blame^] | 325 | status = pj_ice_sess_send_data(im1->ice, 1, data_from_offerer.ptr, data_from_offerer.slen); |
Benny Prijono | 7e0d52c | 2007-03-26 13:25:07 +0000 | [diff] [blame] | 326 | if (status != PJ_SUCCESS) |
| 327 | return -47; |
| 328 | |
| 329 | data_from_answerer = pj_str("from answerer"); |
Benny Prijono | a6bd758 | 2007-03-28 15:49:48 +0000 | [diff] [blame^] | 330 | status = pj_ice_sess_send_data(im2->ice, 1, data_from_answerer.ptr, data_from_answerer.slen); |
Benny Prijono | 7e0d52c | 2007-03-26 13:25:07 +0000 | [diff] [blame] | 331 | if (status != PJ_SUCCESS) |
| 332 | return -48; |
| 333 | |
| 334 | /* Poll to allow data to be received */ |
| 335 | for (;;) { |
| 336 | pj_timestamp t_now; |
| 337 | handle_events(1); |
| 338 | CHECK_COMPLETE(); |
| 339 | pj_get_timestamp(&t_now); |
| 340 | if (pj_elapsed_msec(&t_start, &t_now) >= (wait_before_send + 200)) |
| 341 | break; |
| 342 | } |
Benny Prijono | fd7f147 | 2007-03-22 11:59:03 +0000 | [diff] [blame] | 343 | } |
| 344 | |
Benny Prijono | fb9d987 | 2007-03-21 22:05:58 +0000 | [diff] [blame] | 345 | /* Just wait until both completes, or timed out */ |
Benny Prijono | 2532b9d | 2007-03-22 01:16:37 +0000 | [diff] [blame] | 346 | while (!id1->complete || !id2->complete) { |
| 347 | pj_timestamp t_now; |
| 348 | |
Benny Prijono | fb9d987 | 2007-03-21 22:05:58 +0000 | [diff] [blame] | 349 | handle_events(1); |
| 350 | |
Benny Prijono | 7e0d52c | 2007-03-26 13:25:07 +0000 | [diff] [blame] | 351 | CHECK_COMPLETE(); |
Benny Prijono | 2532b9d | 2007-03-22 01:16:37 +0000 | [diff] [blame] | 352 | pj_get_timestamp(&t_now); |
Benny Prijono | fd7f147 | 2007-03-22 11:59:03 +0000 | [diff] [blame] | 353 | if (pj_elapsed_msec(&t_start, &t_now) >= max_total_time) { |
Benny Prijono | 2532b9d | 2007-03-22 01:16:37 +0000 | [diff] [blame] | 354 | PJ_LOG(3,(THIS_FILE, "....error: timed-out")); |
| 355 | return -50; |
| 356 | } |
| 357 | } |
Benny Prijono | fb9d987 | 2007-03-21 22:05:58 +0000 | [diff] [blame] | 358 | |
Benny Prijono | 2532b9d | 2007-03-22 01:16:37 +0000 | [diff] [blame] | 359 | /* Mark end-time */ |
Benny Prijono | 7e0d52c | 2007-03-26 13:25:07 +0000 | [diff] [blame] | 360 | CHECK_COMPLETE(); |
| 361 | |
| 362 | /* If expected to fail, then just check that both fail */ |
| 363 | if (!expected_success) { |
| 364 | /* Check status */ |
| 365 | if (id1->err_code == PJ_SUCCESS) |
| 366 | return -51; |
| 367 | if (id2->err_code == PJ_SUCCESS) |
| 368 | return -52; |
| 369 | goto on_return; |
| 370 | } |
Benny Prijono | 2532b9d | 2007-03-22 01:16:37 +0000 | [diff] [blame] | 371 | |
| 372 | /* Check status */ |
| 373 | if (id1->err_code != PJ_SUCCESS) |
| 374 | return -53; |
| 375 | if (id2->err_code != PJ_SUCCESS) |
| 376 | return -56; |
| 377 | |
| 378 | /* Verify that offerer gets answerer's transport address */ |
Benny Prijono | 7e0d52c | 2007-03-26 13:25:07 +0000 | [diff] [blame] | 379 | for (i=0; i<comp_cnt; ++i) { |
| 380 | status = verify_address(im1, im2, i+1); |
| 381 | if (status != 0) |
| 382 | return status; |
Benny Prijono | 2532b9d | 2007-03-22 01:16:37 +0000 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | /* And the other way around */ |
Benny Prijono | 7e0d52c | 2007-03-26 13:25:07 +0000 | [diff] [blame] | 386 | for (i=0; i<comp_cnt; ++i) { |
| 387 | status = verify_address(im2, im1, i+1); |
| 388 | if (status != 0) |
| 389 | return status; |
Benny Prijono | 2532b9d | 2007-03-22 01:16:37 +0000 | [diff] [blame] | 390 | } |
| 391 | |
Benny Prijono | fd7f147 | 2007-03-22 11:59:03 +0000 | [diff] [blame] | 392 | /* Check that data is received in offerer */ |
| 393 | if (id1->rx_rtp_cnt != 1) { |
| 394 | PJ_LOG(3,(THIS_FILE, "....error: data not received in offerer")); |
| 395 | return -80; |
| 396 | } |
| 397 | if (pj_strcmp2(&data_from_answerer, id1->last_rx_rtp_data) != 0) { |
| 398 | PJ_LOG(3,(THIS_FILE, "....error: data mismatch in offerer")); |
| 399 | return -82; |
| 400 | } |
| 401 | |
| 402 | /* And the same in answerer */ |
| 403 | if (id2->rx_rtp_cnt != 1) { |
| 404 | PJ_LOG(3,(THIS_FILE, "....error: data not received in answerer")); |
| 405 | return -84; |
| 406 | } |
| 407 | if (pj_strcmp2(&data_from_offerer, id2->last_rx_rtp_data) != 0) { |
| 408 | PJ_LOG(3,(THIS_FILE, "....error: data mismatch in answerer")); |
| 409 | return -82; |
| 410 | } |
| 411 | |
| 412 | |
Benny Prijono | 7e0d52c | 2007-03-26 13:25:07 +0000 | [diff] [blame] | 413 | on_return: |
| 414 | |
Benny Prijono | 2532b9d | 2007-03-22 01:16:37 +0000 | [diff] [blame] | 415 | /* Done */ |
Benny Prijono | 7e0d52c | 2007-03-26 13:25:07 +0000 | [diff] [blame] | 416 | PJ_LOG(3,(THIS_FILE, "....success: ICE completed in %d msec, waiting..", |
Benny Prijono | 2532b9d | 2007-03-22 01:16:37 +0000 | [diff] [blame] | 417 | pj_elapsed_msec(&t_start, &t_end))); |
| 418 | |
| 419 | /* Wait for some more time */ |
Benny Prijono | 2532b9d | 2007-03-22 01:16:37 +0000 | [diff] [blame] | 420 | for (;;) { |
| 421 | pj_timestamp t_now; |
| 422 | |
| 423 | pj_get_timestamp(&t_now); |
Benny Prijono | fd7f147 | 2007-03-22 11:59:03 +0000 | [diff] [blame] | 424 | if (pj_elapsed_msec(&t_start, &t_now) > max_total_time) |
Benny Prijono | 2532b9d | 2007-03-22 01:16:37 +0000 | [diff] [blame] | 425 | break; |
| 426 | |
| 427 | handle_events(1); |
| 428 | } |
| 429 | |
| 430 | |
Benny Prijono | a6bd758 | 2007-03-28 15:49:48 +0000 | [diff] [blame^] | 431 | pj_ice_strans_destroy(im1); |
| 432 | pj_ice_strans_destroy(im2); |
Benny Prijono | 7e0d52c | 2007-03-26 13:25:07 +0000 | [diff] [blame] | 433 | handle_events(100); |
Benny Prijono | 2532b9d | 2007-03-22 01:16:37 +0000 | [diff] [blame] | 434 | return 0; |
Benny Prijono | fb9d987 | 2007-03-21 22:05:58 +0000 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | |
| 438 | int ice_test(void) |
| 439 | { |
| 440 | int rc = 0; |
| 441 | pj_pool_t *pool; |
| 442 | pj_ioqueue_t *ioqueue; |
| 443 | pj_timer_heap_t *timer_heap; |
Benny Prijono | 7e0d52c | 2007-03-26 13:25:07 +0000 | [diff] [blame] | 444 | enum { D1=500, D2=5000, D3=15000 }; |
| 445 | struct dummy_cand ocand[] = |
| 446 | { |
| 447 | {1, PJ_ICE_CAND_TYPE_SRFLX, "127.1.1.1", 65534 }, |
| 448 | {2, PJ_ICE_CAND_TYPE_SRFLX, "127.1.1.1", 65535 }, |
| 449 | }; |
| 450 | struct dummy_cand acand[] = |
| 451 | { |
| 452 | {1, PJ_ICE_CAND_TYPE_SRFLX, "127.2.2.2", 65534 }, |
| 453 | {2, PJ_ICE_CAND_TYPE_SRFLX, "127.2.2.2", 65535 }, |
| 454 | }; |
Benny Prijono | 2532b9d | 2007-03-22 01:16:37 +0000 | [diff] [blame] | 455 | |
Benny Prijono | fb9d987 | 2007-03-21 22:05:58 +0000 | [diff] [blame] | 456 | pool = pj_pool_create(mem, NULL, 4000, 4000, NULL); |
| 457 | pj_ioqueue_create(pool, 12, &ioqueue); |
| 458 | pj_timer_heap_create(pool, 100, &timer_heap); |
| 459 | |
| 460 | pj_stun_config_init(&stun_cfg, mem, 0, ioqueue, timer_heap); |
| 461 | |
Benny Prijono | fd70c9b | 2007-03-27 11:00:38 +0000 | [diff] [blame] | 462 | #if 1 |
| 463 | pj_log_set_level(5); |
| 464 | #endif |
| 465 | |
| 466 | goto test; |
Benny Prijono | fb9d987 | 2007-03-21 22:05:58 +0000 | [diff] [blame] | 467 | |
Benny Prijono | 2532b9d | 2007-03-22 01:16:37 +0000 | [diff] [blame] | 468 | /* Basic create/destroy */ |
Benny Prijono | fb9d987 | 2007-03-21 22:05:58 +0000 | [diff] [blame] | 469 | rc = ice_basic_create_destroy_test(); |
| 470 | if (rc != 0) |
| 471 | goto on_return; |
| 472 | |
Benny Prijono | 2532b9d | 2007-03-22 01:16:37 +0000 | [diff] [blame] | 473 | /* Direct communication */ |
Benny Prijono | 7e0d52c | 2007-03-26 13:25:07 +0000 | [diff] [blame] | 474 | rc = perform_ice_test("Simple test (1 component)", PJ_TRUE, 1, PJ_TRUE, D1, D2, 0, NULL, 0, NULL); |
| 475 | if (rc != 0) |
| 476 | goto on_return; |
| 477 | |
| 478 | /* Failure case (all checks fail) */ |
| 479 | rc = perform_ice_test("Failure case (all checks fail)", PJ_FALSE, 1, PJ_FALSE, D3, D3, 1, ocand, 1, acand); |
Benny Prijono | fb9d987 | 2007-03-21 22:05:58 +0000 | [diff] [blame] | 480 | if (rc != 0) |
| 481 | goto on_return; |
| 482 | |
Benny Prijono | 2532b9d | 2007-03-22 01:16:37 +0000 | [diff] [blame] | 483 | /* Direct communication with invalid address */ |
Benny Prijono | 7e0d52c | 2007-03-26 13:25:07 +0000 | [diff] [blame] | 484 | rc = perform_ice_test("With 1 unreachable address", PJ_TRUE, 1, PJ_TRUE, D1, D2, 1, ocand, 0, NULL); |
| 485 | if (rc != 0) |
| 486 | goto on_return; |
| 487 | |
| 488 | /* Direct communication with invalid address */ |
| 489 | rc = perform_ice_test("With 2 unreachable addresses (one each)", PJ_TRUE, 1, PJ_TRUE, D1, D2, 1, ocand, 1, acand); |
Benny Prijono | 2532b9d | 2007-03-22 01:16:37 +0000 | [diff] [blame] | 490 | if (rc != 0) |
| 491 | goto on_return; |
| 492 | |
Benny Prijono | fd7f147 | 2007-03-22 11:59:03 +0000 | [diff] [blame] | 493 | /* Direct communication with two components */ |
Benny Prijono | fd70c9b | 2007-03-27 11:00:38 +0000 | [diff] [blame] | 494 | test: |
Benny Prijono | 7e0d52c | 2007-03-26 13:25:07 +0000 | [diff] [blame] | 495 | rc = perform_ice_test("With two components (RTP and RTCP)", PJ_TRUE, 2, PJ_TRUE, D1, D2, 0, NULL, 0, NULL); |
| 496 | if (rc != 0) |
| 497 | goto on_return; |
| 498 | |
Benny Prijono | fd70c9b | 2007-03-27 11:00:38 +0000 | [diff] [blame] | 499 | goto on_return; |
| 500 | |
Benny Prijono | 7e0d52c | 2007-03-26 13:25:07 +0000 | [diff] [blame] | 501 | /* Direct communication with mismatch number of components */ |
| 502 | |
| 503 | /* Direct communication with 2 components and 2 invalid address */ |
| 504 | rc = perform_ice_test("With 2 two components and 2 unreachable address", PJ_TRUE, 2, PJ_TRUE, D1, D2, 1, ocand, 1, acand); |
Benny Prijono | fd7f147 | 2007-03-22 11:59:03 +0000 | [diff] [blame] | 505 | if (rc != 0) |
| 506 | goto on_return; |
| 507 | |
| 508 | |
Benny Prijono | 2532b9d | 2007-03-22 01:16:37 +0000 | [diff] [blame] | 509 | |
Benny Prijono | fb9d987 | 2007-03-21 22:05:58 +0000 | [diff] [blame] | 510 | on_return: |
Benny Prijono | fd70c9b | 2007-03-27 11:00:38 +0000 | [diff] [blame] | 511 | pj_log_set_level(3); |
Benny Prijono | fb9d987 | 2007-03-21 22:05:58 +0000 | [diff] [blame] | 512 | pj_ioqueue_destroy(stun_cfg.ioqueue); |
| 513 | pj_pool_release(pool); |
| 514 | return rc; |
| 515 | } |
| 516 | |