xref: /aosp_15_r20/external/cronet/base/win/scoped_localalloc_unittest.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2021 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/scoped_localalloc.h"
6 
7 #include <windows.h>
8 
9 #include <shellapi.h>
10 
11 #include "testing/gtest/include/gtest/gtest.h"
12 
13 namespace base {
14 namespace win {
15 
TEST(ScopedLocalAlloc,SimpleUsage)16 TEST(ScopedLocalAlloc, SimpleUsage) {
17   ScopedLocalAlloc scoped_local_alloc(::LocalAlloc(LMEM_FIXED, 0x1000));
18   EXPECT_TRUE(scoped_local_alloc);
19   scoped_local_alloc.reset();
20   EXPECT_FALSE(scoped_local_alloc);
21 
22   std::wstring input_command_line = L"c:\\test\\process.exe --p1=1";
23   int num_args = 0;
24   base::win::ScopedLocalAllocTyped<wchar_t*> argv(
25       ::CommandLineToArgvW(&input_command_line[0], &num_args));
26   EXPECT_TRUE(argv);
27   EXPECT_STREQ(argv.get()[0], L"c:\\test\\process.exe");
28   argv.reset();
29   EXPECT_FALSE(argv);
30 }
31 
TEST(ScopedLocalAlloc,Transfer)32 TEST(ScopedLocalAlloc, Transfer) {
33   HLOCAL ptr = ::LocalAlloc(LMEM_FIXED, 0x1000);
34   ASSERT_TRUE(ptr);
35   ScopedLocalAlloc scoped_ptr = TakeLocalAlloc(ptr);
36   EXPECT_TRUE(scoped_ptr);
37   EXPECT_FALSE(ptr);
38   scoped_ptr.reset();
39   EXPECT_FALSE(scoped_ptr);
40 
41   wchar_t* str_ptr = static_cast<wchar_t*>(::LocalAlloc(LMEM_FIXED, 0x1000));
42   ASSERT_TRUE(str_ptr);
43   ScopedLocalAllocTyped<wchar_t> scoped_str_ptr = TakeLocalAlloc(str_ptr);
44   EXPECT_TRUE(scoped_str_ptr);
45   EXPECT_FALSE(str_ptr);
46   scoped_str_ptr.reset();
47   EXPECT_FALSE(scoped_str_ptr);
48 }
49 
50 }  // namespace win
51 }  // namespace base
52