printf("####################################\n"); printf("#### Lifetime renewal test ####\n"); printf("####################################\n"); printf(">>> Test parameters \n"); printf(">>> dest_ip = 0.0.0.0 \n"); printf(">>> src_ip = 127.0.0.1 \n"); printf(">>> ext_ip = 10.20.30.40 \n"); printf(">>> dest_port = 1234 \n"); printf(">>> src_port = 1235 \n"); printf(">>> protocol = 6 \n"); printf(">>> lifetime = 10 \n"); pcp_log_level = argc>1?PCP_LOGLVL_DEBUG:PCP_LOGLVL_INFO; ctx = pcp_init(0, NULL); pcp_add_server(ctx, Sock_pton("127.0.0.1"), 2); flow = pcp_new_flow(ctx, (struct sockaddr*)&source_ip4, (struct sockaddr*)&destination_ip4, (struct sockaddr*)&ext_ip4, protocol, lifetime, NULL); finish_time = time(NULL) + 21; while ((cur_time=time(NULL))<finish_time) { switch (test_wait(flow, (int)(finish_time - cur_time + 1)*1000)) { case pcp_state_processing: printf("\nFlow signaling timed out.\n" KNRM);
int main(void) { pcp_ctx_t * ctx; pcp_log_level = PCP_DEBUG_NONE; PD_SOCKET_STARTUP(); ctx = pcp_init(0, NULL); #ifndef PCP_DISABLE_NATPMP { // TEST NATPMP - parsing nat_pmp_announce_resp_t natpmp_a; nat_pmp_map_resp_t natpmp_mt; nat_pmp_map_resp_t natpmp_mu; pcp_recv_msg_t msg; natpmp_a.ver = 0; natpmp_a.opcode = 0; natpmp_a.result = htons(11); natpmp_a.epoch = htonl(1234); natpmp_a.ext_ip = 0x01020304; natpmp_mt.ver = 0; natpmp_mt.opcode = NATPMP_OPCODE_MAP_TCP; natpmp_mt.result = htons(11); natpmp_mt.epoch = htonl(1234); natpmp_mt.ext_port = htons(1234); natpmp_mt.int_port = htons(3456); natpmp_mt.lifetime = htonl(2233); natpmp_mu.ver = 0; natpmp_mu.opcode = NATPMP_OPCODE_MAP_UDP; natpmp_mu.result = htons(11); natpmp_mu.epoch = htonl(1234); natpmp_mu.ext_port = htons(1234); natpmp_mu.int_port = htons(3456); natpmp_mu.lifetime = htonl(2233); memset(&msg,0,sizeof(msg)); memcpy(&msg.pcp_msg_buffer, &natpmp_a, sizeof(natpmp_a)); msg.pcp_msg_len = sizeof(natpmp_a); TEST(parse_response(&msg)==PCP_ERR_SUCCESS); TEST(msg.recv_version==0); TEST(msg.recv_epoch==1234); TEST(msg.recv_result==11); msg.pcp_msg_len = sizeof(natpmp_a)-1; TEST(parse_response(&msg)!=PCP_ERR_SUCCESS); natpmp_a.ver++; memset(&msg,0,sizeof(msg)); memcpy(&msg.pcp_msg_buffer, &natpmp_a, sizeof(natpmp_a)); msg.pcp_msg_len = sizeof(natpmp_a); TEST(parse_response(&msg)!=PCP_ERR_SUCCESS); msg.pcp_msg_len = sizeof(pcp_response_t); TEST(parse_response(&msg)==PCP_ERR_SUCCESS); TEST(msg.recv_version==1); TEST(msg.recv_result==11); natpmp_a.ver++; memset(&msg,0,sizeof(msg)); memcpy(&msg.pcp_msg_buffer, &natpmp_a, sizeof(natpmp_a)); msg.pcp_msg_len = sizeof(natpmp_a); TEST(parse_response(&msg)!=PCP_ERR_SUCCESS); msg.pcp_msg_len = sizeof(pcp_response_t); TEST(parse_response(&msg)==PCP_ERR_SUCCESS); TEST(msg.recv_version==2); TEST(msg.recv_result==11); memset(&msg,0,sizeof(msg)); memcpy(&msg.pcp_msg_buffer, &natpmp_mt, sizeof(natpmp_mt)); msg.pcp_msg_len = sizeof(natpmp_mt); TEST(parse_response(&msg)==PCP_ERR_SUCCESS); TEST(msg.assigned_ext_port==htons(1234)); TEST(msg.kd.map_peer.src_port==htons(3456)); TEST(msg.kd.map_peer.protocol==IPPROTO_TCP); TEST(msg.recv_version==0); TEST(msg.recv_epoch==1234); TEST(msg.recv_lifetime==2233); TEST(msg.recv_result==11); memset(&msg,0,sizeof(msg)); memcpy(&msg.pcp_msg_buffer, &natpmp_mu, sizeof(natpmp_mu)); msg.pcp_msg_len = sizeof(natpmp_mu); TEST(parse_response(&msg)==PCP_ERR_SUCCESS); TEST(msg.assigned_ext_port==htons(1234)); TEST(msg.kd.map_peer.src_port==htons(3456)); TEST(msg.kd.map_peer.protocol==IPPROTO_UDP); TEST(msg.recv_version==0); TEST(msg.recv_epoch==1234); TEST(msg.recv_lifetime==2233); TEST(msg.recv_result==11); msg.pcp_msg_len = sizeof(natpmp_mu)-1; TEST(parse_response(&msg)!=PCP_ERR_SUCCESS); natpmp_mu.opcode=3; memset(&msg,0,sizeof(msg)); memcpy(&msg.pcp_msg_buffer, &natpmp_mu, sizeof(natpmp_mu)); msg.pcp_msg_len = sizeof(natpmp_mu); TEST(parse_response(&msg)!=PCP_ERR_SUCCESS); } #endif { // TEST validate MSG pcp_recv_msg_t msg; pcp_response_t* resp = (pcp_response_t*)msg.pcp_msg_buffer; memset(&msg, 0, sizeof(msg)); resp->r_opcode=0x81; resp->ver=1; msg.pcp_msg_len=sizeof(pcp_response_t); TEST(validate_pcp_msg(&msg)); msg.pcp_msg_len=sizeof(pcp_response_t)-1; TEST(!validate_pcp_msg(&msg)); msg.pcp_msg_len=sizeof(pcp_response_t)-4; TEST(validate_pcp_msg(&msg)); msg.pcp_msg_len=1104; TEST(!validate_pcp_msg(&msg)); msg.pcp_msg_len=0; TEST(!validate_pcp_msg(&msg)); msg.pcp_msg_len=sizeof(pcp_response_t); resp->ver=2; TEST(validate_pcp_msg(&msg)); resp->ver=3; TEST(!validate_pcp_msg(&msg)); resp->ver=2; resp->r_opcode=0x1; TEST(!validate_pcp_msg(&msg)); resp->r_opcode=0x7f; TEST(!validate_pcp_msg(&msg)); resp->r_opcode=0x81; msg.pcp_msg_len=sizeof(pcp_response_t)-1; resp->ver=1; TEST(parse_response(&msg)!=PCP_ERR_SUCCESS); resp->ver=2; TEST(parse_response(&msg)!=PCP_ERR_SUCCESS); resp->r_opcode=0x82; TEST(parse_response(&msg)!=PCP_ERR_SUCCESS); resp->ver=1; TEST(parse_response(&msg)!=PCP_ERR_SUCCESS); resp->r_opcode=0x83; resp->ver=2; TEST(parse_response(&msg)!=PCP_ERR_SUCCESS); resp->r_opcode=0x81; msg.pcp_msg_len=sizeof(pcp_response_t)+1; resp->ver=1; TEST(parse_response(&msg)!=PCP_ERR_SUCCESS); resp->ver=2; TEST(parse_response(&msg)!=PCP_ERR_SUCCESS); resp->r_opcode=0x82; TEST(parse_response(&msg)!=PCP_ERR_SUCCESS); resp->ver=1; TEST(parse_response(&msg)!=PCP_ERR_SUCCESS); resp->r_opcode=0x83; resp->ver=2; TEST(parse_response(&msg)!=PCP_ERR_SUCCESS); msg.pcp_msg_len=sizeof(pcp_response_t); resp->r_opcode=0x84; resp->ver=2; TEST(parse_response(&msg)!=PCP_ERR_SUCCESS); resp->ver=1; TEST(parse_response(&msg)!=PCP_ERR_SUCCESS); resp->ver=3; resp->r_opcode=0x82; TEST(parse_response(&msg)!=PCP_ERR_SUCCESS); } { //TEST build msg struct pcp_flow_s fs; pcp_server_t *s; memset(&fs, 0, sizeof(fs)); fs.ctx=ctx; TEST(build_pcp_msg(&fs)==NULL); fs.pcp_server_indx=pcp_add_server(ctx, Sock_pton("127.0.0.1"),1); s=get_pcp_server(ctx, fs.pcp_server_indx); fs.kd.operation = PCP_OPCODE_ANNOUNCE; TEST(build_pcp_msg(&fs)!=NULL); fs.kd.operation = 0x7f; TEST(build_pcp_msg(&fs)==NULL); fs.kd.operation = PCP_OPCODE_SADSCP; TEST(build_pcp_msg(&fs)==NULL); s->pcp_version = 3; TEST(build_pcp_msg(&fs)==NULL); fs.kd.operation = PCP_OPCODE_MAP; TEST(build_pcp_msg(&fs)==NULL); fs.kd.operation = PCP_OPCODE_PEER; TEST(build_pcp_msg(&fs)==NULL); s->pcp_version = 2; #ifdef PCP_EXPERIMENTAL { uint16_t i; pcp_db_add_md(&fs, 0, NULL,sizeof("string")); pcp_db_add_md(&fs, 1, "string", 0); for (i=2; i<256; ++i) { pcp_db_add_md(&fs, i, "string",sizeof("string")); } TEST(build_pcp_msg(&fs)!=NULL); TEST(fs.pcp_msg_len<=PCP_MAX_LEN); } #endif TEST(build_pcp_msg(NULL)==NULL); } PD_SOCKET_CLEANUP(); return 0; }
int main(void) { pcp_flow_t *f1, *f2; pcp_ctx_t *ctx; pcp_log_level = PCP_DEBUG_NONE; PD_SOCKET_STARTUP(); //test pcp_init & terminate ctx = pcp_init(DISABLE_AUTODISCOVERY, NULL); TEST(get_pcp_server(ctx, 0)==NULL); pcp_terminate(ctx, 1); ctx = pcp_init(ENABLE_AUTODISCOVERY, NULL); TEST(get_pcp_server(ctx, 0)!=NULL); pcp_terminate(ctx, 1); TEST(get_pcp_server(ctx, 0)==NULL); ctx = pcp_init(DISABLE_AUTODISCOVERY, NULL); TEST(pcp_add_server(ctx, Sock_pton("127.0.0.1:5351"), 2)==0); #ifdef PCP_USE_IPV6_SOCKET TEST(pcp_add_server(ctx, Sock_pton("[::1]:5351"), 1)==1); #endif pcp_terminate(ctx, 1); #ifdef PCP_SADSCP //TEST learn DSCP { pcp_flow_t* l1; ctx = pcp_init(DISABLE_AUTODISCOVERY, NULL); TEST((l1=pcp_learn_dscp(ctx, 1,1,1,NULL))==NULL); //NO PCP server to send req TEST(pcp_add_server(ctx, Sock_pton("127.0.0.1:5351"), 2)==0); TEST((l1=pcp_learn_dscp(ctx, 1,1,1,NULL))!=NULL); TEST(l1->kd.operation==PCP_OPCODE_SADSCP); TEST(l1->sadscp_app_name==NULL); TEST(l1->sadscp.app_name_length==0); TEST(l1->sadscp.toler_fields==84); pcp_close_flow(l1); pcp_delete_flow(l1); TEST((l1=pcp_learn_dscp(ctx, 2,2,2,"test"))!=NULL); TEST(l1->sadscp.app_name_length==4); TEST(strncmp(l1->sadscp_app_name,"test",l1->sadscp.app_name_length)==0); TEST(l1->sadscp.toler_fields==168); pcp_terminate(ctx, 1); } #endif ctx = pcp_init(DISABLE_AUTODISCOVERY, NULL); TEST(pcp_add_server(ctx, Sock_pton("127.0.0.1:5351"), 2)==0); TEST((pcp_new_flow(ctx, Sock_pton("[::1]:1234"), Sock_pton("[::1]"), NULL, IPPROTO_TCP, 100, NULL))==NULL); pcp_pulse(NULL, NULL); pcp_pulse(ctx, NULL); pcp_flow_get_info(NULL, NULL, NULL); //PCP PEER/MAP tests TEST(pcp_new_flow(NULL, NULL, NULL, NULL, 0, 0, NULL)==NULL); TEST(pcp_new_flow(ctx, NULL, NULL, NULL, 0, 0, NULL)==NULL); TEST((f1=pcp_new_flow(ctx, Sock_pton("127.0.0.1:1234"), Sock_pton("127.0.0.1"), NULL, IPPROTO_TCP, 100, NULL))!=NULL); TEST((f2=pcp_new_flow(ctx, Sock_pton("127.0.0.1:1234"), NULL, NULL, IPPROTO_TCP, 100, NULL))!=NULL); pcp_flow_set_prefer_failure_opt(f2); pcp_flow_set_prefer_failure_opt(f2); pcp_flow_set_lifetime(f1, 1000); TEST((f1->lifetime)>=99); TEST((f1->timeout.tv_sec>0)||(f1->timeout.tv_usec>0)); f1->timeout.tv_sec=0; f1->timeout.tv_usec=0; pcp_flow_set_lifetime(f1, 0); TEST(f1->lifetime==0); TEST((f1->timeout.tv_sec>0)||(f1->timeout.tv_usec>0)); pcp_set_3rd_party_opt(f1,NULL); printf("Tests succeeded.\n\n"); PD_SOCKET_CLEANUP(); return 0; }