diff options
-rw-r--r-- | proto_lib/api_protocol.h | 8 | ||||
-rw-r--r-- | proto_lib/lib.project | 2 | ||||
-rw-r--r-- | proto_lib/packet.cpp | 42 | ||||
-rw-r--r-- | proto_lib/utilities.cpp | 26 | ||||
-rw-r--r-- | proto_test/main.cpp | 138 | ||||
-rw-r--r-- | proto_test/proto_test.project | 103 | ||||
-rw-r--r-- | restarter.workspace | 16 |
7 files changed, 318 insertions, 17 deletions
diff --git a/proto_lib/api_protocol.h b/proto_lib/api_protocol.h index b18d2e7..bebd401 100644 --- a/proto_lib/api_protocol.h +++ b/proto_lib/api_protocol.h @@ -24,9 +24,16 @@ struct service_s struct cmd { std::string command, description; +#ifdef DEBUG + bool operator==(const cmd&); +#endif }; std::string service; std::list<cmd> cmds; +#ifdef DEBUG + bool operator==(service_s&); + bool operator!=(service_s&); +#endif }; struct svc_cmd @@ -55,6 +62,7 @@ public: //client functions static packet *cli_make_init_packet(); //should be first packet to server 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 std::list<service_s> *cli_extract_services(packet&); diff --git a/proto_lib/lib.project b/proto_lib/lib.project index 65be5d8..d686c48 100644 --- a/proto_lib/lib.project +++ b/proto_lib/lib.project @@ -29,6 +29,7 @@ <Configuration Name="Debug" CompilerType="gnu g++" DebuggerType="GNU gdb debugger" Type="Static Library" BuildCmpWithGlobalSettings="append" BuildLnkWithGlobalSettings="append" BuildResWithGlobalSettings="append"> <Compiler Options="-g;-O0;-Wall" C_Options="-g;-O0;-Wall" Required="yes" PreCompiledHeader="" PCHInCommandLine="no" UseDifferentPCHFlags="no" PCHFlags=""> <IncludePath Value="."/> + <Preprocessor Value="DEBUG"/> </Compiler> <Linker Options="" Required="yes"/> <ResourceCompiler Options="" Required="no"/> @@ -74,6 +75,7 @@ <![CDATA[ + ]]> </Environment> <Debugger IsRemote="no" RemoteHostName="" RemoteHostPort="" DebuggerPath=""> diff --git a/proto_lib/packet.cpp b/proto_lib/packet.cpp index 92272a8..92d8508 100644 --- a/proto_lib/packet.cpp +++ b/proto_lib/packet.cpp @@ -18,6 +18,7 @@ #include <vector> #include <algorithm> #include <string.h> +#include <stdio.h> #include "api_protocol.h" #include "packet.h" #include "utilities.h" @@ -135,6 +136,12 @@ packet *packet::cli_make_command_packet(std::string &service, std::string &comma return new packet(v); } +packet *packet::cli_make_command_packet(const char* service, const char* command) +{ + std::string s = service, c = command; + return cli_make_command_packet(s, c); +} + packet *packet::cli_make_request_services_packet() { std::vector<unsigned char> v; @@ -165,17 +172,20 @@ std::list<service_s> *packet::cli_extract_services(packet& p) i2++; i2 = std::find(i2, i3, delimiter); bool bdesc = false; + std::string cmd, desc; while(i2 < i3) { - std::string cmd, desc; + cmd.clear(); desc.clear(); if(!bdesc) { + i++; //wtf ? for(; i < i2; ++i) cmd.push_back((char)*i); bdesc = true; } else { + i++; //wtf ? for(; i < i2; ++i) desc.push_back((char)*i); bdesc = false; @@ -187,6 +197,14 @@ std::list<service_s> *packet::cli_extract_services(packet& p) i2++; i2 = std::find(i2, i3, delimiter); } + i++; //wtf ? + desc.clear(); + for(; i < i2; ++i) + desc.push_back((char)*i); + service_s::cmd c; + c.command = cmd; + c.description = desc; + cmds.push_back(c); } else //found service without commands { @@ -194,8 +212,8 @@ std::list<service_s> *packet::cli_extract_services(packet& p) service.push_back((char)*i); } i = i3; - i2 = std::find(i, p.raw().end(), delimiter); - i3 = std::find(i, p.raw().end(), block_end); + i2 = std::find(i+1, p.raw().end(), delimiter); + i3 = std::find(i+1, p.raw().end(), block_end); } service_s s; s.cmds = cmds; @@ -223,17 +241,19 @@ std::string packet::cli_parse_command_reply(packet& p) bool packet::serv_validate_client_proto(packet &p) { - const std::vector<unsigned char> &data = p.raw(); - if(data.empty()) - return false; - if(std::search(data.begin(), data.end(), proto_header, proto_header + sizeof(proto_header)) == data.end()) + if(!p.is_good()) return false; - if(std::search(data.begin(), data.end(), proto_footer, proto_footer + sizeof(proto_footer)) == data.end()) + if(!p.is_client_packet()) return false; - std::vector<unsigned char>::const_iterator i = std::search(data.begin(), data.end(), cli_packet, cli_packet + sizeof(cli_packet)); - if(i == data.end()) + std::vector<unsigned char>::const_iterator i = std::search(p.raw().begin(), p.raw().end(), type_auth, type_auth + sizeof(type_auth)); + if(i == p.raw().end()) return false; - i += sizeof(cli_packet); + i += sizeof(type_auth); + i++; //wtf ? +#ifdef DEBUG + printf("version found %d\n", *i); + printf("actual version %d\n", proto_version); +#endif if(*i < proto_version) return false; return true; diff --git a/proto_lib/utilities.cpp b/proto_lib/utilities.cpp index 64f69bb..b54e45a 100644 --- a/proto_lib/utilities.cpp +++ b/proto_lib/utilities.cpp @@ -81,4 +81,30 @@ const unsigned char *to_internal_type(packet_type t) return type; } +#ifdef DEBUG +bool service_s::cmd::operator==(const cmd& c) +{ + if(c.command != command) + return false; + if(c.description != description) + return false; + return true; +} + +bool service_s::operator==(service_s& s) +{ + if(s.service != service) + return false; + for(std::list<service_s::cmd>::iterator i = s.cmds.begin(), end = s.cmds.end(); i != end; ++i) + { + if(std::find(cmds.begin(), cmds.end(), *i) == cmds.end()) + return false; + } + return true; +} +bool service_s::operator!=(service_s& s) +{ + return !(*this == s); +} +#endif }; diff --git a/proto_test/main.cpp b/proto_test/main.cpp new file mode 100644 index 0000000..28d04e9 --- /dev/null +++ b/proto_test/main.cpp @@ -0,0 +1,138 @@ + +#include <string> +#include <list> +#include <vector> +#include <iostream> +#include "api_protocol.h" + +using namespace proto; + +void print_vector(const std::vector<unsigned char> &v) +{ + for(std::vector<unsigned char>::const_iterator i = v.begin(), end = v.end(); i != end; ++i) + std::cout<<*i; + std::cout<<std::endl; +} + +std::list<std::string> test_creation() +{ + std::list<std::string> packets; + packet *p = packet::cli_make_init_packet(); + if(!p->is_good()) + packets.push_back("cli init packet"); +/* else + print_vector(p->raw()); */ + delete p; + p = packet::cli_make_command_packet("test_svc", "test_cmd"); + if(!p->is_good()) + packets.push_back("cli cmd packet"); +/* else + print_vector(p->raw()); */ + delete p; + p = packet::cli_make_request_services_packet(); + if(!p->is_good()) + packets.push_back("cli svc req packet"); +/* else + print_vector(p->raw()); */ + return packets; +} + +bool test_handshake() +{ + packet *p = packet::cli_make_init_packet(); + if(packet::serv_validate_client_proto(*p)) + return true; + else + return false; +} + +bool test_client_to_server_command() +{ + packet *p = packet::cli_make_command_packet("test srv 1", "command with spaces"); + svc_cmd c = packet::serv_extract_command(*p); + if(c.service != "test srv 1") + return false; + if(c.command != "command with spaces") + return false; + return true; +} + +bool test_services_extraction() +{ + packet *p = packet::cli_make_request_services_packet(); + if(p->get_type() != TYPE_SERVICES_REQUEST) + return false; + std::list<service_s> list1; + service_s s1; + s1.service = "test service 1"; + service_s::cmd c; + c.command = "command 1"; + c.description = "some desc"; + s1.cmds.push_back(c); + c.command = "command 2"; + c.description = ""; + s1.cmds.push_back(c); + c.command = "command 3"; + c.description = "command 3 desc"; + s1.cmds.push_back(c); + list1.push_back(s1); + s1.service = "sd,fhjsiufhifhirhwiefbbbbvwyefgweyfuyvbwf service 2 aaaaaaaaaaaaaaaaaa111111111111111111111111asdasdasd"; + c.command = "2command 1"; + c.description = "2some desc"; + s1.cmds.push_back(c); + c.command = "2command 2"; + c.description = ""; + s1.cmds.push_back(c); + c.command = "2command 3"; + c.description = "2command 3 desc"; + s1.cmds.push_back(c); + list1.push_back(s1); + s1.service = "sd,fhjsiufhiadadsdfgdffghghjbnfdghdgffhirhwiefbbbbvwyefgweyfuyvbwf service 2 aaaaaaaaaaaaaaaaaa111111111111111111111111asdasdasd"; + c.command = "32commanasdasdd 1"; + c.description = "32some desc"; + s1.cmds.push_back(c); + c.command = "32command 2"; + c.description = ""; + s1.cmds.push_back(c); + c.command = "32command 3"; + c.description = "32command 3 desc"; + s1.cmds.push_back(c); + list1.push_back(s1); + delete p; + p = packet::serv_make_services_packet(list1); + std::list<service_s> *list2 = packet::cli_extract_services(*p); + for(std::list<service_s>::iterator i = list2->begin(), end = list2->end(); i != end; ++i) + { + //if(std::find(list1.begin(), list1.end(), *i) == list1.end()) //fuck + for(std::list<service_s>::iterator ii = list1.begin(), end = list1.end(); ii != end; ++ii) + if(*ii != *i) + return false; + } + return true; +} + +int main() +{ + std::list<std::string> bad_packets = test_creation(); + if(!bad_packets.empty()) + { + std::cout<<"packet creation test: ERROR"<<std::endl; + for(std::list<std::string>::iterator i = bad_packets.begin(), end = bad_packets.end(); i != end; ++i) + std::cout<<*i<<std::endl; + } + else + std::cout<<"packet creation test: OK"<<std::endl; + if(test_handshake()) + std::cout<<"handshake test: OK"<<std::endl; + else + std::cout<<"handshake test: ERROR"<<std::endl; + if(test_client_to_server_command()) + std::cout<<"client to server cmd test: OK"<<std::endl; + else + std::cout<<"client to server cmd test: ERROR"<<std::endl; + if(test_services_extraction()) + std::cout<<"service extraction test: OK"<<std::endl; + else + std::cout<<"service extraction test: ERROR"<<std::endl; + return 0; +} diff --git a/proto_test/proto_test.project b/proto_test/proto_test.project new file mode 100644 index 0000000..ffb4e33 --- /dev/null +++ b/proto_test/proto_test.project @@ -0,0 +1,103 @@ +<?xml version="1.0" encoding="utf-8"?> +<CodeLite_Project Name="proto_test" InternalType="Console"> + <Plugins> + <Plugin Name="qmake"> + <![CDATA[00010001N0005Debug000000000000]]> + </Plugin> + </Plugins> + <Description/> + <Dependencies/> + <VirtualDirectory Name="src"> + <File Name="main.cpp"/> + </VirtualDirectory> + <Settings Type="Executable"> + <GlobalSettings> + <Compiler Options="" C_Options=""> + <IncludePath Value="."/> + </Compiler> + <Linker Options=""> + <LibraryPath Value="."/> + </Linker> + <ResourceCompiler Options=""/> + </GlobalSettings> + <Configuration Name="Debug" CompilerType="gnu g++" DebuggerType="GNU gdb debugger" Type="Executable" BuildCmpWithGlobalSettings="append" BuildLnkWithGlobalSettings="append" BuildResWithGlobalSettings="append"> + <Compiler Options="-g;-O0;-Wall" C_Options="-g;-O0;-Wall" Required="yes" PreCompiledHeader="" PCHInCommandLine="no" UseDifferentPCHFlags="no" PCHFlags=""> + <IncludePath Value="."/> + <IncludePath Value="../proto_lib"/> + <Preprocessor Value="DEBUG"/> + </Compiler> + <Linker Options="" Required="yes"> + <LibraryPath Value="../proto_lib/Debug"/> + <Library Value="proto"/> + </Linker> + <ResourceCompiler Options="" Required="no"/> + <General OutputFile="$(IntermediateDirectory)/$(ProjectName)" IntermediateDirectory="./Debug" Command="./$(ProjectName)" CommandArguments="" UseSeparateDebugArgs="no" DebugArguments="" WorkingDirectory="$(IntermediateDirectory)" PauseExecWhenProcTerminates="yes"/> + <Environment EnvVarSetName="<Use Defaults>" DbgSetName="<Use Defaults>"> + <![CDATA[]]> + </Environment> + <Debugger IsRemote="no" RemoteHostName="" RemoteHostPort="" DebuggerPath=""> + <PostConnectCommands/> + <StartupCommands/> + </Debugger> + <PreBuild/> + <PostBuild/> + <CustomBuild Enabled="no"> + <RebuildCommand/> + <CleanCommand/> + <BuildCommand/> + <PreprocessFileCommand/> + <SingleFileCommand/> + <MakefileGenerationCommand/> + <ThirdPartyToolName>None</ThirdPartyToolName> + <WorkingDirectory/> + </CustomBuild> + <AdditionalRules> + <CustomPostBuild/> + <CustomPreBuild/> + </AdditionalRules> + <Completion EnableCpp11="no"> + <ClangCmpFlagsC/> + <ClangCmpFlags/> + <ClangPP/> + <SearchPaths/> + </Completion> + </Configuration> + <Configuration Name="Release" CompilerType="gnu gcc" DebuggerType="GNU gdb debugger" Type="" BuildCmpWithGlobalSettings="append" BuildLnkWithGlobalSettings="append" BuildResWithGlobalSettings="append"> + <Compiler Options="-O2;-Wall" C_Options="-O2;-Wall" Required="yes" PreCompiledHeader="" PCHInCommandLine="no" UseDifferentPCHFlags="no" PCHFlags=""> + <IncludePath Value="."/> + </Compiler> + <Linker Options="" Required="yes"/> + <ResourceCompiler Options="" Required="no"/> + <General OutputFile="$(IntermediateDirectory)/$(ProjectName)" IntermediateDirectory="./Release" Command="./$(ProjectName)" CommandArguments="" UseSeparateDebugArgs="no" DebugArguments="" WorkingDirectory="$(IntermediateDirectory)" PauseExecWhenProcTerminates="yes"/> + <Environment EnvVarSetName="<Use Defaults>" DbgSetName="<Use Defaults>"> + <![CDATA[]]> + </Environment> + <Debugger IsRemote="no" RemoteHostName="" RemoteHostPort="" DebuggerPath=""> + <PostConnectCommands/> + <StartupCommands/> + </Debugger> + <PreBuild/> + <PostBuild/> + <CustomBuild Enabled="no"> + <RebuildCommand/> + <CleanCommand/> + <BuildCommand/> + <PreprocessFileCommand/> + <SingleFileCommand/> + <MakefileGenerationCommand/> + <ThirdPartyToolName>None</ThirdPartyToolName> + <WorkingDirectory/> + </CustomBuild> + <AdditionalRules> + <CustomPostBuild/> + <CustomPreBuild/> + </AdditionalRules> + <Completion EnableCpp11="no"> + <ClangCmpFlagsC/> + <ClangCmpFlags/> + <ClangPP/> + <SearchPaths/> + </Completion> + </Configuration> + </Settings> +</CodeLite_Project> diff --git a/restarter.workspace b/restarter.workspace index 5c304ff..bb329b0 100644 --- a/restarter.workspace +++ b/restarter.workspace @@ -2,22 +2,26 @@ <CodeLite_Workspace Name="restarter" Database="./restarter.tags"> <Project Name="restarter_server" Path="server/restarter_server.project" Active="No"/> <Project Name="lib" Path="proto_lib/lib.project" Active="No"/> - <Project Name="unix_exec_service" Path="services/unix_exec_service/unix_exec_service.project" Active="Yes"/> + <Project Name="unix_exec_service" Path="services/unix_exec_service/unix_exec_service.project" Active="No"/> + <Environment> + <![CDATA[ + + + ]]> + </Environment> + <Project Name="proto_test" Path="proto_test/proto_test.project" Active="Yes"/> <BuildMatrix> <WorkspaceConfiguration Name="Debug" Selected="yes"> <Project Name="restarter_server" ConfigName="Debug"/> <Project Name="lib" ConfigName="Debug"/> <Project Name="unix_exec_service" ConfigName="Debug"/> + <Project Name="proto_test" ConfigName="Debug"/> </WorkspaceConfiguration> <WorkspaceConfiguration Name="Release" Selected="yes"> <Project Name="restarter_server" ConfigName="Release"/> <Project Name="lib" ConfigName="Release"/> <Project Name="unix_exec_service" ConfigName="Release"/> + <Project Name="proto_test" ConfigName="Release"/> </WorkspaceConfiguration> </BuildMatrix> - <Environment> - <![CDATA[ - - ]]> - </Environment> </CodeLite_Workspace> |