/usr/share/perl5/SambaTest.pm is in zentyal-samba 2.3.3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 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 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 | # Copyright (C) 2008-2012 eBox Technologies S.L.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2, as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package SambaTest;
use strict;
use warnings;
use base 'Test::Class';
use File::Path;
use File::Basename;
use File::stat;
use Test::More;
use Test::Exception;
use Test::File;
use EBox::Samba;
use EBox::UsersAndGroups;
use Readonly;
Readonly::Scalar my $TEST_DIR => '/tmp/ebox.samba.root.test';
Readonly::Scalar my $CONFIG_BACKUP_DIR => "$TEST_DIR/backup";
Readonly::Scalar my $CONFIG_BACKUP_WO_SHARES_DIR => "$TEST_DIR/backup-wo";
Readonly::Scalar my $FULL_BACKUP_DIR => "$TEST_DIR/full";
Readonly::Scalar my $FULL_BACKUP_WO_SHARES_DIR => "$TEST_DIR/full-wo";
Readonly::Scalar my $FULL_BACKUP_LEFTOVER_DIR => "$TEST_DIR/full-left";
Readonly::Scalar my $CONFIG_BACKUP_LEFTOVER_DIR => "$TEST_DIR/config-left";
Readonly::Scalar my $TEST_USER => 'testUser354';
Readonly::Scalar my $TEST_USER_LEFTOVER => $TEST_USER . 'l';
sub __notice : Test(startup)
{
diag "This test must be run as root\n";
diag "It requires that you had isntalled the samba and usergroups modules and his asscoiate software\n";
diag "WARNING: Do not use it in a production system. It may change or corrupt your data\n";
}
sub _initEBox : Test(startup)
{
EBox::init();
}
sub setupTestDir : Test(startup)
{
if (-e $TEST_DIR) {
File::Path::rmtree($TEST_DIR);
}
File::Path::mkpath([$TEST_DIR, $CONFIG_BACKUP_DIR, $FULL_BACKUP_DIR, $CONFIG_BACKUP_WO_SHARES_DIR, $FULL_BACKUP_WO_SHARES_DIR, $FULL_BACKUP_LEFTOVER_DIR, $CONFIG_BACKUP_LEFTOVER_DIR]);
}
sub _removeTestUsers
{
my $users = EBox::Global->modInstance('users');
foreach my $user ($TEST_USER, $TEST_USER_LEFTOVER) {
if ($users->userExists($user)) {
$users->delUser($user);
}
EBox::Sudo::root('/bin/rm -rf ' . EBox::SambaLdapUser::usersPath() . '/' . $user );
}
}
sub teardownTestUsers : Test(teardown)
{
_removeTestUsers();
}
sub setupTestUser : Test(setup)
{
_removeTestUsers();
}
sub configBackupWithoutSharesTest : Test(2)
{
_checkBackup($CONFIG_BACKUP_WO_SHARES_DIR, 0);
_checkRestore($CONFIG_BACKUP_WO_SHARES_DIR, 0);
}
sub configBackupTest : Test(8)
{
my $status = _setConfig();
_checkBackup($CONFIG_BACKUP_DIR, 0);
ok !( -f "$CONFIG_BACKUP_DIR/samba.bak"), 'Checking that configuration data is not stored in the backup root dir';
_messConfig();
_checkRestore($CONFIG_BACKUP_DIR, 0);
_checkConfig($status);
}
sub fullBackupWithoutSharesTest : Test(2)
{
_checkBackup($FULL_BACKUP_WO_SHARES_DIR, 1);
_checkRestore($FULL_BACKUP_WO_SHARES_DIR, 1);
}
sub fullBackupTest : Test(7)
{
my $status = _setConfig();
_checkBackup($FULL_BACKUP_DIR, 1);
_messConfig();
_checkRestore($FULL_BACKUP_DIR, 1);
_checkConfig($status);
}
sub _checkBackup
{
my ($dir, $fullBackup) = @_;
my $samba = EBox::Global->modInstance('samba');
my $users = EBox::Global->modInstance('users');
lives_ok {
# $users->makeBackup($dir, fullBackup => $fullBackup);
$samba->makeBackup($dir, fullBackup => $fullBackup) ;
} 'Backup data in ' . $fullBackup ? 'full backup mode' : 'configuration backup mode' ;
}
sub _checkRestore
{
my ($dir, $fullRestore) = @_;
my $samba = EBox::Global->modInstance('samba');
my $users = EBox::Global->modInstance('users');
lives_ok {
# $users->restoreBackup($dir, fullRestore => $fullRestore);
$samba->restoreBackup($dir, fullRestore => $fullRestore) ;
} 'Restore data in ' . $fullRestore ? 'full restore mode' : 'configuration restore mode' ;
}
sub _setConfig
{
my $samba = EBox::Global->modInstance('samba');
my $users = EBox::Global->modInstance('users');
$samba->setFileService(1);
$users->addUser({user => $TEST_USER, fullname => 'aa', password => 'a', comment => 'a'});
my $homedir = $users->userInfo($TEST_USER)->{homeDirectory};
my $homedirStat = stat($homedir);
return { homedirStat => $homedirStat };
}
sub _messConfig
{
my $samba = EBox::Global->modInstance('samba');
my $users = EBox::Global->modInstance('users');
# $users->addUser({user => $TEST_USER_LEFTOVER, fullname => 'aa', password => 'a', comment => 'a'});
$samba->setFileService(0);
my $homedir = $users->userInfo($TEST_USER)->{homeDirectory};
EBox::Sudo::root("/bin/rm -rf $homedir");
(! -e $homedir) or die 'homedir not removed' ;
}
sub _checkConfig
{
my ($status) = @_;
my $samba = EBox::Global->modInstance('samba');
my $users = EBox::Global->modInstance('users');
ok $samba->isEnabled(), 'Checking that file service was restored';
# ok !$users->userExists($TEST_USER_LEFTOVER), "checking that user added after backup does not exists";
my $homedir = $users->userInfo($TEST_USER)->{homeDirectory};
_checkRestoredDir($homedir, $status->{homedirStat});
}
# that counts for 4 checks
sub _checkRestoredDir
{
my ($dir, $previousStat) = @_;
my $newStat = EBox::Sudo::stat($dir);
ok $newStat, 'Checking if dir was restored';
SKIP: {
skip 3, "Dir not restored so we don't test ownership and permissions" unless defined $newStat;
foreach (qw(uid gid mode)) {
is $newStat->$_, $previousStat->$_, "Checking restored $_";
}
}
}
# this counts for 49 tests
sub _leftoversTest
{
my ($backupDir, $fullBackup) = @_;
my $samba = EBox::Global->modInstance('samba');
my $users = EBox::Global->modInstance('users');
my $leftoversBase = $samba->leftoversDir();
EBox::Sudo::root("/bin/rm -rf $leftoversBase");
my $status = _setConfig();
_checkBackup($backupDir, $fullBackup);
_messConfig();
# simulate leftover bits from user
my $homedir = EBox::SambaLdapUser::usersPath() . '/leftoverUserTest';
_simulateLeftoverDir($homedir);
# simulate leftover bits from group
my $groupdir = EBox::SambaLdapUser::groupsPath() . '/leftoverGroupTest';
_simulateLeftoverDir($groupdir);
_checkRestore($backupDir, $fullBackup);
_checkConfig($status);
diag "Checking if leftover stuff was correctly treated\n";
my $userLeftoverDir = $leftoversBase . '/users/' . File::Basename::basename($homedir);
_checkLeftoverDirAfterBackup($homedir, $userLeftoverDir);
my $groupLeftoverDir = $leftoversBase . '/groups/' . File::Basename::basename($groupdir);
_checkLeftoverDirAfterBackup($groupdir, $groupLeftoverDir);
diag "we will put leftovers and restore again to be sure that the leftover are respected";
_simulateLeftoverDir($homedir);
_simulateLeftoverDir($groupdir);
_checkRestore($backupDir, $fullBackup);
_checkConfig($status);
my $userLeftoverDir2= "$userLeftoverDir.2";
_checkLeftoverDirAfterBackup($homedir, $userLeftoverDir2);
my $groupLeftoverDir2= "$groupLeftoverDir.2";
_checkLeftoverDirAfterBackup($groupdir, $groupLeftoverDir2);
}
sub _simulateLeftoverDir
{
my ($dir) = @_;
EBox::Sudo::root("/bin/mkdir $dir");
my $dirFile = "$dir/canary";
EBox::Sudo::root("/bin/touch $dirFile");
EBox::Sudo::root("/bin/chown -R 3000.3000 $dir"); # arbitray id for simulate lost id
}
sub _checkLeftoverDirAfterBackup
{
my ($previousDir, $leftoverDir) = @_;
diag "Case $previousDir $leftoverDir \n";
ok !(-d $previousDir), 'Checking if homedir was not left in his previous place';
my $stDir = EBox::Sudo::stat($leftoverDir);
ok $stDir, 'Checking if homedir was moved to leftover dir';
SKIP:{
skip 3, "Home dir not moved so ownership and permissions tests skipped" unless defined $stDir;
is $stDir->uid, 0, 'Checking that file now is owner by root';
is $stDir->gid, 0, 'Checking that file now is owner by root group';
my $permissions = EBox::FileSystem::permissionsFromStat($stDir);
is $permissions, '0755', 'Checking that user leftover dir has restricitive permissions';
}
my $stCanary = EBox::Sudo::stat($leftoverDir . '/canary');
ok $stCanary, 'Checking that canary file was not lost';
SKIP:{
skip 3, "Canary file lost so ownership and permissions tests skipped" unless $stCanary;
is $stCanary->uid, 0, 'Checking that file now is owner by root';
is $stCanary->gid, 0, 'Checking that file now is owner by root group';
my $filePermissions = EBox::FileSystem::permissionsFromStat($stCanary);
is $filePermissions, '0600', 'Checking that canary file has restricitive permissions';
}
}
1;
|