200-dbtests.t 34.7 KB
Newer Older
1
2
3
4
use strict;
use warnings;


5
use AWITPT::Util;
6
use Data::Dumper;
7
use Date::Parse;
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
use POSIX qw(:sys_wait_h);
use Test::Most;
use Test::Most::Exception 'throw_failure';



#
# Check that database tests are enabled
#

# We need DBTESTS enabled to run this
if (!$ENV{'DBTESTS'}) {
	plan skip_all => 'DBTESTS not set in ENV';
	done_testing();
	exit 0;
}



#
# Load database handling libraries
#

require_ok('AWITPT::DB::DBILayer');
require_ok('AWITPT::DB::DBLayer');

use AWITPT::DB::DBLayer;


#
# Load our server and client
#

require_ok("smradius::daemon");
require_ok("smradius::client");


#
# Daemon help
#

can_ok("smradius::daemon","displayHelp");


#
# Try connect to database
#

my $dbh = AWITPT::DB::DBILayer->new({
	'Username' => 'root',
	'DSN' => 'DBI:mysql:database=smradiustest;host=localhost',
});


# If we cannot connect, just bail out
if ($dbh->connect()) {
	BAIL_OUT("ERROR: Failed to connect to database for testing purposes: ".$dbh->error());
}

AWITPT::DB::DBLayer::setHandle($dbh);


#
# Make sure DB is clean
#

my $sth;

Nigel Kukard's avatar
Nigel Kukard committed
76
77
78
$sth = DBDo("DELETE FROM topups");
is(AWITPT::DB::DBLayer::error(),"","Clean table 'topups");

79
80
81
$sth = DBDo("DELETE FROM accounting");
is(AWITPT::DB::DBLayer::error(),"","Clean table 'accounting");

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
$sth = DBDo("DELETE FROM user_attributes");
is(AWITPT::DB::DBLayer::error(),"","Clean table 'user_attributes");

$sth = DBDo("DELETE FROM client_attributes");
is(AWITPT::DB::DBLayer::error(),"","Clean table 'client_attributes");

$sth = DBDo("DELETE FROM users");
is(AWITPT::DB::DBLayer::error(),"","Clean table 'users'");

$sth = DBDo("DELETE FROM clients_to_realms");
is(AWITPT::DB::DBLayer::error(),"","Clean table 'clients_to_realms'");

$sth = DBDo("DELETE FROM clients");
is(AWITPT::DB::DBLayer::error(),"","Clean table 'clients'");

$sth = DBDo("DELETE FROM realms");
is(AWITPT::DB::DBLayer::error(),"","Clean table 'realms'");


#
# Run server and client
#

our $child;
if ($child = fork()) {

	# CHLD handler
	local $SIG{CHLD} = sub {
		warn "SIGCHLD TRIGGER";
		waitpid($child,-1);
	};

	# Install signal handlers to cleanup if we get a TERM or INT
	local $SIG{TERM} = local $SIG{INT} = \&cleanup;


	# Wait before starting
	sleep(2);


	# Setup failure handler
	set_failure_handler( sub { my @params = @_; cleanup(); throw_failure } );


	my $res;


	#
	# Make sure basic test without any config does not authenticate users
	#

	$res = smradius::client->run(
		"--raddb","dicts",
		"127.0.0.1",
		"auth",
		"secret123",
		'User-Name=testuser1',
		'User-Password=test123',
	);
	is(ref($res),"","smradclient ref should return ''");
	is($res,1,"smradclient result should be 1");



	#
	# Create test case data
	#

Nigel Kukard's avatar
Nigel Kukard committed
150
151
152
	my $client1_ID = testDBInsert("Create client 'localhost'",
		"INSERT INTO clients (Name,AccessList,Disabled) VALUES ('localhost','127.0.0.0/8',0)"
	);
153

Nigel Kukard's avatar
Nigel Kukard committed
154
155
156
157
	my $client1attr1_ID = testDBInsert("Create client 'localhost' secret",
		"INSERT INTO client_attributes (ClientID,Name,Operator,Value,Disabled) VALUES (?,?,?,?,0)",
			$client1_ID,'SMRadius-Config-Secret',':=','secret123'
	);
158

159
	# Blank realm
Nigel Kukard's avatar
Nigel Kukard committed
160
161
162
	my $realm1_ID = testDBInsert("Create realm ''",
		"INSERT INTO realms (Name,Disabled) VALUES ('',0)"
	);
163

Nigel Kukard's avatar
Nigel Kukard committed
164
165
166
	my $clientTOrealm1_ID = testDBInsert("Link client 'localhost' to realm ''",
		"INSERT INTO clients_to_realms (ClientID,RealmID,Disabled) VALUES (?,?,0)",$client1_ID,$realm1_ID
	);
167
168
169
170



	#
171
	# Check we get an Access-Accept for a bare user using a blank realm
172
173
	#

Nigel Kukard's avatar
Nigel Kukard committed
174
175
176
	my $user1_ID = testDBInsert("Create user 'testuser1'",
		"INSERT INTO users (UserName,Disabled) VALUES ('testuser1',0)"
	);
177

Nigel Kukard's avatar
Nigel Kukard committed
178
179
180
181
	my $user1attr1_ID = testDBInsert("Create user 'testuser1' attribute 'User-Password'",
		"INSERT INTO user_attributes (UserID,Name,Operator,Value,Disabled) VALUES (?,?,?,?,0)",
			$user1_ID,'User-Password','==','test123'
	);
182

183
184
185
186
187
	my $user1attr2_ID = testDBInsert("Create user 'testuser1' attribute 'Framed-IP-Address'",
		"INSERT INTO user_attributes (UserID,Name,Operator,Value,Disabled) VALUES (?,?,?,?,0)",
			$user1_ID,'Framed-IP-Address',':=','10.0.0.1'
	);

188
189
190
191
192
	my $user1attr3_ID = testDBInsert("Create user 'testuser1' vendor attribute '[14988:Mikrotik-Rate-Limit]'",
		"INSERT INTO user_attributes (UserID,Name,Operator,Value,Disabled) VALUES (?,?,?,?,0)",
			$user1_ID,'[14988:Mikrotik-Rate-Limit]',':=','1024k/512k'
	);

193
194
195
196
197
198
199
200
201
	$res = smradius::client->run(
		"--raddb","dicts",
		"127.0.0.1",
		"auth",
		"secret123",
		'User-Name=testuser1',
		'User-Password=test123',
	);
	is(ref($res),"HASH","smradclient should return a HASH");
202
	is($res->{'response'}->{'code'},"Access-Accept","Check our return is 'Access-Accept' for bare user blank '' realm");
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
	# Test the normal attribute and vendor attribute
	is($res->{'response'}->{'attributes'}->{'Framed-IP-Address'},"10.0.0.1","Check that attribute 'Framed-IP-Address' is".
			" returned");
	is($res->{'response'}->{'vattributes'}->{'14988'}->{'Mikrotik-Rate-Limit'}->[0],"1024k/512k","Check that the vendor attribute".
			"'14988:Mikrotik-Rate-Limit' is returned");

	# Add filter attributes
	my $user1attr4_ID = testDBInsert("Create user 'testuser1' filter attribute for 'Framed-IP-Address'",
		"INSERT INTO user_attributes (UserID,Name,Operator,Value,Disabled) VALUES (?,?,?,?,0)",
			$user1_ID,'SMRadius-Config-Filter-Reply-Attribute',':=','Framed-IP-Address'
	);
	my $user1attr5_ID = testDBInsert("Create user 'testuser1' filter vattribute for 'Mikrotik-Rate-Limit'",
		"INSERT INTO user_attributes (UserID,Name,Operator,Value,Disabled) VALUES (?,?,?,?,0)",
			$user1_ID,'SMRadius-Config-Filter-Reply-VAttribute',':=','Mikrotik-Rate-Limit'
	);
218

219
220
221
222
223
224
225
226
227
228
229
230
	$res = smradius::client->run(
		"--raddb","dicts",
		"127.0.0.1",
		"auth",
		"secret123",
		'User-Name=testuser1',
		'User-Password=test123',
	);
	is(ref($res),"HASH","smradclient should return a HASH");
	# We shouldn't....
	isnt($res->{'response'}->{'attributes'}->{'Framed-IP-Address'},"10.0.0.1","Check that attribute 'Framed-IP-Address' is".
			" returned");
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265

	#
	# Modify data for the default realm
	#

	testDBDelete("Delete blank realm '' link to client",
		"DELETE FROM clients_to_realms WHERE ID = ?",$clientTOrealm1_ID
	);
	testDBDelete("Delete blank realm '' for realm '<DEFAULT>' test",
		"DELETE FROM realms WHERE ID = ?",$realm1_ID
	);

	my $realm1b_ID = testDBInsert("Create default realm '<DEFAULT>'",
		"INSERT INTO realms (Name,Disabled) VALUES ('<DEFAULT>',0)"
	);

	my $clientTOrealm1b_ID = testDBInsert("Link client 'localhost' to realm '<DEFAULT>'",
		"INSERT INTO clients_to_realms (ClientID,RealmID,Disabled) VALUES (?,?,0)",$client1_ID,$realm1b_ID
	);


	#
	# Check we get an Access-Accept for a bare user using the default realm
	#

	$res = smradius::client->run(
		"--raddb","dicts",
		"127.0.0.1",
		"auth",
		"secret123",
		'User-Name=testuser1',
		'User-Password=test123',
	);
	is(ref($res),"HASH","smradclient should return a HASH");
	is($res->{'response'}->{'code'},"Access-Accept","Check our return is 'Access-Accept' for bare user on <DEFAULT> realm");
266
267
268
269



	#
270
	# Check we get a Access-Reject for a traffic topup user
271
272
	#

Nigel Kukard's avatar
Nigel Kukard committed
273
274
275
276
277
278
279
280
281
282
283
284
285
286
	my $user2_ID = testDBInsert("Create user 'testuser2'",
		"INSERT INTO users (UserName,Disabled) VALUES ('testuser2',0)"
	);

	my $user2attr1_ID = testDBInsert("Create user 'testuser2' attribute 'User-Password'",
		"INSERT INTO user_attributes (UserID,Name,Operator,Value,Disabled) VALUES (?,?,?,?,0)",
			$user2_ID,'User-Password','==','test123'
	);

	my $user2attr2_ID = testDBInsert("Create user 'testuser2' attribute 'SMRadius-Capping-Traffic-Limit'",
		"INSERT INTO user_attributes (UserID,Name,Operator,Value,Disabled) VALUES (?,?,?,?,0)",
			$user2_ID,'SMRadius-Capping-Traffic-Limit',':=','0'
	);

287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
	$res = smradius::client->run(
		"--raddb","dicts",
		"127.0.0.1",
		"auth",
		"secret123",
		'User-Name=testuser2',
		'User-Password=test123',
	);
	is(ref($res),"HASH","smradclient should return a HASH");
	is($res->{'response'}->{'code'},"Access-Reject","Check our return is 'Access-Reject' for a traffic topup user");


	#
	# Check we get a Access-Reject for a uptime topup user
	#

	my $user2b_ID = testDBInsert("Create user 'testuser2b'",
		"INSERT INTO users (UserName,Disabled) VALUES ('testuser2b',0)"
	);

	my $user2battr1_ID = testDBInsert("Create user 'testuser2b' attribute 'User-Password'",
Nigel Kukard's avatar
Nigel Kukard committed
308
		"INSERT INTO user_attributes (UserID,Name,Operator,Value,Disabled) VALUES (?,?,?,?,0)",
309
310
311
312
313
314
			$user2b_ID,'User-Password','==','test123'
	);

	my $user2battr3_ID = testDBInsert("Create user 'testuser2b' attribute 'SMRadius-Capping-Uptime-Limit'",
		"INSERT INTO user_attributes (UserID,Name,Operator,Value,Disabled) VALUES (?,?,?,?,0)",
			$user2b_ID,'SMRadius-Capping-Uptime-Limit',':=','0'
Nigel Kukard's avatar
Nigel Kukard committed
315
	);
316
317
318
319
320
321

	$res = smradius::client->run(
		"--raddb","dicts",
		"127.0.0.1",
		"auth",
		"secret123",
322
		'User-Name=testuser2b',
323
324
325
		'User-Password=test123',
	);
	is(ref($res),"HASH","smradclient should return a HASH");
326
	is($res->{'response'}->{'code'},"Access-Reject","Check our return is 'Access-Reject' for a uptime topup user");
327
328


329
	#
330
	# Test accounting START packet
331
332
	#

333
	my $session1_ID = "09d15244";
334
335
336
337
	my $session1_Timestamp = time();
	my $session1_Timestamp_str = DateTime->from_epoch(epoch => $session1_Timestamp,time_zone => 'UTC')
			->strftime('%Y-%m-%d %H:%M:%S');

338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
	$res = smradius::client->run(
		"--raddb","dicts",
		"127.0.0.1",
		"acct",
		"secret123",
		'NAS-IP-Address=10.0.0.3',
		'Acct-Delay-Time=11',
		'NAS-Identifier=Test-NAS1',
		'Acct-Status-Type=Start',
		'Event-Timestamp='.$session1_Timestamp,
		'Framed-IP-Address=10.0.1.3',
		'Acct-Session-Id='.$session1_ID,
		'NAS-Port-Id=test iface name1',
		'Called-Station-Id=testservice1',
		'Calling-Station-Id=00:00:0C:EE:47:AA',
		'User-Name=testuser1',
		'NAS-Port-Type=Ethernet',
		'NAS-Port=45355555',
		'Framed-Protocol=PPP',
		'Service-Type=Framed-User',
	);
	is(ref($res),"HASH","smradclient should return a HASH");

	testDBResults("Check accounting record is created correctly",'accounting',{'AcctSessionID' => $session1_ID},
		{
			'NASIPAddress' => '10.0.0.3',
			'AcctDelayTime' => '11',
			'NASIdentifier' => 'Test-NAS1',
			'AcctStatusType' => 1,
367
			'EventTimestamp' => sub{ _timestampCheck(shift,$session1_Timestamp_str) },
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
			'FramedIPAddress' => '10.0.1.3',
			'AcctSessionId' => $session1_ID,
			'NASPortId' => 'test iface name1',
			'CalledStationId' => 'testservice1',
			'CallingStationId' => '00:00:0C:EE:47:AA',
			'Username' => 'testuser1',
			'NASPortType' => 15,
			'NASPort' => '45355555',
			'FramedProtocol' => 1,
			'ServiceType' => 2,
			'AcctOutputPackets' => undef,
			'AcctOutputGigawords' => undef,
			'AcctOutputOctets' => undef,
			'AcctInputPackets' => undef,
			'AcctInputGigawords' => undef,
			'AcctInputOctets' => undef,
			'AcctSessionTime' => undef,
		}
	);


	#
	# Test accounting ALIVE packet
	#

	$res = smradius::client->run(
		"--raddb","dicts",
		"127.0.0.1",
		"acct",
		"secret123",

		'Acct-Status-Type=Interim-Update',
		'Acct-Output-Packets=800000',
		'Acct-Output-Gigawords=0',
		'Acct-Output-Octets=810000000',
		'Acct-Input-Packets=777777',
		'Acct-Input-Gigawords=0',
		'Acct-Input-Octets=123456789',
		'Acct-Session-Time=999',

		'User-Name=testuser1',
		'Acct-Session-Id='.$session1_ID,
		'NAS-IP-Address=10.0.0.3',
		'NAS-Port=45355555',
	);
	is(ref($res),"HASH","smradclient should return a HASH");

	testDBResults("Check accounting record is updated correctly",'accounting',{'AcctSessionID' => $session1_ID},
		{
			'NASIPAddress' => '10.0.0.3',
			'AcctDelayTime' => '11',
			'NASIdentifier' => 'Test-NAS1',
			'AcctStatusType' => 3,
421
			'EventTimestamp' => sub{ _timestampCheck(shift,$session1_Timestamp_str) },
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
			'FramedIPAddress' => '10.0.1.3',
			'AcctSessionId' => $session1_ID,
			'NASPortId' => 'test iface name1',
			'CalledStationId' => 'testservice1',
			'CallingStationId' => '00:00:0C:EE:47:AA',
			'Username' => 'testuser1',
			'NASPortType' => 15,
			'NASPort' => '45355555',
			'FramedProtocol' => 1,
			'ServiceType' => 2,
			'AcctOutputPackets' => '800000',
			'AcctOutputGigawords' => '0',
			'AcctOutputOctets' => '810000000',
			'AcctInputPackets' => '777777',
			'AcctInputGigawords' => '0',
			'AcctInputOctets' => '123456789',
			'AcctSessionTime' => '999',
		}
	);


	#
	# Test accounting STOP packet
	#

	$res = smradius::client->run(
		"--raddb","dicts",
		"127.0.0.1",
		"acct",
		"secret123",

		'Acct-Status-Type=Stop',
		'Acct-Output-Packets=999999',
		'Acct-Output-Gigawords=0',
		'Acct-Output-Octets=888888888',
		'Acct-Input-Packets=1111111',
		'Acct-Input-Gigawords=0',
		'Acct-Input-Octets=222222222',
		'Acct-Session-Time=3998',
		'Acct-Terminate-Cause=Session-Timeout',

		'User-Name=testuser1',
		'Acct-Session-Id='.$session1_ID,
		'NAS-IP-Address=10.0.0.3',
		'NAS-Port=45355555',
	);
	is(ref($res),"HASH","smradclient should return a HASH");

	testDBResults("Check accounting record is stopped correctly",'accounting',{'AcctSessionID' => $session1_ID},
		{
			'NASIPAddress' => '10.0.0.3',
			'AcctDelayTime' => '11',
			'NASIdentifier' => 'Test-NAS1',
			'AcctStatusType' => 2,
476
			'EventTimestamp' => sub{ _timestampCheck(shift,$session1_Timestamp_str) },
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
			'FramedIPAddress' => '10.0.1.3',
			'AcctSessionId' => $session1_ID,
			'NASPortId' => 'test iface name1',
			'CalledStationId' => 'testservice1',
			'CallingStationId' => '00:00:0C:EE:47:AA',
			'Username' => 'testuser1',
			'NASPortType' => 15,
			'NASPort' => '45355555',
			'FramedProtocol' => 1,
			'ServiceType' => 2,
			'AcctOutputPackets' => '999999',
			'AcctOutputGigawords' => '0',
			'AcctOutputOctets' => '888888888',
			'AcctInputPackets' => '1111111',
			'AcctInputGigawords' => '0',
			'AcctInputOctets' => '222222222',
			'AcctSessionTime' => '3998',
			'AcctTerminateCause' => '5',
		}
	);


	#
	# Test missing accounting START packet
	#

Nigel Kukard's avatar
Nigel Kukard committed
503
	my $session2_ID = "817a0f1b";
504
505
506
507
	my $session2_Timestamp = time();
	my $session2_Timestamp_str = DateTime->from_epoch(epoch => $session2_Timestamp,time_zone => 'UTC')
			->strftime('%Y-%m-%d %H:%M:%S');

508
509
510
511
512
513
514
515
	$res = smradius::client->run(
		"--raddb","dicts",
		"127.0.0.1",
		"acct",
		"secret123",
		'User-Name=testuser2',
		'NAS-IP-Address=10.0.0.1',
		'Acct-Delay-Time=12',
516
		'NAS-Identifier=Test-NAS2',
517
518
519
520
521
522
523
524
		'Acct-Status-Type=Interim-Update',
		'Acct-Output-Packets=786933',
		'Acct-Output-Gigawords=0',
		'Acct-Output-Octets=708163705',
		'Acct-Input-Packets=670235',
		'Acct-Input-Gigawords=0',
		'Acct-Input-Octets=102600046',
		'Acct-Session-Time=800',
525
		'Event-Timestamp='.$session2_Timestamp,
526
		'Framed-IP-Address=10.0.1.1',
527
		'Acct-Session-Id='.$session2_ID,
528
		'NAS-Port-Id=wlan1',
529
		'Called-Station-Id=testservice2',
530
531
532
533
534
535
536
537
		'Calling-Station-Id=00:00:0C:EE:47:BF',
		'NAS-Port-Type=Ethernet',
		'NAS-Port=15729175',
		'Framed-Protocol=PPP',
		'Service-Type=Framed-User',
	);
	is(ref($res),"HASH","smradclient should return a HASH");

538
	testDBResults("Check missing accounting record is created correctly",'accounting',{'AcctSessionID' => $session2_ID},
539
540
541
542
		{
			'Username' => 'testuser2',
			'NASIPAddress' => '10.0.0.1',
			'AcctDelayTime' => '12',
543
			'NASIdentifier' => 'Test-NAS2',
544
545
546
547
548
549
550
551
			'AcctStatusType' => 3,
			'AcctOutputPackets' => '786933',
			'AcctOutputGigawords' => '0',
			'AcctOutputOctets' => '708163705',
			'AcctInputPackets' => '670235',
			'AcctInputGigawords' => '0',
			'AcctInputOctets' => '102600046',
			'AcctSessionTime' => '800',
552
			'EventTimestamp' => sub{ _timestampCheck(shift,$session2_Timestamp_str) },
553
			'FramedIPAddress' => '10.0.1.1',
554
			'AcctSessionId' => $session2_ID,
555
			'NASPortId' => 'wlan1',
556
			'CalledStationId' => 'testservice2',
557
558
559
560
561
562
563
564
565
			'CallingStationId' => '00:00:0C:EE:47:BF',
			'NASPortType' => 15,
			'NASPort' => '15729175',
			'FramedProtocol' => 1,
			'ServiceType' => 2,
		}
	);


Nigel Kukard's avatar
Nigel Kukard committed
566
	#
567
	# Check we get a Access-Accept for a traffic autotopup user
Nigel Kukard's avatar
Nigel Kukard committed
568
569
	#

Nigel Kukard's avatar
Nigel Kukard committed
570
571
	my $topuptest1_amount = 100;

Nigel Kukard's avatar
Nigel Kukard committed
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
	my $user3_ID = testDBInsert("Create user 'testuser3'",
		"INSERT INTO users (UserName,Disabled) VALUES ('testuser3',0)"
	);

	my $user3attr1_ID = testDBInsert("Create user 'testuser3' attribute 'User-Password'",
		"INSERT INTO user_attributes (UserID,Name,Operator,Value,Disabled) VALUES (?,?,?,?,0)",
			$user3_ID,'User-Password','==','test456'
	);

	my $user3attr2_ID = testDBInsert("Create user 'testuser3' attribute 'SMRadius-AutoTopup-Traffic-Enabled'",
		"INSERT INTO user_attributes (UserID,Name,Operator,Value,Disabled) VALUES (?,?,?,?,0)",
			$user3_ID,'SMRadius-AutoTopup-Traffic-Enabled',':=','yes'
	);

	my $user3attr3_ID = testDBInsert("Create user 'testuser3' attribute 'SMRadius-AutoTopup-Traffic-Amount'",
		"INSERT INTO user_attributes (UserID,Name,Operator,Value,Disabled) VALUES (?,?,?,?,0)",
Nigel Kukard's avatar
Nigel Kukard committed
588
			$user3_ID,'SMRadius-AutoTopup-Traffic-Amount',':=',$topuptest1_amount
Nigel Kukard's avatar
Nigel Kukard committed
589
590
591
592
593
594
595
	);

	my $user3attr4_ID = testDBInsert("Create user 'testuser3' attribute 'SMRadius-AutoTopup-Traffic-Limit'",
		"INSERT INTO user_attributes (UserID,Name,Operator,Value,Disabled) VALUES (?,?,?,?,0)",
			$user3_ID,'SMRadius-AutoTopup-Traffic-Limit',':=','500'
	);

596
	my $user3attr5_ID = testDBInsert("Create user 'testuser3' attribute 'SMRadius-Capping-Traffic-Limit'",
Nigel Kukard's avatar
Nigel Kukard committed
597
		"INSERT INTO user_attributes (UserID,Name,Operator,Value,Disabled) VALUES (?,?,?,?,0)",
598
			$user3_ID,'SMRadius-Capping-Traffic-Limit',':=','0'
Nigel Kukard's avatar
Nigel Kukard committed
599
600
601
602
603
604
605
606
607
608
609
	);

	$res = smradius::client->run(
		"--raddb","dicts",
		"127.0.0.1",
		"auth",
		"secret123",
		'User-Name=testuser3',
		'User-Password=test456',
	);
	is(ref($res),"HASH","smradclient should return a HASH");
610
	is($res->{'response'}->{'code'},"Access-Accept","Check our return is 'Access-Accept' for a traffic autotopup user");
Nigel Kukard's avatar
Nigel Kukard committed
611

Nigel Kukard's avatar
Nigel Kukard committed
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
	# Get the time now
	my $topuptest1_now = time();
	my $topuptest1 = DateTime->from_epoch( 'epoch' => $topuptest1_now, 'time_zone' => 'UTC');

	# Use truncate to set all values after 'month' to their default values
	my $topuptest1_thisMonth = $topuptest1->clone()->truncate( to => "month" );
	# This month, in string form
	my $topuptest1_thisMonth_str = $topuptest1_thisMonth->strftime("%Y-%m-%d %H:%M:%S");
	# Next month..
	my $topuptest1_nextMonth = $topuptest1_thisMonth->clone()->add( months => 1 );
	my $topuptest1_nextMonth_str = $topuptest1_nextMonth->strftime("%Y-%m-%d %H:%M:%S");

	testDBResults("Check autotopup is added correctly",'topups',{'UserID' => $user3_ID},
		{
			'UserID' => $user3_ID,
			'Timestamp' => sub { return _timestampCheck(shift,$topuptest1_now) },
			'Type' => 5,
			'ValidFrom' => $topuptest1_thisMonth_str,
			'ValidTo' => $topuptest1_nextMonth_str,
			'Value' => $topuptest1_amount,
			'Depleted' => 0,
			'SMAdminDepletedOn' => undef,
		}
	);



639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
	#
	# Check we get a Access-Accept for a uptime autotopup user
	#

	my $topuptest1b_amount = 100;

	my $user3b_ID = testDBInsert("Create user 'testuser3b'",
		"INSERT INTO users (UserName,Disabled) VALUES ('testuser3b',0)"
	);

	my $user3battr1_ID = testDBInsert("Create user 'testuser3b' attribute 'User-Password'",
		"INSERT INTO user_attributes (UserID,Name,Operator,Value,Disabled) VALUES (?,?,?,?,0)",
			$user3b_ID,'User-Password','==','test456'
	);

	my $user3battr2_ID = testDBInsert("Create user 'testuser3b' attribute 'SMRadius-AutoTopup-Uptime-Enabled'",
		"INSERT INTO user_attributes (UserID,Name,Operator,Value,Disabled) VALUES (?,?,?,?,0)",
			$user3b_ID,'SMRadius-AutoTopup-Uptime-Enabled',':=','yes'
	);

	my $user3battr3_ID = testDBInsert("Create user 'testuser3b' attribute 'SMRadius-AutoTopup-Uptime-Amount'",
		"INSERT INTO user_attributes (UserID,Name,Operator,Value,Disabled) VALUES (?,?,?,?,0)",
			$user3b_ID,'SMRadius-AutoTopup-Uptime-Amount',':=',$topuptest1b_amount
	);

	my $user3battr4_ID = testDBInsert("Create user 'testuser3b' attribute 'SMRadius-AutoTopup-Uptime-Limit'",
		"INSERT INTO user_attributes (UserID,Name,Operator,Value,Disabled) VALUES (?,?,?,?,0)",
			$user3b_ID,'SMRadius-AutoTopup-Uptime-Limit',':=','500'
	);

	my $user3battr5_ID = testDBInsert("Create user 'testuser3b' attribute 'SMRadius-Capping-Uptime-Limit'",
		"INSERT INTO user_attributes (UserID,Name,Operator,Value,Disabled) VALUES (?,?,?,?,0)",
			$user3b_ID,'SMRadius-Capping-Uptime-Limit',':=','0'
	);

	$res = smradius::client->run(
		"--raddb","dicts",
		"127.0.0.1",
		"auth",
		"secret123",
		'User-Name=testuser3b',
		'User-Password=test456',
	);
	is(ref($res),"HASH","smradclient should return a HASH");
	is($res->{'response'}->{'code'},"Access-Accept","Check our return is 'Access-Accept' for a uptime autotopup user");

	# Get the time now
	my $topuptest1b_now = time();
	my $topuptest1b = DateTime->from_epoch( 'epoch' => $topuptest1b_now, 'time_zone' => 'UTC');

	# Use truncate to set all values after 'month' to their default values
	my $topuptest1b_thisMonth = $topuptest1b->clone()->truncate( to => "month" );
	# This month, in string form
	my $topuptest1b_thisMonth_str = $topuptest1b_thisMonth->strftime("%Y-%m-%d %H:%M:%S");
	# Next month..
	my $topuptest1b_nextMonth = $topuptest1b_thisMonth->clone()->add( months => 1 );
	my $topuptest1b_nextMonth_str = $topuptest1b_nextMonth->strftime("%Y-%m-%d %H:%M:%S");

	testDBResults("Check autotopup is added correctly",'topups',{'UserID' => $user3b_ID},
		{
			'UserID' => $user3b_ID,
			'Timestamp' => sub { return _timestampCheck(shift,$topuptest1b_now) },
			'Type' => 6,
			'ValidFrom' => $topuptest1b_thisMonth_str,
			'ValidTo' => $topuptest1b_nextMonth_str,
			'Value' => $topuptest1b_amount,
			'Depleted' => 0,
			'SMAdminDepletedOn' => undef,
		}
	);



Nigel Kukard's avatar
Nigel Kukard committed
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
	#
	# Check that if we send an accounting ALIVE we update the auto-topups
	#

	my $topuptest2_amount = 100;

	my $user4_ID = testDBInsert("Create user 'testuser4'",
		"INSERT INTO users (UserName,Disabled) VALUES ('testuser4',0)"
	);

	my $user4attr1_ID = testDBInsert("Create user 'testuser4' attribute 'User-Password'",
		"INSERT INTO user_attributes (UserID,Name,Operator,Value,Disabled) VALUES (?,?,?,?,0)",
			$user4_ID,'User-Password','==','test456'
	);

	my $user4attr2_ID = testDBInsert("Create user 'testuser4' attribute 'SMRadius-AutoTopup-Traffic-Enabled'",
		"INSERT INTO user_attributes (UserID,Name,Operator,Value,Disabled) VALUES (?,?,?,?,0)",
			$user4_ID,'SMRadius-AutoTopup-Traffic-Enabled',':=','yes'
	);

	my $user4attr3_ID = testDBInsert("Create user 'testuser4' attribute 'SMRadius-AutoTopup-Traffic-Amount'",
		"INSERT INTO user_attributes (UserID,Name,Operator,Value,Disabled) VALUES (?,?,?,?,0)",
			$user4_ID,'SMRadius-AutoTopup-Traffic-Amount',':=',$topuptest2_amount
	);

	my $user4attr4_ID = testDBInsert("Create user 'testuser4' attribute 'SMRadius-AutoTopup-Traffic-Limit'",
		"INSERT INTO user_attributes (UserID,Name,Operator,Value,Disabled) VALUES (?,?,?,?,0)",
			$user4_ID,'SMRadius-AutoTopup-Traffic-Limit',':=','500'
	);

742
	my $user4attr5_ID = testDBInsert("Create user 'testuser4' attribute 'SMRadius-Capping-Traffic-Limit'",
Nigel Kukard's avatar
Nigel Kukard committed
743
		"INSERT INTO user_attributes (UserID,Name,Operator,Value,Disabled) VALUES (?,?,?,?,0)",
744
			$user4_ID,'SMRadius-Capping-Traffic-Limit',':=','0'
Nigel Kukard's avatar
Nigel Kukard committed
745
746
747
748
749
750
751
752
	);

	my $user4attr6_ID = testDBInsert("Create user 'testuser4' attribute 'SMRadius-AutoTopup-Traffic-Notify'",
		"INSERT INTO user_attributes (UserID,Name,Operator,Value,Disabled) VALUES (?,?,?,?,0)",
			$user4_ID,'SMRadius-AutoTopup-Traffic-Notify',':=','root@localhost'
	);


Nigel Kukard's avatar
Nigel Kukard committed
753
	my $session3_ID = "9c5f24a";
Nigel Kukard's avatar
Nigel Kukard committed
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
	my $session3_Timestamp = time();

	$res = smradius::client->run(
		"--raddb","dicts",
		"127.0.0.1",
		"acct",
		"secret123",
		'User-Name=testuser4',
		'NAS-IP-Address=10.0.0.1',
		'Acct-Delay-Time=12',
		'NAS-Identifier=Test-NAS2',
		'Acct-Status-Type=Interim-Update',
		'Acct-Output-Packets=786933',
		'Acct-Output-Gigawords=0',
		'Acct-Output-Octets=708163705',
		'Acct-Input-Packets=670235',
		'Acct-Input-Gigawords=0',
		'Acct-Input-Octets=102600046',
		'Acct-Session-Time=800',
		'Event-Timestamp='.$session3_Timestamp,
		'Framed-IP-Address=10.0.1.1',
		'Acct-Session-Id='.$session3_ID,
		'NAS-Port-Id=wlan1',
		'Called-Station-Id=testservice2',
		'Calling-Station-Id=00:00:0C:EE:47:BF',
		'NAS-Port-Type=Ethernet',
		'NAS-Port=15729175',
		'Framed-Protocol=PPP',
		'Service-Type=Framed-User',
	);
	is(ref($res),"HASH","smradclient should return a HASH");

	# Get the time now
	my $topuptest2_now = time();
	my $topuptest2 = DateTime->from_epoch( 'epoch' => $topuptest2_now, 'time_zone' => 'UTC');

	# Use truncate to set all values after 'month' to their default values
	my $topuptest2_thisMonth = $topuptest2->clone()->truncate( to => "month" );
	# This month, in string form
	my $topuptest2_thisMonth_str = $topuptest2_thisMonth->strftime("%Y-%m-%d %H:%M:%S");
	# Next month..
	my $topuptest2_nextMonth = $topuptest2_thisMonth->clone()->add( months => 1 );
	my $topuptest2_nextMonth_str = $topuptest2_nextMonth->strftime("%Y-%m-%d %H:%M:%S");

	testDBResults("Check autotopup is added correctly after acct_log",'topups',{'UserID' => $user4_ID},
		{
			'UserID' => $user4_ID,
			'Timestamp' => sub { return _timestampCheck(shift,$topuptest2_now) },
			'Type' => 5,
			'ValidFrom' => $topuptest2_thisMonth_str,
			'ValidTo' => $topuptest2_nextMonth_str,
			'Value' => $topuptest2_amount,
			'Depleted' => 0,
			'SMAdminDepletedOn' => undef,
		}
	);

Nigel Kukard's avatar
Nigel Kukard committed
811

Nigel Kukard's avatar
Nigel Kukard committed
812
	#
Nigel Kukard's avatar
Nigel Kukard committed
813
	# Check that if we send an accounting ALIVE we do not trigger FUP
Nigel Kukard's avatar
Nigel Kukard committed
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
	#

	my $user5_ID = testDBInsert("Create user 'testuser5'",
		"INSERT INTO users (UserName,Disabled) VALUES ('testuser5',0)"
	);

	my $user5attr1_ID = testDBInsert("Create user 'testuser5' attribute 'User-Password'",
		"INSERT INTO user_attributes (UserID,Name,Operator,Value,Disabled) VALUES (?,?,?,?,0)",
			$user5_ID,'User-Password','==','test456'
	);

	my $user5attr2_ID = testDBInsert("Create user 'testuser5' attribute 'SMRadius-FUP-Period'",
		"INSERT INTO user_attributes (UserID,Name,Operator,Value,Disabled) VALUES (?,?,?,?,0)",
			$user5_ID,'SMRadius-FUP-Period',':=','1'
	);

	my $user5attr3_ID = testDBInsert("Create user 'testuser5' attribute 'SMRadius-FUP-Traffic-Threshold'",
		"INSERT INTO user_attributes (UserID,Name,Operator,Value,Disabled) VALUES (?,?,?,?,0)",
			$user5_ID,'SMRadius-FUP-Traffic-Threshold',':=',800
	);

	# Add an attribute so we can check the FUP match results
	my $user5attr4_ID = testDBInsert("Create user 'testuser5' attribute 'SMRadius-Evaluate'",
		"INSERT INTO user_attributes (UserID,Name,Operator,Value,Disabled) VALUES (?,?,?,?,0)",
Nigel Kukard's avatar
Nigel Kukard committed
838
			$user5_ID,'SMRadius-Evaluate','||+=',"SMRadius_FUP > 0 ? [14988:Mikrotik-Rate-Limit] = 1638k/8m : [14988:Mikrotik-Rate-Limit] = 1k/1m"
Nigel Kukard's avatar
Nigel Kukard committed
839
840
	);

Nigel Kukard's avatar
Nigel Kukard committed
841
	my $session4_ID = "a8abc40";
842
	my $session4_Timestamp = time();
Nigel Kukard's avatar
Nigel Kukard committed
843
844
845

	$res = smradius::client->run(
		"--raddb","dicts",
846
		"--listen","127.0.0.1:1700",
Nigel Kukard's avatar
Nigel Kukard committed
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
		"127.0.0.1",
		"acct",
		"secret123",
		'User-Name=testuser5',
		'NAS-IP-Address=10.0.0.1',
		'Acct-Delay-Time=12',
		'NAS-Identifier=Test-NAS2',
		'Acct-Status-Type=Interim-Update',
		'Acct-Output-Packets=786933',
		'Acct-Output-Gigawords=0',
		'Acct-Output-Octets=708163705',
		'Acct-Input-Packets=670235',
		'Acct-Input-Gigawords=0',
		'Acct-Input-Octets=102600046',
		'Acct-Session-Time=800',
		'Event-Timestamp='.$session4_Timestamp,
		'Framed-IP-Address=10.0.1.1',
		'Acct-Session-Id='.$session4_ID,
		'NAS-Port-Id=wlan1',
		'Called-Station-Id=testservice2',
		'Calling-Station-Id=00:00:0C:EE:47:BF',
		'NAS-Port-Type=Ethernet',
		'NAS-Port=15729175',
		'Framed-Protocol=PPP',
		'Service-Type=Framed-User',
	);
	is(ref($res),"HASH","smradclient should return a HASH");
874
875
876
	warn "DUMP: ".Dumper($res);
	is($res->{'listen'}->{'response'}->{'code'},"CoA-Request","Check that the packet we got back is infact a ".
			"CoA-Request");
Nigel Kukard's avatar
Nigel Kukard committed
877
878
	is($res->{'listen'}->{'response'}->{'vattributes'}->{'14988'}->{'Mikrotik-Rate-Limit'}->[0],"1k/1m","Check that the vendor attribute".
			"'14988:Mikrotik-Rate-Limit' is returned on the negative side of the IF");
Nigel Kukard's avatar
Nigel Kukard committed
879

880
881
882
883
884
885
	testDBResults("Check FUP state was added to the user stats table as 0",'users_data',
		{'UserID' => $user5_ID,	'Name' => "FUPState"},
		{'Value' => 0},
		1, # Disable order
	);

Nigel Kukard's avatar
Nigel Kukard committed
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912

	#
	# Check that if we send an accounting ALIVE with a usage amount that exceeds FUP, that we trigger it
	#

	my $user6_ID = testDBInsert("Create user 'testuser6'",
		"INSERT INTO users (UserName,Disabled) VALUES ('testuser6',0)"
	);

	my $user6attr1_ID = testDBInsert("Create user 'testuser6' attribute 'User-Password'",
		"INSERT INTO user_attributes (UserID,Name,Operator,Value,Disabled) VALUES (?,?,?,?,0)",
			$user6_ID,'User-Password','==','test456'
	);

	my $user6attr2_ID = testDBInsert("Create user 'testuser6' attribute 'SMRadius-FUP-Period'",
		"INSERT INTO user_attributes (UserID,Name,Operator,Value,Disabled) VALUES (?,?,?,?,0)",
			$user6_ID,'SMRadius-FUP-Period',':=','1'
	);

	my $user6attr3_ID = testDBInsert("Create user 'testuser6' attribute 'SMRadius-FUP-Traffic-Threshold'",
		"INSERT INTO user_attributes (UserID,Name,Operator,Value,Disabled) VALUES (?,?,?,?,0)",
			$user6_ID,'SMRadius-FUP-Traffic-Threshold',':=',800
	);

	# Add an attribute so we can check the FUP match results
	my $user6attr4_ID = testDBInsert("Create user 'testuser6' attribute 'SMRadius-Evaluate'",
		"INSERT INTO user_attributes (UserID,Name,Operator,Value,Disabled) VALUES (?,?,?,?,0)",
Nigel Kukard's avatar
Nigel Kukard committed
913
			$user6_ID,'SMRadius-Evaluate','||+=',"SMRadius_FUP > 0 ? [14988:Mikrotik-Rate-Limit] = 1638k/8m : [14988:Mikrotik-Rate-Limit] = 1k/1m"
Nigel Kukard's avatar
Nigel Kukard committed
914
915
916
	);

	my $session5_ID = "582dc00";
917
	my $session5_Timestamp = time();
Nigel Kukard's avatar
Nigel Kukard committed
918
919
920

	$res = smradius::client->run(
		"--raddb","dicts",
921
		"--listen","127.0.0.1:1700",
Nigel Kukard's avatar
Nigel Kukard committed
922
923
924
		"127.0.0.1",
		"acct",
		"secret123",
Nigel Kukard's avatar
Nigel Kukard committed
925
		'User-Name=testuser6',
Nigel Kukard's avatar
Nigel Kukard committed
926
		'NAS-IP-Address=10.0.0.1',
Nigel Kukard's avatar
Nigel Kukard committed
927
		'Acct-Delay-Time=12',
Nigel Kukard's avatar
Nigel Kukard committed
928
929
		'NAS-Identifier=Test-NAS2',
		'Acct-Status-Type=Interim-Update',
Nigel Kukard's avatar
Nigel Kukard committed
930
		'Acct-Output-Packets=786933',
Nigel Kukard's avatar
Nigel Kukard committed
931
		'Acct-Output-Gigawords=0',
Nigel Kukard's avatar
Nigel Kukard committed
932
933
		'Acct-Output-Octets=808163705',
		'Acct-Input-Packets=670235',
Nigel Kukard's avatar
Nigel Kukard committed
934
		'Acct-Input-Gigawords=0',
Nigel Kukard's avatar
Nigel Kukard committed
935
936
937
		'Acct-Input-Octets=202600046',
		'Acct-Session-Time=800',
		'Event-Timestamp='.$session5_Timestamp,
Nigel Kukard's avatar
Nigel Kukard committed
938
		'Framed-IP-Address=10.0.1.1',
Nigel Kukard's avatar
Nigel Kukard committed
939
		'Acct-Session-Id='.$session5_ID,
Nigel Kukard's avatar
Nigel Kukard committed
940
941
942
943
944
945
946
947
948
		'NAS-Port-Id=wlan1',
		'Called-Station-Id=testservice2',
		'Calling-Station-Id=00:00:0C:EE:47:BF',
		'NAS-Port-Type=Ethernet',
		'NAS-Port=15729175',
		'Framed-Protocol=PPP',
		'Service-Type=Framed-User',
	);
	is(ref($res),"HASH","smradclient should return a HASH");
949
950
951
	is($res->{'listen'}->{'response'}->{'code'},"CoA-Request","Check that the packet we got back is infact a ".
			"CoA-Request");
	is($res->{'listen'}->{'response'}->{'vattributes'}->{'14988'}->{'Mikrotik-Rate-Limit'}->[0],"1638k/8m","Check that the ".
952
953
954
955
956
957
958
			"vendor attribute '14988:Mikrotik-Rate-Limit' is returned on the success side of the FUP check");

	testDBResults("Check FUP state was added to the user stats table as 1",'users_data',
		{'UserID' => $user6_ID,	'Name' => "FUPState"},
		{'Value' => 1},
		1, # Disable order
	);
959
960
961
962
963


	#
	# Check that if we send an accounting ALIVE with a usage amount that exceeds capping, that we trigger a POD
	#
Nigel Kukard's avatar
Nigel Kukard committed
964

965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
	my $user7_ID = testDBInsert("Create user 'testuser7'",
		"INSERT INTO users (UserName,Disabled) VALUES ('testuser7',0)"
	);

	my $user7attr1_ID = testDBInsert("Create user 'testuser7' attribute 'User-Password'",
		"INSERT INTO user_attributes (UserID,Name,Operator,Value,Disabled) VALUES (?,?,?,?,0)",
			$user7_ID,'User-Password','==','test456'
	);

	# Add an attribute so we can check the capping does a POD
	my $user7attr4_ID = testDBInsert("Create user 'testuser7' attribute 'SMRadius-Capping-Traffic-Limit'",
		"INSERT INTO user_attributes (UserID,Name,Operator,Value,Disabled) VALUES (?,?,?,?,0)",
			$user7_ID,'SMRadius-Capping-Traffic-Limit',':=','1'
	);


	my $session6_ID = "5209dac0";
	my $session6_Timestamp = time();

	$res = smradius::client->run(
		"--raddb","dicts",
		"--listen","127.0.0.1:1700",
		"127.0.0.1",
		"acct",
		"secret123",
		'User-Name=testuser7',
		'NAS-IP-Address=10.0.0.1',
		'Acct-Delay-Time=12',
		'NAS-Identifier=Test-NAS2',
		'Acct-Status-Type=Interim-Update',
		'Acct-Output-Packets=786933',
		'Acct-Output-Gigawords=0',
		'Acct-Output-Octets=808163705',
		'Acct-Input-Packets=670235',
		'Acct-Input-Gigawords=0',
		'Acct-Input-Octets=202600046',
For faster browsing, not all history is shown. View entire blame