xref: /aosp_15_r20/external/cronet/base/win/windows_version_unittest.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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_23H2);
50   EXPECT_EQ(OSInfo::MajorMinorBuildToVersion(10, 0, 22631),
51             Version::WIN11_23H2);
52   EXPECT_EQ(OSInfo::MajorMinorBuildToVersion(10, 0, 22621),
53             Version::WIN11_22H2);
54   EXPECT_EQ(OSInfo::MajorMinorBuildToVersion(10, 0, 22000), Version::WIN11);
55   EXPECT_EQ(OSInfo::MajorMinorBuildToVersion(10, 0, 21999),
56             Version::SERVER_2022);
57   EXPECT_EQ(OSInfo::MajorMinorBuildToVersion(10, 0, 20348),
58             Version::SERVER_2022);
59   EXPECT_EQ(OSInfo::MajorMinorBuildToVersion(10, 0, 20347),
60             Version::WIN10_22H2);
61   EXPECT_EQ(OSInfo::MajorMinorBuildToVersion(10, 0, 19045),
62             Version::WIN10_22H2);
63   EXPECT_EQ(OSInfo::MajorMinorBuildToVersion(10, 0, 19044),
64             Version::WIN10_21H2);
65   EXPECT_EQ(OSInfo::MajorMinorBuildToVersion(10, 0, 19043),
66             Version::WIN10_21H1);
67   EXPECT_EQ(OSInfo::MajorMinorBuildToVersion(10, 0, 19042),
68             Version::WIN10_20H2);
69   EXPECT_EQ(OSInfo::MajorMinorBuildToVersion(10, 0, 19041),
70             Version::WIN10_20H1);
71   EXPECT_EQ(OSInfo::MajorMinorBuildToVersion(10, 0, 18363),
72             Version::WIN10_19H2);
73   EXPECT_EQ(OSInfo::MajorMinorBuildToVersion(10, 0, 18362),
74             Version::WIN10_19H1);
75   EXPECT_EQ(OSInfo::MajorMinorBuildToVersion(10, 0, 17763), Version::WIN10_RS5);
76   EXPECT_EQ(OSInfo::MajorMinorBuildToVersion(10, 0, 17134), Version::WIN10_RS4);
77   EXPECT_EQ(OSInfo::MajorMinorBuildToVersion(10, 0, 16299), Version::WIN10_RS3);
78   EXPECT_EQ(OSInfo::MajorMinorBuildToVersion(10, 0, 15063), Version::WIN10_RS2);
79   EXPECT_EQ(OSInfo::MajorMinorBuildToVersion(10, 0, 14393), Version::WIN10_RS1);
80   EXPECT_EQ(OSInfo::MajorMinorBuildToVersion(10, 0, 10586), Version::WIN10_TH2);
81   EXPECT_EQ(OSInfo::MajorMinorBuildToVersion(10, 0, 10240), Version::WIN10);
82   EXPECT_EQ(OSInfo::MajorMinorBuildToVersion(10, 0, 0), Version::WIN10);
83   EXPECT_EQ(OSInfo::MajorMinorBuildToVersion(6, 3, 0), Version::WIN8_1);
84   EXPECT_EQ(OSInfo::MajorMinorBuildToVersion(6, 2, 0), Version::WIN8);
85   EXPECT_EQ(OSInfo::MajorMinorBuildToVersion(6, 1, 0), Version::WIN7);
86   EXPECT_EQ(OSInfo::MajorMinorBuildToVersion(6, 0, 0), Version::VISTA);
87   EXPECT_EQ(OSInfo::MajorMinorBuildToVersion(5, 3, 0), Version::SERVER_2003);
88   EXPECT_EQ(OSInfo::MajorMinorBuildToVersion(5, 2, 0), Version::SERVER_2003);
89   EXPECT_EQ(OSInfo::MajorMinorBuildToVersion(5, 1, 0), Version::XP);
90   EXPECT_EQ(OSInfo::MajorMinorBuildToVersion(5, 0, 0), Version::PRE_XP);
91   EXPECT_EQ(OSInfo::MajorMinorBuildToVersion(4, 0, 0), Version::PRE_XP);
92   EXPECT_EQ(OSInfo::MajorMinorBuildToVersion(3, 0, 0), Version::PRE_XP);
93   EXPECT_EQ(OSInfo::MajorMinorBuildToVersion(2, 0, 0), Version::PRE_XP);
94   EXPECT_EQ(OSInfo::MajorMinorBuildToVersion(1, 0, 0), Version::PRE_XP);
95   EXPECT_EQ(OSInfo::MajorMinorBuildToVersion(0, 0, 0), Version::PRE_XP);
96 
97 #if !DCHECK_IS_ON()
98   EXPECT_EQ(OSInfo::MajorMinorBuildToVersion(12, 0, 0), Version::WIN_LAST);
99   EXPECT_EQ(OSInfo::MajorMinorBuildToVersion(9, 0, 0), Version::WIN_LAST);
100   EXPECT_EQ(OSInfo::MajorMinorBuildToVersion(8, 0, 0), Version::WIN_LAST);
101   EXPECT_EQ(OSInfo::MajorMinorBuildToVersion(7, 0, 0), Version::WIN_LAST);
102   EXPECT_EQ(OSInfo::MajorMinorBuildToVersion(6, 4, 0), Version::WIN8_1);
103 #endif  // !DCHECK_IS_ON()
104 }
105 
106 // For more info on what processor information is defined, see:
107 // http://msdn.microsoft.com/en-us/library/b0084kay.aspx
TEST(OSInfo,GetWowStatusForProcess)108 TEST(OSInfo, GetWowStatusForProcess) {
109 #if defined(ARCH_CPU_64_BITS)
110   EXPECT_TRUE(OSInfo::GetInstance()->IsWowDisabled());
111 #elif defined(ARCH_CPU_X86)
112   if (OSInfo::GetArchitecture() == OSInfo::X86_ARCHITECTURE) {
113     EXPECT_TRUE(OSInfo::GetInstance()->IsWowDisabled());
114   } else if (OSInfo::GetArchitecture() == OSInfo::X64_ARCHITECTURE) {
115     EXPECT_TRUE(OSInfo::GetInstance()->IsWowX86OnAMD64());
116   } else {
117     // Currently, the only way to determine if a WOW emulation is
118     // running on an ARM64 device is via the function that the
119     // |IsWow*| helper functions rely on.  As such, it is not possible
120     // to separate out x86 running on ARM64 machines vs x86 running on
121     // other host machines.
122     EXPECT_TRUE(OSInfo::GetInstance()->IsWowX86OnARM64() ||
123                 OSInfo::GetInstance()->IsWowX86OnOther());
124   }
125 #else
126   ADD_FAILURE()
127       << "This test fails when we're using a process or host machine that is "
128          "not being considered by our helper functions.  If you're seeing this "
129          "error, please add a helper function for determining WOW emulation "
130          "for your process and host machine.";
131 #endif
132 }
133 
134 }  // namespace win
135 }  // namespace base
136