Skip to content

Commit

Permalink
add ppp config request unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
w180112 committed Oct 14, 2023
1 parent 89cd29a commit c0477fc
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 24 deletions.
28 changes: 9 additions & 19 deletions src/pppd/codec.c
Original file line number Diff line number Diff line change
Expand Up @@ -638,8 +638,6 @@ void build_padt(U8 *buffer, U16 *mulen, PPP_INFO_t *s_ppp_ccb)
pppoe_header->length = 0;

*mulen = sizeof(struct rte_ether_hdr) + sizeof(vlan_header_t) + sizeof(pppoe_header_t);

return SUCCESS;
}

/**
Expand All @@ -652,14 +650,14 @@ void build_padt(U8 *buffer, U16 *mulen, PPP_INFO_t *s_ppp_ccb)
* output: TRUE/FALSE
* return: packet buffer
*/
STATUS build_config_request(unsigned char *buffer, PPP_INFO_t *s_ppp_ccb, U16 *mulen)
void build_config_request(U8 *buffer, U16 *mulen, PPP_INFO_t *s_ppp_ccb)
{
struct rte_ether_hdr *eth_hdr = s_ppp_ccb->ppp_phase[s_ppp_ccb->cp].eth_hdr;
vlan_header_t *vlan_header = s_ppp_ccb->ppp_phase[s_ppp_ccb->cp].vlan_header;
pppoe_header_t *pppoe_header = s_ppp_ccb->ppp_phase[s_ppp_ccb->cp].pppoe_header;
ppp_payload_t *ppp_payload = s_ppp_ccb->ppp_phase[s_ppp_ccb->cp].ppp_payload;
ppp_header_t *ppp_hdr = s_ppp_ccb->ppp_phase[s_ppp_ccb->cp].ppp_hdr;
ppp_options_t *ppp_options = s_ppp_ccb->ppp_phase[s_ppp_ccb->cp].ppp_options;
struct rte_ether_hdr *eth_hdr = (struct rte_ether_hdr *)buffer;
vlan_header_t *vlan_header = (vlan_header_t *)(eth_hdr + 1);
pppoe_header_t *pppoe_header = (pppoe_header_t *)(vlan_header + 1);
ppp_payload_t *ppp_payload = (ppp_payload_t *)(pppoe_header + 1);
ppp_header_t *ppp_hdr = (ppp_header_t *)(ppp_payload + 1);
ppp_options_t *ppp_options = (ppp_options_t *)(ppp_hdr + 1);

srand(time(NULL));

Expand Down Expand Up @@ -691,7 +689,7 @@ STATUS build_config_request(unsigned char *buffer, PPP_INFO_t *s_ppp_ccb, U16 *m
if (s_ppp_ccb->cp == 1) {
ppp_payload->ppp_protocol = rte_cpu_to_be_16(IPCP_PROTOCOL);
ppp_options->type = IP_ADDRESS;
rte_memcpy(ppp_options->val,&(s_ppp_ccb->hsi_ipv4),4);
rte_memcpy(ppp_options->val, &(s_ppp_ccb->hsi_ipv4), 4);
ppp_options->length = sizeof(s_ppp_ccb->hsi_ipv4) + sizeof(ppp_options_t);
pppoe_header->length += ppp_options->length;
ppp_hdr->length += ppp_options->length;
Expand Down Expand Up @@ -745,16 +743,8 @@ STATUS build_config_request(unsigned char *buffer, PPP_INFO_t *s_ppp_ccb, U16 *m

pppoe_header->length = rte_cpu_to_be_16(pppoe_header->length);
ppp_hdr->length = rte_cpu_to_be_16(ppp_hdr->length);
memset(buffer,0,MSG_BUF);
rte_memcpy(buffer,eth_hdr,14);
rte_memcpy(buffer+14,vlan_header,sizeof(vlan_header_t));
rte_memcpy(buffer+14+sizeof(vlan_header_t),pppoe_header,sizeof(pppoe_header_t));
rte_memcpy(buffer+14+sizeof(vlan_header_t)+sizeof(pppoe_header_t),ppp_payload,sizeof(ppp_payload_t));
rte_memcpy(buffer+14+sizeof(vlan_header_t)+sizeof(pppoe_header_t)+sizeof(ppp_payload_t),ppp_hdr,sizeof(ppp_header_t));
rte_memcpy(buffer+14+sizeof(vlan_header_t)+sizeof(pppoe_header_t)+sizeof(ppp_payload_t)+sizeof(ppp_header_t),ppp_options,rte_cpu_to_be_16(ppp_hdr->length) - sizeof(ppp_header_t));


VRG_LOG(DBG, vrg_ccb->fp, s_ppp_ccb, PPPLOGMSG, "User %" PRIu16 " config request built.", s_ppp_ccb->user_num);
return TRUE;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/pppd/codec.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ extern STATUS decode_ipcp(pppoe_header_t *pppoe_header, ppp_payload_t *ppp_paylo

extern void DECODE_OBJID(U8 *vp, U8 vlen, U32 *oids, U8 *oids_len);

extern STATUS build_config_request(unsigned char *buffer, PPP_INFO_t *s_ppp_ccb, U16 *mulen);
void build_config_request(unsigned char *buffer, U16 *mulen, PPP_INFO_t *s_ppp_ccb);
extern STATUS build_config_ack(unsigned char *buffer, PPP_INFO_t *s_ppp_ccb, U16 *mulen);
extern STATUS build_config_nak_rej(unsigned char *buffer, PPP_INFO_t *s_ppp_ccb, U16 *mulen);
extern STATUS build_terminate_ack(unsigned char *buffer, PPP_INFO_t *s_ppp_ccb, U16 *mulen);
Expand Down
3 changes: 1 addition & 2 deletions src/pppd/fsm.c
Original file line number Diff line number Diff line change
Expand Up @@ -789,8 +789,7 @@ STATUS A_send_config_request(__attribute__((unused)) struct rte_timer *tim, __at
VRG_LOG(DBG, vrg_ccb->fp, s_ppp_ccb, PPPLOGMSG, "User %" PRIu16 " config request timeout.\n", s_ppp_ccb->user_num);
PPP_FSM(tim,s_ppp_ccb,E_TIMEOUT_COUNTER_EXPIRED);
}
if (build_config_request(buffer,s_ppp_ccb,&mulen) < 0)
return FALSE;
build_config_request(buffer, &mulen, s_ppp_ccb);
drv_xmit(vrg_ccb, buffer, mulen);
s_ppp_ccb->ppp_phase[s_ppp_ccb->cp].timer_counter--;

Expand Down
61 changes: 59 additions & 2 deletions unit_test/pppd/codec_test.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <assert.h>
#include <stdlib.h>
#include <assert.h>
#include <common.h>
#include "../../src/pppd/codec.h"
#include "../../src/protocol.h"
Expand Down Expand Up @@ -114,7 +114,64 @@ void test_build_padt() {
0x16, 0xdd, 0x81, 0x00, 0x00, 0x02, 0x88, 0x63, 0x11, 0xa7, 0x00, 0x0a, 0x00,
0x00};

assert(build_padt(buffer, &mulen, &s_ppp_ccb_1) == SUCCESS);
build_padt(buffer, &mulen, &s_ppp_ccb_1);
assert(mulen == sizeof(pkt_1));
assert(memcmp(buffer, pkt_1, mulen) == 0);
}

void test_build_config_request() {
U8 buffer[80];
U16 mulen = 0;

PPP_INFO_t s_ppp_ccb_1 = {
.ppp_phase = {{
.timer_counter = 0,
.max_retransmit = 10,
},{
.timer_counter = 0,
.max_retransmit = 10,
},},
.user_num = 1,
.vlan = 2,
.PPP_dst_mac = (struct rte_ether_addr){
.addr_bytes = {0x74, 0x4d, 0x28, 0x8d, 0x00, 0x31},
},
.session_id = htons(0x000a),
.cp = 0,
};

char pkt_lcp[] = {0x74, 0x4d, 0x28, 0x8d, 0x00, 0x31, 0x9c, 0x69, 0xb4, 0x61,
0x16, 0xdd, 0x81, 0x00, 0x00, 0x02, 0x88, 0x64, 0x11, 0x00, 0x00, 0x0a, 0x00,
0x10, 0xc0, 0x21, 0x01, 0x01, 0x00, 0x0e, 0x01, 0x04, 0x05, 0xd0, 0x05, 0x06,
0x00, 0x01, 0x02, 0x03};
char pkt_ipcp[] = {0x74, 0x4d, 0x28, 0x8d, 0x00, 0x31, 0x9c, 0x69, 0xb4, 0x61,
0x16, 0xdd, 0x81, 0x00, 0x00, 0x02, 0x88, 0x64, 0x11, 0x00, 0x00, 0x0a, 0x00,
0x0c, 0x80, 0x21, 0x01, 0x01, 0x00, 0x0a, 0x03, 0x06, 0x00, 0x00, 0x00, 0x00};

/* test LCP */
build_config_request(buffer, &mulen, &s_ppp_ccb_1);
assert(mulen == sizeof(pkt_lcp));
assert(memcmp(buffer, pkt_lcp, 26/* only memcmp to lcp field */) == 0);
ppp_header_t *test_ppp_hdr = (ppp_header_t *)(buffer + 26);
assert(test_ppp_hdr->code == CONFIG_REQUEST);
assert(test_ppp_hdr->length == htons(0x000e));
ppp_options_t *test_ppp_options = (ppp_options_t *)(test_ppp_hdr + 1);
assert(test_ppp_options->type == 0x01);
assert(test_ppp_options->length == 0x04);
test_ppp_options = (ppp_options_t *)((U8 *)test_ppp_options + test_ppp_options->length);
assert(test_ppp_options->type == 0x05);
assert(test_ppp_options->length == 0x06);

/* test IPCP */
memset(buffer, 0, sizeof(buffer));
s_ppp_ccb_1.cp = 1;
build_config_request(buffer, &mulen, &s_ppp_ccb_1);
assert(mulen == sizeof(pkt_ipcp));
assert(memcmp(buffer, pkt_ipcp, 26/* only memcmp to ipcp field */) == 0);
test_ppp_hdr = (ppp_header_t *)(buffer + 26);
assert(test_ppp_hdr->code == CONFIG_REQUEST);
assert(test_ppp_hdr->length == htons(0x000a));
test_ppp_options = (ppp_options_t *)(test_ppp_hdr + 1);
assert(test_ppp_options->type == 0x03);
assert(test_ppp_options->length == 0x06);
}
1 change: 1 addition & 0 deletions unit_test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ int main()
test_build_padi();
test_build_padr();
test_build_padt();
test_build_config_request();
puts("ok!");

puts("\nall test successfully");
Expand Down
1 change: 1 addition & 0 deletions unit_test/test.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
void test_build_padi();
void test_build_padr();
void test_build_padt();
void test_build_config_request();

#endif

0 comments on commit c0477fc

Please sign in to comment.