2012-12-31 12:17:50 +01:00
|
|
|
//===- unittest/Support/ProcessTest.cpp -----------------------------------===//
|
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2012-12-31 12:17:50 +01:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2020-01-09 19:14:05 +01:00
|
|
|
#include "llvm/ADT/Triple.h"
|
|
|
|
#include "llvm/Support/Error.h"
|
|
|
|
#include "llvm/Support/Host.h"
|
2012-12-31 12:17:50 +01:00
|
|
|
#include "llvm/Support/Process.h"
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
2018-04-29 02:45:03 +02:00
|
|
|
#ifdef _WIN32
|
2013-12-25 11:50:11 +01:00
|
|
|
#include <windows.h>
|
2012-12-31 12:17:50 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace sys;
|
|
|
|
|
2020-04-13 13:26:35 +02:00
|
|
|
TEST(ProcessTest, GetProcessIdTest) {
|
|
|
|
const Process::Pid pid = Process::getProcessId();
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
2020-05-05 22:01:36 +02:00
|
|
|
EXPECT_EQ((DWORD)pid, ::GetCurrentProcessId());
|
2020-04-13 13:26:35 +02:00
|
|
|
#else
|
|
|
|
EXPECT_EQ(pid, ::getpid());
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-02-04 15:49:21 +01:00
|
|
|
TEST(ProcessTest, GetRandomNumberTest) {
|
|
|
|
const unsigned r1 = Process::GetRandomNumber();
|
|
|
|
const unsigned r2 = Process::GetRandomNumber();
|
|
|
|
// It should be extremely unlikely that both r1 and r2 are 0.
|
2014-02-04 23:53:45 +01:00
|
|
|
EXPECT_NE((r1 | r2), 0u);
|
2014-02-04 15:49:21 +01:00
|
|
|
}
|
|
|
|
|
2013-09-10 23:32:42 +02:00
|
|
|
#ifdef _MSC_VER
|
2013-09-10 21:45:51 +02:00
|
|
|
#define setenv(name, var, ignore) _putenv_s(name, var)
|
|
|
|
#endif
|
|
|
|
|
2013-09-10 23:32:42 +02:00
|
|
|
#if HAVE_SETENV || _MSC_VER
|
2013-09-10 21:45:51 +02:00
|
|
|
TEST(ProcessTest, Basic) {
|
|
|
|
setenv("__LLVM_TEST_ENVIRON_VAR__", "abc", true);
|
|
|
|
Optional<std::string> val(Process::GetEnv("__LLVM_TEST_ENVIRON_VAR__"));
|
|
|
|
EXPECT_TRUE(val.hasValue());
|
|
|
|
EXPECT_STREQ("abc", val->c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ProcessTest, None) {
|
|
|
|
Optional<std::string> val(
|
|
|
|
Process::GetEnv("__LLVM_TEST_ENVIRON_NO_SUCH_VAR__"));
|
|
|
|
EXPECT_FALSE(val.hasValue());
|
2018-11-19 13:10:07 +01:00
|
|
|
}
|
2013-09-10 21:45:51 +02:00
|
|
|
#endif
|
|
|
|
|
2018-04-29 02:45:03 +02:00
|
|
|
#ifdef _WIN32
|
2017-08-18 18:55:44 +02:00
|
|
|
|
|
|
|
TEST(ProcessTest, EmptyVal) {
|
|
|
|
SetEnvironmentVariableA("__LLVM_TEST_ENVIRON_VAR__", "");
|
|
|
|
Optional<std::string> val(Process::GetEnv("__LLVM_TEST_ENVIRON_VAR__"));
|
|
|
|
EXPECT_TRUE(val.hasValue());
|
|
|
|
EXPECT_STREQ("", val->c_str());
|
|
|
|
}
|
|
|
|
|
2013-09-10 21:45:51 +02:00
|
|
|
TEST(ProcessTest, Wchar) {
|
|
|
|
SetEnvironmentVariableW(L"__LLVM_TEST_ENVIRON_VAR__", L"abcdefghijklmnopqrs");
|
|
|
|
Optional<std::string> val(Process::GetEnv("__LLVM_TEST_ENVIRON_VAR__"));
|
|
|
|
EXPECT_TRUE(val.hasValue());
|
|
|
|
EXPECT_STREQ("abcdefghijklmnopqrs", val->c_str());
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-01-09 19:14:05 +01:00
|
|
|
class PageSizeTest : public testing::Test {
|
|
|
|
Triple Host;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
PageSizeTest() : Host(Triple::normalize(sys::getProcessTriple())) {}
|
|
|
|
|
|
|
|
bool isSupported() const {
|
|
|
|
// For now just on X86-64 and Aarch64. This can be expanded in the future.
|
|
|
|
return (Host.getArch() == Triple::x86_64 ||
|
|
|
|
Host.getArch() == Triple::aarch64) &&
|
|
|
|
Host.getOS() == Triple::Linux;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool pageSizeAsExpected(unsigned PageSize) const {
|
|
|
|
switch (Host.getArch()) {
|
|
|
|
case Triple::x86_64:
|
|
|
|
return PageSize == 4096;
|
|
|
|
case Triple::aarch64:
|
|
|
|
// supported granule sizes are 4k, 16k and 64k
|
|
|
|
return PageSize == 4096 || PageSize == 16384 || PageSize == 65536;
|
|
|
|
default:
|
|
|
|
llvm_unreachable("unexpected arch!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(PageSizeTest, PageSize) {
|
|
|
|
if (!isSupported())
|
|
|
|
return;
|
|
|
|
|
|
|
|
llvm::Expected<unsigned> Result = llvm::sys::Process::getPageSize();
|
|
|
|
ASSERT_FALSE(!Result);
|
|
|
|
ASSERT_TRUE(pageSizeAsExpected(*Result));
|
|
|
|
}
|
|
|
|
|
2012-12-31 12:17:50 +01:00
|
|
|
} // end anonymous namespace
|