From 9420780c0ac7d37ddec58401046269304f83f7d9 Mon Sep 17 00:00:00 2001 From: Kacper Kasper Date: Mon, 6 Apr 2026 21:45:55 +0200 Subject: [PATCH] Refactor libroot tests Change-Id: I114e06ce63da2d5447ae301fc9c359759046bf86 Reviewed-on: https://review.haiku-os.org/c/haiku/+/10681 Tested-by: Commit checker robot Reviewed-by: waddlesplash --- src/tests/system/libroot/posix/CryptTest.cpp | 181 +++++++----------- src/tests/system/libroot/posix/CryptTest.h | 37 ---- .../system/libroot/posix/LibRootPosix.cpp | 11 +- 3 files changed, 71 insertions(+), 158 deletions(-) delete mode 100644 src/tests/system/libroot/posix/CryptTest.h diff --git a/src/tests/system/libroot/posix/CryptTest.cpp b/src/tests/system/libroot/posix/CryptTest.cpp index ec29df98d9..3e9c6929f5 100644 --- a/src/tests/system/libroot/posix/CryptTest.cpp +++ b/src/tests/system/libroot/posix/CryptTest.cpp @@ -12,10 +12,9 @@ #include #include -#include "CryptTest.h" - -#include -#include +#include +#include +#include #define PASSWORD "password" @@ -32,114 +31,70 @@ #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: + void TestLegacy() + { + char* buf = crypt(PASSWORD, LEGACY_SALT); + CPPUNIT_ASSERT(buf != NULL); + CPPUNIT_ASSERT(strcmp(buf, LEGACY_RESULT) == 0); + } + + void TestLegacyBSD() + { + char* buf = crypt(PASSWORD, BSD_SALT); + CPPUNIT_ASSERT(buf != NULL); + CPPUNIT_ASSERT(strcmp(buf, BSD_RESULT) == 0); + } + + void TestCustomSalt() + { + char* buf = crypt(PASSWORD, HASH_SALT); + CPPUNIT_ASSERT(buf != NULL); + CPPUNIT_ASSERT(strcmp(buf, HASH_RESULT) == 0); + } + + void TestSaltGeneration() + { + char tmp[200]; + + char* buf = crypt(PASSWORD, NULL); + CPPUNIT_ASSERT(buf != NULL); + strlcpy(tmp, buf, sizeof(tmp)); + buf = crypt(PASSWORD, tmp); + CPPUNIT_ASSERT(strcmp(buf, tmp) == 0); + } + + void TestBadSalt() + { + errno = 0; + CPPUNIT_ASSERT(crypt(PASSWORD, HASH_BAD_SALT) == NULL); + CPPUNIT_ASSERT(errno == EINVAL); + } + + void TestCryptR() + { + char tmp[200]; + + struct crypt_data data; + data.initialized = 0; + + char* buf = crypt_r(PASSWORD, NULL, &data); + CPPUNIT_ASSERT(buf != NULL); + strlcpy(tmp, buf, sizeof(tmp)); + buf = crypt(PASSWORD, tmp); + CPPUNIT_ASSERT(strcmp(buf, tmp) == 0); + } +}; -CryptTest::~CryptTest() -{ -} - - -void -CryptTest::setUp() -{ -} - - -void -CryptTest::tearDown() -{ -} - - -void -CryptTest::TestLegacy() -{ - char* buf = crypt(PASSWORD, LEGACY_SALT); - CPPUNIT_ASSERT(buf != NULL); - CPPUNIT_ASSERT(strcmp(buf, LEGACY_RESULT) == 0); -} - - -void -CryptTest::TestLegacyBSD() -{ - char* buf = crypt(PASSWORD, BSD_SALT); - CPPUNIT_ASSERT(buf != NULL); - CPPUNIT_ASSERT(strcmp(buf, BSD_RESULT) == 0); -} - - -void -CryptTest::TestCustomSalt() -{ - char* buf = crypt(PASSWORD, HASH_SALT); - CPPUNIT_ASSERT(buf != NULL); - CPPUNIT_ASSERT(strcmp(buf, HASH_RESULT) == 0); -} - - -void -CryptTest::TestSaltGeneration() -{ - char tmp[200]; - - char* buf = crypt(PASSWORD, NULL); - CPPUNIT_ASSERT(buf != NULL); - strlcpy(tmp, buf, sizeof(tmp)); - buf = crypt(PASSWORD, tmp); - CPPUNIT_ASSERT(strcmp(buf, tmp) == 0); -} - - -void -CryptTest::TestBadSalt() -{ - errno = 0; - CPPUNIT_ASSERT(crypt(PASSWORD, HASH_BAD_SALT) == NULL); - CPPUNIT_ASSERT(errno == EINVAL); -} - - -void -CryptTest::TestCryptR() -{ - char tmp[200]; - - struct crypt_data data; - data.initialized = 0; - - char* buf = crypt_r(PASSWORD, NULL, &data); - CPPUNIT_ASSERT(buf != NULL); - strlcpy(tmp, buf, sizeof(tmp)); - buf = crypt(PASSWORD, tmp); - CPPUNIT_ASSERT(strcmp(buf, tmp) == 0); -} - - -void -CryptTest::AddTests(BTestSuite& parent) -{ - CppUnit::TestSuite& suite = *new CppUnit::TestSuite("CryptTest"); - suite.addTest(new CppUnit::TestCaller( - "CryptTest::TestLegacy", - &CryptTest::TestLegacy)); - suite.addTest(new CppUnit::TestCaller( - "CryptTest::TestLegacyBSD", - &CryptTest::TestLegacyBSD)); - suite.addTest(new CppUnit::TestCaller( - "CryptTest::TestCustomSalt", - &CryptTest::TestCustomSalt)); - suite.addTest(new CppUnit::TestCaller( - "CryptTest::TestSaltGeneration", - &CryptTest::TestSaltGeneration)); - suite.addTest(new CppUnit::TestCaller( - "CryptTest::TestBadSalt", - &CryptTest::TestBadSalt)); - suite.addTest(new CppUnit::TestCaller( - "CryptTest::TestCryptR", - &CryptTest::TestCryptR)); - parent.addTest("CryptTest", &suite); -} +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(CryptTest, getTestSuiteName()); diff --git a/src/tests/system/libroot/posix/CryptTest.h b/src/tests/system/libroot/posix/CryptTest.h deleted file mode 100644 index 564f58c617..0000000000 --- a/src/tests/system/libroot/posix/CryptTest.h +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright 2017, Haiku, Inc. All Rights Reserved. - * Distributed under the terms of the MIT License. - * - * Authors: - * Andrew Aldridge, i80and@foxquill.com - */ - - -#ifndef CRYPT_TEST_H -#define CRYPT_TEST_H - - -#include -#include - - -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 diff --git a/src/tests/system/libroot/posix/LibRootPosix.cpp b/src/tests/system/libroot/posix/LibRootPosix.cpp index c7c32b5466..8e7be9d75d 100644 --- a/src/tests/system/libroot/posix/LibRootPosix.cpp +++ b/src/tests/system/libroot/posix/LibRootPosix.cpp @@ -7,16 +7,11 @@ */ -#include #include -#include "CryptTest.h" - -BTestSuite* -getTestSuite() +const char* +getTestSuiteName() { - BTestSuite* suite = new BTestSuite("LibRootPosix"); - CryptTest::AddTests(*suite); - return suite; + return "LibRootPosix"; }