403 lines · cpp
1//===- unittests/Driver/DistroTest.cpp --- ToolChains tests ---------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8//9// Unit tests for Distro detection.10//11//===----------------------------------------------------------------------===//12 13#include "clang/Driver/Distro.h"14#include "llvm/Support/VirtualFileSystem.h"15#include "llvm/Support/raw_ostream.h"16#include "llvm/TargetParser/Host.h"17#include "gtest/gtest.h"18 19using namespace clang;20using namespace clang::driver;21 22namespace {23 24// The tests include all release-related files for each distribution25// in the VFS, in order to make sure that earlier tests do not26// accidentally result in incorrect distribution guess.27 28TEST(DistroTest, DetectUbuntu) {29 llvm::vfs::InMemoryFileSystem UbuntuTrustyFileSystem;30 // Ubuntu uses Debian Sid version.31 UbuntuTrustyFileSystem.addFile("/etc/debian_version", 0,32 llvm::MemoryBuffer::getMemBuffer("jessie/sid\n"));33 UbuntuTrustyFileSystem.addFile("/etc/lsb-release", 0,34 llvm::MemoryBuffer::getMemBuffer("DISTRIB_ID=Ubuntu\n"35 "DISTRIB_RELEASE=14.04\n"36 "DISTRIB_CODENAME=trusty\n"37 "DISTRIB_DESCRIPTION=\"Ubuntu 14.04 LTS\"\n"));38 UbuntuTrustyFileSystem.addFile("/etc/os-release", 0,39 llvm::MemoryBuffer::getMemBuffer("NAME=\"Ubuntu\"\n"40 "VERSION=\"14.04, Trusty Tahr\"\n"41 "ID=ubuntu\n"42 "ID_LIKE=debian\n"43 "PRETTY_NAME=\"Ubuntu 14.04 LTS\"\n"44 "VERSION_ID=\"14.04\"\n"45 "HOME_URL=\"http://www.ubuntu.com/\"\n"46 "SUPPORT_URL=\"http://help.ubuntu.com/\"\n"47 "BUG_REPORT_URL=\"http://bugs.launchpad.net/ubuntu/\"\n"));48 49 Distro UbuntuTrusty{UbuntuTrustyFileSystem, llvm::Triple("unknown-pc-linux")};50 ASSERT_EQ(Distro(Distro::UbuntuTrusty), UbuntuTrusty);51 ASSERT_TRUE(UbuntuTrusty.IsUbuntu());52 ASSERT_FALSE(UbuntuTrusty.IsRedhat());53 ASSERT_FALSE(UbuntuTrusty.IsOpenSUSE());54 ASSERT_FALSE(UbuntuTrusty.IsDebian());55 ASSERT_FALSE(UbuntuTrusty.IsGentoo());56 57 Distro UbuntuTrusty2{UbuntuTrustyFileSystem, llvm::Triple("unknown-pc-windows")};58 ASSERT_EQ(Distro(Distro::UnknownDistro), UbuntuTrusty2);59 60 llvm::vfs::InMemoryFileSystem UbuntuYakketyFileSystem;61 UbuntuYakketyFileSystem.addFile("/etc/debian_version", 0,62 llvm::MemoryBuffer::getMemBuffer("stretch/sid\n"));63 UbuntuYakketyFileSystem.addFile("/etc/lsb-release", 0,64 llvm::MemoryBuffer::getMemBuffer("DISTRIB_ID=Ubuntu\n"65 "DISTRIB_RELEASE=16.10\n"66 "DISTRIB_CODENAME=yakkety\n"67 "DISTRIB_DESCRIPTION=\"Ubuntu 16.10\"\n"));68 UbuntuYakketyFileSystem.addFile("/etc/os-release", 0,69 llvm::MemoryBuffer::getMemBuffer("NAME=\"Ubuntu\"\n"70 "VERSION=\"16.10 (Yakkety Yak)\"\n"71 "ID=ubuntu\n"72 "ID_LIKE=debian\n"73 "PRETTY_NAME=\"Ubuntu 16.10\"\n"74 "VERSION_ID=\"16.10\"\n"75 "HOME_URL=\"http://www.ubuntu.com/\"\n"76 "SUPPORT_URL=\"http://help.ubuntu.com/\"\n"77 "BUG_REPORT_URL=\"http://bugs.launchpad.net/ubuntu/\"\n"78 "PRIVACY_POLICY_URL=\"http://www.ubuntu.com/legal/terms-and-policies/privacy-policy\"\n"79 "VERSION_CODENAME=yakkety\n"80 "UBUNTU_CODENAME=yakkety\n"));81 82 Distro UbuntuYakkety{UbuntuYakketyFileSystem, llvm::Triple("unknown-pc-linux")};83 ASSERT_EQ(Distro(Distro::UbuntuYakkety), UbuntuYakkety);84 ASSERT_TRUE(UbuntuYakkety.IsUbuntu());85 ASSERT_FALSE(UbuntuYakkety.IsRedhat());86 ASSERT_FALSE(UbuntuYakkety.IsOpenSUSE());87 ASSERT_FALSE(UbuntuYakkety.IsDebian());88 ASSERT_FALSE(UbuntuYakkety.IsGentoo());89}90 91TEST(DistroTest, DetectRedhat) {92 llvm::vfs::InMemoryFileSystem Fedora25FileSystem;93 Fedora25FileSystem.addFile("/etc/system-release-cpe", 0,94 llvm::MemoryBuffer::getMemBuffer("cpe:/o:fedoraproject:fedora:25\n"));95 // Both files are symlinks to fedora-release.96 Fedora25FileSystem.addFile("/etc/system-release", 0,97 llvm::MemoryBuffer::getMemBuffer("Fedora release 25 (Twenty Five)\n"));98 Fedora25FileSystem.addFile("/etc/redhat-release", 0,99 llvm::MemoryBuffer::getMemBuffer("Fedora release 25 (Twenty Five)\n"));100 Fedora25FileSystem.addFile("/etc/fedora-release", 0,101 llvm::MemoryBuffer::getMemBuffer("Fedora release 25 (Twenty Five)\n"));102 Fedora25FileSystem.addFile("/etc/os-release", 0,103 llvm::MemoryBuffer::getMemBuffer("NAME=Fedora\n"104 "VERSION=\"25 (Twenty Five)\"\n"105 "ID=fedora\n"106 "VERSION_ID=25\n"107 "PRETTY_NAME=\"Fedora 25 (Twenty Five)\"\n"108 "ANSI_COLOR=\"0;34\"\n"109 "CPE_NAME=\"cpe:/o:fedoraproject:fedora:25\"\n"110 "HOME_URL=\"https://fedoraproject.org/\"\n"111 "BUG_REPORT_URL=\"https://bugzilla.redhat.com/\"\n"112 "REDHAT_BUGZILLA_PRODUCT=\"Fedora\"\n"113 "REDHAT_BUGZILLA_PRODUCT_VERSION=25\n"114 "REDHAT_SUPPORT_PRODUCT=\"Fedora\"\n"115 "REDHAT_SUPPORT_PRODUCT_VERSION=25\n"116 "PRIVACY_POLICY_URL=https://fedoraproject.org/wiki/Legal:PrivacyPolicy\n"));117 Distro Fedora25{Fedora25FileSystem, llvm::Triple("unknown-pc-linux")};118 ASSERT_EQ(Distro(Distro::Fedora), Fedora25);119 ASSERT_FALSE(Fedora25.IsUbuntu());120 ASSERT_TRUE(Fedora25.IsRedhat());121 ASSERT_FALSE(Fedora25.IsOpenSUSE());122 ASSERT_FALSE(Fedora25.IsDebian());123 ASSERT_FALSE(Fedora25.IsGentoo());124 125 llvm::vfs::InMemoryFileSystem CentOS7FileSystem;126 CentOS7FileSystem.addFile("/etc/system-release-cpe", 0,127 llvm::MemoryBuffer::getMemBuffer("cpe:/o:centos:centos:7\n"));128 // Both files are symlinks to centos-release.129 CentOS7FileSystem.addFile("/etc/system-release", 0,130 llvm::MemoryBuffer::getMemBuffer("CentOS Linux release 7.2.1511 (Core) \n"));131 CentOS7FileSystem.addFile("/etc/redhat-release", 0,132 llvm::MemoryBuffer::getMemBuffer("CentOS Linux release 7.2.1511 (Core) \n"));133 CentOS7FileSystem.addFile("/etc/centos-release", 0,134 llvm::MemoryBuffer::getMemBuffer("CentOS Linux release 7.2.1511 (Core) \n"));135 CentOS7FileSystem.addFile("/etc/centos-release-upstream", 0,136 llvm::MemoryBuffer::getMemBuffer("Derived from Red Hat Enterprise Linux 7.2 (Source)\n"));137 CentOS7FileSystem.addFile("/etc/os-release", 0,138 llvm::MemoryBuffer::getMemBuffer("NAME=\"CentOS Linux\"\n"139 "VERSION=\"7 (Core)\"\n"140 "ID=\"centos\"\n"141 "ID_LIKE=\"rhel fedora\"\n"142 "VERSION_ID=\"7\"\n"143 "PRETTY_NAME=\"CentOS Linux 7 (Core)\"\n"144 "ANSI_COLOR=\"0;31\"\n"145 "CPE_NAME=\"cpe:/o:centos:centos:7\"\n"146 "HOME_URL=\"https://www.centos.org/\"\n"147 "BUG_REPORT_URL=\"https://bugs.centos.org/\"\n"148 "\n"149 "CENTOS_MANTISBT_PROJECT=\"CentOS-7\"\n"150 "CENTOS_MANTISBT_PROJECT_VERSION=\"7\"\n"151 "REDHAT_SUPPORT_PRODUCT=\"centos\"\n"152 "REDHAT_SUPPORT_PRODUCT_VERSION=\"7\"\n"));153 154 Distro CentOS7{CentOS7FileSystem, llvm::Triple("unknown-pc-linux")};155 ASSERT_EQ(Distro(Distro::RHEL7), CentOS7);156 ASSERT_FALSE(CentOS7.IsUbuntu());157 ASSERT_TRUE(CentOS7.IsRedhat());158 ASSERT_FALSE(CentOS7.IsOpenSUSE());159 ASSERT_FALSE(CentOS7.IsDebian());160 ASSERT_FALSE(CentOS7.IsGentoo());161}162 163TEST(DistroTest, DetectOpenSUSE) {164 llvm::vfs::InMemoryFileSystem OpenSUSELeap421FileSystem;165 OpenSUSELeap421FileSystem.addFile("/etc/SuSE-release", 0,166 llvm::MemoryBuffer::getMemBuffer("openSUSE 42.1 (x86_64)\n"167 "VERSION = 42.1\n"168 "CODENAME = Malachite\n"169 "# /etc/SuSE-release is deprecated and will be removed in the future, use /etc/os-release instead\n"));170 OpenSUSELeap421FileSystem.addFile("/etc/os-release", 0,171 llvm::MemoryBuffer::getMemBuffer("NAME=\"openSUSE Leap\"\n"172 "VERSION=\"42.1\"\n"173 "VERSION_ID=\"42.1\"\n"174 "PRETTY_NAME=\"openSUSE Leap 42.1 (x86_64)\"\n"175 "ID=opensuse\n"176 "ANSI_COLOR=\"0;32\"\n"177 "CPE_NAME=\"cpe:/o:opensuse:opensuse:42.1\"\n"178 "BUG_REPORT_URL=\"https://bugs.opensuse.org\"\n"179 "HOME_URL=\"https://opensuse.org/\"\n"180 "ID_LIKE=\"suse\"\n"));181 182 Distro OpenSUSELeap421{OpenSUSELeap421FileSystem, llvm::Triple("unknown-pc-linux")};183 ASSERT_EQ(Distro(Distro::OpenSUSE), OpenSUSELeap421);184 ASSERT_FALSE(OpenSUSELeap421.IsUbuntu());185 ASSERT_FALSE(OpenSUSELeap421.IsRedhat());186 ASSERT_TRUE(OpenSUSELeap421.IsOpenSUSE());187 ASSERT_FALSE(OpenSUSELeap421.IsDebian());188 ASSERT_FALSE(OpenSUSELeap421.IsGentoo());189 190 llvm::vfs::InMemoryFileSystem OpenSUSE132FileSystem;191 OpenSUSE132FileSystem.addFile("/etc/SuSE-release", 0,192 llvm::MemoryBuffer::getMemBuffer("openSUSE 13.2 (x86_64)\n"193 "VERSION = 13.2\n"194 "CODENAME = Harlequin\n"195 "# /etc/SuSE-release is deprecated and will be removed in the future, use /etc/os-release instead\n"));196 OpenSUSE132FileSystem.addFile("/etc/os-release", 0,197 llvm::MemoryBuffer::getMemBuffer("NAME=openSUSE\n"198 "VERSION=\"13.2 (Harlequin)\"\n"199 "VERSION_ID=\"13.2\"\n"200 "PRETTY_NAME=\"openSUSE 13.2 (Harlequin) (x86_64)\"\n"201 "ID=opensuse\n"202 "ANSI_COLOR=\"0;32\"\n"203 "CPE_NAME=\"cpe:/o:opensuse:opensuse:13.2\"\n"204 "BUG_REPORT_URL=\"https://bugs.opensuse.org\"\n"205 "HOME_URL=\"https://opensuse.org/\"\n"206 "ID_LIKE=\"suse\"\n"));207 208 Distro OpenSUSE132{OpenSUSE132FileSystem, llvm::Triple("unknown-pc-linux")};209 ASSERT_EQ(Distro(Distro::OpenSUSE), OpenSUSE132);210 ASSERT_FALSE(OpenSUSE132.IsUbuntu());211 ASSERT_FALSE(OpenSUSE132.IsRedhat());212 ASSERT_TRUE(OpenSUSE132.IsOpenSUSE());213 ASSERT_FALSE(OpenSUSE132.IsDebian());214 ASSERT_FALSE(OpenSUSE132.IsGentoo());215 216 llvm::vfs::InMemoryFileSystem SLES10FileSystem;217 SLES10FileSystem.addFile("/etc/SuSE-release", 0,218 llvm::MemoryBuffer::getMemBuffer("SUSE Linux Enterprise Server 10 (x86_64)\n"219 "VERSION = 10\n"220 "PATCHLEVEL = 4\n"));221 SLES10FileSystem.addFile("/etc/lsb_release", 0,222 llvm::MemoryBuffer::getMemBuffer("LSB_VERSION=\"core-2.0-noarch:core-3.0-noarch:core-2.0-x86_64:core-3.0-x86_64\"\n"));223 224 // SLES10 is unsupported and therefore evaluates to unknown225 Distro SLES10{SLES10FileSystem, llvm::Triple("unknown-pc-linux")};226 ASSERT_EQ(Distro(Distro::UnknownDistro), SLES10);227 ASSERT_FALSE(SLES10.IsUbuntu());228 ASSERT_FALSE(SLES10.IsRedhat());229 ASSERT_FALSE(SLES10.IsOpenSUSE());230 ASSERT_FALSE(SLES10.IsDebian());231 ASSERT_FALSE(SLES10.IsGentoo());232}233 234TEST(DistroTest, DetectDebian) {235 llvm::vfs::InMemoryFileSystem DebianJessieFileSystem;236 DebianJessieFileSystem.addFile("/etc/debian_version", 0,237 llvm::MemoryBuffer::getMemBuffer("8.6\n"));238 DebianJessieFileSystem.addFile("/etc/os-release", 0,239 llvm::MemoryBuffer::getMemBuffer("PRETTY_NAME=\"Debian GNU/Linux 8 (jessie)\"\n"240 "NAME=\"Debian GNU/Linux\"\n"241 "VERSION_ID=\"8\"\n"242 "VERSION=\"8 (jessie)\"\n"243 "ID=debian\n"244 "HOME_URL=\"http://www.debian.org/\"\n"245 "SUPPORT_URL=\"http://www.debian.org/support\"\n"246 "BUG_REPORT_URL=\"https://bugs.debian.org/\"\n"));247 248 Distro DebianJessie{DebianJessieFileSystem, llvm::Triple("unknown-pc-linux")};249 ASSERT_EQ(Distro(Distro::DebianJessie), DebianJessie);250 ASSERT_FALSE(DebianJessie.IsUbuntu());251 ASSERT_FALSE(DebianJessie.IsRedhat());252 ASSERT_FALSE(DebianJessie.IsOpenSUSE());253 ASSERT_TRUE(DebianJessie.IsDebian());254 ASSERT_FALSE(DebianJessie.IsGentoo());255 256 llvm::vfs::InMemoryFileSystem DebianStretchSidFileSystem;257 DebianStretchSidFileSystem.addFile("/etc/debian_version", 0,258 llvm::MemoryBuffer::getMemBuffer("stretch/sid\n"));259 DebianStretchSidFileSystem.addFile("/etc/os-release", 0,260 llvm::MemoryBuffer::getMemBuffer("PRETTY_NAME=\"Debian GNU/Linux stretch/sid\"\n"261 "NAME=\"Debian GNU/Linux\"\n"262 "ID=debian\n"263 "HOME_URL=\"http://www.debian.org/\"\n"264 "SUPPORT_URL=\"http://www.debian.org/support\"\n"265 "BUG_REPORT_URL=\"https://bugs.debian.org/\"\n"));266 267 Distro DebianStretchSid{DebianStretchSidFileSystem, llvm::Triple("unknown-pc-linux")};268 ASSERT_EQ(Distro(Distro::DebianStretch), DebianStretchSid);269 ASSERT_FALSE(DebianStretchSid.IsUbuntu());270 ASSERT_FALSE(DebianStretchSid.IsRedhat());271 ASSERT_FALSE(DebianStretchSid.IsOpenSUSE());272 ASSERT_TRUE(DebianStretchSid.IsDebian());273 ASSERT_FALSE(DebianStretchSid.IsGentoo());274}275 276TEST(DistroTest, DetectExherbo) {277 llvm::vfs::InMemoryFileSystem ExherboFileSystem;278 ExherboFileSystem.addFile("/etc/os-release", 0,279 llvm::MemoryBuffer::getMemBuffer("NAME=\"Exherbo\"\n"280 "PRETTY_NAME=\"Exherbo Linux\"\n"281 "ID=exherbo\n"282 "ANSI_COLOR=\"0;32\"\n"283 "HOME_URL=\"https://www.exherbo.org/\"\n"284 "SUPPORT_URL=\"irc://irc.freenode.net/#exherbo\"\n"285 "BUG_REPORT_URL=\"https://bugs.exherbo.org/\"\n"));286 287 Distro Exherbo{ExherboFileSystem, llvm::Triple("unknown-pc-linux")};288 ASSERT_EQ(Distro(Distro::Exherbo), Exherbo);289 ASSERT_FALSE(Exherbo.IsUbuntu());290 ASSERT_FALSE(Exherbo.IsRedhat());291 ASSERT_FALSE(Exherbo.IsOpenSUSE());292 ASSERT_FALSE(Exherbo.IsDebian());293 ASSERT_FALSE(Exherbo.IsGentoo());294}295 296TEST(DistroTest, DetectArchLinux) {297 llvm::vfs::InMemoryFileSystem ArchLinuxFileSystem;298 ArchLinuxFileSystem.addFile("/etc/arch-release", 0, // (empty)299 llvm::MemoryBuffer::getMemBuffer(""));300 ArchLinuxFileSystem.addFile("/etc/os-release", 0,301 llvm::MemoryBuffer::getMemBuffer("NAME=\"Arch Linux\"\n"302 "ID=arch\n"303 "PRETTY_NAME=\"Arch Linux\"\n"304 "ANSI_COLOR=\"0;36\"\n"305 "HOME_URL=\"https://www.archlinux.org/\"\n"306 "SUPPORT_URL=\"https://bbs.archlinux.org/\"\n"307 "BUG_REPORT_URL=\"https://bugs.archlinux.org/\"\n"));308 309 Distro ArchLinux{ArchLinuxFileSystem, llvm::Triple("unknown-pc-linux")};310 ASSERT_EQ(Distro(Distro::ArchLinux), ArchLinux);311 ASSERT_FALSE(ArchLinux.IsUbuntu());312 ASSERT_FALSE(ArchLinux.IsRedhat());313 ASSERT_FALSE(ArchLinux.IsOpenSUSE());314 ASSERT_FALSE(ArchLinux.IsDebian());315 ASSERT_FALSE(ArchLinux.IsGentoo());316}317 318TEST(DistroTest, DetectGentoo) {319 llvm::vfs::InMemoryFileSystem GentooFileSystem;320 GentooFileSystem.addFile(321 "/etc/gentoo-release", 0,322 llvm::MemoryBuffer::getMemBuffer("Gentoo Base System release 2.6"));323 GentooFileSystem.addFile(324 "/etc/os-release", 0,325 llvm::MemoryBuffer::getMemBuffer(326 "NAME=Gentoo\n"327 "ID=gentoo\n"328 "PRETTY_NAME=\"Gentoo/Linux\"\n"329 "ANSI_COLOR=\"1;32\"\n"330 "HOME_URL=\"https://www.gentoo.org/\"\n"331 "SUPPORT_URL=\"https://www.gentoo.org/support/\"\n"332 "BUG_REPORT_URL=\"https://bugs.gentoo.org/\"\n"));333 334 Distro Gentoo{GentooFileSystem, llvm::Triple("unknown-pc-linux")};335 ASSERT_EQ(Distro(Distro::Gentoo), Gentoo);336 ASSERT_FALSE(Gentoo.IsUbuntu());337 ASSERT_FALSE(Gentoo.IsRedhat());338 ASSERT_FALSE(Gentoo.IsOpenSUSE());339 ASSERT_FALSE(Gentoo.IsDebian());340 ASSERT_TRUE(Gentoo.IsGentoo());341}342 343TEST(DistroTest, DetectWindowsAndCrossCompile) {344 345 class CountingFileSystem : public llvm::vfs::ProxyFileSystem {346 public:347 CountingFileSystem() : ProxyFileSystem(llvm::vfs::getRealFileSystem()) {}348 349 llvm::ErrorOr<llvm::vfs::Status> status(const llvm::Twine &Path) override {350 ++Count;351 return llvm::vfs::ProxyFileSystem::status(Path);352 }353 354 llvm::ErrorOr<std::unique_ptr<llvm::vfs::File>>355 openFileForRead(const llvm::Twine &Path) override {356 ++Count;357 return llvm::vfs::ProxyFileSystem::openFileForRead(Path);358 }359 360 unsigned Count{};361 };362 363 llvm::Triple Host(llvm::sys::getProcessTriple());364 if (!Host.isOSWindows())365 GTEST_SKIP();366 367 llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> RFS =368 llvm::vfs::getRealFileSystem();369 370 CountingFileSystem CFileSystem;371 Distro LinuxDistro{CFileSystem, llvm::Triple("unknown-pc-linux")};372 ASSERT_EQ(Distro(Distro::UnknownDistro), LinuxDistro);373 ASSERT_GT(CFileSystem.Count, 0U);374 375 Distro WinDistro{CFileSystem, llvm::Triple("unknown-pc-windows")};376 ASSERT_EQ(Distro(Distro::UnknownDistro), WinDistro);377 ASSERT_GT(CFileSystem.Count, 0U);378 379 // When running on Windows along with a real file system, ensure that no380 // distro is returned if targeting Linux381 Distro LinuxRealDistro{*RFS, llvm::Triple("unknown-pc-linux")};382 ASSERT_EQ(Distro(Distro::UnknownDistro), LinuxRealDistro);383 384 Distro WinRealDistro{*RFS, llvm::Triple("unknown-pc-windows")};385 ASSERT_EQ(Distro(Distro::UnknownDistro), WinRealDistro);386}387 388TEST(DistroTest, DetectLinux) {389 llvm::Triple Host(llvm::sys::getProcessTriple());390 if (!Host.isOSLinux())391 GTEST_SKIP();392 393 // When running on Linux, check if the distro is the same as the host when394 // targeting Linux395 llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> RFS =396 llvm::vfs::getRealFileSystem();397 Distro HostDistro{*RFS, Host};398 Distro LinuxRealDistro{*RFS, llvm::Triple("unknown-pc-linux")};399 ASSERT_EQ(HostDistro, LinuxRealDistro);400}401 402} // end anonymous namespace403