view modules/xdg/test/main.cpp @ 492:b5b2bb02412a

Socket: add mutable because inet_ntop on Windows is not const
author David Demelier <markand@malikania.fr>
date Thu, 19 Nov 2015 16:41:02 +0100
parents 7ee8da32da98
children
line wrap: on
line source

/*
 * main.cpp -- main test file for XDG
 *
 * Copyright (c) 2013-2015 David Demelier <markand@malikania.fr>
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <gtest/gtest.h>

#include <xdg.h>

using namespace testing;

namespace {

std::string myhome;

}

TEST(HomeEmpty, config)
{
	ASSERT_TRUE(unsetenv("XDG_CONFIG_HOME") == 0);

	try {
		Xdg xdg;

		ASSERT_EQ(myhome + "/.config", xdg.configHome());
	} catch (const std::exception &) {
		ASSERT_TRUE(false);
	}
}

TEST(HomeEmpty, data)
{
	ASSERT_TRUE(unsetenv("XDG_DATA_HOME") == 0);

	try {
		Xdg xdg;

		ASSERT_EQ(myhome + "/.local/share", xdg.dataHome());
	} catch (const std::exception &) {
		ASSERT_TRUE(false);
	}
}

TEST(HomeEmpty, cache)
{
	ASSERT_TRUE(unsetenv("XDG_CACHE_HOME") == 0);

	try {
		Xdg xdg;

		ASSERT_EQ(myhome + "/.cache", xdg.cacheHome());
	} catch (const std::exception &) {
		ASSERT_TRUE(false);
	}
}

TEST(HomeEmpty, runtime)
{
	ASSERT_TRUE(unsetenv("XDG_RUNTIME_DIR") == 0);

	try {
		Xdg xdg;

		try {
			xdg.runtimeDir();

			ASSERT_TRUE(false);
		} catch (const std::exception &) { }
	} catch (const std::exception &) {
		ASSERT_TRUE(false);
	}
}

TEST(HomeValid, config)
{
	ASSERT_TRUE(setenv("XDG_CONFIG_HOME", "/test/config", true) == 0);

	try {
		Xdg xdg;

		ASSERT_EQ("/test/config", xdg.configHome());
	} catch (const std::exception &) {
		ASSERT_TRUE(false);
	}
}

TEST(HomeValid, data)
{
	ASSERT_TRUE(setenv("XDG_DATA_HOME", "/test/data", true) == 0);

	try {
		Xdg xdg;

		ASSERT_EQ("/test/data", xdg.dataHome());
	} catch (const std::exception &) {
		ASSERT_TRUE(false);
	}
}

TEST(HomeValid, cache)
{
	ASSERT_TRUE(setenv("XDG_CACHE_HOME", "/test/cache", true) == 0);

	try {
		Xdg xdg;

		ASSERT_EQ("/test/cache", xdg.cacheHome());
	} catch (const std::exception &) {
		ASSERT_TRUE(false);
	}
}

TEST(HomeValid, runtime)
{
	ASSERT_TRUE(setenv("XDG_RUNTIME_DIR", "/test/runtime", true) == 0);

	try {
		Xdg xdg;

		ASSERT_EQ("/test/runtime", xdg.runtimeDir());
	} catch (const std::exception &) {
		ASSERT_TRUE(false);
	}
}

TEST(HomeInvalid, config)
{
	ASSERT_TRUE(setenv("XDG_CONFIG_HOME", "invalid", true) == 0);

	try {
		Xdg xdg;

		ASSERT_EQ(myhome + "/.config", xdg.configHome());
	} catch (const std::exception &) {
		ASSERT_TRUE(false);
	}
}

TEST(HomeInvalid, data)
{
	ASSERT_TRUE(setenv("XDG_DATA_HOME", "invalid", true) == 0);

	try {
		Xdg xdg;

		ASSERT_EQ(myhome + "/.local/share", xdg.dataHome());
	} catch (const std::exception &) {
		ASSERT_TRUE(false);
	}
}

TEST(HomeInvalid, cache)
{
	ASSERT_TRUE(setenv("XDG_CACHE_HOME", "invalid", true) == 0);

	try {
		Xdg xdg;

		ASSERT_EQ(myhome + "/.cache", xdg.cacheHome());
	} catch (const std::exception &) {
		ASSERT_TRUE(false);
	}
}

TEST(HomeInvalid, runtime)
{
	ASSERT_TRUE(setenv("XDG_RUNTIME_DIR", "invalid", true) == 0);

	try {
		Xdg xdg;

		try {
			xdg.runtimeDir();

			ASSERT_TRUE(false);
		} catch (const std::exception &) { }
	} catch (const std::exception &) {
		ASSERT_TRUE(false);
	}
}

TEST(DirectoriesEmpty, config)
{
	ASSERT_TRUE(unsetenv("XDG_CONFIG_DIRS") == 0);

	try {
		Xdg xdg;

		const auto &list = xdg.configDirs();

		ASSERT_EQ((size_t)1, list.size());
		ASSERT_EQ("/etc/xdg", list[0]);
	} catch (const std::exception &) {
		ASSERT_TRUE(false);
	}
}

TEST(DirectoriesEmpty, data)
{
	ASSERT_TRUE(unsetenv("XDG_DATA_DIRS") == 0);

	try {
		Xdg xdg;

		const auto &list = xdg.dataDirs();

		ASSERT_EQ((size_t)2, list.size());
		ASSERT_EQ("/usr/local/share", list[0]);
		ASSERT_EQ("/usr/share", list[1]);
	} catch (const std::exception &) {
		ASSERT_TRUE(false);
	}
}

TEST(DirectoriesValid, config)
{
	ASSERT_TRUE(setenv("XDG_CONFIG_DIRS", "/config1:/config2", true) == 0);

	try {
		Xdg xdg;

		const auto &list = xdg.configDirs();

		ASSERT_EQ((size_t)2, list.size());
		ASSERT_EQ("/config1", list[0]);
		ASSERT_EQ("/config2", list[1]);
	} catch (const std::exception &) {
		ASSERT_TRUE(false);
	}
}

TEST(DirectoriesValid, data)
{
	ASSERT_TRUE(setenv("XDG_DATA_DIRS", "/data1:/data2", true) == 0);

	try {
		Xdg xdg;

		const auto &list = xdg.dataDirs();

		ASSERT_EQ((size_t)2, list.size());
		ASSERT_EQ("/data1", list[0]);
		ASSERT_EQ("/data2", list[1]);
	} catch (const std::exception &) {
		ASSERT_TRUE(false);
	}
}

TEST(DirectoriesInvalid, config)
{
	ASSERT_TRUE(setenv("XDG_CONFIG_DIRS", "bad1:bad2", true) == 0);

	try {
		Xdg xdg;

		const auto &list = xdg.configDirs();

		ASSERT_EQ((size_t)1, list.size());
		ASSERT_EQ("/etc/xdg", list[0]);
	} catch (const std::exception &) {
		ASSERT_TRUE(false);
	}
}

TEST(DirectoriesInvalid, data)
{
	ASSERT_TRUE(setenv("XDG_DATA_DIRS", "bad1:bad2", true) == 0);

	try {
		Xdg xdg;

		const auto &list = xdg.dataDirs();

		ASSERT_EQ((size_t)2, list.size());
		ASSERT_EQ("/usr/local/share", list[0]);
		ASSERT_EQ("/usr/share", list[1]);
	} catch (const std::exception &) {
		ASSERT_TRUE(false);
	}
}

TEST(DirectoriesMixed, config)
{
	ASSERT_TRUE(setenv("XDG_CONFIG_DIRS", "/config1:bad:/config2", true) == 0);

	try {
		Xdg xdg;

		const auto &list = xdg.configDirs();

		ASSERT_EQ((size_t)2, list.size());
		ASSERT_EQ("/config1", list[0]);
		ASSERT_EQ("/config2", list[1]);
	} catch (const std::exception &) {
		ASSERT_TRUE(false);
	}
}

TEST(DirectoriesMixed, data)
{
	ASSERT_TRUE(setenv("XDG_DATA_DIRS", "/data1:bad:/data2", true) == 0);

	try {
		Xdg xdg;

		const auto &list = xdg.dataDirs();

		ASSERT_EQ((size_t)2, list.size());
		ASSERT_EQ("/data1", list[0]);
		ASSERT_EQ("/data2", list[1]);
	} catch (const std::exception &) {
		ASSERT_TRUE(false);
	}
}

int main(int argc, char **argv)
{
	auto home = getenv("HOME");

	if (home == nullptr)
		return 0;

	myhome = home;
	InitGoogleTest(&argc, argv);

	return RUN_ALL_TESTS();
}