1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
|
#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)
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.cmds.clear();
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.cmds.clear();
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);
/* std::cout<<"\n\nlist1 content\n\n\n";
for(std::list<service_s>::iterator i = list1.begin(), end = list1.end(); i != end; ++i)
{
std::cout<<"service name: "<<i->service<<"\n\t";
for(std::list<service_s::cmd>::iterator ii = i->cmds.begin(), eend = i->cmds.end(); ii != eend; ++ii)
std::cout<<ii->command<<"|"<<ii->description<<"; ";
std::cout<<"\n\n";
}
std::cout<<"\n\nlist2 content\n\n\n";
for(std::list<service_s>::iterator i = list2->begin(), end = list2->end(); i != end; ++i)
{
std::cout<<"service name: "<<i->service<<"\n\t";
for(std::list<service_s::cmd>::iterator ii = i->cmds.begin(), eend = i->cmds.end(); ii != eend; ++ii)
std::cout<<ii->command<<"|"<<ii->description<<"; ";
std::cout<<"\n\n";
} */
if(list1 != *list2)
return false;
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();
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;
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;
}
|