1 /* 2 * PROJECT: ReactOS api tests 3 * LICENSE: GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+) 4 * PURPOSE: Test for CComBSTR 5 * COPYRIGHT: Copyright 2015-2017 Mark Jansen (mark.jansen@reactos.org) 6 */ 7 8 #include <apitest.h> 9 #include <atlbase.h> 10 11 #include "resource.h" 12 13 #define verify_str (winetest_set_location(__FILE__, __LINE__), 0) ? (void)0 : verify_str_imp 14 #define verify_str2 (winetest_set_location(__FILE__, __LINE__), 0) ? (void)0 : verify_str_imp2 15 16 17 static void verify_str_imp2(const CComBSTR& comstr, PCWSTR expected, size_t ExpectedLength) 18 { 19 BSTR str = (BSTR)comstr; 20 if (expected || ExpectedLength) 21 { 22 winetest_ok(str != NULL, "Expected str to be a valid pointer\n"); 23 if (str) 24 { 25 if (expected) 26 { 27 winetest_ok(!wcscmp(str, expected), "Expected the string to be '%s', was '%s'\n", wine_dbgstr_w(expected), wine_dbgstr_w(str)); 28 } 29 size_t Length = comstr.Length(); 30 winetest_ok(Length == ExpectedLength, "Expected Length to be %u, was: %u\n", ExpectedLength, Length); 31 Length = comstr.ByteLength(); 32 ExpectedLength *= sizeof(WCHAR); 33 winetest_ok(Length == ExpectedLength, "Expected ByteLength to be %u, was: %u\n", ExpectedLength, Length); 34 } 35 } 36 else 37 { 38 winetest_ok(str == NULL || str[0] == '\0', "Expected str to be empty, was: '%s'\n", wine_dbgstr_w(str)); 39 } 40 } 41 42 static void verify_str_imp(const CComBSTR& comstr, PCWSTR expected) 43 { 44 verify_str_imp2(comstr, expected, expected ? wcslen(expected) : 0); 45 } 46 47 void test_construction() 48 { 49 CComBSTR empty1, empty2; 50 CComBSTR happyW(L"I am a happy BSTR"); 51 CComBSTR happyA("I am a happy BSTR"); 52 CComBSTR happyW4(4, L"I am a happy BSTR"); 53 CComBSTR fromlen1(1), fromlen10(10); 54 CComBSTR fromBSTRW(happyW), fromBSTRA(happyA), fromBSTRW4(happyW4); 55 CComBSTR fromBSTRlen1(fromlen1), fromBSTRlen10(fromlen10); 56 57 verify_str(empty1, NULL); 58 verify_str(empty2, NULL); 59 verify_str(happyW, L"I am a happy BSTR"); 60 verify_str(happyA, L"I am a happy BSTR"); 61 verify_str(happyW4, L"I am"); 62 verify_str2(fromlen1, NULL, 1); 63 verify_str2(fromlen10, NULL, 10); 64 verify_str(fromBSTRW, L"I am a happy BSTR"); 65 verify_str(fromBSTRA, L"I am a happy BSTR"); 66 verify_str(fromBSTRW4, L"I am"); 67 verify_str2(fromBSTRlen1, NULL, 1); 68 verify_str2(fromBSTRlen10, NULL, 10); 69 } 70 71 void test_copyassignment() 72 { 73 CComBSTR happy(L"I am a happy BSTR"), empty, odd; 74 CComBSTR happyCopy1, happyCopy2, emptyCopy, oddCopy; 75 76 odd = ::SysAllocStringByteLen("aaaaa", 3); 77 78 happyCopy1 = happy.Copy(); 79 happyCopy2 = happy; // Calls happyW.Copy() 80 emptyCopy = empty.Copy(); 81 oddCopy = odd.Copy(); 82 83 verify_str(happy, L"I am a happy BSTR"); 84 verify_str(empty, NULL); 85 verify_str2(odd, L"\u6161a", 2); 86 verify_str(happyCopy1, L"I am a happy BSTR"); 87 verify_str(happyCopy2, L"I am a happy BSTR"); 88 verify_str(emptyCopy, NULL); 89 verify_str2(oddCopy, L"\u6161a", 2); 90 ok((BSTR)happy != (BSTR)happyCopy1, "Expected pointers to be different\n"); 91 ok((BSTR)happy != (BSTR)happyCopy2, "Expected pointers to be different\n"); 92 93 94 happyCopy1 = (LPCOLESTR)NULL; 95 happyCopy2 = (LPCSTR)NULL; 96 97 verify_str(happyCopy1, NULL); 98 verify_str(happyCopy2, NULL); 99 100 HRESULT hr = happy.CopyTo(&happyCopy1); 101 ok(hr == S_OK, "Expected hr to be E_POINTER, was: %08lx\n", hr); 102 103 #if 0 104 // This asserts 105 hr = happy.CopyTo((BSTR*)NULL); 106 ok(hr == E_POINTER, "Expected hr to be E_POINTER, was: %u\n"); 107 #endif 108 109 BSTR RawPtr = ::SysAllocString(L"TEST--"); 110 happy.Attach(RawPtr); 111 ok_ptr(happy.m_str, RawPtr); 112 verify_str(happy, L"TEST--"); 113 happyCopy1.Attach(happy.Detach()); 114 ok_ptr(happyCopy1.m_str, RawPtr); 115 verify_str(happyCopy1, L"TEST--"); 116 ok_ptr(happy.m_str, NULL); 117 } 118 119 void test_fromguid() 120 { 121 GUID guid = { 0x12345678, 0x9abc, 0xdef0, { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0} }; 122 CComBSTR fromGuid(guid), empty; 123 verify_str(fromGuid, L"{12345678-9ABC-DEF0-1234-56789ABCDEF0}"); 124 verify_str(empty, NULL); 125 empty = fromGuid; 126 verify_str(empty, L"{12345678-9ABC-DEF0-1234-56789ABCDEF0}"); 127 } 128 129 void test_loadres() 130 { 131 CComBSTR test1, test2, test3; 132 HMODULE mod = GetModuleHandle(NULL); 133 134 ok(true == test1.LoadString(mod, IDS_TEST1), "Expected LoadString to succeed\n"); 135 ok(true == test2.LoadString(mod, IDS_TEST2), "Expected LoadString to succeed\n"); 136 ok(false == test3.LoadString(mod, IDS_TEST2 + 1), "Expected LoadString to fail\n"); 137 138 verify_str(test1, L"Test string one."); 139 verify_str(test2, L"I am a happy BSTR"); 140 verify_str(test3, NULL); 141 } 142 143 START_TEST(CComBSTR) 144 { 145 test_construction(); 146 test_copyassignment(); 147 test_fromguid(); 148 test_loadres(); 149 } 150