Teonet library  0.4.7
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
test_tcp_proxy.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <CUnit/Basic.h>
#include "ev_mgr.h"
#include "modules/tcp_proxy.h"

Macros

#define ke_emul()
 Emulate ksnetEvMgrClass. More...
 

Functions

size_t ksnTCPProxyPackageCreate (void *buffer, size_t buffer_len, const char *addr, int port, int cmd, const void *data, size_t data_len)
 Create TCP Proxy package. More...
 
uint8_t get_byte_checksum (const uint8_t *data, size_t data_len)
 
int ksnTCPProxyPackageProcess (ksnTCPProxyPacketData *packet, void *data, size_t data_length)
 Process TCP proxy package. More...
 
void test_5_1 ()
 Initialize/Destroy TCP Proxy module. More...
 
void test_5_2 ()
 Create and Process TCP Proxy Package functions. More...
 
int add_suite_5_tests (void)
 Add TCP Proxy suite tests. More...
 

Variables

CU_pSuite pSuite
 

Detailed Description

Author
Kirill Scherba kiril.nosp@m.l@sc.nosp@m.herba.nosp@m..ru

Teonet TCP Proxy module tests suite

Tests TCP Proxy functions:

  • Initialize/Destroy TCP Proxy module: test_5_1()
  • Create and Process TCP Proxy Package functions: test_5_2()

cUnit test suite code:

#include <stdio.h>
#include <stdlib.h>
#include <CUnit/Basic.h>
#include "ev_mgr.h"
extern CU_pSuite pSuite;
size_t ksnTCPProxyPackageCreate(void *buffer, size_t buffer_len,
const char *addr, int port, int cmd, const void *data, size_t data_len);
uint8_t get_byte_checksum(const uint8_t *data, size_t data_len);
size_t data_length);
#define ke_emul() \
ksnetEvMgrClass ke_obj; \
ksnetEvMgrClass *ke = &ke_obj; \
ke->teo_cfg.tcp_allow_f = 0; \
ke->teo_cfg.r_tcp_f = 0
void test_5_1() {
// Emulate ksnCoreClass
ksnTCPProxyClass *tp; // Initialize ksnTRUDPClass
CU_ASSERT_PTR_NOT_NULL_FATAL((tp = ksnTCPProxyInit(ke)));
ksnTCPProxyDestroy(tp); // Destroy ksnTRUDPClass
CU_PASS("Destroy ksnTCPProxyClass done");
}
void test_5_2() {
// Emulate ksnCoreClass
// Initialize ksnTRUDPClass
CU_ASSERT_PTR_NOT_NULL_FATAL((tp = ksnTCPProxyInit(ke)));
ksnTCPProxyData *tpd = &_tpd;
// Initialize input packet buffer parameters
tpd->packet.ptr = 0; // Pointer to data end in packet buffer
tpd->packet.length = 0; // Length of received packet
tpd->packet.stage = WAIT_FOR_START; // Stage of receiving packet
tpd->packet.header = (ksnTCPProxyHeader*) tpd->packet.buffer; // Pointer to packet header
// 1) Create package function check
const int port = 9090;
const char *addr = "127.0.0.1";
const char *data = "Some package data (1) ...";
const char *data2 = "Some package data (2) with additional text ...";
const size_t data_len = strlen(data) + 1;
const size_t data2_len = strlen(data2) + 1;
size_t buffer_len = KSN_BUFFER_DB_SIZE;
char buffer[buffer_len];
// Create package
size_t pl = ksnTCPProxyPackageCreate(buffer, buffer_len, addr, port, CMD_TCPP_PROXY, data, data_len);
// Check result
CU_ASSERT(pl > 0); // Packet created (the buffer has enough size)
CU_ASSERT_EQUAL(th->checksum, get_byte_checksum((void*)buffer + 1, pl - 1)); // Check checksum
CU_ASSERT_EQUAL(th->addr_length, strlen(addr) + 1); // Check address string length
CU_ASSERT_EQUAL(th->port, port); // Check port number
CU_ASSERT_STRING_EQUAL(buffer + sizeof(ksnTCPProxyHeader), addr); // Check address
CU_ASSERT_EQUAL(th->packet_length, data_len); // Check package length
CU_ASSERT_STRING_EQUAL(buffer + sizeof(ksnTCPProxyHeader) + th->addr_length, data); // Check package
CU_ASSERT_EQUAL(pl, sizeof(ksnTCPProxyHeader) + th->addr_length + data_len); // Check package length
// 2) Check Create package function buffer size errors
// Create package with buffer less than header size
size_t pl_err = ksnTCPProxyPackageCreate(buffer, sizeof(ksnTCPProxyHeader) - 1, addr, port, CMD_TCPP_PROXY, data, data_len);
CU_ASSERT_EQUAL(pl_err, -1);
// Create package with buffer less than package size
pl_err = ksnTCPProxyPackageCreate(buffer, pl - 1, addr, port, CMD_TCPP_PROXY, data, data_len);
CU_ASSERT_EQUAL(pl_err, -2);
// 3) Process package function
// Create regular package
pl = ksnTCPProxyPackageCreate(buffer, buffer_len, addr, port, CMD_TCPP_PROXY, data, data_len);
CU_ASSERT(pl > 0); // Packet created (the buffer has enough size)
// Process package
int rv = ksnTCPProxyPackageProcess(&tpd->packet, buffer, pl);
// Check saved buffer header parameters
CU_ASSERT(rv > 0); // Packet process successfully
//CU_ASSERT(pl > 0); // Packet created (the buffer has enough size)
CU_ASSERT_EQUAL(tpd->packet.header->checksum, get_byte_checksum((void*)tpd->packet.buffer + 1, pl - 1)); // Check checksum
CU_ASSERT_EQUAL(tpd->packet.header->addr_length, strlen(addr) + 1); // Check address string length
CU_ASSERT_EQUAL(tpd->packet.header->port, port); // Check port number
CU_ASSERT_STRING_EQUAL(tpd->packet.buffer + sizeof(ksnTCPProxyHeader), addr); // Check address
CU_ASSERT_EQUAL(tpd->packet.header->packet_length, data_len); // Check package length
CU_ASSERT_STRING_EQUAL(tpd->packet.buffer + sizeof(ksnTCPProxyHeader) + tpd->packet.header->addr_length, data); // Check package
CU_ASSERT_EQUAL(rv, pl); // Check package length
// 4) Check receiving two packets which was combined into one buffer by small parts
// Create buffer with two packet
size_t pl_comb = 0; // Combined packet size
int num_pack = 0; // Number of packets
pl = ksnTCPProxyPackageCreate(buffer, buffer_len, addr, port, CMD_TCPP_PROXY, data, data_len);
CU_ASSERT(pl > 0); // Packet created (the buffer has enough size)
// printf(" pl = %d,", (int) pl );
pl_comb += pl;
num_pack++;
pl = ksnTCPProxyPackageCreate(buffer + pl, buffer_len - pl, addr, port, CMD_TCPP_PROXY, data2, data2_len);
CU_ASSERT(pl > 0); // Packet created (the buffer has enough size)
// printf(" pl = %d,", (int) pl );
pl_comb += pl;
num_pack++;
// Process combined buffer
int i;
size_t ptr = 0; // Pointer in combined buffer
size_t num_processed = 0; // Number of processed packets
size_t len = pl_comb / 10;
// printf(" pl_comb = %d, len = %d, i:", (int) pl_comb, (int) len );
for(i = 0; ptr < pl_comb; i++) {
// printf(" %d", i+1);
// Send part of buffer to Package Process
if( (len + ptr) > pl_comb ) {
len = pl_comb - ptr;
// printf(", final len = %d", len);
}
if(len > 0) {
rv = ksnTCPProxyPackageProcess(&tpd->packet, buffer + ptr, len);
CU_ASSERT(rv >= 0);
if(rv > 0) {
// printf(", processed = %d, ptr = %d", rv, tp->packet.ptr);
CU_ASSERT_EQUAL(tpd->packet.header->checksum, get_byte_checksum((void*)tpd->packet.buffer + 1, rv - 1)); // Check checksum
CU_ASSERT_EQUAL(tpd->packet.header->addr_length, strlen(addr) + 1); // Check address string length
CU_ASSERT_EQUAL(tpd->packet.header->port, port); // Check port number
CU_ASSERT_STRING_EQUAL(tpd->packet.buffer + sizeof(ksnTCPProxyHeader), addr); // Check address
num_processed++;
}
ptr += len;
}
}
// printf(", num_processed = %d ", num_processed);
CU_ASSERT(num_processed == num_pack);
// 5) Check receiving two packets which was combined into one buffer by one buffer
// Create buffer with two packet
pl_comb = 0; // Combined packet size
num_pack = 0; // Number of packets
num_processed = 0; // Number of processed packets
pl = ksnTCPProxyPackageCreate(buffer, buffer_len, addr, port, CMD_TCPP_PROXY, data, data_len);
CU_ASSERT(pl > 0); // Packet created (the buffer has enough size)
// printf(" pl = %d,", (int) pl );
pl_comb += pl;
num_pack++;
pl = ksnTCPProxyPackageCreate(buffer + pl, buffer_len - pl, addr, port, CMD_TCPP_PROXY, data2, data2_len);
CU_ASSERT(pl > 0); // Packet created (the buffer has enough size)
// printf(" pl = %d,", (int) pl );
pl_comb += pl;
num_pack++;
// Process first packet in combined buffer
rv = ksnTCPProxyPackageProcess(&tpd->packet, buffer, pl_comb);
CU_ASSERT(rv >= 0);
if(rv > 0) {
num_processed++;
// printf(" processed = %d, ptr = %d", rv, tp->packet.ptr);
// Process next packets in combined buffer
while((pl_comb - rv) > 0) {
pl_comb -= rv;
rv = ksnTCPProxyPackageProcess(&tpd->packet, buffer, 0);
CU_ASSERT(rv >= 0);
if(rv > 0) {
// printf(" processed = %d, ptr = %d", rv, tp->packet.ptr);
num_processed++;
}
}
// printf(", num_processed = %d ", num_processed);
}
// Destroy ksnTRUDPClass
CU_PASS("Destroy ksnTCPProxyClass done");
}
int add_suite_5_tests(void) {
// Add the tests to the suite
if ((NULL == CU_add_test(pSuite, "Initialize/Destroy TCP Proxy module", test_5_1))
|| (NULL == CU_add_test(pSuite, "Create and Process TCP Proxy Package functions", test_5_2))
) {
CU_cleanup_registry();
return CU_get_error();
}
return 0;
}

Created on Sep 8, 2015, 11:10:22 AM

Macro Definition Documentation

#define ke_emul ( )
Value:
ksnetEvMgrClass *ke = &ke_obj; \
ke->teo_cfg.tcp_allow_f = 0; \
ke->teo_cfg.r_tcp_f = 0
struct ksnetEvMgrClass ksnetEvMgrClass
KSNet event manager functions data.
#define ke
KSNet event manager functions data.
Definition: ev_mgr.h:245

Emulate ksnetEvMgrClass.

Referenced by test_5_1(), and test_5_2().

Function Documentation

int add_suite_5_tests ( void  )

Add TCP Proxy suite tests.

Returns

References pSuite, test_5_1(), and test_5_2().

Referenced by main().

uint8_t get_byte_checksum ( const uint8_t *  data,
size_t  data_len 
)
size_t ksnTCPProxyPackageCreate ( void *  buffer,
size_t  buffer_length,
const char *  addr,
int  port,
int  cmd,
const void *  data,
size_t  data_length 
)

Create TCP Proxy package.

Create TCP Proxy package from peers UDP address and port, data buffer and its length

Parameters
bufferThe buffer to create package in
buffer_lengthPackage data length
addrString with peer UDP address
portUDP port number
cmdTCP Proxy protocol command
dataPackage data
data_lengthPackage data length
Returns
Return size of created package or error
Return values
>0 - size of created package
-1- error: The output buffer less than packet header
-2- error: The output buffer less than packet header + data

References ksnTCPProxyHeader::addr_length, ksnTCPProxyHeader::checksum, ksnTCPProxyHeader::command, get_byte_checksum(), ksnTCPProxyHeader::packet_checksum, ksnTCPProxyHeader::packet_length, ksnTCPProxyHeader::port, TCP_PROXY_VERSION, and ksnTCPProxyHeader::version.

Referenced by cmd_udpp_read_cb(), ksnTCPProxySendTo(), and test_5_2().

int ksnTCPProxyPackageProcess ( ksnTCPProxyPacketData packet,
void *  data,
size_t  data_length 
)

Process TCP proxy package.

Read tcp data from input buffer to tp->buffer until end of tcp proxy package, check checksum, take UDP address, port number and UDP package data.

Parameters
packetPointer to ksnTCPProxyPacketData
dataPointer to received TCP data
data_lengthTCP data length
Returns
Length of received packet, zero or error code. The TCP packets may be received combined to one big buffer. If packet processed and this function return value grate than 0 and less than input buffer size we need run this function again with data_length = 0 to process next part of input buffer.
(see the "5) Check receiving" code of the test_5_2() function of the test_tcp_proxy.c test)
Return values
>0- receiving done, the return value contain length of packet, the packet saved to ksnTCPProxyClass::buffer
0- continue reading current packet
-1- wrong process package stage
-2- wrong packet header checksum
-3- wrong packet checksum

References ksnTCPProxyHeader::addr_length, ksnTCPProxyPacketData::buffer, ksnTCPProxyHeader::checksum, get_byte_checksum(), ksnTCPProxyPacketData::header, ksnTCPProxyPackageProcess(), ksnTCPProxyPacketData::length, ksnTCPProxyHeader::packet_checksum, ksnTCPProxyHeader::packet_length, ksnTCPProxyHeader::port, PROCESS_PACKET, ksnTCPProxyPacketData::ptr, ksnTCPProxyPacketData::stage, WAIT_FOR_END, and WAIT_FOR_START.

Referenced by _cmd_tcpp_read_cb(), ksnTCPProxyPackageProcess(), and test_5_2().

void test_5_1 ( )

Initialize/Destroy TCP Proxy module.

References ke, ke_emul, ksnTCPProxyDestroy(), and ksnTCPProxyInit().

Referenced by add_suite_5_tests().

Variable Documentation

CU_pSuite pSuite

Referenced by add_suite_5_tests().