]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/authorization/stress/stress.cpp
TEST_SETTINGS_LOCAL moved outside the namespace, more assertions added
[stg.git] / projects / stargazer / plugins / authorization / stress / stress.cpp
1 /*
2  *    This program is free software; you can redistribute it and/or modify
3  *    it under the terms of the GNU General Public License as published by
4  *    the Free Software Foundation; either version 2 of the License, or
5  *    (at your option) any later version.
6  *
7  *    This program is distributed in the hope that it will be useful,
8  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
9  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  *    GNU General Public License for more details.
11  *
12  *    You should have received a copy of the GNU General Public License
13  *    along with this program; if not, write to the Free Software
14  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15  */
16
17 /*
18  *    Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
19  */
20
21 /*
22  $Revision: 1.5 $
23  $Date: 2009/06/19 12:50:32 $
24  $Author: faust $
25  */
26
27 #include <stdio.h>
28 #include <unistd.h>
29
30 #include <csignal>
31 #include <algorithm>
32
33 #include "stg/user.h"
34 #include "stg/common.h"
35 #include "stg/user_property.h"
36
37 #include "stress.h"
38
39 class STRESS_CREATOR
40 {
41 private:
42     AUTH_STRESS * dc;
43
44 public:
45     STRESS_CREATOR()
46         {
47         printfd(__FILE__, "constructor STRESS_CREATOR\n");
48         dc = new AUTH_STRESS();
49         };
50     ~STRESS_CREATOR()
51         {
52         printfd(__FILE__, "destructor STRESS_CREATOR\n");
53         delete dc;
54         };
55
56     PLUGIN * GetPlugin()
57     {
58         return dc;
59     };
60 };
61 //-----------------------------------------------------------------------------
62 //-----------------------------------------------------------------------------
63 //-----------------------------------------------------------------------------
64 STRESS_CREATOR stressc;
65 //-----------------------------------------------------------------------------
66 //-----------------------------------------------------------------------------
67 //-----------------------------------------------------------------------------
68 // ëÌÁÓÓ ÄÌÑ ÐÏÉÓËÁ ÀÚÅÒÁ × ÓÐÉÓËÅ ÎÏÔÉÆÉËÁÔÏÒÏ×
69 template <typename varType>
70 class IS_CONTAINS_USER: public binary_function<varType, USER_PTR, bool>
71 {
72 public:
73     bool operator()(varType notifier, USER_PTR user) const
74         {
75         return notifier.GetUser() == user;
76         };
77 };
78 //-----------------------------------------------------------------------------
79 //-----------------------------------------------------------------------------
80 //-----------------------------------------------------------------------------
81 PLUGIN * GetPlugin()
82 {
83 //printf("BASE_CAPTURER * GetCapturer()\n");
84 return stressc.GetPlugin();
85 }
86 //-----------------------------------------------------------------------------
87 //-----------------------------------------------------------------------------
88 //-----------------------------------------------------------------------------
89 AUTH_STRESS_SETTINGS::AUTH_STRESS_SETTINGS()
90     : averageOnlineTime(0)
91 {
92 }
93 //-----------------------------------------------------------------------------
94 int AUTH_STRESS_SETTINGS::ParseIntInRange(const string & str, int min, int max, int * val)
95 {
96 if (str2x(str.c_str(), *val))
97     {
98     errorStr = "Incorrect value \'" + str + "\'.";
99     return -1;
100     }
101 if (*val < min || *val > max)
102     {
103     errorStr = "Value \'" + str + "\' out of range.";
104     return -1;
105     }
106 return 0;
107 }
108 //-----------------------------------------------------------------------------
109 int AUTH_STRESS_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
110 {
111 PARAM_VALUE pv;
112 vector<PARAM_VALUE>::const_iterator pvi;
113
114 pv.param = "AverageOnlineTime";
115 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
116 if (pvi == s.moduleParams.end())
117     {
118     errorStr = "Parameter \'" + pv.param + "\' not found.";
119     return -1;
120     }
121
122 if (ParseIntInRange(pvi->value[0], 5, 10*3600, &averageOnlineTime))
123     {
124     errorStr = "Cannot parse parameter \'" + pv.param + "\': " + errorStr;
125     return -1;
126     }
127
128 return 0;
129 }
130 //-----------------------------------------------------------------------------
131 int AUTH_STRESS_SETTINGS::GetAverageOnlineTime() const
132 {
133 return averageOnlineTime;
134 }
135 //-----------------------------------------------------------------------------
136 //-----------------------------------------------------------------------------
137 //-----------------------------------------------------------------------------
138 const string AUTH_STRESS::GetVersion() const
139 {
140 return "Stress authorizator v.0.1";
141 }
142 //-----------------------------------------------------------------------------
143 AUTH_STRESS::AUTH_STRESS()
144 {
145 pthread_mutex_init(&mutex, NULL);
146 isRunning = false;
147 }
148 //-----------------------------------------------------------------------------
149 void AUTH_STRESS::SetUsers(USERS * u)
150 {
151 users = u;
152 }
153 //-----------------------------------------------------------------------------
154 void AUTH_STRESS::SetSettings(const MODULE_SETTINGS & s)
155 {
156 settings = s;
157 }
158 //-----------------------------------------------------------------------------
159 int AUTH_STRESS::ParseSettings()
160 {
161 int ret = stressSettings.ParseSettings(settings);
162 if (ret)
163     errorStr = stressSettings.GetStrError();
164 return ret;
165 }
166 //-----------------------------------------------------------------------------
167 const string & AUTH_STRESS::GetStrError() const
168 {
169 return errorStr;
170 }
171 //-----------------------------------------------------------------------------
172 int AUTH_STRESS::Start()
173 {
174 GetUsers();
175 nonstop = true;
176
177 list<USER_PTR>::iterator users_iter;
178
179 onAddUserNotifier.SetAuthorizator(this);
180 onDelUserNotifier.SetAuthorizator(this);
181 users->AddNotifierUserAdd(&onAddUserNotifier);
182 users->AddNotifierUserDel(&onDelUserNotifier);
183
184 if (!isRunning)
185     {
186     if (pthread_create(&thread, NULL, Run, this))
187         {
188         errorStr = "Cannot create thread.";
189         return -1;
190         }
191     }
192
193 users_iter = usersList.begin();
194 while (users_iter != usersList.end())
195     {
196     Authorize(*users_iter);
197     users_iter++;
198     }
199
200 //isRunning = true;
201 return 0;
202 }
203 //-----------------------------------------------------------------------------
204 int AUTH_STRESS::Stop()
205 {
206 nonstop = false;
207 if (isRunning)
208     {
209     //5 seconds to thread stops itself
210     int i;
211     for (i = 0; i < 25; i++)
212         {
213         if (!isRunning)
214             break;
215         usleep(200000);
216         }
217
218     //after 5 seconds waiting thread still running. now killing it
219     if (isRunning)
220         {
221         if (pthread_kill(thread, SIGINT))
222             {
223             errorStr = "Cannot kill thread.";
224             return -1;
225             }
226         printfd(__FILE__, "AUTH_STRESS killed Run\n");
227         }
228     }
229
230 users->DelNotifierUserAdd(&onAddUserNotifier);
231 users->DelNotifierUserDel(&onDelUserNotifier);
232
233 return 0;
234 }
235 //-----------------------------------------------------------------------------
236 bool AUTH_STRESS::IsRunning()
237 {
238 return isRunning;
239 }
240 //-----------------------------------------------------------------------------
241 uint16_t AUTH_STRESS::GetStartPosition() const
242 {
243 return 70;
244 }
245 //-----------------------------------------------------------------------------
246 uint16_t AUTH_STRESS::GetStopPosition() const
247 {
248 return 70;
249 }
250 //-----------------------------------------------------------------------------
251 void AUTH_STRESS::SetUserNotifiers(USER_PTR u)
252 {
253 // ---------- IP -------------------
254 CHG_BEFORE_NOTIFIER<USER_IPS> BeforeChgIPNotifier;
255 CHG_AFTER_NOTIFIER<USER_IPS>  AfterChgIPNotifier;
256
257 BeforeChgIPNotifier.SetAuthorizator(this);
258 BeforeChgIPNotifier.SetUser(u);
259 BeforeChgIPNotifierList.push_front(BeforeChgIPNotifier);
260
261 AfterChgIPNotifier.SetAuthorizator(this);
262 AfterChgIPNotifier.SetUser(u);
263 AfterChgIPNotifierList.push_front(AfterChgIPNotifier);
264
265 u->GetProperty().ips.AddBeforeNotifier(&(*BeforeChgIPNotifierList.begin()));
266 u->GetProperty().ips.AddAfterNotifier(&(*AfterChgIPNotifierList.begin()));
267 // ---------- IP end ---------------
268 }
269 //-----------------------------------------------------------------------------
270 void AUTH_STRESS::UnSetUserNotifiers(USER_PTR u)
271 {
272 // ---          IP              ---
273 IS_CONTAINS_USER<CHG_BEFORE_NOTIFIER<USER_IPS> > IsContainsUserIPB;
274 IS_CONTAINS_USER<CHG_AFTER_NOTIFIER<USER_IPS> >  IsContainsUserIPA;
275
276 list<CHG_BEFORE_NOTIFIER<USER_IPS> >::iterator ipBIter;
277 list<CHG_AFTER_NOTIFIER<USER_IPS> >::iterator  ipAIter;
278
279 ipBIter = find_if(BeforeChgIPNotifierList.begin(),
280                   BeforeChgIPNotifierList.end(),
281                   bind2nd(IsContainsUserIPB, u));
282
283 if (ipBIter != BeforeChgIPNotifierList.end())
284     {
285     ipBIter->GetUser()->GetProperty().ips.DelBeforeNotifier(&(*ipBIter));
286     BeforeChgIPNotifierList.erase(ipBIter);
287     }
288
289 ipAIter = find_if(AfterChgIPNotifierList.begin(),
290                   AfterChgIPNotifierList.end(),
291                   bind2nd(IsContainsUserIPA, u));
292
293 if (ipAIter != AfterChgIPNotifierList.end())
294     {
295     ipAIter->GetUser()->GetProperty().ips.DelAfterNotifier(&(*ipAIter));
296     AfterChgIPNotifierList.erase(ipAIter);
297     }
298 // ---          IP end          ---
299 }
300 //-----------------------------------------------------------------------------
301 void AUTH_STRESS::GetUsers()
302 {
303 USER_PTR u;
304 printfd(__FILE__, "users->OpenSearch() usernum=%d\n", users->GetUserNum());
305 int h = users->OpenSearch();
306 if (!h)
307     {
308     printfd(__FILE__, "users->OpenSearch() error\n");
309     return;
310     }
311
312 while (1)
313     {
314     if (users->SearchNext(h, &u))
315         {
316         break;
317         }
318     usersList.push_back(u);
319     SetUserNotifiers(u);
320     }
321
322 users->CloseSearch(h);
323 }
324 //-----------------------------------------------------------------------------
325 void AUTH_STRESS::Unauthorize(USER_PTR u) const
326 {
327 if (!u->IsAuthorizedBy(this))
328     return;
329
330 printfd(__FILE__, "Unauthorized user %s\n", u->GetLogin().c_str());
331 u->Unauthorize(this);
332 }
333 //-----------------------------------------------------------------------------
334 void AUTH_STRESS::Authorize(USER_PTR u) const
335 {
336 USER_IPS ips = u->GetProperty().ips;
337 if (ips.OnlyOneIP() && !u->IsAuthorizedBy(this))
338     {
339     if (u->Authorize(ips[0].ip, 0xFFffFFff, this) == 0)
340         {
341         printfd(__FILE__, "Authorized user %s\n", u->GetLogin().c_str());
342         }
343     }
344 }
345 //-----------------------------------------------------------------------------
346 void AUTH_STRESS::AddUser(USER_PTR u)
347 {
348 //printfd(__FILE__, "User added to list %s\n", u->GetLogin().c_str());
349 SetUserNotifiers(u);
350 usersList.push_back(u);
351 }
352 //-----------------------------------------------------------------------------
353 void AUTH_STRESS::DelUser(USER_PTR u)
354 {
355 Unauthorize(u);
356 UnSetUserNotifiers(u);
357
358 list<USER_PTR>::iterator users_iter;
359 users_iter = usersList.begin();
360
361 while (users_iter != usersList.end())
362     {
363     if (u == *users_iter)
364         {
365         usersList.erase(users_iter);
366         printfd(__FILE__, "User removed from list %s\n", u->GetLogin().c_str());
367         break;
368         }
369     users_iter++;
370     }
371 }
372 //-----------------------------------------------------------------------------
373 int AUTH_STRESS::SendMessage(const STG_MSG &, uint32_t) const
374 {
375 errorStr = "Authorization modele \'AUTH_STRESS\' does not support sending messages";
376 return -1;
377 }
378 //-----------------------------------------------------------------------------
379 void * AUTH_STRESS::Run(void * d)
380 {
381 AUTH_STRESS * ia;
382 ia = (AUTH_STRESS *)d;
383
384 ia->isRunning = true;
385
386 while (ia->nonstop)
387     {
388     printfd(__FILE__, "AUTH_STRESS::Run - averageTime: %d\n", random() % (2*ia->stressSettings.GetAverageOnlineTime()));
389
390     list<USER_PTR>::iterator users_iter;
391     users_iter = ia->usersList.begin();
392     while (users_iter != ia->usersList.end())
393         {
394         if (random() % (2*ia->stressSettings.GetAverageOnlineTime()) == 1)
395             {
396             ia->Authorize(*users_iter);
397             printfd(__FILE__, "AUTH_STRESS::Authorize - user: '%s'\n", (*users_iter)->GetLogin().c_str());
398             }
399         if (random() % (2*ia->stressSettings.GetAverageOnlineTime()) == 2)
400             {
401             ia->Unauthorize(*users_iter);
402             printfd(__FILE__, "AUTH_STRESS::Unauthorize - user: '%s'\n", (*users_iter)->GetLogin().c_str());
403             }
404
405         users_iter++;
406         }
407
408     sleep(1);
409     }
410
411 ia->isRunning = false;
412 return NULL;
413 }
414 //-----------------------------------------------------------------------------
415 template <typename varParamType>
416 void CHG_BEFORE_NOTIFIER<varParamType>::Notify(const varParamType &, const varParamType &)
417 {
418 auth->Unauthorize(user);
419 }
420 //-----------------------------------------------------------------------------
421 template <typename varParamType>
422 void CHG_AFTER_NOTIFIER<varParamType>::Notify(const varParamType &, const varParamType &)
423 {
424 auth->Authorize(user);
425 }
426 //-----------------------------------------------------------------------------