Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ci/apiv2/test_cracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def test_create_with_both_sources_rejects(self):

def test_create_with_missing_source_data_rejects(self):
obj = Cracker(crackerBinaryTypeId=1, version='7.2.7', binaryName='cracker',
sourceType='inline')
sourceType='inline', accessGroupId=1)
with self.assertRaises(HashtopolisError) as e:
obj.save()
self.assertEqual(400, e.exception.status_code)
Expand Down
8 changes: 7 additions & 1 deletion ci/apiv2/test_logentry.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ class LogEntryTest(BaseTest):
model_class = LogEntry

def test_get_one(self):
obj = LogEntry.objects.get(pk=1)
# the id of the oldest entry cannot be assumed, the server deletes the
# oldest log entries once the configured limit is exceeded
entries = LogEntry.objects.all()
if len(entries) == 0:
self.skipTest('no log entries exist yet on this database')
obj = LogEntry.objects.get(pk=entries[0].id)
self.assertIsNotNone(obj)
self.assertEqual(entries[0].id, obj.id)

# TODO: Create event which generate logenties and check if logentry is created
3 changes: 2 additions & 1 deletion ci/apiv2/test_taskwrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ def test_helper_create_supertask_generic_cracker(self):
crackerBinaryTypeId=crackertype.id,
version='1.2.3',
downloadUrl='https://example.org/generic-1.2.3.gz',
binaryName='generic-x64')
binaryName='generic-x64',
accessGroupId=1)
cracker.save()
self.delete_after_test(cracker)
hashlist = self.create_hashlist()
Expand Down
4 changes: 2 additions & 2 deletions ci/apiv2/testfiles/cracker/create_cracker_001.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
"crackerBinaryTypeId": 1,
"version": "0.0.1",
"downloadUrl": "https://example.org/files/cracker-0.0.1.7z",
"binaryName": "cracker"
"binaryName": "cracker",
"accessGroupId": 1
}

4 changes: 2 additions & 2 deletions ci/apiv2/testfiles/cracker/create_cracker_002.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"crackerBinaryTypeId": 1,
"version": "0.0.1",
"downloadUrl": "https://example.org/files/cracker-0.0.1.7z"
"downloadUrl": "https://example.org/files/cracker-0.0.1.7z",
"accessGroupId": 1
}

4 changes: 2 additions & 2 deletions ci/phpunit/TestBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,10 @@ protected function createCrackerBinaryType(): CrackerBinaryType {
/**
* @throws Exception
*/
protected function createCrackerBinary(CrackerBinaryType $crackerBinaryType): CrackerBinary {
protected function createCrackerBinary(CrackerBinaryType $crackerBinaryType, int $accessGroupId = 1): CrackerBinary {
$crackerBinary = $this->createDatabaseObject(
Factory::getCrackerBinaryFactory(),
new CrackerBinary(null, $crackerBinaryType->getId(), '1.0.' . uniqid(), 'https://example.invalid/' . uniqid(), 'binary_' . uniqid(), null)
new CrackerBinary(null, $crackerBinaryType->getId(), '1.0.' . uniqid(), 'https://example.invalid/' . uniqid(), 'binary_' . uniqid(), null, $accessGroupId)
);
$this->assertTrue($crackerBinary instanceof CrackerBinary);
return $crackerBinary;
Expand Down
2 changes: 1 addition & 1 deletion ci/phpunit/dba/AbstractModelFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1681,7 +1681,7 @@ private function setUpHealthCheck(): array {
$crackerBinaryType = new CrackerBinaryType(null, '', 0);
$crackerBinaryType = $this->createDatabaseObject(Factory::getCrackerBinaryTypeFactory(), $crackerBinaryType);

$crackerBinary = new CrackerBinary(null, $crackerBinaryType->getId(), '', '', '', null);
$crackerBinary = new CrackerBinary(null, $crackerBinaryType->getId(), '', '', '', null, 1);
$crackerBinary = $this->createDatabaseObject(Factory::getCrackerBinaryFactory(), $crackerBinary);

$healthCheck = new HealthCheck(null, 0, 0, 0, $hashType->getId(), $crackerBinary->getId(), 0, '');
Expand Down
2 changes: 1 addition & 1 deletion ci/phpunit/dba/MassUpdateSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public function testMassSingleUpdateWithMappedColumn(): void {
$agent = $this->createDatabaseObject(Factory::getAgentFactory(), new Agent(null, '', '', 0, '', '', 0, 0, 0, '', '', 0, '', null, 0, ''));
$hashType = $this->createDatabaseObject(Factory::getHashTypeFactory(), new HashType(null, $prefix . '_ht', 0, 0));
$cbt = $this->createDatabaseObject(Factory::getCrackerBinaryTypeFactory(), new CrackerBinaryType(null, '', 0));
$cb = $this->createDatabaseObject(Factory::getCrackerBinaryFactory(), new CrackerBinary(null, $cbt->getId(), '', '', '', null));
$cb = $this->createDatabaseObject(Factory::getCrackerBinaryFactory(), new CrackerBinary(null, $cbt->getId(), '', '', '', null, 1));
$healthCheck = $this->createDatabaseObject(Factory::getHealthCheckFactory(), new HealthCheck(null, 0, 0, 0, $hashType->getId(), $cb->getId(), 0, ''));

$hca1 = $this->createDatabaseObject(Factory::getHealthCheckAgentFactory(), new HealthCheckAgent(null, $healthCheck->getId(), $agent->getId(), 0, 0, 0, 0, 100, ''));
Expand Down
11 changes: 8 additions & 3 deletions ci/phpunit/downloadapi/DownloadAppTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Hashtopolis\dba\Factory;
use Hashtopolis\dba\models\Agent;
use Hashtopolis\dba\models\AccessGroupAgent;
use Hashtopolis\dba\models\CrackerBinary;
use Hashtopolis\dba\models\CrackerBinaryType;
use Hashtopolis\inc\defines\DDirectories;
Expand Down Expand Up @@ -43,20 +44,24 @@ protected function setUp(): void {
);
$this->externalBinary = $this->createDatabaseObject(
Factory::getCrackerBinaryFactory(),
new CrackerBinary(null, $this->type->getId(), '1.0.0', 'http://example.com/hc.7z', 'testcracker', null)
new CrackerBinary(null, $this->type->getId(), '1.0.0', 'http://example.com/hc.7z', 'testcracker', null, 1)
);

// create a locally stored binary through the import source
$this->agentToken = 'dl-test-' . uniqid();
$this->createDatabaseObject(
$agent = $this->createDatabaseObject(
Factory::getAgentFactory(),
new Agent(null, 'download-test-agent', '', 0, '', '', 0, 0, 0, $this->agentToken, '', 0, '', null, 0, '')
);
$this->createDatabaseObject(
Factory::getAccessGroupAgentFactory(),
new AccessGroupAgent(null, 1, $agent->getId())
);

$importName = 'download-test-' . uniqid() . '.7z';
$this->archiveContent = self::SEVEN_ZIP_MAGIC . 'download-test-content';
file_put_contents(self::getImportPath() . $importName, $this->archiveContent);
$this->localBinary = CrackerUtils::createBinaryFromUpload('7.2.7', 'testcracker', $this->type->getId(), 'import', $importName);
$this->localBinary = CrackerUtils::createBinaryFromUpload('7.2.7', 'testcracker', $this->type->getId(), 'import', $importName, 1);
$this->registerDatabaseObject(Factory::getCrackerBinaryFactory(), $this->localBinary);

if (isset($_SERVER['HTTP_RANGE'])) {
Expand Down
28 changes: 24 additions & 4 deletions ci/phpunit/fixtures/openapi/crackerbinarytype.spec.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion ci/phpunit/inc/UtilTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -974,7 +974,8 @@ public function testCheckOrCreateInitialObjectCrackerBinary(): void {
'crackerBinaryTypeId' => $typeId,
'version' => '7.0.0',
'downloadUrl' => 'https://example.com/test.7z',
'binaryName' => 'testHashcat'
'binaryName' => 'testHashcat',
'accessGroupId' => 1
];
Util::checkOrCreateInitialObject(Factory::getCrackerBinaryFactory(), $data);
$obj = Factory::getCrackerBinaryFactory()->get($id);
Expand Down
7 changes: 7 additions & 0 deletions ci/phpunit/inc/utils/AccessGroupUtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Hashtopolis\dba\models\AccessGroupAgent;
use Hashtopolis\dba\models\AccessGroupUser;
use Hashtopolis\dba\models\Agent;
use Hashtopolis\dba\models\CrackerBinary;
use Hashtopolis\dba\models\Chunk;
use Hashtopolis\dba\models\File;
use Hashtopolis\dba\models\Hashlist;
Expand All @@ -18,6 +19,7 @@
use Hashtopolis\inc\defines\DHashcatStatus;
use Hashtopolis\inc\defines\DLimits;
use Hashtopolis\inc\HTException;
use Hashtopolis\inc\utils\CrackerUtils;
use Hashtopolis\TestBase;
use Override;

Expand Down Expand Up @@ -262,12 +264,15 @@ public function testDeleteGroupReassignsDependentEntitiesToDefaultGroup(): void
$hashlist = $this->createHashlist($groupToDelete, $hashType);
$taskWrapper = $this->createTaskWrapper($groupToDelete, $hashlist);
$file = $this->createFile($groupToDelete);
$crackerBinary = CrackerUtils::createBinary('1.0.0', 'delgroup-cracker', 'http://example.com/hc.7z', 1, $groupToDelete->getId());
$this->registerDatabaseObject(Factory::getCrackerBinaryFactory(), $crackerBinary);

AccessGroupUtils::deleteGroup($groupToDelete->getId());

$updatedHashlist = Factory::getHashlistFactory()->get($hashlist->getId());
$updatedTaskWrapper = Factory::getTaskWrapperFactory()->get($taskWrapper->getId());
$updatedFile = Factory::getFileFactory()->get($file->getId());
$updatedCrackerBinary = Factory::getCrackerBinaryFactory()->get($crackerBinary->getId());
$deletedGroup = Factory::getAccessGroupFactory()->get($groupToDelete->getId());
$remainingUsers = AccessGroupUtils::getUsers($groupToDelete->getId());
$remainingAgents = AccessGroupUtils::getAgents($groupToDelete->getId());
Expand All @@ -278,6 +283,8 @@ public function testDeleteGroupReassignsDependentEntitiesToDefaultGroup(): void
$this->assertSame($defaultGroup->getId(), $updatedTaskWrapper->getAccessGroupId());
$this->assertInstanceOf(File::class, $updatedFile);
$this->assertSame($defaultGroup->getId(), $updatedFile->getAccessGroupId());
$this->assertInstanceOf(CrackerBinary::class, $updatedCrackerBinary);
$this->assertSame($defaultGroup->getId(), $updatedCrackerBinary->getAccessGroupId());
$this->assertNull($deletedGroup);
$this->assertSame([], $remainingUsers);
$this->assertSame([], $remainingAgents);
Expand Down
4 changes: 2 additions & 2 deletions ci/phpunit/inc/utils/AccessUtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ public function testAgentCanAccessTaskWhenWrapperHashlistAndFilesAreAllowed(): v
$hashlist = $this->createHashlist($group, $hashType);
$taskWrapper = $this->createTaskWrapper($group, $hashlist);
$crackerBinaryType = $this->createCrackerBinaryType();
$crackerBinary = $this->createCrackerBinary($crackerBinaryType);
$crackerBinary = $this->createCrackerBinary($crackerBinaryType, $group->getId());
$task = $this->createTask($taskWrapper, $crackerBinary, $crackerBinaryType);
$file = $this->createFile($group);

Expand All @@ -426,4 +426,4 @@ public function testAgentCanAccessTaskWhenWrapperHashlistAndFilesAreAllowed(): v

$this->assertTrue(AccessUtils::agentCanAccessTask($agent, $task));
}
}
}
41 changes: 40 additions & 1 deletion ci/phpunit/inc/utils/CrackerBinaryUtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@
use Hashtopolis\dba\Factory;
use Hashtopolis\dba\models\CrackerBinary;
use Hashtopolis\dba\models\CrackerBinaryType;
use Hashtopolis\dba\models\AccessGroupUser;
use Hashtopolis\inc\HTException;
use Hashtopolis\inc\utils\AccessGroupUtils;
use Hashtopolis\inc\utils\AccessUtils;
use Hashtopolis\inc\utils\CrackerBinaryUtils;
use Hashtopolis\inc\utils\CrackerUtils;
use Hashtopolis\TestBase;


Expand Down Expand Up @@ -36,7 +40,7 @@ protected function setUp(): void {
private function addBinary(string $version): AbstractModel {
return $this->createDatabaseObject(
Factory::getCrackerBinaryFactory(),
new CrackerBinary(null, $this->type->getId(), $version, 'http://example.com', 'testcracker', null)
new CrackerBinary(null, $this->type->getId(), $version, 'http://example.com', 'testcracker', null, 1)
);
}

Expand Down Expand Up @@ -75,4 +79,39 @@ public function testGetNewestVersionOutOfOrderInsertStillReturnsHighest(): void
$result = CrackerBinaryUtils::getNewestVersion($this->type->getId());
$this->assertSame($newest->getId(), $result->getId());
}

// Verifies that getNewestVersion() only considers binaries of access groups the
// user is a member of — binaries of one type can be in different groups.
public function testGetNewestVersionRespectsUserGroups(): void {
$group = $this->createAccessGroup('ag-newestversion');
$user = $this->createUser('newestversion-user');

// 2.0.0 is in the default group, 1.0.0 in the group of the user
$highVersion = $this->addBinary('2.0.0');
$lowVersion = CrackerUtils::createBinary('1.0.0', 'testcracker', 'http://example.com', $this->type->getId(), $group->getId());
$this->registerDatabaseObject(Factory::getCrackerBinaryFactory(), $lowVersion);
$this->createDatabaseObject(
Factory::getAccessGroupUserFactory(),
new AccessGroupUser(null, $group->getId(), $user->getId())
);
// createUser made the user a member of the default group too, remove it so
// only the binary in the test group is accessible
AccessGroupUtils::removeUser($user->getId(), AccessUtils::getOrCreateDefaultAccessGroup()->getId());

// without a user all binaries are considered
$this->assertSame($highVersion->getId(), CrackerBinaryUtils::getNewestVersion($this->type->getId())->getId());
// the user can only access 1.0.0, so it is picked over the newer 2.0.0
$this->assertSame($lowVersion->getId(), CrackerBinaryUtils::getNewestVersion($this->type->getId(), $user)->getId());

// a user without access to any binary of the type gets no version
$otherUser = $this->createUser('newestversion-other-user');
AccessGroupUtils::removeUser($otherUser->getId(), AccessUtils::getOrCreateDefaultAccessGroup()->getId());
try {
CrackerBinaryUtils::getNewestVersion($this->type->getId(), $otherUser);
$this->fail('Expected HTException when the user has no accessible binary');
}
catch (HTException $e) {
$this->assertStringContainsString('No binary versions available', $e->getMessage());
}
}
}
Loading