akashi-esquizolandia/tests/unittest_music_manager/tst_unittest_music_manager.cpp
in1tiate 83c41c05f7 Squashed commit of the following:
commit e946bf124602f224ce0e371ba1374f0355b537eb
Merge: d6a4e64 4505909
Author: Rosemary Witchaven <32779090+in1tiate@users.noreply.github.com>
Date:   Fri Jan 28 19:43:36 2022 -0600

    Merge pull request #225 from Salanto/Dynamic-Area-Musiclist-Take2

    Allow users to add custom songs to the music list on a per-area basis

commit 45059092d2888b60912f721e43a380910d10ccd8
Author: Salanto <62221668+Salanto@users.noreply.github.com>
Date:   Mon Jan 24 22:05:27 2022 +0100

    TIL what a typedef is

commit 02584db9640fff0a1969a7f516c4bccfae9b5388
Author: Salanto <62221668+Salanto@users.noreply.github.com>
Date:   Mon Jan 24 21:39:17 2022 +0100

    Not all OR are equal. Explain weird command splitting

    Remove hardcoded URLs

commit d00ebd5692296cd0c29dd377113b53fe0e7b99c0
Author: Salanto <62221668+Salanto@users.noreply.github.com>
Date:   Mon Jan 24 21:28:08 2022 +0100

    Salanto PR Language Update by in1tiate

    As usual, my English is absolutely unreadable.

    Co-authored-by: Rosemary Witchaven <32779090+in1tiate@users.noreply.github.com>

commit d3842106e06350dc02d8864bb28232fdc5643f00
Author: Salanto <62221668+Salanto@users.noreply.github.com>
Date:   Mon Jan 24 20:35:32 2022 +0100

    Add missing config file + document commands for help

commit ac64360e1c1741023b01052977de77a7d5ea4f8c
Author: Salanto <62221668+Salanto@users.noreply.github.com>
Date:   Mon Jan 24 19:52:13 2022 +0100

    Minor improvements to command usage and addition of clear command

commit c614578e78ce9afa0c8e22aa36bdf46a70a97169
Author: Salanto <62221668+Salanto@users.noreply.github.com>
Date:   Sun Jan 23 22:19:54 2022 +0100

    Purge last traces of old songInformation system

commit 07618761f044a13d75587b28a9c994342a5980e2
Author: Salanto <62221668+Salanto@users.noreply.github.com>
Date:   Sun Jan 23 22:10:54 2022 +0100

    Working version, needs some refinement and debugging in AOClient

commit 33c0358c98c0fd2de805356a9aa3ac7bbed204e1
Author: Salanto <62221668+Salanto@users.noreply.github.com>
Date:   Sun Jan 23 21:48:05 2022 +0100

    Almost functional implementation

    Now only need to determine why I can't play the customs yet

commit b0acbace78b3f16f2fe4f3c6f65a422e3343f992
Author: Salanto <62221668+Salanto@users.noreply.github.com>
Date:   Sun Jan 23 15:26:42 2022 +0100

    Fix build error, expand validation test slightly

commit a48c4f503998ce8e42f0bb409c5a3c7dc5e40329
Author: Salanto <62221668+Salanto@users.noreply.github.com>
Date:   Sun Jan 23 01:03:27 2022 +0100

    Add commands

commit 88ab0b473953873166e291e5009b97df31547b3f
Author: Salanto <62221668+Salanto@users.noreply.github.com>
Date:   Sat Jan 22 15:10:26 2022 +0100

    Float sucks, int has to be good enough

    + add retrival of song information

commit e924e1340be1a0909eba84072f1646fe9770bd02
Author: Salanto <62221668+Salanto@users.noreply.github.com>
Date:   Thu Jan 20 22:28:35 2022 +0100

    Fix removing moving

    Add necessary tests

commit 3df088f8d07ce7e0d8fe08b6a97608a623e6ef97
Author: Salanto <62221668+Salanto@users.noreply.github.com>
Date:   Wed Jan 19 19:34:16 2022 +0100

    Start work on adding this shit into commands

commit c293ecfa99d1b2bd1e0b34cb8752d69b2eca057c
Author: Salanto <62221668+Salanto@users.noreply.github.com>
Date:   Tue Jan 18 19:07:11 2022 +0100

    Fix typo and add singal for incremental upgrades

commit 10a42322e1e23af5795278a40b2ac59f3ab952ef
Author: Salanto <62221668+Salanto@users.noreply.github.com>
Date:   Tue Jan 18 06:17:51 2022 +0100

    Hookup packet sending to music manager

    This might sound like a bad idea on first glance, but otherwise it breaks the AreaData tests and I am NOT gonna try to fix those without even understanding why they break.

commit 319836296374162b0b847432e8a626778317b869
Author: Salanto <62221668+Salanto@users.noreply.github.com>
Date:   Tue Jan 18 06:17:51 2022 +0100

    Enraged comments, make area send FM packet

    Revert "Enraged comments, make area send FM packet"

    This reverts commit ec7a1a25646b2c2acc8a3a748b853851cc47d205.

commit 224a0d7efe989a5f336167c3f716061813f93ee3
Author: Salanto <62221668+Salanto@users.noreply.github.com>
Date:   Tue Jan 18 05:10:18 2022 +0100

    Change packet communication from area to client

    First steps to hookup the custom musiclist.

commit 65aa8f7855a36f2c668b1399a5ed22fefeaf186d
Author: Salanto <62221668+Salanto@users.noreply.github.com>
Date:   Mon Jan 17 00:48:38 2022 +0100

    Add test for custom list sanitisation.

    + Fix intentionally broken tests

commit 7c00ab437a6ff12033742d029ce49037f5bb1ebe
Author: Salanto <62221668+Salanto@users.noreply.github.com>
Date:   Mon Jan 17 00:29:51 2022 +0100

    Sanitise the custom list opposed to deleting it

    This will fail tests intentionally to test the CI.

commit 80ad401267068e75707b2517a0bf836763141f8b
Author: Salanto <62221668+Salanto@users.noreply.github.com>
Date:   Sun Jan 16 17:17:12 2022 +0100

    Add custom category capabilities

commit 08d8f5f8f683816ceba532f9c47cd0d5ab34389a
Author: Salanto <62221668+Salanto@users.noreply.github.com>
Date:   Sun Jan 16 03:58:18 2022 +0100

    Fix music addition and move relevant tests

commit 6ebf0d03b5da61a9c287115009d28038710ba7af
Author: Salanto <62221668+Salanto@users.noreply.github.com>
Date:   Sat Jan 15 08:08:20 2022 +0100

    Hookup music_manager into server, change default musiclist source for new clients

    + More tests 🆒

commit bd50c62376f131e2508ecdd3e272209894ecaec1
Author: Salanto <62221668+Salanto@users.noreply.github.com>
Date:   Sat Jan 15 03:13:42 2022 +0100

    Add central song validator for other classes

    Also added applicable test cases to ensure proper operation.
2022-01-28 19:51:30 -06:00

323 lines
11 KiB
C++

#include <QTest>
#include <include/music_manager.h>
namespace tests {
namespace unittests {
/**
* @brief Unit Tester class for the musiclist-related functions.
*/
class MusicListManager : public QObject
{
Q_OBJECT
public :
MusicManager* m_music_manager;
private slots:
/**
* @brief Initialises every tests with creating a new MusicManager with a small sample root list.
*/
void init();
/**
* @brief Tests the registration of areas in the music manager.
*/
void registerArea();
/**
* @brief Tests toggling the enabling/disabling of the prepend behaviour of our root list.
*/
void toggleRootEnabled();
/**
* @brief The data function for validateSong()
*/
void validateSong_data();
/**
* @brief Tests validation of song candidates.
*/
void validateSong();
/**
* @brief Tests the addition of custom music.
*/
void addCustomSong();
/**
* @brief Tests the addition of a custom category.
*/
void addCustomCategory();
/**
* @brief Test the sanitisation of the custom list when root prepend is reenabled.
*/
void sanitiseCustomList();
/**
* @brief Tests the removing of custom songs and categories.
*/
void removeCategorySong();
/**
* @brief Tests the retrieval of song information.
*/
void songInformation();
/**
* @brief Tests the retrieval of the full musiclist for an area.
*/
void musiclist();
};
void MusicListManager::init()
{
QMap<QString,QPair<QString,int>> l_test_list;
l_test_list.insert("==Music==",{"==Music==",0});
l_test_list.insert("Announce The Truth (AJ).opus",{"Announce The Truth (AJ).opus",59});
l_test_list.insert("Announce The Truth (JFA).opus",{"Announce The Truth (JFA).opus",98});
QStringList l_list = {};
l_list << "==Music==" << "Announce The Truth (AJ).opus" << "Announce The Truth (JFA).opus";
m_music_manager = new MusicManager(nullptr, l_list ,{"my.cdn.com","your.cdn.com"}, l_test_list);
}
void MusicListManager::registerArea()
{
{
//We register a single area with the music manager of ID 0.
//Creation should work as there are no other areas yet.
bool l_creation_success = m_music_manager->registerArea(0);
QCOMPARE(l_creation_success,true);
}
{
//Someone tries to register the same area again!
//This should fail as this area already exists.
bool l_creation_success = m_music_manager->registerArea(0);
QCOMPARE(l_creation_success,false);
}
}
void MusicListManager::toggleRootEnabled()
{
{
//We register an area of ID0 and toggle the inclusion of global list.
m_music_manager->registerArea(0);
QCOMPARE(m_music_manager->toggleRootEnabled(0), false);
}
{
//We toggle it again. It should return true now.
//Since this is now true, we should have the root list with customs cleared.
QCOMPARE(m_music_manager->toggleRootEnabled(0), true);
}
}
void MusicListManager::validateSong_data()
{
//Songname can also be the realname.
QTest::addColumn<QString>("songname");
QTest::addColumn<bool>("expectedResult");
QTest::addRow("Songname - No extension") << "Announce The Truth (AA)" << false;
QTest::addRow("Songname - Valid Extension") << "Announce The Truth (AA).opus" << true;
QTest::addRow("Songname - Invalid Extension") << "Announce The Truth (AA).aac" << false;
QTest::addRow("URL - Valid primary") << "https://my.cdn.com/mysong.opus" << true;
QTest::addRow("URL - Valid secondary") << "https://your.cdn.com/mysong.opus" << true;
QTest::addRow("URL - Invalid extension") << "https://my.cdn.com/mysong.aac." << false;
QTest::addRow("URL - Invalid prefix") << "ftp://my.cdn.com/mysong.opus" << false;
QTest::addRow("URL - Invalid missing prefix") << "my.cdn.com/mysong.opus" << false;
QTest::addRow("URL - Invalid CDN") << "https://myipgrabber.com/mysong.opus" << false;
QTest::addRow("URL - Subdomain Attack") << "https://my.cdn.com.fakedomain.com/mysong.opus" << false;
}
void MusicListManager::validateSong()
{
QFETCH(QString,songname);
QFETCH(bool,expectedResult);
bool l_result = m_music_manager->validateSong(songname, {"my.cdn.com","your.cdn.com"});
QCOMPARE(expectedResult,l_result);
}
void MusicListManager::addCustomSong()
{
{
//Dummy register.
m_music_manager->registerArea(0);
//No custom songs, so musiclist = root_list.size()
QCOMPARE(m_music_manager->musiclist(0).size(), 3);
}
{
//Add a song that's valid. The musiclist is now root_list.size() + custom_list.size()
m_music_manager->addCustomSong("mysong","mysong.opus",0,0);
QCOMPARE(m_music_manager->musiclist(0).size(), 4);
}
{
//Add a song that's part of the root list. This should fail and not increase the size.
bool l_result = m_music_manager->addCustomSong("Announce The Truth (AJ)","Announce The Truth (AJ).opus",0,0);
QCOMPARE(l_result,false);
QCOMPARE(m_music_manager->musiclist(0).size(), 4);
}
{
//Disable the root list. Musiclist is now custom_list.size()
m_music_manager->toggleRootEnabled(0);
QCOMPARE(m_music_manager->musiclist(0).size(), 1);
}
{
//Add an item that is in the root list into the custom list. Size is still custom_list.size()
bool l_result = m_music_manager->addCustomSong("Announce The Truth (AJ)","Announce The Truth (AJ).opus",0,0);
QCOMPARE(l_result,true);
QCOMPARE(m_music_manager->musiclist(0).size(), 2);
}
{
bool l_result = m_music_manager->addCustomSong("Announce The Truth (AJ)2","https://my.cdn.com/mysong.opus",0,0);
QCOMPARE(l_result,true);
QCOMPARE(m_music_manager->musiclist(0).size(), 3);
}
}
void MusicListManager::addCustomCategory()
{
{
//Dummy register.
m_music_manager->registerArea(0);
//Add category to the custom list. Category marker are added manually.
bool l_result = m_music_manager->addCustomCategory("Music2",0);
QCOMPARE(l_result,true);
QCOMPARE(m_music_manager->musiclist(0).size(), 4);
QCOMPARE(m_music_manager->musiclist(0).at(3), "==Music2==");
}
{
//Add a category that already exists on root. This should fail and not increase the size of our list.
bool l_result = m_music_manager->addCustomCategory("Music",0);
QCOMPARE(l_result, false);
QCOMPARE(m_music_manager->musiclist(0).size(), 4);
}
{
//We disable the root list. We now insert the category again.
m_music_manager->toggleRootEnabled(0);
bool l_result = m_music_manager->addCustomCategory("Music",0);
QCOMPARE(l_result, true);
QCOMPARE(m_music_manager->musiclist(0).size(), 2);
QCOMPARE(m_music_manager->musiclist(0).at(1), "==Music==");
}
{
//Global now enabled. We add a song with three ===.
m_music_manager->toggleRootEnabled(0);
bool l_result = m_music_manager->addCustomCategory("===Music===",0);
QCOMPARE(l_result, true);
QCOMPARE(m_music_manager->musiclist(0).size(), 5);
QCOMPARE(m_music_manager->musiclist(0).at(4), "===Music===");
}
}
void MusicListManager::sanitiseCustomList()
{
//Prepare a dummy area with root list disabled.Insert both non-root and root elements.
m_music_manager->registerArea(0);
m_music_manager->toggleRootEnabled(0);
m_music_manager->addCustomCategory("Music",0);
m_music_manager->addCustomCategory("Music2",0);
m_music_manager->addCustomSong("Announce The Truth (AJ)","Announce The Truth (AJ).opus",0,0);
m_music_manager->addCustomSong("mysong","mysong.opus",0,0);
//We now only have custom elements.
QCOMPARE(m_music_manager->musiclist(0).size(), 4);
//We reenable the root list. Sanisation should only leave the non-root elements in the custom list.
m_music_manager->toggleRootEnabled(0);
QCOMPARE(m_music_manager->musiclist(0).size(), 5);
QCOMPARE(m_music_manager->musiclist(0).at(3), "==Music2==");
QCOMPARE(m_music_manager->musiclist(0).at(4), "mysong.opus");
}
void MusicListManager::removeCategorySong()
{
{
//Prepare dummy area. Add both custom songs and categories.
m_music_manager->registerArea(0);
m_music_manager->addCustomCategory("Music2",0);
m_music_manager->addCustomSong("mysong","mysong.opus",0,0);
m_music_manager->addCustomCategory("Music3",0);
m_music_manager->addCustomSong("mysong2","mysong.opus",0,0);
QCOMPARE(m_music_manager->musiclist(0).size(), 7);
}
{
//Delete a category that is not custom. This should fail.
bool l_success = m_music_manager->removeCategorySong("==Music==",0);
QCOMPARE(l_success, false);
QCOMPARE(m_music_manager->musiclist(0).size(), 7);
}
{
//Correct category name, wrong format.
bool l_success = m_music_manager->removeCategorySong("Music2",0);
QCOMPARE(l_success, false);
QCOMPARE(m_music_manager->musiclist(0).size(), 7);
}
{
//Correct category name. This should be removed.
bool l_success = m_music_manager->removeCategorySong("==Music2==",0);
QCOMPARE(l_success, true);
QCOMPARE(m_music_manager->musiclist(0).size(), 6);
}
{
//Correct song name. This should be removed. This needs to be with the extension.
bool l_success = m_music_manager->removeCategorySong("mysong2.opus",0);
QCOMPARE(l_success, true);
QCOMPARE(m_music_manager->musiclist(0).size(), 5);
}
}
void MusicListManager::songInformation()
{
{
//Prepare dummy area. Add both custom songs and categories.
m_music_manager->registerArea(0);
m_music_manager->addCustomCategory("Music2",0);
m_music_manager->addCustomSong("mysong","realmysong.opus",47,0);
m_music_manager->addCustomCategory("Music3",0);
m_music_manager->addCustomSong("mysong2","mysong.opus",42,0);
}
{
QPair<QString,int> l_song_information = m_music_manager->songInformation("mysong.opus",0);
QCOMPARE(l_song_information.first, "realmysong.opus");
QCOMPARE(l_song_information.second, 47);
}
{
QPair<QString,int> l_song_information = m_music_manager->songInformation("Announce The Truth (AJ).opus",0);
QCOMPARE(l_song_information.first, "Announce The Truth (AJ).opus");
QCOMPARE(l_song_information.second, 59);
}
}
void MusicListManager::musiclist()
{
{
//Prepare dummy area. Add both custom songs and categories.
m_music_manager->registerArea(0);
m_music_manager->addCustomCategory("Music2",0);
m_music_manager->addCustomSong("mysong","realmysong.opus",47,0);
m_music_manager->addCustomCategory("Music3",0);
m_music_manager->addCustomSong("mysong2","mysong.opus",42,0);
}
{
QCOMPARE(m_music_manager->musiclist(0).size(), 7);
}
}
}
}
QTEST_APPLESS_MAIN(tests::unittests::MusicListManager)
#include "tst_unittest_music_manager.moc"