shlwapi: Add some tests for AssocQueryStringW.
[wine] / dlls / shlwapi / tests / assoc.c
1 /* Unit test suite for SHLWAPI IQueryAssociations functions
2  *
3  * Copyright 2008 Google (Lei Zhang)
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18  */
19
20 #include <stdarg.h>
21
22 #include "wine/test.h"
23 #include "shlwapi.h"
24
25 #define expect(expected, got) ok ( expected == got, "Expected %d, got %d\n", expected, got)
26 #define expect_hr(expected, got) ok ( expected == got, "Expected %08x, got %08x\n", expected, got)
27
28 /* Every version of Windows with IE should have this association? */
29 static const WCHAR dotHtml[] = { '.','h','t','m','l',0 };
30 static const WCHAR badBad[] = { 'b','a','d','b','a','d',0 };
31 static const WCHAR dotBad[] = { '.','b','a','d',0 };
32 static const WCHAR open[] = { 'o','p','e','n',0 };
33 static const WCHAR invalid[] = { 'i','n','v','a','l','i','d',0 };
34
35 static void test_getstring_bad(void)
36 {
37     HRESULT hr;
38     DWORD len;
39
40     hr = AssocQueryStringW(0, ASSOCSTR_EXECUTABLE, NULL, open, NULL, &len);
41     todo_wine expect_hr(E_INVALIDARG, hr);
42     hr = AssocQueryStringW(0, ASSOCSTR_EXECUTABLE, badBad, open, NULL, &len);
43     todo_wine expect_hr(E_FAIL, hr);
44     hr = AssocQueryStringW(0, ASSOCSTR_EXECUTABLE, dotBad, open, NULL, &len);
45     todo_wine expect_hr(E_FAIL, hr);
46     hr = AssocQueryStringW(0, ASSOCSTR_EXECUTABLE, dotHtml, invalid, NULL,
47                            &len);
48     todo_wine expect_hr(0x80070002, hr); /* NOT FOUND */
49     hr = AssocQueryStringW(0, ASSOCSTR_EXECUTABLE, dotHtml, open, NULL, NULL);
50     todo_wine expect_hr(E_UNEXPECTED, hr);
51
52     hr = AssocQueryStringW(0, ASSOCSTR_FRIENDLYAPPNAME, NULL, open, NULL, &len);
53     todo_wine expect_hr(E_INVALIDARG, hr);
54     hr = AssocQueryStringW(0, ASSOCSTR_FRIENDLYAPPNAME, badBad, open, NULL,
55                            &len);
56     todo_wine expect_hr(E_FAIL, hr);
57     hr = AssocQueryStringW(0, ASSOCSTR_FRIENDLYAPPNAME, dotBad, open, NULL,
58                            &len);
59     todo_wine expect_hr(E_FAIL, hr);
60     hr = AssocQueryStringW(0, ASSOCSTR_FRIENDLYAPPNAME, dotHtml, invalid, NULL,
61                            &len);
62     todo_wine expect_hr(0x80070002, hr); /* NOT FOUND */
63     hr = AssocQueryStringW(0, ASSOCSTR_FRIENDLYAPPNAME, dotHtml, open, NULL,
64                            NULL);
65     todo_wine expect_hr(E_UNEXPECTED, hr);
66 }
67
68 static void test_getstring_basic(void)
69 {
70     HRESULT hr;
71     WCHAR * friendlyName;
72     WCHAR * executableName;
73     DWORD len, len2, slen;
74
75     hr = AssocQueryStringW(0, ASSOCSTR_EXECUTABLE, dotHtml, open, NULL, &len);
76     todo_wine expect_hr(S_FALSE, hr);
77     if (hr != S_FALSE)
78     {
79         skip("failed to get initial len\n");
80         return;
81     }
82
83     executableName = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
84                                len * sizeof(WCHAR));
85     if (!executableName)
86     {
87         skip("failed to allocate memory\n");
88         return;
89     }
90
91     len2 = len;
92     hr = AssocQueryStringW(0, ASSOCSTR_EXECUTABLE, dotHtml, open,
93                            executableName, &len2);
94     expect_hr(S_OK, hr);
95     slen = lstrlenW(executableName) + 1;
96     expect(len, len2);
97     expect(len, slen);
98
99     hr = AssocQueryStringW(0, ASSOCSTR_FRIENDLYAPPNAME, dotHtml, open, NULL,
100                            &len);
101     expect_hr(S_FALSE, hr);
102     if (hr != S_FALSE)
103     {
104         HeapFree(GetProcessHeap(), 0, executableName);
105         skip("failed to get initial len\n");
106         return;
107     }
108
109     friendlyName = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
110                                len * sizeof(WCHAR));
111     if (!friendlyName)
112     {
113         HeapFree(GetProcessHeap(), 0, executableName);
114         skip("failed to allocate memory\n");
115         return;
116     }
117
118     len2 = len;
119     hr = AssocQueryStringW(0, ASSOCSTR_FRIENDLYAPPNAME, dotHtml, open,
120                            friendlyName, &len2);
121     expect_hr(S_OK, hr);
122     slen = lstrlenW(friendlyName) + 1;
123     expect(len, len2);
124     expect(len, slen);
125
126     HeapFree(GetProcessHeap(), 0, executableName);
127     HeapFree(GetProcessHeap(), 0, friendlyName);
128 }
129
130 START_TEST(assoc)
131 {
132     test_getstring_bad();
133     test_getstring_basic();
134 }