summaryrefslogtreecommitdiffstats
path: root/debian/tests/hammer_slapd
blob: 9ad7f9932d126a1c91755785e7804c4779ef78f3 (plain)
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
#! /usr/bin/perl -w

use Net::LDAP;
use Data::Dumper;

$host = "localhost";			# LDAP server
$basedn = "dc=galaxy";			# Base DN
$admindn = "cn=admin, $basedn";		# Admin entry
$adminpass = "foo";			# Password
$group = $ARGV[0] || "People";

$ldap = Net::LDAP->new("$host", onerror => "die");
$ldap->bind($admindn, password => $adminpass);

sub create_group {
	$results = $ldap->search(base => "$basedn", 
		filter => "ou=$group", scope => "one");
	unless ($results->count > 0) {
		$ldap->add("ou=$group, $basedn", attr => [
			  ou => "$group", 
			  objectClass => [ "top", "organizationalUnit" ]
			]);
	}
}

sub invent_name {
	our @words;
	unless (@words) {
		open WORDS, "/usr/share/dict/british-english-large";
		@words = grep /^[A-Z]\w{0,11}$/, <WORDS>;
		map { chomp } @words;
		close WORDS;
	}

	my $index = int(rand(@words));
	$index = int(rand(@words)) while not defined $words[$index];
	my $word = $words[$index];
	delete $words[$index];
	return $word;
}

sub invent_names {
	our @names;

	foreach (1..1000) {
		push @names, { cn => invent_name, sn => invent_name };
	}
}

sub create_entries {
	foreach my $name (@names) {
		create_account(%$name);
	}
}

sub create_account {
	our $uid; 
	$uid = 1000 if not defined $uid;

	my %id = @_;
	my $login = $id{cn};
	$login =~ tr/A-Z/a-z/;
	$ldap->add("uid=$login, ou=$group, $basedn", attr => [
		%id, 
		objectClass => [ "top", "person", "posixAccount" ], 
		uid => $login, 
		uidNumber => $uid++, 
		gidNumber => 1000, 
		homeDirectory => "/home/$login" ]);
}

sub delete_entries {
	foreach my $name (@names) {
		delete_account(%$name);
	}
}

sub delete_account {
	my %id = @_;
	my $login = $id{cn};
	$login =~ tr/A-Z/a-z/;
	$ldap->delete("uid=$login, ou=$group, $basedn");
}

sub search_entries {
	foreach (1..10000) {
		my $num = int(rand(@names));
		$login = $names[$num]->{cn};
		$login =~ tr/A-Z/a-z/;
		$ldap->search(base => "$basedn", filter => "uid=$login");
	}
}

create_group;
invent_names;
create_entries;
search_entries;
delete_entries;