rosavox/lib/alveolata/auth/test.spec.php
2025-05-23 07:33:29 +00:00

139 lines
3.9 KiB
PHP

<?php
// require_once(DIR_ALVEOLATA . '/definitions.php');
require_once(DIR_ALVEOLATA . '/auth/implementation-srp/wrapper-class-client.php');
require_once(DIR_ALVEOLATA . '/auth/implementation-srp/wrapper-class-server.php');
\alveolata\test\add(
[
'name' => 'alveolata',
'sections' => [
[
'name' => 'auth',
'sections' => [
[
'name' => 'test',
'setup' => function (&$environment) {
// vars
{
$environment['users'] = [];
$environment['state'] = null;
}
// constants
{
$srp_subject_client = \alveolata\auth\srp_subject_test();
$srp_subject_server = \alveolata\auth\srp_subject_test();
$proc_server_register = \alveolata\auth\srp_server_register(
$srp_subject_server,
function ($username, $salt, $verifier_encoded) use (&$environment) {
$environment['users'][$username] = [
'salt' => $salt,
'verifier' => $verifier_encoded,
];
}
);
$proc_client_register = \alveolata\auth\srp_client_register(
$srp_subject_client,
fn() => 'salt',
fn($username, $salt, $verifier_encoded) => $proc_server_register($username, $salt, $verifier_encoded)
);
$proc_server_login_1 = \alveolata\auth\srp_server_login_1(
$srp_subject_server,
function ($username) use (&$environment) {
if (! array_key_exists($username, $environment['users'])) {
return [
// 'found' => true,
'found' => false,
'salt' => '',
'verifier' => '',
];
}
else {
$user = $environment['users'][$username];
return [
'found' => true,
'salt' => $user['salt'],
'verifier' => $user['verifier'],
];
}
},
function ($state) use (&$environment) {
$environment['state'] = $state;
}
);
$proc_server_login_2 = \alveolata\auth\srp_server_login_2(
$srp_subject_server,
function () use (&$environment) {
return $environment['state'];
}
);
$environment['proc_client_login'] = \alveolata\auth\srp_client_login(
$srp_subject_client,
(fn($username, $a_value) => $proc_server_login_1($username, $a_value)),
(fn($m1_client) => $proc_server_login_2($m1_client))
);
$environment['user_name'] = 'hans';
$environment['user_password'] = '1234';
$proc_client_register($environment['user_name'], $environment['user_password']);
}
},
'cases' => [
[
'name' => 'user_missing',
'procedure' => function ($assert, $environment) {
// execution
{
$success = $environment['proc_client_login'](
$environment['user_name'] . '_some_bogus',
$environment['user_password']
);
}
// assertions
{
$assert->is(! $success);
}
},
],
[
'name' => 'user_existing_and_password_wrong',
'procedure' => function ($assert, $environment) {
// execution
{
$success = $environment['proc_client_login'](
$environment['user_name'],
$environment['user_password'] . '_some_bogus'
);
}
// assertions
{
$assert->is(! $success);
}
},
],
[
'name' => 'user_existing_and_password_correct',
'procedure' => function ($assert, $environment) {
// execution
{
$success = $environment['proc_client_login'](
$environment['user_name'],
$environment['user_password']
);
}
// assertions
{
$assert->is($success);
}
},
],
],
'cleanup' => function (&$environment) {
},
],
]
]
]
]
);