1 // Copyright 2016 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/win/windows_version.h"
6
7 #include "base/check_op.h"
8 #include "base/file_version_info_win.h"
9 #include "base/files/file_path.h"
10 #include "base/files/file_util.h"
11 #include "base/path_service.h"
12 #include "build/build_config.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 namespace base {
16 namespace win {
17
TEST(WindowsVersion,GetVersionExAndKernelOsVersionMatch)18 TEST(WindowsVersion, GetVersionExAndKernelOsVersionMatch) {
19 // If this fails, we're running in compatibility mode, or need to update the
20 // application manifest.
21 // Note: not all versions of Windows return identical build numbers e.g.
22 // 1909/19H2 kernel32.dll has build number 18362 but OS version build number
23 // 18363.
24 EXPECT_EQ(OSInfo::Kernel32VersionNumber().major,
25 OSInfo::GetInstance()->version_number().major);
26 EXPECT_EQ(OSInfo::Kernel32VersionNumber().minor,
27 OSInfo::GetInstance()->version_number().minor);
28 }
29
TEST(WindowsVersion,CheckDbgHelpVersion)30 TEST(WindowsVersion, CheckDbgHelpVersion) {
31 // Make sure that dbghelp.dll is present and is a recent enough version to
32 // handle large-page PDBs. This requires dbghelp.dll from the Windows 11 SDK
33 // or later.
34 base::FilePath exe_dir;
35 ASSERT_TRUE(base::PathService::Get(base::DIR_EXE, &exe_dir));
36 FilePath dbghelp_path = exe_dir.Append(FILE_PATH_LITERAL("dbghelp.dll"));
37 ASSERT_TRUE(base::PathExists(dbghelp_path));
38 auto file_version =
39 FileVersionInfoWin::CreateFileVersionInfoWin(dbghelp_path);
40 ASSERT_TRUE(file_version);
41 auto version = file_version->GetFileVersion();
42 // Check against Windows 11 SDK version.
43 EXPECT_GE(version, base::Version({10, 0, 22621, 755}));
44 }
45
TEST(OSInfo,MajorMinorBuildToVersion)46 TEST(OSInfo, MajorMinorBuildToVersion) {
47 EXPECT_EQ(OSInfo::MajorMinorBuildToVersion(11, 0, 0), Version::WIN11);
48 EXPECT_EQ(OSInfo::MajorMinorBuildToVersion(10, 0, 32767),
49 Version::WIN11_24H2);
50 EXPECT_EQ(OSInfo::MajorMinorBuildToVersion(10, 0, 26100),
51 Version::WIN11_24H2);
52 EXPECT_EQ(OSInfo::MajorMinorBuildToVersion(10, 0, 22631),
53 Version::WIN11_23H2);
54 EXPECT_EQ(OSInfo::MajorMinorBuildToVersion(10, 0, 22621),
55 Version::WIN11_22H2);
56 EXPECT_EQ(OSInfo::MajorMinorBuildToVersion(10, 0, 22000), Version::WIN11);
57 EXPECT_EQ(OSInfo::MajorMinorBuildToVersion(10, 0, 21999),
58 Version::SERVER_2022);
59 EXPECT_EQ(OSInfo::MajorMinorBuildToVersion(10, 0, 20348),
60 Version::SERVER_2022);
61 EXPECT_EQ(OSInfo::MajorMinorBuildToVersion(10, 0, 20347),
62 Version::WIN10_22H2);
63 EXPECT_EQ(OSInfo::MajorMinorBuildToVersion(10, 0, 19045),
64 Version::WIN10_22H2);
65 EXPECT_EQ(OSInfo::MajorMinorBuildToVersion(10, 0, 19044),
66 Version::WIN10_21H2);
67 EXPECT_EQ(OSInfo::MajorMinorBuildToVersion(10, 0, 19043),
68 Version::WIN10_21H1);
69 EXPECT_EQ(OSInfo::MajorMinorBuildToVersion(10, 0, 19042),
70 Version::WIN10_20H2);
71 EXPECT_EQ(OSInfo::MajorMinorBuildToVersion(10, 0, 19041),
72 Version::WIN10_20H1);
73 EXPECT_EQ(OSInfo::MajorMinorBuildToVersion(10, 0, 18363),
74 Version::WIN10_19H2);
75 EXPECT_EQ(OSInfo::MajorMinorBuildToVersion(10, 0, 18362),
76 Version::WIN10_19H1);
77 EXPECT_EQ(OSInfo::MajorMinorBuildToVersion(10, 0, 17763), Version::WIN10_RS5);
78 EXPECT_EQ(OSInfo::MajorMinorBuildToVersion(10, 0, 17134), Version::WIN10_RS4);
79 EXPECT_EQ(OSInfo::MajorMinorBuildToVersion(10, 0, 16299), Version::WIN10_RS3);
80 EXPECT_EQ(OSInfo::MajorMinorBuildToVersion(10, 0, 15063), Version::WIN10_RS2);
81 EXPECT_EQ(OSInfo::MajorMinorBuildToVersion(10, 0, 14393), Version::WIN10_RS1);
82 EXPECT_EQ(OSInfo::MajorMinorBuildToVersion(10, 0, 10586), Version::WIN10_TH2);
83 EXPECT_EQ(OSInfo::MajorMinorBuildToVersion(10, 0, 10240), Version::WIN10);
84 EXPECT_EQ(OSInfo::MajorMinorBuildToVersion(10, 0, 0), Version::WIN10);
85 EXPECT_EQ(OSInfo::MajorMinorBuildToVersion(6, 3, 0), Version::WIN8_1);
86 EXPECT_EQ(OSInfo::MajorMinorBuildToVersion(6, 2, 0), Version::WIN8);
87 EXPECT_EQ(OSInfo::MajorMinorBuildToVersion(6, 1, 0), Version::WIN7);
88 EXPECT_EQ(OSInfo::MajorMinorBuildToVersion(6, 0, 0), Version::VISTA);
89 EXPECT_EQ(OSInfo::MajorMinorBuildToVersion(5, 3, 0), Version::SERVER_2003);
90 EXPECT_EQ(OSInfo::MajorMinorBuildToVersion(5, 2, 0), Version::SERVER_2003);
91 EXPECT_EQ(OSInfo::MajorMinorBuildToVersion(5, 1, 0), Version::XP);
92 EXPECT_EQ(OSInfo::MajorMinorBuildToVersion(5, 0, 0), Version::PRE_XP);
93 EXPECT_EQ(OSInfo::MajorMinorBuildToVersion(4, 0, 0), Version::PRE_XP);
94 EXPECT_EQ(OSInfo::MajorMinorBuildToVersion(3, 0, 0), Version::PRE_XP);
95 EXPECT_EQ(OSInfo::MajorMinorBuildToVersion(2, 0, 0), Version::PRE_XP);
96 EXPECT_EQ(OSInfo::MajorMinorBuildToVersion(1, 0, 0), Version::PRE_XP);
97 EXPECT_EQ(OSInfo::MajorMinorBuildToVersion(0, 0, 0), Version::PRE_XP);
98
99 #if !DCHECK_IS_ON()
100 EXPECT_EQ(OSInfo::MajorMinorBuildToVersion(12, 0, 0), Version::WIN_LAST);
101 EXPECT_EQ(OSInfo::MajorMinorBuildToVersion(9, 0, 0), Version::WIN_LAST);
102 EXPECT_EQ(OSInfo::MajorMinorBuildToVersion(8, 0, 0), Version::WIN_LAST);
103 EXPECT_EQ(OSInfo::MajorMinorBuildToVersion(7, 0, 0), Version::WIN_LAST);
104 EXPECT_EQ(OSInfo::MajorMinorBuildToVersion(6, 4, 0), Version::WIN8_1);
105 #endif // !DCHECK_IS_ON()
106 }
107
108 // For more info on what processor information is defined, see:
109 // http://msdn.microsoft.com/en-us/library/b0084kay.aspx
TEST(OSInfo,GetWowStatusForProcess)110 TEST(OSInfo, GetWowStatusForProcess) {
111 #if defined(ARCH_CPU_64_BITS)
112 EXPECT_TRUE(OSInfo::GetInstance()->IsWowDisabled());
113 #elif defined(ARCH_CPU_X86)
114 if (OSInfo::GetArchitecture() == OSInfo::X86_ARCHITECTURE) {
115 EXPECT_TRUE(OSInfo::GetInstance()->IsWowDisabled());
116 } else if (OSInfo::GetArchitecture() == OSInfo::X64_ARCHITECTURE) {
117 EXPECT_TRUE(OSInfo::GetInstance()->IsWowX86OnAMD64());
118 } else {
119 // Currently, the only way to determine if a WOW emulation is
120 // running on an ARM64 device is via the function that the
121 // |IsWow*| helper functions rely on. As such, it is not possible
122 // to separate out x86 running on ARM64 machines vs x86 running on
123 // other host machines.
124 EXPECT_TRUE(OSInfo::GetInstance()->IsWowX86OnARM64() ||
125 OSInfo::GetInstance()->IsWowX86OnOther());
126 }
127 #else
128 ADD_FAILURE()
129 << "This test fails when we're using a process or host machine that is "
130 "not being considered by our helper functions. If you're seeing this "
131 "error, please add a helper function for determining WOW emulation "
132 "for your process and host machine.";
133 #endif
134 }
135
136 } // namespace win
137 } // namespace base
138