2014-01-20 21:31:40 +01:00
|
|
|
/*
|
|
|
|
* Unit tests for playlist_check_translate_song().
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
2014-01-23 23:30:12 +01:00
|
|
|
#include "playlist/PlaylistSong.hxx"
|
2014-01-20 21:31:40 +01:00
|
|
|
#include "DetachedSong.hxx"
|
2014-02-02 14:37:52 +01:00
|
|
|
#include "SongLoader.hxx"
|
|
|
|
#include "client/Client.hxx"
|
2017-02-08 08:26:58 +01:00
|
|
|
#include "tag/Builder.hxx"
|
2014-01-20 21:31:40 +01:00
|
|
|
#include "tag/Tag.hxx"
|
|
|
|
#include "util/Domain.hxx"
|
|
|
|
#include "fs/AllocatedPath.hxx"
|
|
|
|
#include "ls.hxx"
|
|
|
|
#include "Log.hxx"
|
2014-01-24 16:18:50 +01:00
|
|
|
#include "db/DatabaseSong.hxx"
|
2014-02-07 19:01:06 +01:00
|
|
|
#include "storage/plugins/LocalStorage.hxx"
|
2014-01-20 21:31:40 +01:00
|
|
|
#include "Mapper.hxx"
|
|
|
|
|
|
|
|
#include <cppunit/TestFixture.h>
|
|
|
|
#include <cppunit/extensions/TestFactoryRegistry.h>
|
|
|
|
#include <cppunit/ui/text/TestRunner.h>
|
|
|
|
#include <cppunit/extensions/HelperMacros.h>
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
void
|
|
|
|
Log(const Domain &domain, gcc_unused LogLevel level, const char *msg)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "[%s] %s\n", domain.GetName(), msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
uri_supported_scheme(const char *uri)
|
|
|
|
{
|
2017-01-03 13:16:29 +01:00
|
|
|
return strncmp(uri, "http://", 7) == 0;
|
2014-01-20 21:31:40 +01:00
|
|
|
}
|
|
|
|
|
2015-06-22 22:12:02 +02:00
|
|
|
static constexpr auto music_directory = PATH_LITERAL("/music");
|
2014-02-07 23:41:06 +01:00
|
|
|
static Storage *storage;
|
2014-01-20 21:31:40 +01:00
|
|
|
|
|
|
|
static void
|
|
|
|
BuildTag(gcc_unused TagBuilder &tag)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename... Args>
|
|
|
|
static void
|
|
|
|
BuildTag(TagBuilder &tag, TagType type, const char *value, Args&&... args)
|
|
|
|
{
|
|
|
|
tag.AddItem(type, value);
|
|
|
|
BuildTag(tag, std::forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename... Args>
|
|
|
|
static Tag
|
|
|
|
MakeTag(Args&&... args)
|
|
|
|
{
|
|
|
|
TagBuilder tag;
|
|
|
|
BuildTag(tag, std::forward<Args>(args)...);
|
|
|
|
return tag.Commit();
|
|
|
|
}
|
|
|
|
|
|
|
|
static Tag
|
|
|
|
MakeTag1a()
|
|
|
|
{
|
|
|
|
return MakeTag(TAG_ARTIST, "artist_a1", TAG_TITLE, "title_a1",
|
|
|
|
TAG_ALBUM, "album_a1");
|
|
|
|
}
|
|
|
|
|
|
|
|
static Tag
|
|
|
|
MakeTag1b()
|
|
|
|
{
|
|
|
|
return MakeTag(TAG_ARTIST, "artist_b1", TAG_TITLE, "title_b1",
|
|
|
|
TAG_COMMENT, "comment_b1");
|
|
|
|
}
|
|
|
|
|
|
|
|
static Tag
|
|
|
|
MakeTag1c()
|
|
|
|
{
|
|
|
|
return MakeTag(TAG_ARTIST, "artist_b1", TAG_TITLE, "title_b1",
|
|
|
|
TAG_COMMENT, "comment_b1", TAG_ALBUM, "album_a1");
|
|
|
|
}
|
|
|
|
|
|
|
|
static Tag
|
|
|
|
MakeTag2a()
|
|
|
|
{
|
|
|
|
return MakeTag(TAG_ARTIST, "artist_a2", TAG_TITLE, "title_a2",
|
|
|
|
TAG_ALBUM, "album_a2");
|
|
|
|
}
|
|
|
|
|
|
|
|
static Tag
|
|
|
|
MakeTag2b()
|
|
|
|
{
|
|
|
|
return MakeTag(TAG_ARTIST, "artist_b2", TAG_TITLE, "title_b2",
|
|
|
|
TAG_COMMENT, "comment_b2");
|
|
|
|
}
|
|
|
|
|
|
|
|
static Tag
|
|
|
|
MakeTag2c()
|
|
|
|
{
|
|
|
|
return MakeTag(TAG_ARTIST, "artist_b2", TAG_TITLE, "title_b2",
|
|
|
|
TAG_COMMENT, "comment_b2", TAG_ALBUM, "album_a2");
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *uri1 = "/foo/bar.ogg";
|
|
|
|
static const char *uri2 = "foo/bar.ogg";
|
|
|
|
|
|
|
|
DetachedSong *
|
2014-02-07 00:29:07 +01:00
|
|
|
DatabaseDetachSong(gcc_unused const Database &db,
|
2014-02-07 19:01:06 +01:00
|
|
|
gcc_unused const Storage &_storage,
|
2016-03-19 00:13:57 +01:00
|
|
|
const char *uri)
|
2014-01-20 21:31:40 +01:00
|
|
|
{
|
|
|
|
if (strcmp(uri, uri2) == 0)
|
|
|
|
return new DetachedSong(uri, MakeTag2a());
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2015-10-20 12:10:42 +02:00
|
|
|
DetachedSong::LoadFile(Path path)
|
2014-01-20 21:31:40 +01:00
|
|
|
{
|
2015-10-20 12:10:42 +02:00
|
|
|
if (path.ToUTF8() == uri1) {
|
2014-01-20 21:31:40 +01:00
|
|
|
SetTag(MakeTag1a());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-02-01 00:26:34 +01:00
|
|
|
const Database *
|
2016-10-26 18:52:00 +02:00
|
|
|
Client::GetDatabase() const
|
2014-02-01 00:26:34 +01:00
|
|
|
{
|
|
|
|
return reinterpret_cast<const Database *>(this);
|
|
|
|
}
|
|
|
|
|
2014-02-07 00:29:07 +01:00
|
|
|
const Storage *
|
|
|
|
Client::GetStorage() const
|
|
|
|
{
|
2014-02-07 19:01:06 +01:00
|
|
|
return ::storage;
|
2014-02-07 00:29:07 +01:00
|
|
|
}
|
|
|
|
|
2016-10-27 21:59:17 +02:00
|
|
|
void
|
|
|
|
Client::AllowFile(gcc_unused Path path_fs) const
|
2014-02-02 14:37:52 +01:00
|
|
|
{
|
2016-10-27 21:59:17 +02:00
|
|
|
/* always fail, so a SongLoader with a non-nullptr
|
2014-02-02 14:37:52 +01:00
|
|
|
Client pointer will be regarded "insecure", while one with
|
|
|
|
client==nullptr will allow all files */
|
2016-10-27 21:59:17 +02:00
|
|
|
throw std::runtime_error("foo");
|
2014-02-02 14:37:52 +01:00
|
|
|
}
|
|
|
|
|
2014-01-20 21:31:40 +01:00
|
|
|
static std::string
|
|
|
|
ToString(const Tag &tag)
|
|
|
|
{
|
2014-08-29 12:14:27 +02:00
|
|
|
std::string result;
|
2014-01-20 21:31:40 +01:00
|
|
|
|
2014-08-29 12:14:27 +02:00
|
|
|
if (!tag.duration.IsNegative()) {
|
|
|
|
char buffer[64];
|
|
|
|
sprintf(buffer, "%d", tag.duration.ToMS());
|
|
|
|
result.append(buffer);
|
|
|
|
}
|
2014-01-20 21:31:40 +01:00
|
|
|
|
2014-07-12 17:22:39 +02:00
|
|
|
for (const auto &item : tag) {
|
2014-01-20 21:31:40 +01:00
|
|
|
result.push_back('|');
|
|
|
|
result.append(tag_item_names[item.type]);
|
|
|
|
result.push_back('=');
|
|
|
|
result.append(item.value);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::string
|
|
|
|
ToString(const DetachedSong &song)
|
|
|
|
{
|
|
|
|
std::string result = song.GetURI();
|
|
|
|
result.push_back('|');
|
|
|
|
|
|
|
|
char buffer[64];
|
|
|
|
|
|
|
|
if (song.GetLastModified() > 0) {
|
|
|
|
sprintf(buffer, "%lu", (unsigned long)song.GetLastModified());
|
|
|
|
result.append(buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
result.push_back('|');
|
|
|
|
|
2014-08-28 06:54:19 +02:00
|
|
|
if (song.GetStartTime().IsPositive()) {
|
|
|
|
sprintf(buffer, "%u", song.GetStartTime().ToMS());
|
2014-01-20 21:31:40 +01:00
|
|
|
result.append(buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
result.push_back('-');
|
|
|
|
|
2014-08-28 06:54:19 +02:00
|
|
|
if (song.GetEndTime().IsPositive()) {
|
|
|
|
sprintf(buffer, "%u", song.GetEndTime().ToMS());
|
2014-01-20 21:31:40 +01:00
|
|
|
result.append(buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
result.push_back('|');
|
|
|
|
|
|
|
|
result.append(ToString(song.GetTag()));
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
class TranslateSongTest : public CppUnit::TestFixture {
|
|
|
|
CPPUNIT_TEST_SUITE(TranslateSongTest);
|
|
|
|
CPPUNIT_TEST(TestAbsoluteURI);
|
|
|
|
CPPUNIT_TEST(TestInsecure);
|
|
|
|
CPPUNIT_TEST(TestSecure);
|
|
|
|
CPPUNIT_TEST(TestInDatabase);
|
|
|
|
CPPUNIT_TEST(TestRelative);
|
|
|
|
CPPUNIT_TEST_SUITE_END();
|
|
|
|
|
|
|
|
void TestAbsoluteURI() {
|
2014-01-20 23:48:46 +01:00
|
|
|
DetachedSong song1("http://example.com/foo.ogg");
|
|
|
|
auto se = ToString(song1);
|
2014-02-07 00:29:07 +01:00
|
|
|
const SongLoader loader(nullptr, nullptr);
|
2014-02-02 14:37:52 +01:00
|
|
|
CPPUNIT_ASSERT(playlist_check_translate_song(song1, "/ignored",
|
|
|
|
loader));
|
2014-01-20 23:48:46 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(se, ToString(song1));
|
2014-01-20 21:31:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestInsecure() {
|
|
|
|
/* illegal because secure=false */
|
2014-01-20 23:48:46 +01:00
|
|
|
DetachedSong song1 (uri1);
|
2014-02-01 00:26:34 +01:00
|
|
|
const SongLoader loader(*reinterpret_cast<const Client *>(1));
|
2014-02-02 14:37:52 +01:00
|
|
|
CPPUNIT_ASSERT(!playlist_check_translate_song(song1, nullptr,
|
|
|
|
loader));
|
2014-01-20 21:31:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestSecure() {
|
2014-01-20 23:48:46 +01:00
|
|
|
DetachedSong song1(uri1, MakeTag1b());
|
2014-01-20 21:31:40 +01:00
|
|
|
auto s1 = ToString(song1);
|
|
|
|
auto se = ToString(DetachedSong(uri1, MakeTag1c()));
|
2014-02-02 14:37:52 +01:00
|
|
|
|
2014-02-07 00:29:07 +01:00
|
|
|
const SongLoader loader(nullptr, nullptr);
|
2014-02-02 14:37:52 +01:00
|
|
|
CPPUNIT_ASSERT(playlist_check_translate_song(song1, "/ignored",
|
|
|
|
loader));
|
2014-01-20 23:48:46 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(se, ToString(song1));
|
2014-01-20 21:31:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestInDatabase() {
|
2014-02-07 00:29:07 +01:00
|
|
|
const SongLoader loader(reinterpret_cast<const Database *>(1),
|
2014-02-07 19:01:06 +01:00
|
|
|
storage);
|
2014-02-02 14:37:52 +01:00
|
|
|
|
2014-01-20 23:48:46 +01:00
|
|
|
DetachedSong song1("doesntexist");
|
2014-02-02 14:37:52 +01:00
|
|
|
CPPUNIT_ASSERT(!playlist_check_translate_song(song1, nullptr,
|
|
|
|
loader));
|
2014-01-20 21:31:40 +01:00
|
|
|
|
2014-01-20 23:48:46 +01:00
|
|
|
DetachedSong song2(uri2, MakeTag2b());
|
|
|
|
auto s1 = ToString(song2);
|
2014-01-20 21:31:40 +01:00
|
|
|
auto se = ToString(DetachedSong(uri2, MakeTag2c()));
|
2014-02-02 14:37:52 +01:00
|
|
|
CPPUNIT_ASSERT(playlist_check_translate_song(song2, nullptr,
|
|
|
|
loader));
|
2014-01-20 21:31:40 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(se, ToString(song2));
|
|
|
|
|
2014-01-20 23:48:46 +01:00
|
|
|
DetachedSong song3("/music/foo/bar.ogg", MakeTag2b());
|
|
|
|
s1 = ToString(song3);
|
2014-01-20 21:31:40 +01:00
|
|
|
se = ToString(DetachedSong(uri2, MakeTag2c()));
|
2014-02-02 14:37:52 +01:00
|
|
|
CPPUNIT_ASSERT(playlist_check_translate_song(song3, nullptr,
|
|
|
|
loader));
|
2014-01-20 23:48:46 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(se, ToString(song3));
|
2014-01-20 21:31:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestRelative() {
|
2014-02-01 01:11:50 +01:00
|
|
|
const Database &db = *reinterpret_cast<const Database *>(1);
|
2014-02-07 19:01:06 +01:00
|
|
|
const SongLoader secure_loader(&db, storage);
|
2014-02-01 00:26:34 +01:00
|
|
|
const SongLoader insecure_loader(*reinterpret_cast<const Client *>(1),
|
2014-02-07 19:01:06 +01:00
|
|
|
&db, storage);
|
2014-02-02 14:37:52 +01:00
|
|
|
|
2014-01-20 21:31:40 +01:00
|
|
|
/* map to music_directory */
|
2014-01-20 23:48:46 +01:00
|
|
|
DetachedSong song1("bar.ogg", MakeTag2b());
|
2014-01-20 21:31:40 +01:00
|
|
|
auto s1 = ToString(song1);
|
|
|
|
auto se = ToString(DetachedSong(uri2, MakeTag2c()));
|
2014-02-02 14:37:52 +01:00
|
|
|
CPPUNIT_ASSERT(playlist_check_translate_song(song1, "/music/foo",
|
|
|
|
insecure_loader));
|
2014-01-20 23:48:46 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(se, ToString(song1));
|
2014-01-20 21:31:40 +01:00
|
|
|
|
|
|
|
/* illegal because secure=false */
|
2014-01-20 23:48:46 +01:00
|
|
|
DetachedSong song2("bar.ogg", MakeTag2b());
|
2014-02-02 14:37:52 +01:00
|
|
|
CPPUNIT_ASSERT(!playlist_check_translate_song(song1, "/foo",
|
|
|
|
insecure_loader));
|
2014-01-20 21:31:40 +01:00
|
|
|
|
|
|
|
/* legal because secure=true */
|
2014-01-20 23:48:46 +01:00
|
|
|
DetachedSong song3("bar.ogg", MakeTag1b());
|
|
|
|
s1 = ToString(song3);
|
2014-01-20 21:31:40 +01:00
|
|
|
se = ToString(DetachedSong(uri1, MakeTag1c()));
|
2014-02-02 14:37:52 +01:00
|
|
|
CPPUNIT_ASSERT(playlist_check_translate_song(song3, "/foo",
|
|
|
|
secure_loader));
|
2014-01-20 23:48:46 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(se, ToString(song3));
|
2014-01-20 21:31:40 +01:00
|
|
|
|
|
|
|
/* relative to http:// */
|
2014-01-20 23:48:46 +01:00
|
|
|
DetachedSong song4("bar.ogg", MakeTag2a());
|
|
|
|
s1 = ToString(song4);
|
2014-01-20 21:31:40 +01:00
|
|
|
se = ToString(DetachedSong("http://example.com/foo/bar.ogg", MakeTag2a()));
|
2014-02-02 14:37:52 +01:00
|
|
|
CPPUNIT_ASSERT(playlist_check_translate_song(song4, "http://example.com/foo",
|
|
|
|
insecure_loader));
|
2014-01-20 23:48:46 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(se, ToString(song4));
|
2014-01-20 21:31:40 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION(TranslateSongTest);
|
|
|
|
|
|
|
|
int
|
|
|
|
main(gcc_unused int argc, gcc_unused char **argv)
|
|
|
|
{
|
2014-02-07 23:41:06 +01:00
|
|
|
storage = CreateLocalStorage(Path::FromFS(music_directory));
|
|
|
|
|
2014-01-20 21:31:40 +01:00
|
|
|
CppUnit::TextUi::TestRunner runner;
|
|
|
|
auto ®istry = CppUnit::TestFactoryRegistry::getRegistry();
|
|
|
|
runner.addTest(registry.makeTest());
|
|
|
|
return runner.run() ? EXIT_SUCCESS : EXIT_FAILURE;
|
|
|
|
}
|