Refactor libroot tests

Change-Id: I114e06ce63da2d5447ae301fc9c359759046bf86
Reviewed-on: https://review.haiku-os.org/c/haiku/+/10681
Tested-by: Commit checker robot <[email protected]>
Reviewed-by: waddlesplash <[email protected]>
This commit is contained in:
Kacper Kasper
2026-04-07 04:35:59 +00:00
parent cdb71b052a
commit 9420780c0a
3 changed files with 71 additions and 158 deletions
+33 -78
View File
@@ -12,10 +12,9 @@
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include "CryptTest.h" #include <TestSuiteAddon.h>
#include <cppunit/TestFixture.h>
#include <cppunit/TestCaller.h> #include <cppunit/extensions/HelperMacros.h>
#include <cppunit/TestSuite.h>
#define PASSWORD "password" #define PASSWORD "password"
@@ -32,58 +31,40 @@
#define BSD_RESULT "_7C/.Bf/4gZk10RYRs4Y" #define BSD_RESULT "_7C/.Bf/4gZk10RYRs4Y"
CryptTest::CryptTest() class CryptTest : public CppUnit::TestFixture {
{ CPPUNIT_TEST_SUITE(CryptTest);
} CPPUNIT_TEST(TestLegacy);
CPPUNIT_TEST(TestLegacyBSD);
CPPUNIT_TEST(TestCustomSalt);
CPPUNIT_TEST(TestSaltGeneration);
CPPUNIT_TEST(TestBadSalt);
CPPUNIT_TEST(TestCryptR);
CPPUNIT_TEST_SUITE_END();
public:
CryptTest::~CryptTest() void TestLegacy()
{ {
}
void
CryptTest::setUp()
{
}
void
CryptTest::tearDown()
{
}
void
CryptTest::TestLegacy()
{
char* buf = crypt(PASSWORD, LEGACY_SALT); char* buf = crypt(PASSWORD, LEGACY_SALT);
CPPUNIT_ASSERT(buf != NULL); CPPUNIT_ASSERT(buf != NULL);
CPPUNIT_ASSERT(strcmp(buf, LEGACY_RESULT) == 0); CPPUNIT_ASSERT(strcmp(buf, LEGACY_RESULT) == 0);
} }
void TestLegacyBSD()
void {
CryptTest::TestLegacyBSD()
{
char* buf = crypt(PASSWORD, BSD_SALT); char* buf = crypt(PASSWORD, BSD_SALT);
CPPUNIT_ASSERT(buf != NULL); CPPUNIT_ASSERT(buf != NULL);
CPPUNIT_ASSERT(strcmp(buf, BSD_RESULT) == 0); CPPUNIT_ASSERT(strcmp(buf, BSD_RESULT) == 0);
} }
void TestCustomSalt()
void {
CryptTest::TestCustomSalt()
{
char* buf = crypt(PASSWORD, HASH_SALT); char* buf = crypt(PASSWORD, HASH_SALT);
CPPUNIT_ASSERT(buf != NULL); CPPUNIT_ASSERT(buf != NULL);
CPPUNIT_ASSERT(strcmp(buf, HASH_RESULT) == 0); CPPUNIT_ASSERT(strcmp(buf, HASH_RESULT) == 0);
} }
void TestSaltGeneration()
void {
CryptTest::TestSaltGeneration()
{
char tmp[200]; char tmp[200];
char* buf = crypt(PASSWORD, NULL); char* buf = crypt(PASSWORD, NULL);
@@ -91,21 +72,17 @@ CryptTest::TestSaltGeneration()
strlcpy(tmp, buf, sizeof(tmp)); strlcpy(tmp, buf, sizeof(tmp));
buf = crypt(PASSWORD, tmp); buf = crypt(PASSWORD, tmp);
CPPUNIT_ASSERT(strcmp(buf, tmp) == 0); CPPUNIT_ASSERT(strcmp(buf, tmp) == 0);
} }
void TestBadSalt()
void {
CryptTest::TestBadSalt()
{
errno = 0; errno = 0;
CPPUNIT_ASSERT(crypt(PASSWORD, HASH_BAD_SALT) == NULL); CPPUNIT_ASSERT(crypt(PASSWORD, HASH_BAD_SALT) == NULL);
CPPUNIT_ASSERT(errno == EINVAL); CPPUNIT_ASSERT(errno == EINVAL);
} }
void TestCryptR()
void {
CryptTest::TestCryptR()
{
char tmp[200]; char tmp[200];
struct crypt_data data; struct crypt_data data;
@@ -116,30 +93,8 @@ CryptTest::TestCryptR()
strlcpy(tmp, buf, sizeof(tmp)); strlcpy(tmp, buf, sizeof(tmp));
buf = crypt(PASSWORD, tmp); buf = crypt(PASSWORD, tmp);
CPPUNIT_ASSERT(strcmp(buf, tmp) == 0); CPPUNIT_ASSERT(strcmp(buf, tmp) == 0);
} }
};
void CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(CryptTest, getTestSuiteName());
CryptTest::AddTests(BTestSuite& parent)
{
CppUnit::TestSuite& suite = *new CppUnit::TestSuite("CryptTest");
suite.addTest(new CppUnit::TestCaller<CryptTest>(
"CryptTest::TestLegacy",
&CryptTest::TestLegacy));
suite.addTest(new CppUnit::TestCaller<CryptTest>(
"CryptTest::TestLegacyBSD",
&CryptTest::TestLegacyBSD));
suite.addTest(new CppUnit::TestCaller<CryptTest>(
"CryptTest::TestCustomSalt",
&CryptTest::TestCustomSalt));
suite.addTest(new CppUnit::TestCaller<CryptTest>(
"CryptTest::TestSaltGeneration",
&CryptTest::TestSaltGeneration));
suite.addTest(new CppUnit::TestCaller<CryptTest>(
"CryptTest::TestBadSalt",
&CryptTest::TestBadSalt));
suite.addTest(new CppUnit::TestCaller<CryptTest>(
"CryptTest::TestCryptR",
&CryptTest::TestCryptR));
parent.addTest("CryptTest", &suite);
}
@@ -1,37 +0,0 @@
/*
* Copyright 2017, Haiku, Inc. All Rights Reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
* Andrew Aldridge, [email protected]
*/
#ifndef CRYPT_TEST_H
#define CRYPT_TEST_H
#include <TestCase.h>
#include <TestSuite.h>
class CryptTest : public CppUnit::TestCase {
public:
CryptTest();
virtual ~CryptTest();
virtual void setUp();
virtual void tearDown();
void TestLegacy();
void TestLegacyBSD();
void TestCustomSalt();
void TestSaltGeneration();
void TestBadSalt();
void TestCryptR();
static void AddTests(BTestSuite& suite);
};
#endif // CRYPT_TEST_H
@@ -7,16 +7,11 @@
*/ */
#include <TestSuite.h>
#include <TestSuiteAddon.h> #include <TestSuiteAddon.h>
#include "CryptTest.h"
const char*
BTestSuite* getTestSuiteName()
getTestSuite()
{ {
BTestSuite* suite = new BTestSuite("LibRootPosix"); return "LibRootPosix";
CryptTest::AddTests(*suite);
return suite;
} }