summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGluzskiy Alexandr <sss@sss.chaoslab.ru>2013-02-13 20:32:10 +0200
committerGluzskiy Alexandr <sss@sss.chaoslab.ru>2013-02-13 20:32:10 +0200
commit728653fbb36727acfb5a4cd480b7fa5d45adfa89 (patch)
tree0f5f6c90749571af4375f9ff59f39cecaff6cfac
parent49729a7593320662545b7c795f028d0f87827b0f (diff)
proto lib fixes
more tests
-rw-r--r--proto_lib/api_protocol.h10
-rw-r--r--proto_lib/packet.cpp44
-rw-r--r--proto_lib/utilities.cpp15
-rw-r--r--proto_lib/utilities.h2
-rw-r--r--proto_test/main.cpp61
5 files changed, 106 insertions, 26 deletions
diff --git a/proto_lib/api_protocol.h b/proto_lib/api_protocol.h
index afa8224..9a82e31 100644
--- a/proto_lib/api_protocol.h
+++ b/proto_lib/api_protocol.h
@@ -41,7 +41,7 @@ struct svc_cmd
std::string service, command;
};
-enum packet_type {TYPE_AUTH_REPLY, TYPE_AUTH_REQUEST, TYPE_SERVICES_REPLY, TYPE_SERVICES_REQUEST, TYPE_COMMAND_REPLY, TYPE_COMMAND_REQUEST, TYPE_CUSTOM, TYPE_UNKNOWN};
+enum packet_type {TYPE_AUTH, TYPE_SERVICES, TYPE_COMMAND, TYPE_CUSTOM, TYPE_UNKNOWN};
enum status {STATUS_FAILURE = 0x00, STATUS_SUCCESS = 0x01};
class packet
@@ -51,7 +51,7 @@ public:
const std::vector<unsigned char> &raw();
packet_type get_type();
bool is_good();
- bool check_status(); //false on fail
+ status get_status(); //false on fail
bool is_server_packet();
bool is_client_packet();
bool is_status_packet();
@@ -64,6 +64,7 @@ public:
static packet *cli_make_command_packet(std::string &service, std::string &command);
static packet *cli_make_command_packet(const char* service, const char* command);
static packet *cli_make_request_services_packet();
+ static packet *cli_make_status_packet(packet_type type, status s);
static std::list<service_s> *cli_extract_services(packet&);
static std::string cli_parse_command_reply(packet&);
@@ -72,12 +73,11 @@ public:
static bool serv_validate_client_proto(packet&); //false on fail
static packet *serv_make_services_packet(std::list<service_s>&);
static packet *serv_make_command_reply_packet(std::string&, status s);
+ static packet *serv_make_command_reply_packet(const char*, status s);
static packet *serv_make_command_reply_packet(std::vector<unsigned char>&, status s);
+ static packet *serv_make_status_packet(packet_type type, status s);
static svc_cmd serv_extract_command(packet&);
- //generic
- static packet *make_status_packet(packet_type type, status s);
-
private:
std::vector<unsigned char> data;
};
diff --git a/proto_lib/packet.cpp b/proto_lib/packet.cpp
index 0487f4f..8dc1b12 100644
--- a/proto_lib/packet.cpp
+++ b/proto_lib/packet.cpp
@@ -93,21 +93,20 @@ packet_type packet::get_type()
type.push_back(*i);
i++;
type.push_back(*i);
- bool cli = is_client_packet();
if(std::search(type.begin(), type.end(), type_auth, type_auth + sizeof(type_auth)) != type.end())
- return cli?TYPE_AUTH_REQUEST:TYPE_AUTH_REPLY;
+ return TYPE_AUTH;
if(std::search(type.begin(), type.end(), type_services, type_services + sizeof(type_services)) != type.end())
- return cli?TYPE_SERVICES_REQUEST:TYPE_SERVICES_REPLY;
+ return TYPE_SERVICES;
if(std::search(type.begin(), type.end(), type_command, type_command + sizeof(type_command)) != type.end())
- return cli?TYPE_COMMAND_REQUEST:TYPE_COMMAND_REPLY;
+ return TYPE_COMMAND;
return TYPE_UNKNOWN;
}
bool packet::is_status_packet()
{
const unsigned char *type = to_internal_type(get_type());
- std::vector<unsigned char>::iterator i = std::search(data.begin(), data.end(), type, type + sizeof(type));
- i += sizeof(type);
+ std::vector<unsigned char>::iterator i = std::search(data.begin(), data.end(), type, type + 2);
+ i += 2;
if(*i == STATUS_SUCCESS || *i == STATUS_FAILURE)
return true;
return false;
@@ -283,6 +282,12 @@ packet *packet::serv_make_command_reply_packet(std::string &str, status s)
return new packet(v);
}
+packet *packet::serv_make_command_reply_packet(const char* data, status s)
+{
+ std::string str = data;
+ return serv_make_command_reply_packet(str, s);
+}
+
packet *packet::serv_make_command_reply_packet(std::vector<unsigned char>& data, status s)
{
std::vector<unsigned char> v;
@@ -294,28 +299,33 @@ packet *packet::serv_make_command_reply_packet(std::vector<unsigned char>& data,
return new packet(v);
}
-packet *packet::make_status_packet(packet_type t, status s)
+packet *packet::cli_make_status_packet(packet_type type, status s)
+{
+ std::vector<unsigned char> v;
+ pack_cli_header(v);
+ make_status_packet(type, s, v);
+ return new packet(v);
+}
+
+packet *packet::serv_make_status_packet(packet_type type, status s)
{
- const unsigned char *type = to_internal_type(t);
std::vector<unsigned char> v;
pack_serv_header(v);
- pack_buffer(type, sizeof(type), v);
- v.push_back(s);
- pack_buffer(proto_footer, sizeof(proto_footer), v);
+ make_status_packet(type, s, v);
return new packet(v);
}
-bool packet::check_status()
+
+status packet::get_status()
{
const unsigned char *type = to_internal_type(get_type());
- std::vector<unsigned char>::const_iterator i = std::search(data.begin(), data.end(), type, type + sizeof(type));
+ std::vector<unsigned char>::const_iterator i = std::search(data.begin(), data.end(), type, type + 2);
if(i != data.end())
{
- i += sizeof(type);
- if(*i == STATUS_SUCCESS)
- return true;
+ i += 2;
+ return (status)*i;
}
- return false;
+ return STATUS_FAILURE;
}
svc_cmd packet::serv_extract_command(packet& p)
diff --git a/proto_lib/utilities.cpp b/proto_lib/utilities.cpp
index ee5e9b4..2a7d43d 100644
--- a/proto_lib/utilities.cpp
+++ b/proto_lib/utilities.cpp
@@ -66,13 +66,13 @@ const unsigned char *to_internal_type(packet_type t)
const unsigned char *type = NULL;
switch(t)
{
- case TYPE_AUTH_REPLY: case TYPE_AUTH_REQUEST:
+ case TYPE_AUTH:
type = type_auth;
break;
- case TYPE_SERVICES_REPLY: case TYPE_SERVICES_REQUEST:
+ case TYPE_SERVICES:
type = type_services;
break;
- case TYPE_COMMAND_REPLY: case TYPE_COMMAND_REQUEST:
+ case TYPE_COMMAND:
type = type_command;
break;
case TYPE_UNKNOWN: default:
@@ -81,6 +81,15 @@ const unsigned char *to_internal_type(packet_type t)
return type;
}
+void make_status_packet(packet_type t, status s, std::vector<unsigned char> &v)
+{
+ const unsigned char *type = to_internal_type(t);
+ pack_buffer(type, 2, v);
+ v.push_back(s);
+ pack_buffer(proto_footer, sizeof(proto_footer), v);
+}
+
+
#ifdef DEBUG
bool service_s::cmd::operator==(const cmd& c) const
{
diff --git a/proto_lib/utilities.h b/proto_lib/utilities.h
index 6b41ca3..d2a3199 100644
--- a/proto_lib/utilities.h
+++ b/proto_lib/utilities.h
@@ -30,6 +30,8 @@ void pack_buffer(std::vector<unsigned char> &source, std::vector<unsigned char>
void pack_buffer(std::string &str, std::vector<unsigned char> &v);
void pack_buffer(const char* str, std::vector<unsigned char> &v);
const unsigned char *to_internal_type(packet_type t);
+void make_status_packet(packet_type t, status s, std::vector<unsigned char> &v);
+
};
#endif
diff --git a/proto_test/main.cpp b/proto_test/main.cpp
index ba0b8e8..1083e44 100644
--- a/proto_test/main.cpp
+++ b/proto_test/main.cpp
@@ -60,7 +60,7 @@ bool test_client_to_server_command()
bool test_services_extraction()
{
packet *p = packet::cli_make_request_services_packet();
- if(p->get_type() != TYPE_SERVICES_REQUEST)
+ if(p->get_type() != TYPE_SERVICES)
return false;
std::list<service_s> list1;
service_s s1;
@@ -124,6 +124,57 @@ bool test_services_extraction()
return true;
}
+bool test_command_exchange()
+{
+ packet *p = packet::cli_make_command_packet("test service", "test command");
+ svc_cmd c = packet::serv_extract_command(*p);
+ if(c.command != "test command")
+ return false;
+ if(c.service != "test service")
+ return false;
+ delete p;
+ p = packet::serv_make_command_reply_packet("success", STATUS_SUCCESS);
+ std::string s = packet::cli_parse_command_reply(*p);
+ if(s != "success")
+ return false;
+ if(p->get_status() != STATUS_SUCCESS)
+ return false;
+ delete p;
+ p = packet::serv_make_command_reply_packet("failure", STATUS_FAILURE);
+ s = packet::cli_parse_command_reply(*p);
+ if(s != "failure")
+ return false;
+ if(p->get_status() != STATUS_FAILURE)
+ return false;
+ delete p;
+ return true;
+}
+
+bool test_status_packet()
+{
+ packet *p = packet::serv_make_status_packet(TYPE_AUTH, STATUS_SUCCESS);
+ if(!p->is_good())
+ return false;
+ if(p->get_status() != STATUS_SUCCESS)
+ return false;
+ if(p->get_type() != TYPE_AUTH)
+ return false;
+ if(!p->is_server_packet())
+ return false;
+ delete p;
+ p = packet::cli_make_status_packet(TYPE_SERVICES, STATUS_FAILURE);
+ if(p->get_status() != STATUS_FAILURE)
+ return false;
+ if(p->get_type() != TYPE_SERVICES)
+ return false;
+ if(!p->is_good())
+ return false;
+ if(!p->is_client_packet())
+ return false;
+ delete p;
+ return true;
+}
+
int main()
{
std::list<std::string> bad_packets = test_creation();
@@ -147,5 +198,13 @@ int main()
std::cout<<"service extraction test: OK"<<std::endl;
else
std::cout<<"service extraction test: ERROR"<<std::endl;
+ if(test_command_exchange())
+ std::cout<<"command exchange test: OK"<<std::endl;
+ else
+ std::cout<<"command exchange test: ERROR"<<std::endl;
+ if(test_status_packet())
+ std::cout<<"status packet test: OK"<<std::endl;
+ else
+ std::cout<<"status packet test: ERROR"<<std::endl;
return 0;
}