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

5
#include "MakeTag.hxx"
6
#include "playlist/PlaylistSong.hxx"
7
#include "song/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 "time/ChronoUtil.hxx"
21

22
#include <gtest/gtest.h>
23 24 25 26 27

#include <string.h>
#include <stdio.h>

void
28
Log(LogLevel, const Domain &domain, const char *msg) noexcept
29 30 31 32 33
{
	fprintf(stderr, "[%s] %s\n", domain.GetName(), msg);
}

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

39
static constexpr auto music_directory = PATH_LITERAL("/music");
40
static Storage *storage;
41 42 43 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

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

87
DetachedSong
88
DatabaseDetachSong(gcc_unused const Database &db,
89
		   gcc_unused const Storage *_storage,
90
		   const char *uri)
91 92
{
	if (strcmp(uri, uri2) == 0)
93
		return DetachedSong(uri, MakeTag2a());
94

95
	throw std::runtime_error("No such song");
96 97 98
}

bool
99
DetachedSong::LoadFile(Path path)
100
{
101
	if (path.ToUTF8() == uri1) {
102 103 104 105 106 107 108
		SetTag(MakeTag1a());
		return true;
	}

	return false;
}

109
const Database *
110
Client::GetDatabase() const noexcept
111 112 113 114
{
	return reinterpret_cast<const Database *>(this);
}

115
const Storage *
116
Client::GetStorage() const noexcept
117
{
118
	return ::storage;
119 120
}

121 122
void
Client::AllowFile(gcc_unused Path path_fs) const
123
{
124
	/* always fail, so a SongLoader with a non-nullptr
125 126
	   Client pointer will be regarded "insecure", while one with
	   client==nullptr will allow all files */
127
	throw std::runtime_error("foo");
128 129
}

130 131 132
static std::string
ToString(const Tag &tag)
{
133
	std::string result;
134

135 136
	if (!tag.duration.IsNegative())
		result.append(std::to_string(tag.duration.ToMS()));
137

138
	for (const auto &item : tag) {
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
		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('|');

154 155
	if (!IsNegative(song.GetLastModified()))
		result.append(std::to_string(std::chrono::system_clock::to_time_t(song.GetLastModified())));
156 157 158

	result.push_back('|');

159 160
	if (song.GetStartTime().IsPositive())
		result.append(std::to_string(song.GetStartTime().ToMS()));
161 162 163

	result.push_back('-');

164 165
	if (song.GetEndTime().IsPositive())
		result.append(std::to_string(song.GetEndTime().ToMS()));
166 167 168 169 170 171 172 173

	result.push_back('|');

	result.append(ToString(song.GetTag()));

	return result;
}

174 175
class TranslateSongTest : public ::testing::Test {
	std::unique_ptr<Storage> _storage;
176

177 178 179 180
protected:
	void SetUp() override {
		_storage = CreateLocalStorage(Path::FromFS(music_directory));
		storage = _storage.get();
181 182
	}

183 184
	void TearDown() override {
		_storage.reset();
185
	}
186
};
187

188 189 190 191 192 193 194 195 196
TEST_F(TranslateSongTest, AbsoluteURI)
{
	DetachedSong song1("http://example.com/foo.ogg");
	auto se = ToString(song1);
	const SongLoader loader(nullptr, nullptr);
	EXPECT_TRUE(playlist_check_translate_song(song1, "/ignored",
						  loader));
	EXPECT_EQ(se, ToString(song1));
}
197

198 199 200 201 202 203 204 205
TEST_F(TranslateSongTest, Insecure)
{
	/* illegal because secure=false */
	DetachedSong song1 (uri1);
	const SongLoader loader(*reinterpret_cast<const Client *>(1));
	EXPECT_FALSE(playlist_check_translate_song(song1, nullptr,
						   loader));
}
206

207 208 209 210 211 212 213 214 215 216
TEST_F(TranslateSongTest, Secure)
{
	DetachedSong song1(uri1, MakeTag1b());
	auto se = ToString(DetachedSong(uri1, MakeTag1c()));

	const SongLoader loader(nullptr, nullptr);
	EXPECT_TRUE(playlist_check_translate_song(song1, "/ignored",
						  loader));
	EXPECT_EQ(se, ToString(song1));
}
217

218
TEST_F(TranslateSongTest, InDatabase)
219
{
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
	const SongLoader loader(reinterpret_cast<const Database *>(1),
				storage);

	DetachedSong song1("doesntexist");
	EXPECT_FALSE(playlist_check_translate_song(song1, nullptr,
						   loader));

	DetachedSong song2(uri2, MakeTag2b());
	auto se = ToString(DetachedSong(uri2, MakeTag2c()));
	EXPECT_TRUE(playlist_check_translate_song(song2, nullptr,
						  loader));
	EXPECT_EQ(se, ToString(song2));

	DetachedSong song3("/music/foo/bar.ogg", MakeTag2b());
	se = ToString(DetachedSong(uri2, MakeTag2c()));
	EXPECT_TRUE(playlist_check_translate_song(song3, nullptr,
						  loader));
	EXPECT_EQ(se, ToString(song3));
}
239

240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
TEST_F(TranslateSongTest, Relative)
{
	const Database &db = *reinterpret_cast<const Database *>(1);
	const SongLoader secure_loader(&db, storage);
	const SongLoader insecure_loader(*reinterpret_cast<const Client *>(1),
					 &db, storage);

	/* map to music_directory */
	DetachedSong song1("bar.ogg", MakeTag2b());
	auto se = ToString(DetachedSong(uri2, MakeTag2c()));
	EXPECT_TRUE(playlist_check_translate_song(song1, "/music/foo",
						  insecure_loader));
	EXPECT_EQ(se, ToString(song1));

	/* illegal because secure=false */
	DetachedSong song2("bar.ogg", MakeTag2b());
	EXPECT_FALSE(playlist_check_translate_song(song1, "/foo",
						   insecure_loader));

	/* legal because secure=true */
	DetachedSong song3("bar.ogg", MakeTag1b());
	se = ToString(DetachedSong(uri1, MakeTag1c()));
	EXPECT_TRUE(playlist_check_translate_song(song3, "/foo",
						  secure_loader));
	EXPECT_EQ(se, ToString(song3));

	/* relative to http:// */
	DetachedSong song4("bar.ogg", MakeTag2a());
	se = ToString(DetachedSong("http://example.com/foo/bar.ogg", MakeTag2a()));
	EXPECT_TRUE(playlist_check_translate_song(song4, "http://example.com/foo",
						  insecure_loader));
	EXPECT_EQ(se, ToString(song4));
272
}
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292

TEST_F(TranslateSongTest, Backslash)
{
	const SongLoader loader(reinterpret_cast<const Database *>(1),
				storage);

	DetachedSong song1("foo\\bar.ogg", MakeTag2b());
#ifdef _WIN32
	/* on Windows, all backslashes are converted to slashes in
	   relative paths from playlists */
	auto se = ToString(DetachedSong(uri2, MakeTag2c()));
	EXPECT_TRUE(playlist_check_translate_song(song1, nullptr,
						  loader));
	EXPECT_EQ(se, ToString(song1));
#else
	/* backslash only supported on Windows */
	EXPECT_FALSE(playlist_check_translate_song(song1, nullptr,
						   loader));
#endif
}