1 : // Copyright 2012 Google Inc. All Rights Reserved.
2 : //
3 : // Licensed under the Apache License, Version 2.0 (the "License");
4 : // you may not use this file except in compliance with the License.
5 : // You may obtain a copy of the License at
6 : //
7 : // http://www.apache.org/licenses/LICENSE-2.0
8 : //
9 : // Unless required by applicable law or agreed to in writing, software
10 : // distributed under the License is distributed on an "AS IS" BASIS,
11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : // See the License for the specific language governing permissions and
13 : // limitations under the License.
14 :
15 : #include "syzygy/trace/service/process_info.h"
16 :
17 : #include <psapi.h>
18 :
19 : #include "gtest/gtest.h"
20 : #include "syzygy/pe/pe_file.h"
21 :
22 : namespace trace {
23 : namespace service {
24 :
25 : namespace {
26 :
27 : class ScopedEnvironment {
28 : public:
29 E : ScopedEnvironment() {
30 E : env_ = ::GetEnvironmentStrings();
31 E : DCHECK(env_ != NULL);
32 E : }
33 :
34 E : ~ScopedEnvironment() {
35 E : ::FreeEnvironmentStrings(env_);
36 E : }
37 :
38 E : const wchar_t* Get() { return env_; }
39 :
40 : private:
41 : wchar_t* env_;
42 : };
43 :
44 : } // namespace
45 :
46 E : TEST(ProcessInfoTest, CurrentProcess) {
47 E : HANDLE process = ::GetCurrentProcess();
48 E : ASSERT_TRUE(process != NULL);
49 :
50 E : HMODULE module = GetModuleHandle(NULL);
51 E : ASSERT_TRUE(module != NULL);
52 :
53 : MODULEINFO module_info;
54 : ASSERT_TRUE(::GetModuleInformation(process, module, &module_info,
55 E : sizeof(module_info)));
56 :
57 : wchar_t executable_path[MAX_PATH];
58 : DWORD length = ::GetModuleFileName(module, &executable_path[0],
59 E : arraysize(executable_path));
60 E : ASSERT_TRUE(length != 0);
61 E : ASSERT_LT(length, arraysize(executable_path));
62 :
63 E : ScopedEnvironment env;
64 E : ASSERT_TRUE(env.Get() != NULL);
65 :
66 E : pe::PEFile pe_file;
67 E : ASSERT_TRUE(pe_file.Init(base::FilePath(executable_path)));
68 E : pe::PEFile::Signature pe_sig;
69 E : pe_file.GetSignature(&pe_sig);
70 :
71 E : ProcessInfo process_info;
72 E : EXPECT_TRUE(process_info.Initialize(::GetCurrentProcessId()));
73 :
74 E : EXPECT_STREQ(process_info.command_line.c_str(), ::GetCommandLineW());
75 E : EXPECT_STREQ(process_info.executable_path.value().c_str(), executable_path);
76 : EXPECT_EQ(
77 : reinterpret_cast<void*>(process_info.exe_base_address),
78 E : module_info.lpBaseOfDll);
79 E : EXPECT_EQ(process_info.exe_image_size, module_info.SizeOfImage);
80 E : EXPECT_EQ(process_info.exe_checksum, pe_sig.module_checksum);
81 E : EXPECT_EQ(process_info.exe_time_date_stamp, pe_sig.module_time_date_stamp);
82 :
83 E : EXPECT_LE(2u, process_info.environment.size());
84 E : EXPECT_EQ(0, *(process_info.environment.end() - 2));
85 E : EXPECT_EQ(0, *(process_info.environment.end() - 1));
86 : EXPECT_EQ(0, memcmp(env.Get(), &process_info.environment[0],
87 E : process_info.environment.size()));
88 :
89 E : OSVERSIONINFOEX os_version_info = {};
90 E : os_version_info.dwOSVersionInfoSize = sizeof(os_version_info);
91 : ASSERT_TRUE(::GetVersionEx(
92 E : reinterpret_cast<OSVERSIONINFO*>(&os_version_info)));
93 : EXPECT_EQ(0u, ::memcmp(&os_version_info, &process_info.os_version_info,
94 E : sizeof(os_version_info)));
95 :
96 E : SYSTEM_INFO system_info = {};
97 E : ::GetSystemInfo(&system_info);
98 : EXPECT_EQ(0u, ::memcmp(&system_info, &process_info.system_info,
99 E : sizeof(system_info)));
100 :
101 E : MEMORYSTATUSEX memory_status = {};
102 E : memory_status.dwLength = sizeof(memory_status);
103 E : ASSERT_TRUE(::GlobalMemoryStatusEx(&memory_status));
104 : EXPECT_EQ(memory_status.ullTotalPhys,
105 E : process_info.memory_status.ullTotalPhys);
106 :
107 E : process_info.Reset();
108 E : EXPECT_EQ(0, process_info.process_id);
109 E : EXPECT_FALSE(process_info.process_handle.IsValid());
110 E : EXPECT_TRUE(process_info.executable_path.empty());
111 E : EXPECT_TRUE(process_info.command_line.empty());
112 E : EXPECT_EQ(0, process_info.environment.size());
113 E : EXPECT_EQ(0, process_info.exe_base_address);
114 E : EXPECT_EQ(0, process_info.exe_image_size);
115 E : EXPECT_EQ(0, process_info.exe_checksum);
116 E : EXPECT_EQ(0, process_info.exe_time_date_stamp);
117 E : }
118 :
119 : } // namespace trace::service
120 : } // namespace trace
|