1
0
mirror of https://github.com/altlinux/admc.git synced 2025-01-22 18:03:57 +03:00

warn about bad chars in sam name

add tests for SamNameEdit that tests this functionality
This commit is contained in:
Dmitry Degtyarev 2022-01-25 17:18:03 +04:00
parent 86df3eea63
commit 7d44a07a28
7 changed files with 152 additions and 0 deletions

View File

@ -107,6 +107,7 @@ Tests for ADMC
%_bindir/admc_test_rename_object_dialog
%_bindir/admc_test_create_object_dialog
%_bindir/admc_test_select_classes_widget
%_bindir/admc_test_sam_name_edit
# NOTE: remove this changelog entry when merging into sisyphus branch. This is an auto-generated changelog entry for the upstream branch. For release builds, you should add a hand-written changelog entry.
%changelog

View File

@ -247,6 +247,8 @@ const long long MILLIS_TO_100_NANOS = 10000LL;
#define SACL_SECURITY_INFORMATION 0x08
#define DACL_SECURITY_INFORMATION 0x10
#define SAM_NAME_BAD_CHARS "@\"[]:;|=+*?<>/\\,"
enum SearchScope {
SearchScope_Object,
SearchScope_Children,

View File

@ -25,6 +25,7 @@
#include "utils.h"
#include <QLineEdit>
#include <QRegularExpression>
SamNameEdit::SamNameEdit(QLineEdit *edit_arg, QLineEdit *domain_edit, QObject *parent)
: AttributeEdit(parent) {
@ -53,6 +54,39 @@ void SamNameEdit::load(AdInterface &ad, const AdObject &object) {
edit->setText(value);
}
// NOTE: requirements are from here
// https://social.technet.microsoft.com/wiki/contents/articles/11216.active-directory-requirements-for-creating-objects.aspx#Note_Regarding_the_quot_quot_Character_in_sAMAccountName
bool SamNameEdit::verify(AdInterface &ad, const QString &dn) const {
UNUSED_ARG(ad);
UNUSED_ARG(dn);
const QString new_value = edit->text();
const bool contains_bad_chars = [&]() {
const QRegularExpression sam_name_regexp = [&]() {
const QString sam_name_bad_chars_escaped = QRegularExpression::escape(SAM_NAME_BAD_CHARS);
const QString regexp_string = QString("[%1]").arg(sam_name_bad_chars_escaped);
const QRegularExpression out = QRegularExpression(regexp_string);
return out;
}();
const bool out = new_value.contains(sam_name_regexp);
return out;
}();
const bool ends_with_dot = new_value.endsWith(".");
const bool value_is_valid = (!contains_bad_chars && !ends_with_dot);
if (!value_is_valid) {
message_box_warning(edit, tr("Error"), "bad!");
}
return value_is_valid;
}
bool SamNameEdit::apply(AdInterface &ad, const QString &dn) const {
const QString new_value = edit->text();
const bool success = ad.attribute_replace_string(dn, ATTRIBUTE_SAM_ACCOUNT_NAME, new_value);

View File

@ -31,6 +31,7 @@ public:
SamNameEdit(QLineEdit *edit, QLineEdit *domain_edit_arg, QObject *parent);
void load(AdInterface &ad, const AdObject &object) override;
bool verify(AdInterface &ad, const QString &dn) const override;
bool apply(AdInterface &ad, const QString &dn) const override;
private:

View File

@ -71,6 +71,7 @@ set(TEST_TARGETS
admc_test_rename_object_dialog
admc_test_create_object_dialog
admc_test_select_classes_widget
admc_test_sam_name_edit
)
foreach(target ${TEST_TARGETS})

View File

@ -0,0 +1,70 @@
/*
* ADMC - AD Management Center
*
* Copyright (C) 2020-2021 BaseALT Ltd.
* Copyright (C) 2020-2021 Dmitry Degtyarev
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* 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, see <http://www.gnu.org/licenses/>.
*/
#include "admc_test_sam_name_edit.h"
#include "attribute_edits/sam_name_edit.h"
#include <QLineEdit>
#define TEST_ATTRIBUTE ATTRIBUTE_FIRST_NAME
void ADMCTestSamNameEdit::init() {
ADMCTest::init();
line_edit = new QLineEdit(parent_widget);
auto domain_edit = new QLineEdit(parent_widget);
edit = new SamNameEdit(line_edit, domain_edit, parent_widget);
}
void ADMCTestSamNameEdit::verify_data() {
QTest::addColumn<QString>("value");
QTest::addColumn<bool>("correct_result");
const QString bad_chars_string = SAM_NAME_BAD_CHARS;
for (int i = 0; i < bad_chars_string.length(); i++) {
const QChar bad_char = bad_chars_string.at(i);
const QString bad_char_string = QString(bad_char);
const QByteArray bad_char_bytes = bad_char_string.toUtf8();
const QString value = QString("test%1value").arg(bad_char);
QTest::newRow(bad_char_bytes.constData()) << value << false;
}
QTest::newRow("ends with dot") << "testvalue." << false;
QTest::newRow("contains dot") << "test.value" << true;
}
void ADMCTestSamNameEdit::verify() {
QFETCH(QString, value);
QFETCH(bool, correct_result);
line_edit->setText(value);
const bool actual_result = edit->verify(ad, QString());
QCOMPARE(actual_result, correct_result);
}
QTEST_MAIN(ADMCTestSamNameEdit)

View File

@ -0,0 +1,43 @@
/*
* ADMC - AD Management Center
*
* Copyright (C) 2020-2021 BaseALT Ltd.
* Copyright (C) 2020-2021 Dmitry Degtyarev
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* 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, see <http://www.gnu.org/licenses/>.
*/
#ifndef ADMC_TEST_SAM_NAME_EDIT_H
#define ADMC_TEST_SAM_NAME_EDIT_H
#include "admc_test.h"
class SamNameEdit;
class QLineEdit;
class ADMCTestSamNameEdit : public ADMCTest {
Q_OBJECT
private slots:
void init() override;
void verify_data();
void verify();
private:
SamNameEdit *edit;
QLineEdit *line_edit;
};
#endif /* ADMC_TEST_SAM_NAME_EDIT_H */