diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 16:51:28 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 16:51:28 +0000 |
commit | 940b4d1848e8c70ab7642901a68594e8016caffc (patch) | |
tree | eb72f344ee6c3d9b80a7ecc079ea79e9fba8676d /comphelper/qa/unit | |
parent | Initial commit. (diff) | |
download | libreoffice-940b4d1848e8c70ab7642901a68594e8016caffc.tar.xz libreoffice-940b4d1848e8c70ab7642901a68594e8016caffc.zip |
Adding upstream version 1:7.0.4.upstream/1%7.0.4upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'comphelper/qa/unit')
-rw-r--r-- | comphelper/qa/unit/base64_test.cxx | 111 | ||||
-rw-r--r-- | comphelper/qa/unit/parallelsorttest.cxx | 101 | ||||
-rw-r--r-- | comphelper/qa/unit/syntaxhighlighttest.cxx | 122 | ||||
-rw-r--r-- | comphelper/qa/unit/test_guards.cxx | 57 | ||||
-rw-r--r-- | comphelper/qa/unit/test_hash.cxx | 134 | ||||
-rw-r--r-- | comphelper/qa/unit/threadpooltest.cxx | 55 | ||||
-rw-r--r-- | comphelper/qa/unit/types_test.cxx | 97 | ||||
-rw-r--r-- | comphelper/qa/unit/variadictemplates.cxx | 181 |
8 files changed, 858 insertions, 0 deletions
diff --git a/comphelper/qa/unit/base64_test.cxx b/comphelper/qa/unit/base64_test.cxx new file mode 100644 index 000000000..97f7b78aa --- /dev/null +++ b/comphelper/qa/unit/base64_test.cxx @@ -0,0 +1,111 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <sal/types.h> +#include <cppunit/TestAssert.h> +#include <cppunit/TestFixture.h> +#include <cppunit/extensions/HelperMacros.h> + +#include <rtl/ustrbuf.hxx> + +#include <com/sun/star/uno/Sequence.hxx> + +#include <comphelper/base64.hxx> + +using namespace css; + +namespace { + +class Base64Test : public CppUnit::TestFixture +{ +public: + + void testBase64Encode(); + void testBase64Decode(); + void testBase64EncodeForOStringBuffer(); + + CPPUNIT_TEST_SUITE(Base64Test); + CPPUNIT_TEST(testBase64Encode); + CPPUNIT_TEST(testBase64Decode); + CPPUNIT_TEST(testBase64EncodeForOStringBuffer); + CPPUNIT_TEST_SUITE_END(); +}; + +void Base64Test::testBase64Encode() +{ + OUStringBuffer aBuffer(32); + uno::Sequence<sal_Int8> inputSequence; + + inputSequence = { 0, 0, 0, 0, 0, 1, 2, 3 }; + comphelper::Base64::encode(aBuffer, inputSequence); + CPPUNIT_ASSERT_EQUAL(OUString("AAAAAAABAgM="), aBuffer.toString()); + aBuffer.setLength(0); + + inputSequence = { 5, 2, 3, 0, 0, 1, 2, 3 }; + comphelper::Base64::encode(aBuffer, inputSequence); + CPPUNIT_ASSERT_EQUAL(OUString("BQIDAAABAgM="), aBuffer.toString()); + aBuffer.setLength(0); + + inputSequence = { sal_Int8(sal_uInt8(200)), 31, 77, 111, 0, 1, 2, 3 }; + comphelper::Base64::encode(aBuffer, inputSequence); + CPPUNIT_ASSERT_EQUAL(OUString("yB9NbwABAgM="), aBuffer.makeStringAndClear()); +} + +void Base64Test::testBase64Decode() +{ + uno::Sequence<sal_Int8> decodedSequence; + + uno::Sequence<sal_Int8> expectedSequence = { 0, 0, 0, 0, 0, 1, 2, 3 }; + comphelper::Base64::decode(decodedSequence, "AAAAAAABAgM="); + CPPUNIT_ASSERT(std::equal(expectedSequence.begin(), expectedSequence.end(), decodedSequence.begin())); + + expectedSequence = { 5, 2, 3, 0, 0, 1, 2, 3 }; + comphelper::Base64::decode(decodedSequence, "BQIDAAABAgM="); + CPPUNIT_ASSERT(std::equal(expectedSequence.begin(), expectedSequence.end(), decodedSequence.begin())); + + expectedSequence = { sal_Int8(sal_uInt8(200)), 31, 77, 111, 0, 1, 2, 3 }; + comphelper::Base64::decode(decodedSequence, "yB9NbwABAgM="); + CPPUNIT_ASSERT(std::equal(expectedSequence.begin(), expectedSequence.end(), decodedSequence.begin())); +} + +void Base64Test::testBase64EncodeForOStringBuffer() +{ + OStringBuffer aBuffer(32); + uno::Sequence<sal_Int8> inputSequence; + + inputSequence = { 0, 0, 0, 0, 0, 1, 2, 3 }; + comphelper::Base64::encode(aBuffer, inputSequence); + CPPUNIT_ASSERT_EQUAL(OString("AAAAAAABAgM="), aBuffer.toString()); + aBuffer.setLength(0); + + inputSequence = { 5, 2, 3, 0, 0, 1, 2, 3 }; + comphelper::Base64::encode(aBuffer, inputSequence); + CPPUNIT_ASSERT_EQUAL(OString("BQIDAAABAgM="), aBuffer.toString()); + aBuffer.setLength(0); + + inputSequence = { sal_Int8(sal_uInt8(200)), 31, 77, 111, 0, 1, 2, 3 }; + comphelper::Base64::encode(aBuffer, inputSequence); + CPPUNIT_ASSERT_EQUAL(OString("yB9NbwABAgM="), aBuffer.makeStringAndClear()); +} + +CPPUNIT_TEST_SUITE_REGISTRATION(Base64Test); + +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/comphelper/qa/unit/parallelsorttest.cxx b/comphelper/qa/unit/parallelsorttest.cxx new file mode 100644 index 000000000..90dcb3c23 --- /dev/null +++ b/comphelper/qa/unit/parallelsorttest.cxx @@ -0,0 +1,101 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#include <comphelper/parallelsort.hxx> +#include <comphelper/threadpool.hxx> +#include <rtl/string.hxx> +#include <cppunit/TestAssert.h> +#include <cppunit/TestFixture.h> +#include <cppunit/extensions/HelperMacros.h> +#include <cppunit/plugin/TestPlugIn.h> + +#include <cstdlib> +#include <vector> +#include <algorithm> +#include <random> + +class ParallelSortTest : public CppUnit::TestFixture +{ +public: + void testSortTiny(); + void testSortMedium(); + void testSortBig(); + + virtual void setUp() override; + virtual void tearDown() override; + + CPPUNIT_TEST_SUITE(ParallelSortTest); + CPPUNIT_TEST(testSortTiny); + CPPUNIT_TEST(testSortMedium); + CPPUNIT_TEST(testSortBig); + CPPUNIT_TEST_SUITE_END(); + +private: + void sortTest(size_t nLen); + void fillRandomUptoN(std::vector<size_t>& rVector, size_t N); + + comphelper::ThreadPool* pThreadPool; + size_t mnThreads; +}; + +void ParallelSortTest::setUp() +{ + pThreadPool = &comphelper::ThreadPool::getSharedOptimalPool(); + mnThreads = pThreadPool->getWorkerCount(); +} + +void ParallelSortTest::tearDown() +{ + if (pThreadPool) + pThreadPool->joinAll(); +} + +void ParallelSortTest::fillRandomUptoN(std::vector<size_t>& rVector, size_t N) +{ + rVector.resize(N); + for (size_t nIdx = 0; nIdx < N; ++nIdx) + rVector[nIdx] = nIdx; + std::shuffle(rVector.begin(), rVector.end(), std::default_random_engine(42)); +} + +void ParallelSortTest::sortTest(size_t nLen) +{ + std::vector<size_t> aVector(nLen); + fillRandomUptoN(aVector, nLen); + comphelper::parallelSort(aVector.begin(), aVector.end()); + for (size_t nIdx = 0; nIdx < nLen; ++nIdx) + { + OString aMsg = "Wrong aVector[" + OString::number(nIdx) + "]"; + CPPUNIT_ASSERT_EQUAL_MESSAGE(aMsg.getStr(), nIdx, aVector[nIdx]); + } +} + +void ParallelSortTest::testSortTiny() +{ + sortTest(5); + sortTest(15); + sortTest(16); + sortTest(17); +} + +void ParallelSortTest::testSortMedium() +{ + sortTest(1025); + sortTest(1029); + sortTest(1024 * 2 + 1); + sortTest(1024 * 2 + 9); +} + +void ParallelSortTest::testSortBig() { sortTest(1024 * 16 + 3); } + +CPPUNIT_TEST_SUITE_REGISTRATION(ParallelSortTest); + +CPPUNIT_PLUGIN_IMPLEMENT(); + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/comphelper/qa/unit/syntaxhighlighttest.cxx b/comphelper/qa/unit/syntaxhighlighttest.cxx new file mode 100644 index 000000000..8a5b66ed2 --- /dev/null +++ b/comphelper/qa/unit/syntaxhighlighttest.cxx @@ -0,0 +1,122 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#include <comphelper/syntaxhighlight.hxx> +#include <cppunit/TestAssert.h> +#include <cppunit/TestFixture.h> +#include <cppunit/extensions/HelperMacros.h> +#include <cppunit/plugin/TestPlugIn.h> +#include <rtl/ustring.hxx> + +#include <vector> + +class SyntaxHighlightTest : public CppUnit::TestFixture +{ +public: + void testBasicString(); + void testBasicComment(); + void testBasicCommentNewline(); + void testBasicEmptyComment(); + void testBasicEmptyCommentNewline(); + void testBasic(); + + CPPUNIT_TEST_SUITE(SyntaxHighlightTest); + CPPUNIT_TEST(testBasicString); + CPPUNIT_TEST(testBasicComment); + CPPUNIT_TEST(testBasicCommentNewline); + CPPUNIT_TEST(testBasicEmptyComment); + CPPUNIT_TEST(testBasicEmptyCommentNewline); + CPPUNIT_TEST(testBasic); + CPPUNIT_TEST_SUITE_END(); +}; + +void SyntaxHighlightTest::testBasicString() { + OUString s("\"foo\""); + std::vector<HighlightPortion> ps; + SyntaxHighlighter(HighlighterLanguage::Basic).getHighlightPortions(s, ps); + CPPUNIT_ASSERT_EQUAL( + static_cast<std::vector<HighlightPortion>::size_type>(1), ps.size()); + CPPUNIT_ASSERT_EQUAL(sal_Int32(0), ps[0].nBegin); + CPPUNIT_ASSERT_EQUAL(sal_Int32(5), ps[0].nEnd); + CPPUNIT_ASSERT_EQUAL(TokenType::String, ps[0].tokenType); +} + +void SyntaxHighlightTest::testBasicComment() { + OUString s("' foo"); + std::vector<HighlightPortion> ps; + SyntaxHighlighter(HighlighterLanguage::Basic).getHighlightPortions(s, ps); + CPPUNIT_ASSERT_EQUAL( + static_cast<std::vector<HighlightPortion>::size_type>(1), ps.size()); + CPPUNIT_ASSERT_EQUAL(sal_Int32(0), ps[0].nBegin); + CPPUNIT_ASSERT_EQUAL(sal_Int32(5), ps[0].nEnd); + CPPUNIT_ASSERT_EQUAL(TokenType::Comment, ps[0].tokenType); +} + +void SyntaxHighlightTest::testBasicCommentNewline() { + OUString s("' foo\n"); + std::vector<HighlightPortion> ps; + SyntaxHighlighter(HighlighterLanguage::Basic).getHighlightPortions(s, ps); + CPPUNIT_ASSERT_EQUAL( + static_cast<std::vector<HighlightPortion>::size_type>(2), ps.size()); + CPPUNIT_ASSERT_EQUAL(sal_Int32(0), ps[0].nBegin); + CPPUNIT_ASSERT_EQUAL(sal_Int32(5), ps[0].nEnd); + CPPUNIT_ASSERT_EQUAL(TokenType::Comment, ps[0].tokenType); + CPPUNIT_ASSERT_EQUAL(sal_Int32(5), ps[1].nBegin); + CPPUNIT_ASSERT_EQUAL(sal_Int32(6), ps[1].nEnd); + CPPUNIT_ASSERT_EQUAL(TokenType::EOL, ps[1].tokenType); +} + +void SyntaxHighlightTest::testBasicEmptyComment() { + OUString s("'"); + std::vector<HighlightPortion> ps; + SyntaxHighlighter(HighlighterLanguage::Basic).getHighlightPortions(s, ps); + CPPUNIT_ASSERT_EQUAL( + static_cast<std::vector<HighlightPortion>::size_type>(1), ps.size()); + CPPUNIT_ASSERT_EQUAL(sal_Int32(0), ps[0].nBegin); + CPPUNIT_ASSERT_EQUAL(sal_Int32(1), ps[0].nEnd); + CPPUNIT_ASSERT_EQUAL(TokenType::Comment, ps[0].tokenType); +} + +void SyntaxHighlightTest::testBasicEmptyCommentNewline() { + OUString s("'\n"); + std::vector<HighlightPortion> ps; + SyntaxHighlighter(HighlighterLanguage::Basic).getHighlightPortions(s, ps); + CPPUNIT_ASSERT_EQUAL( + static_cast<std::vector<HighlightPortion>::size_type>(2), ps.size()); + CPPUNIT_ASSERT_EQUAL(sal_Int32(0), ps[0].nBegin); + CPPUNIT_ASSERT_EQUAL(sal_Int32(1), ps[0].nEnd); + CPPUNIT_ASSERT_EQUAL(TokenType::Comment, ps[0].tokenType); + CPPUNIT_ASSERT_EQUAL(sal_Int32(1), ps[1].nBegin); + CPPUNIT_ASSERT_EQUAL(sal_Int32(2), ps[1].nEnd); + CPPUNIT_ASSERT_EQUAL(TokenType::EOL, ps[1].tokenType); +} + +void SyntaxHighlightTest::testBasic() +{ + OUString aBasicString(" if Mid(sText,iRun,1 )<> \" \" then Mid( sText ,iRun, 1, Chr( 1 + Asc( Mid(sText,iRun,1 )) ) '"); + + std::vector<HighlightPortion> aPortions; + SyntaxHighlighter(HighlighterLanguage::Basic).getHighlightPortions( + aBasicString, aPortions ); + + sal_Int32 prevEnd = 0; + for (auto const& portion : aPortions) + { + CPPUNIT_ASSERT_EQUAL(prevEnd, portion.nBegin); + CPPUNIT_ASSERT(portion.nBegin < portion.nEnd); + prevEnd = portion.nEnd; + } + CPPUNIT_ASSERT_EQUAL(aBasicString.getLength(), prevEnd); +} + +CPPUNIT_TEST_SUITE_REGISTRATION(SyntaxHighlightTest); + +CPPUNIT_PLUGIN_IMPLEMENT(); + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/comphelper/qa/unit/test_guards.cxx b/comphelper/qa/unit/test_guards.cxx new file mode 100644 index 000000000..1d2b5f209 --- /dev/null +++ b/comphelper/qa/unit/test_guards.cxx @@ -0,0 +1,57 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#include <sal/types.h> +#include <comphelper/flagguard.hxx> +#include <unotest/bootstrapfixturebase.hxx> + +CPPUNIT_TEST_FIXTURE(CppUnit::TestFixture, test_comphelperGuards) +{ + bool bFlag = true; + { + // Test that comphelper::ScopeGuard executes its parameter on destruction + comphelper::ScopeGuard aGuard([&bFlag] { bFlag = false; }); + CPPUNIT_ASSERT(bFlag); + } + CPPUNIT_ASSERT(!bFlag); + + { + // Test that comphelper::FlagGuard properly sets and resets the flag + comphelper::FlagGuard aGuard(bFlag); + CPPUNIT_ASSERT(bFlag); + } + CPPUNIT_ASSERT(!bFlag); + + bFlag = true; + { + // Test that comphelper::FlagGuard properly sets and resets the flag + comphelper::FlagGuard aGuard(bFlag); + CPPUNIT_ASSERT(bFlag); + } + // comphelper::FlagGuard must reset flag to false on destruction unconditionally + CPPUNIT_ASSERT(!bFlag); + + { + // Test that comphelper::FlagRestorationGuard properly sets and resets the flag + comphelper::FlagRestorationGuard aGuard(bFlag, true); + CPPUNIT_ASSERT(bFlag); + } + CPPUNIT_ASSERT(!bFlag); + + bFlag = true; + { + // Test that comphelper::FlagRestorationGuard properly sets and resets the flag + comphelper::FlagRestorationGuard aGuard(bFlag, false); + CPPUNIT_ASSERT(!bFlag); + } + // comphelper::FlagRestorationGuard must reset flag to initial state on destruction + CPPUNIT_ASSERT(bFlag); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/comphelper/qa/unit/test_hash.cxx b/comphelper/qa/unit/test_hash.cxx new file mode 100644 index 000000000..99ce5bfd1 --- /dev/null +++ b/comphelper/qa/unit/test_hash.cxx @@ -0,0 +1,134 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#include <sal/config.h> +#include <config_oox.h> +#include <comphelper/hash.hxx> +#include <comphelper/docpasswordhelper.hxx> + +#include <rtl/ustring.hxx> +#include <iomanip> + +#include <cppunit/TestFixture.h> +#include <cppunit/extensions/HelperMacros.h> + +#if USE_TLS_NSS +#include <nss.h> +#endif + +class TestHash : public CppUnit::TestFixture +{ +public: + void testMD5(); + void testSHA1(); + void testSHA256(); + void testSHA512(); + void testSHA512_NoSaltNoSpin(); + void testSHA512_saltspin(); + + virtual void tearDown() + { +#if USE_TLS_NSS + NSS_Shutdown(); +#endif + } + CPPUNIT_TEST_SUITE(TestHash); + CPPUNIT_TEST(testMD5); + CPPUNIT_TEST(testSHA1); + CPPUNIT_TEST(testSHA256); + CPPUNIT_TEST(testSHA512); + CPPUNIT_TEST(testSHA512_NoSaltNoSpin); + CPPUNIT_TEST(testSHA512_saltspin); + CPPUNIT_TEST_SUITE_END(); +}; + +namespace { + +std::string tostring(const std::vector<unsigned char>& a) +{ + std::stringstream aStrm; + for (auto& i:a) + { + aStrm << std::setw(2) << std::setfill('0') << std::hex << static_cast<int>(i); + } + + return aStrm.str(); +} + +} + +void TestHash::testMD5() +{ + comphelper::Hash aHash(comphelper::HashType::MD5); + const char* const pInput = ""; + aHash.update(reinterpret_cast<const unsigned char*>(pInput), 0); + std::vector<unsigned char> calculate_hash = aHash.finalize(); + CPPUNIT_ASSERT_EQUAL(size_t(16), calculate_hash.size()); + CPPUNIT_ASSERT_EQUAL(std::string("d41d8cd98f00b204e9800998ecf8427e"), tostring(calculate_hash)); +} + +void TestHash::testSHA1() +{ + comphelper::Hash aHash(comphelper::HashType::SHA1); + const char* const pInput = ""; + aHash.update(reinterpret_cast<const unsigned char*>(pInput), 0); + std::vector<unsigned char> calculate_hash = aHash.finalize(); + CPPUNIT_ASSERT_EQUAL(size_t(20), calculate_hash.size()); + CPPUNIT_ASSERT_EQUAL(std::string("da39a3ee5e6b4b0d3255bfef95601890afd80709"), tostring(calculate_hash)); +} + +void TestHash::testSHA256() +{ + comphelper::Hash aHash(comphelper::HashType::SHA256); + const char* const pInput = ""; + aHash.update(reinterpret_cast<const unsigned char*>(pInput), 0); + std::vector<unsigned char> calculate_hash = aHash.finalize(); + CPPUNIT_ASSERT_EQUAL(size_t(32), calculate_hash.size()); + CPPUNIT_ASSERT_EQUAL(std::string("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"), tostring(calculate_hash)); +} + +void TestHash::testSHA512() +{ + comphelper::Hash aHash(comphelper::HashType::SHA512); + const char* const pInput = ""; + aHash.update(reinterpret_cast<const unsigned char*>(pInput), 0); + std::vector<unsigned char> calculate_hash = aHash.finalize(); + CPPUNIT_ASSERT_EQUAL(size_t(64), calculate_hash.size()); + std::string aStr("cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e"); + CPPUNIT_ASSERT_EQUAL(aStr, tostring(calculate_hash)); +} + +// Must be identical to testSHA512() +void TestHash::testSHA512_NoSaltNoSpin() +{ + const char* const pInput = ""; + std::vector<unsigned char> calculate_hash = + comphelper::Hash::calculateHash( reinterpret_cast<const unsigned char*>(pInput), 0, + nullptr, 0, 0, comphelper::Hash::IterCount::NONE, comphelper::HashType::SHA512); + CPPUNIT_ASSERT_EQUAL(size_t(64), calculate_hash.size()); + std::string aStr("cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e"); + CPPUNIT_ASSERT_EQUAL(aStr, tostring(calculate_hash)); +} + +// Password, salt, hash and spin count taken from OOXML sheetProtection of +// tdf#104250 https://bugs.documentfoundation.org/attachment.cgi?id=129104 +void TestHash::testSHA512_saltspin() +{ + const OUString aPass("pwd"); + const OUString aAlgo("SHA-512"); + const OUString aSalt("876MLoKTq42+/DLp415iZQ=="); + const OUString aHash = comphelper::DocPasswordHelper::GetOoxHashAsBase64( aPass, aSalt, 100000, + comphelper::Hash::IterCount::APPEND, aAlgo); + const OUString aStr("5l3mgNHXpWiFaBPv5Yso1Xd/UifWvQWmlDnl/hsCYbFT2sJCzorjRmBCQ/3qeDu6Q/4+GIE8a1DsdaTwYh1q2g=="); + CPPUNIT_ASSERT_EQUAL(aStr, aHash); +} + +CPPUNIT_TEST_SUITE_REGISTRATION(TestHash); + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/comphelper/qa/unit/threadpooltest.cxx b/comphelper/qa/unit/threadpooltest.cxx new file mode 100644 index 000000000..10fb90c30 --- /dev/null +++ b/comphelper/qa/unit/threadpooltest.cxx @@ -0,0 +1,55 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#include <comphelper/threadpool.hxx> +#include <cppunit/TestAssert.h> +#include <cppunit/TestFixture.h> +#include <cppunit/extensions/HelperMacros.h> +#include <cppunit/plugin/TestPlugIn.h> + +#include <stdlib.h> +#include <thread> + +class ThreadPoolTest : public CppUnit::TestFixture +{ +public: + void testPreferredConcurrency(); + + CPPUNIT_TEST_SUITE(ThreadPoolTest); + CPPUNIT_TEST(testPreferredConcurrency); + CPPUNIT_TEST_SUITE_END(); +}; + +void ThreadPoolTest::testPreferredConcurrency() +{ + // Check default. + auto nThreads = comphelper::ThreadPool::getPreferredConcurrency(); + sal_Int32 nExpected = 4; // UTs are capped to 4. + CPPUNIT_ASSERT_MESSAGE("Expected no more than 4 threads", nExpected >= nThreads); + +#ifndef _WIN32 + // The result should be cached, so this should change anything. + nThreads = std::thread::hardware_concurrency() * 2; + setenv("MAX_CONCURRENCY", std::to_string(nThreads).c_str(), true); + nThreads = comphelper::ThreadPool::getPreferredConcurrency(); + CPPUNIT_ASSERT_MESSAGE("Expected no more than hardware threads", + nThreads <= static_cast<sal_Int32>(std::thread::hardware_concurrency())); + + // Revert and check. Again, nothing should change. + unsetenv("MAX_CONCURRENCY"); + nThreads = comphelper::ThreadPool::getPreferredConcurrency(); + CPPUNIT_ASSERT_MESSAGE("Expected no more than 4 threads", nExpected >= nThreads); +#endif +} + +CPPUNIT_TEST_SUITE_REGISTRATION(ThreadPoolTest); + +CPPUNIT_PLUGIN_IMPLEMENT(); + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/comphelper/qa/unit/types_test.cxx b/comphelper/qa/unit/types_test.cxx new file mode 100644 index 000000000..89ad6cae8 --- /dev/null +++ b/comphelper/qa/unit/types_test.cxx @@ -0,0 +1,97 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#include <comphelper/types.hxx> +#include <sal/types.h> + +#include <cppunit/TestAssert.h> +#include <cppunit/TestFixture.h> +#include <cppunit/extensions/HelperMacros.h> +#include <cppunit/plugin/TestPlugIn.h> + +using namespace css; + +namespace +{ +class TypesTest : public CppUnit::TestFixture +{ +public: + void testGetINT64(); + void testGetINT32(); + void testGetINT16(); + void testGetDouble(); + void testGetFloat(); + void testGetString(); + + CPPUNIT_TEST_SUITE(TypesTest); + + CPPUNIT_TEST(testGetINT64); + CPPUNIT_TEST(testGetINT32); + CPPUNIT_TEST(testGetINT16); + CPPUNIT_TEST(testGetDouble); + CPPUNIT_TEST(testGetFloat); + CPPUNIT_TEST(testGetString); + + CPPUNIT_TEST_SUITE_END(); +}; + +void TypesTest::testGetINT64() +{ + CPPUNIT_ASSERT_EQUAL(sal_Int64(1337), ::comphelper::getINT64(uno::makeAny(sal_Int64(1337)))); + + uno::Any aValue; + CPPUNIT_ASSERT_EQUAL(sal_Int64(0), ::comphelper::getINT64(aValue)); +} + +void TypesTest::testGetINT32() +{ + CPPUNIT_ASSERT_EQUAL(sal_Int32(1337), ::comphelper::getINT32(uno::makeAny(sal_Int32(1337)))); + + uno::Any aValue; + CPPUNIT_ASSERT_EQUAL(sal_Int32(0), ::comphelper::getINT32(aValue)); +} + +void TypesTest::testGetINT16() +{ + CPPUNIT_ASSERT_EQUAL(sal_Int16(1337), ::comphelper::getINT16(uno::makeAny(sal_Int16(1337)))); + + uno::Any aValue; + CPPUNIT_ASSERT_EQUAL(sal_Int16(0), ::comphelper::getINT16(aValue)); +} + +void TypesTest::testGetDouble() +{ + CPPUNIT_ASSERT_EQUAL(1337.1337, ::comphelper::getDouble(uno::makeAny(1337.1337))); + + uno::Any aValue; + CPPUNIT_ASSERT_EQUAL(0.0, ::comphelper::getDouble(aValue)); +} + +void TypesTest::testGetFloat() +{ + CPPUNIT_ASSERT_EQUAL(static_cast<float>(1337.0), + ::comphelper::getFloat(uno::makeAny(static_cast<float>(1337.0)))); + + uno::Any aValue; + CPPUNIT_ASSERT_EQUAL(static_cast<float>(0.0), ::comphelper::getFloat(aValue)); +} + +void TypesTest::testGetString() +{ + CPPUNIT_ASSERT_EQUAL(OUString("1337"), ::comphelper::getString(uno::makeAny(OUString("1337")))); + + uno::Any aValue; + CPPUNIT_ASSERT_EQUAL(OUString(""), ::comphelper::getString(aValue)); +} + +CPPUNIT_TEST_SUITE_REGISTRATION(TypesTest); + +} // namespace + +/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ diff --git a/comphelper/qa/unit/variadictemplates.cxx b/comphelper/qa/unit/variadictemplates.cxx new file mode 100644 index 000000000..c894cf1a7 --- /dev/null +++ b/comphelper/qa/unit/variadictemplates.cxx @@ -0,0 +1,181 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#include <optional> +#include <sal/types.h> +#include <comphelper/unwrapargs.hxx> +#include <cppunit/TestAssert.h> +#include <cppunit/TestFixture.h> +#include <cppunit/extensions/HelperMacros.h> +#include <cppunit/plugin/TestPlugIn.h> + +#include <sstream> + +class VariadicTemplatesTest : public CppUnit::TestFixture +{ +public: + void testUnwrapArgs(); + + CPPUNIT_TEST_SUITE(VariadicTemplatesTest); + CPPUNIT_TEST(testUnwrapArgs); + CPPUNIT_TEST_SUITE_END(); +}; + +namespace { + +namespace detail { + +template <typename T> +void extract( + ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any> const& seq, + sal_Int32 nArg, T & v, + ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface> + const& xErrorContext ) +{ + if (nArg >= seq.getLength()) { + throw ::com::sun::star::lang::IllegalArgumentException( + "No such argument available!", + xErrorContext, static_cast<sal_Int16>(nArg) ); + } + if (! fromAny(seq[nArg], &v)) { + throw ::com::sun::star::lang::IllegalArgumentException( + "Cannot extract ANY { " + + seq[nArg].getValueType().getTypeName() + + " } to " + ::cppu::UnoType<T>::get().getTypeName(), + xErrorContext, + static_cast<sal_Int16>(nArg) ); + } +} + +template <typename T> +void extract( + ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any> const& seq, + sal_Int32 nArg, ::std::optional<T> & v, + ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface> + const& xErrorContext ) +{ + if (nArg < seq.getLength()) { + T t; + extract( seq, nArg, t, xErrorContext ); + v = t; + } +} + +} // namespace detail + +template < typename T0, typename T1, typename T2, typename T3, typename T4 > +void unwrapArgsBaseline( + ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any > const& seq, + T0& v0, T1& v1, T2& v2, T3& v3, T4& v4, + ::com::sun::star::uno::Reference< + ::com::sun::star::uno::XInterface> const& xErrorContext = + ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface>() ) +{ + ::detail::extract( seq, 0, v0, xErrorContext ); + ::detail::extract( seq, 1, v1, xErrorContext ); + ::detail::extract( seq, 2, v2, xErrorContext ); + ::detail::extract( seq, 3, v3, xErrorContext ); + ::detail::extract( seq, 4, v4, xErrorContext ); +} + +} + +void VariadicTemplatesTest::testUnwrapArgs() { + OUString tmp1 = "Test1"; + sal_Int32 tmp2 = 42; + sal_uInt32 tmp3 = 42; + OUString tmp4 = "Test2"; + OUString tmp5 = "Test3"; + ::com::sun::star::uno::Any tmp6( + ::com::sun::star::uno::makeAny( tmp1 ) + ); + ::com::sun::star::uno::Any tmp7( + ::com::sun::star::uno::makeAny( tmp2 ) + ); + ::com::sun::star::uno::Any tmp8( + ::com::sun::star::uno::makeAny( tmp3 ) + ); + ::com::sun::star::uno::Any tmp9( + ::com::sun::star::uno::makeAny( tmp4 ) + ); + ::std::optional< ::com::sun::star::uno::Any > tmp10( + ::com::sun::star::uno::makeAny( tmp5 ) + ); + ::std::optional< ::com::sun::star::uno::Any > tmp11( + ::com::sun::star::uno::makeAny( tmp1 ) + ); + + // test equality with the baseline and template specialization with + // std::optional< T > + try { + ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any > seq1( + static_cast< sal_uInt32 >( 5 ) ); + ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any > seq2( + static_cast< sal_uInt32 >( 5 ) ); + + // tmp11 should be ignored as it is ::std::optional< T > + ::comphelper::unwrapArgs( seq1, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11 ); + unwrapArgsBaseline( seq2, tmp6, tmp7, tmp8, tmp9, tmp10 ); + ::com::sun::star::uno::Any* p1 = seq1.getArray(); + ::com::sun::star::uno::Any* p2 = seq2.getArray(); + + for( sal_Int32 i = 0; i < seq1.getLength() && i < seq2.getLength(); ++i ) { + CPPUNIT_ASSERT_EQUAL_MESSAGE( "seq1 and seq2 are equal", + p1[i], p2[i] ); + } + CPPUNIT_ASSERT_MESSAGE( "seq1 and seq2 are equal", + bool(seq1 == seq2) ); + } + catch( ::com::sun::star::lang::IllegalArgumentException& err ) { + std::stringstream ss; + ss << "IllegalArgumentException when unwrapping arguments at: " << + err.ArgumentPosition; + CPPUNIT_FAIL( ss.str() ); + } + + // test argument counting + try { + ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any > seq( + static_cast< sal_uInt32 >( 4 ) ); + ::comphelper::unwrapArgs( seq, tmp6, tmp7, tmp10, tmp11, tmp10, tmp6 ); + } + catch( ::com::sun::star::lang::IllegalArgumentException& err ) { + CPPUNIT_ASSERT_EQUAL( static_cast< short >( 5 ), err.ArgumentPosition ); + } + + OUString test1( "Test2" ); + OUString test2( "Test2" ); + OUString test3( "Test3" ); + OUString test4( "Test4" ); + OUString test5( "Test5" ); + + try { + ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any > seq( + static_cast< sal_uInt32 >( 4 ) ); + ::comphelper::unwrapArgs( seq, test1, test2, test3, test4, test5 ); + } + catch( ::com::sun::star::lang::IllegalArgumentException& err1 ) { + try { + ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any > seq( + static_cast< sal_uInt32 >( 4 ) ); + unwrapArgsBaseline( seq, test1, test2, test3, test4, test5 ); + CPPUNIT_FAIL( "unwrapArgs failed while the baseline did not throw" ); + } + catch( ::com::sun::star::lang::IllegalArgumentException& err2 ) { + CPPUNIT_ASSERT_EQUAL_MESSAGE( "err1.ArgumentPosition == err2.ArgumentPosition", + err1.ArgumentPosition, err2.ArgumentPosition ); + } + } +} + +CPPUNIT_TEST_SUITE_REGISTRATION(VariadicTemplatesTest); + +CPPUNIT_PLUGIN_IMPLEMENT(); + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |