test_translate_song.cxx 7.54 KB
Newer Older
1 2 3 4 5
/*
 * Unit tests for playlist_check_translate_song().
 */

#include "config.h"
6
#include "playlist/PlaylistSong.hxx"
7
#include "DetachedSong.hxx"
8 9
#include "SongLoader.hxx"
#include "client/Client.hxx"
10
#include "tag/Builder.hxx"
11 12 13 14 15
#include "tag/Tag.hxx"
#include "util/Domain.hxx"
#include "fs/AllocatedPath.hxx"
#include "ls.hxx"
#include "Log.hxx"
Max Kellermann's avatar
Max Kellermann committed
16
#include "db/DatabaseSong.hxx"
17
#include "storage/StorageInterface.hxx"
18
#include "storage/plugins/LocalStorage.hxx"
19
#include "Mapper.hxx"
20
#include "util/ChronoUtil.hxx"
21 22 23 24 25 26 27 28 29 30

#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
Max Kellermann's avatar
Max Kellermann committed
31
Log(const Domain &domain, gcc_unused LogLevel level, const char *msg) noexcept
32 33 34 35 36
{
	fprintf(stderr, "[%s] %s\n", domain.GetName(), msg);
}

bool
37
uri_supported_scheme(const char *uri) noexcept
38
{
39
	return strncmp(uri, "http://", 7) == 0;
40 41
}

42
static constexpr auto music_directory = PATH_LITERAL("/music");
43
static Storage *storage;
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111

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";

112
DetachedSong
113
DatabaseDetachSong(gcc_unused const Database &db,
114
		   gcc_unused const Storage *_storage,
115
		   const char *uri)
116 117
{
	if (strcmp(uri, uri2) == 0)
118
		return DetachedSong(uri, MakeTag2a());
119

120
	throw std::runtime_error("No such song");
121 122 123
}

bool
124
DetachedSong::LoadFile(Path path) noexcept
125
{
126
	if (path.ToUTF8() == uri1) {
127 128 129 130 131 132 133
		SetTag(MakeTag1a());
		return true;
	}

	return false;
}

134
const Database *
135
Client::GetDatabase() const noexcept
136 137 138 139
{
	return reinterpret_cast<const Database *>(this);
}

140
const Storage *
141
Client::GetStorage() const noexcept
142
{
143
	return ::storage;
144 145
}

146 147
void
Client::AllowFile(gcc_unused Path path_fs) const
148
{
149
	/* always fail, so a SongLoader with a non-nullptr
150 151
	   Client pointer will be regarded "insecure", while one with
	   client==nullptr will allow all files */
152
	throw std::runtime_error("foo");
153 154
}

155 156 157
static std::string
ToString(const Tag &tag)
{
158
	std::string result;
159

160 161
	if (!tag.duration.IsNegative())
		result.append(std::to_string(tag.duration.ToMS()));
162

163
	for (const auto &item : tag) {
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
		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('|');

179 180
	if (!IsNegative(song.GetLastModified()))
		result.append(std::to_string(std::chrono::system_clock::to_time_t(song.GetLastModified())));
181 182 183

	result.push_back('|');

184 185
	if (song.GetStartTime().IsPositive())
		result.append(std::to_string(song.GetStartTime().ToMS()));
186 187 188

	result.push_back('-');

189 190
	if (song.GetEndTime().IsPositive())
		result.append(std::to_string(song.GetEndTime().ToMS()));
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208

	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() {
209 210
		DetachedSong song1("http://example.com/foo.ogg");
		auto se = ToString(song1);
211
		const SongLoader loader(nullptr, nullptr);
212 213
		CPPUNIT_ASSERT(playlist_check_translate_song(song1, "/ignored",
							     loader));
214
		CPPUNIT_ASSERT_EQUAL(se, ToString(song1));
215 216 217 218
	}

	void TestInsecure() {
		/* illegal because secure=false */
219
		DetachedSong song1 (uri1);
220
		const SongLoader loader(*reinterpret_cast<const Client *>(1));
221 222
		CPPUNIT_ASSERT(!playlist_check_translate_song(song1, nullptr,
							      loader));
223 224 225
	}

	void TestSecure() {
226
		DetachedSong song1(uri1, MakeTag1b());
227 228
		auto s1 = ToString(song1);
		auto se = ToString(DetachedSong(uri1, MakeTag1c()));
229

230
		const SongLoader loader(nullptr, nullptr);
231 232
		CPPUNIT_ASSERT(playlist_check_translate_song(song1, "/ignored",
							     loader));
233
		CPPUNIT_ASSERT_EQUAL(se, ToString(song1));
234 235 236
	}

	void TestInDatabase() {
237
		const SongLoader loader(reinterpret_cast<const Database *>(1),
238
					storage);
239

240
		DetachedSong song1("doesntexist");
241 242
		CPPUNIT_ASSERT(!playlist_check_translate_song(song1, nullptr,
							      loader));
243

244 245
		DetachedSong song2(uri2, MakeTag2b());
		auto s1 = ToString(song2);
246
		auto se = ToString(DetachedSong(uri2, MakeTag2c()));
247 248
		CPPUNIT_ASSERT(playlist_check_translate_song(song2, nullptr,
							     loader));
249 250
		CPPUNIT_ASSERT_EQUAL(se, ToString(song2));

251 252
		DetachedSong song3("/music/foo/bar.ogg", MakeTag2b());
		s1 = ToString(song3);
253
		se = ToString(DetachedSong(uri2, MakeTag2c()));
254 255
		CPPUNIT_ASSERT(playlist_check_translate_song(song3, nullptr,
							     loader));
256
		CPPUNIT_ASSERT_EQUAL(se, ToString(song3));
257 258 259
	}

	void TestRelative() {
260
		const Database &db = *reinterpret_cast<const Database *>(1);
261
		const SongLoader secure_loader(&db, storage);
262
		const SongLoader insecure_loader(*reinterpret_cast<const Client *>(1),
263
						 &db, storage);
264

265
		/* map to music_directory */
266
		DetachedSong song1("bar.ogg", MakeTag2b());
267 268
		auto s1 = ToString(song1);
		auto se = ToString(DetachedSong(uri2, MakeTag2c()));
269 270
		CPPUNIT_ASSERT(playlist_check_translate_song(song1, "/music/foo",
							     insecure_loader));
271
		CPPUNIT_ASSERT_EQUAL(se, ToString(song1));
272 273

		/* illegal because secure=false */
274
		DetachedSong song2("bar.ogg", MakeTag2b());
275 276
		CPPUNIT_ASSERT(!playlist_check_translate_song(song1, "/foo",
							      insecure_loader));
277 278

		/* legal because secure=true */
279 280
		DetachedSong song3("bar.ogg", MakeTag1b());
		s1 = ToString(song3);
281
		se = ToString(DetachedSong(uri1, MakeTag1c()));
282 283
		CPPUNIT_ASSERT(playlist_check_translate_song(song3, "/foo",
							     secure_loader));
284
		CPPUNIT_ASSERT_EQUAL(se, ToString(song3));
285 286

		/* relative to http:// */
287 288
		DetachedSong song4("bar.ogg", MakeTag2a());
		s1 = ToString(song4);
289
		se = ToString(DetachedSong("http://example.com/foo/bar.ogg", MakeTag2a()));
290 291
		CPPUNIT_ASSERT(playlist_check_translate_song(song4, "http://example.com/foo",
							     insecure_loader));
292
		CPPUNIT_ASSERT_EQUAL(se, ToString(song4));
293 294 295 296 297 298 299 300
	}
};

CPPUNIT_TEST_SUITE_REGISTRATION(TranslateSongTest);

int
main(gcc_unused int argc, gcc_unused char **argv)
{
301 302
	auto _storage = CreateLocalStorage(Path::FromFS(music_directory));
	storage = _storage.get();
303

304 305 306 307 308
	CppUnit::TextUi::TestRunner runner;
	auto &registry = CppUnit::TestFactoryRegistry::getRegistry();
	runner.addTest(registry.makeTest());
	return runner.run() ? EXIT_SUCCESS : EXIT_FAILURE;
}