inetmib1: Support the MIB2 UDP table.
[wine] / dlls / inetmib1 / tests / main.c
1 /*
2  * Copyright 2008 Juan Lang
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18 #include <stdio.h>
19 #include <stdarg.h>
20 #include <windef.h>
21 #include <winbase.h>
22 #include <snmp.h>
23
24 #include "wine/test.h"
25
26 static HMODULE inetmib1;
27
28 static void testInit(void)
29 {
30     BOOL (WINAPI *pInit)(DWORD, HANDLE *, AsnObjectIdentifier *);
31     BOOL ret;
32     HANDLE event;
33     AsnObjectIdentifier oid;
34
35     pInit = (void *)GetProcAddress(inetmib1, "SnmpExtensionInit");
36     if (!pInit)
37     {
38         skip("no SnmpExtensionInit\n");
39         return;
40     }
41     /* Crash
42     ret = pInit(0, NULL, NULL);
43     ret = pInit(0, NULL, &oid);
44     ret = pInit(0, &event, NULL);
45      */
46     ret = pInit(0, &event, &oid);
47     ok(ret, "SnmpExtensionInit failed: %d\n", GetLastError());
48     ok(!strcmp("1.3.6.1.2.1.1", SnmpUtilOidToA(&oid)),
49         "Expected 1.3.6.1.2.1.1, got %s\n", SnmpUtilOidToA(&oid));
50 }
51
52 static void testQuery(void)
53 {
54     BOOL (WINAPI *pQuery)(BYTE, SnmpVarBindList *, AsnInteger32 *,
55         AsnInteger32 *);
56     BOOL ret, moreData;
57     SnmpVarBindList list;
58     AsnInteger32 error, index;
59     UINT bogus[] = { 1,2,3,4 };
60     UINT mib2System[] = { 1,3,6,1,2,1,1 };
61     UINT mib2If[] = { 1,3,6,1,2,1,2 };
62     UINT mib2IfTable[] = { 1,3,6,1,2,1,2,2 };
63     UINT mib2IfDescr[] = { 1,3,6,1,2,1,2,2,1,2 };
64     UINT mib2IfAdminStatus[] = { 1,3,6,1,2,1,2,2,1,7 };
65     UINT mib2IfOperStatus[] = { 1,3,6,1,2,1,2,2,1,8 };
66     UINT mib2IpAddr[] = { 1,3,6,1,2,1,4,20,1,1 };
67     UINT mib2IpRouteTable[] = { 1,3,6,1,2,1,4,21,1,1 };
68     UINT mib2UdpTable[] = { 1,3,6,1,2,1,7,5,1,1 };
69     SnmpVarBind vars[3], vars2[3];
70     UINT entry;
71
72     pQuery = (void *)GetProcAddress(inetmib1, "SnmpExtensionQuery");
73     if (!pQuery)
74     {
75         skip("couldn't find SnmpExtensionQuery\n");
76         return;
77     }
78     /* Crash
79     ret = pQuery(0, NULL, NULL, NULL);
80     ret = pQuery(0, NULL, &error, NULL);
81     ret = pQuery(0, NULL, NULL, &index);
82     ret = pQuery(0, &list, NULL, NULL);
83     ret = pQuery(0, &list, &error, NULL);
84      */
85
86     /* An empty list succeeds */
87     list.len = 0;
88     error = 0xdeadbeef;
89     index = 0xdeadbeef;
90     ret = pQuery(SNMP_PDU_GET, &list, &error, &index);
91     ok(ret, "SnmpExtensionQuery failed: %d\n", GetLastError());
92     ok(error == SNMP_ERRORSTATUS_NOERROR,
93         "expected SNMP_ERRORSTATUS_NOERROR, got %d\n", error);
94     ok(index == 0, "expected index 0, got %d\n", index);
95
96     /* Oddly enough, this "succeeds," even though the OID is clearly
97      * unsupported.
98      */
99     vars[0].name.idLength = sizeof(bogus) / sizeof(bogus[0]);
100     vars[0].name.ids = bogus;
101     vars[0].value.asnType = 0;
102     list.len = 1;
103     list.list = vars;
104     SetLastError(0xdeadbeef);
105     error = 0xdeadbeef;
106     index = 0xdeadbeef;
107     ret = pQuery(SNMP_PDU_GET, &list, &error, &index);
108     ok(ret, "SnmpExtensionQuery failed: %d\n", GetLastError());
109     ok(error == SNMP_ERRORSTATUS_NOERROR,
110         "expected SNMP_ERRORSTATUS_NOERROR, got %d\n", error);
111     ok(index == 0, "expected index 0, got %d\n", index);
112     /* The OID isn't changed either: */
113     ok(!strcmp("1.2.3.4", SnmpUtilOidToA(&vars[0].name)),
114         "expected 1.2.3.4, got %s\n", SnmpUtilOidToA(&vars[0].name));
115
116     /* The table is not an accessible variable, so it fails */
117     vars[0].name.idLength = sizeof(mib2IfTable) / sizeof(mib2IfTable[0]);
118     vars[0].name.ids = mib2IfTable;
119     SetLastError(0xdeadbeef);
120     error = 0xdeadbeef;
121     index = 0xdeadbeef;
122     ret = pQuery(SNMP_PDU_GET, &list, &error, &index);
123     ok(ret, "SnmpExtensionQuery failed: %d\n", GetLastError());
124     ok(error == SNMP_ERRORSTATUS_NOSUCHNAME,
125         "expected SNMP_ERRORSTATUS_NOSUCHNAME, got %d\n", error);
126     /* The index is 1-based rather than 0-based */
127     ok(index == 1, "expected index 1, got %d\n", index);
128
129     /* A Get fails on something that specifies a table (but not a particular
130      * entry in it)...
131      */
132     vars[0].name.idLength = sizeof(mib2IfDescr) / sizeof(mib2IfDescr[0]);
133     vars[0].name.ids = mib2IfDescr;
134     vars[1].name.idLength =
135         sizeof(mib2IfAdminStatus) / sizeof(mib2IfAdminStatus[0]);
136     vars[1].name.ids = mib2IfAdminStatus;
137     vars[2].name.idLength =
138         sizeof(mib2IfOperStatus) / sizeof(mib2IfOperStatus[0]);
139     vars[2].name.ids = mib2IfOperStatus;
140     list.len = 3;
141     SetLastError(0xdeadbeef);
142     error = 0xdeadbeef;
143     index = 0xdeadbeef;
144     ret = pQuery(SNMP_PDU_GET, &list, &error, &index);
145     ok(ret, "SnmpExtensionQuery failed: %d\n", GetLastError());
146     ok(error == SNMP_ERRORSTATUS_NOSUCHNAME,
147         "expected SNMP_ERRORSTATUS_NOSUCHNAME, got %d\n", error);
148     ok(index == 1, "expected index 1, got %d\n", index);
149     /* but a GetNext succeeds with the same values, because GetNext gets the
150      * entry after the specified OID, not the entry specified by it.  The
151      * successor to the table is the first entry in the table.
152      * The OIDs need to be allocated, because GetNext modifies them to indicate
153      * the end of data.
154      */
155     SnmpUtilOidCpy(&vars2[0].name, &vars[0].name);
156     SnmpUtilOidCpy(&vars2[1].name, &vars[1].name);
157     SnmpUtilOidCpy(&vars2[2].name, &vars[2].name);
158     list.list = vars2;
159     moreData = TRUE;
160     entry = 1;
161     do {
162         SetLastError(0xdeadbeef);
163         error = 0xdeadbeef;
164         index = 0xdeadbeef;
165         ret = pQuery(SNMP_PDU_GETNEXT, &list, &error, &index);
166         ok(ret, "SnmpExtensionQuery failed: %d\n", GetLastError());
167         ok(error == SNMP_ERRORSTATUS_NOERROR,
168             "expected SNMP_ERRORSTATUS_NOERROR, got %d\n", error);
169         ok(index == 0, "expected index 0, got %d\n", index);
170         if (!ret)
171             moreData = FALSE;
172         else if (error)
173             moreData = FALSE;
174         else if (SnmpUtilOidNCmp(&vars2[0].name, &vars[0].name,
175             vars[0].name.idLength))
176             moreData = FALSE;
177         else if (SnmpUtilOidNCmp(&vars2[1].name, &vars[1].name,
178             vars[1].name.idLength))
179             moreData = FALSE;
180         else if (SnmpUtilOidNCmp(&vars2[2].name, &vars[2].name,
181             vars[2].name.idLength))
182             moreData = FALSE;
183         if (moreData)
184         {
185             /* Check the OIDs.  For these types of values (display strings and
186              * integers) they increase by 1 for each element of the table.
187              */
188             ok(vars2[0].name.idLength == vars[0].name.idLength + 1,
189                 "expected length %d, got %d\n", vars[0].name.idLength + 1,
190                 vars2[0].name.idLength);
191             ok(vars2[0].name.ids[vars2[0].name.idLength - 1] == entry,
192                 "expected %d, got %d\n", entry,
193                 vars2[0].name.ids[vars2[0].name.idLength - 1]);
194             ok(vars2[1].name.idLength == vars[1].name.idLength + 1,
195                 "expected length %d, got %d\n", vars[1].name.idLength + 1,
196                 vars2[1].name.idLength);
197             ok(vars2[1].name.ids[vars2[1].name.idLength - 1] == entry,
198                 "expected %d, got %d\n", entry,
199                 vars2[1].name.ids[vars2[1].name.idLength - 1]);
200             ok(vars2[2].name.idLength == vars[2].name.idLength + 1,
201                 "expected length %d, got %d\n", vars[2].name.idLength + 1,
202                 vars2[2].name.idLength);
203             ok(vars2[2].name.ids[vars2[2].name.idLength - 1] == entry,
204                 "expected %d, got %d\n", entry,
205                 vars2[2].name.ids[vars2[2].name.idLength - 1]);
206             ++entry;
207             /* Check the types while we're at it */
208             ok(vars2[0].value.asnType == ASN_OCTETSTRING,
209                 "expected ASN_OCTETSTRING, got %02x\n", vars2[0].value.asnType);
210             ok(vars2[1].value.asnType == ASN_INTEGER,
211                 "expected ASN_INTEGER, got %02x\n", vars2[1].value.asnType);
212             ok(vars2[2].value.asnType == ASN_INTEGER,
213                 "expected ASN_INTEGER, got %02x\n", vars2[2].value.asnType);
214             /* Check that the operational status of an interface correctly
215              * follows the MIB2 definition of it, rather than the values
216              * defined for IPHlpApi's dwOperStatus field.
217              */
218             ok(vars2[2].value.asnValue.unsigned32 <= 2,
219                 "expected a value of 0, 1, or 2, got %u\n",
220                 vars2[2].value.asnValue.unsigned32);
221         }
222     } while (moreData);
223     SnmpUtilVarBindFree(&vars2[0]);
224     SnmpUtilVarBindFree(&vars2[1]);
225     SnmpUtilVarBindFree(&vars2[2]);
226
227     /* Even though SnmpExtensionInit says this DLL supports the MIB2 system
228      * variables, the first variable it returns a value for is the first
229      * interface.
230      */
231     vars[0].name.idLength = sizeof(mib2System) / sizeof(mib2System[0]);
232     vars[0].name.ids = mib2System;
233     SnmpUtilOidCpy(&vars2[0].name, &vars[0].name);
234     vars2[0].value.asnType = 0;
235     list.len = 1;
236     list.list = vars2;
237     moreData = TRUE;
238     ret = pQuery(SNMP_PDU_GETNEXT, &list, &error, &index);
239     ok(ret, "SnmpExtensionQuery failed: %d\n", GetLastError());
240     ok(error == SNMP_ERRORSTATUS_NOERROR,
241         "expected SNMP_ERRORSTATUS_NOERROR, got %d\n", error);
242     ok(index == 0, "expected index 0, got %d\n", index);
243     vars[0].name.idLength = sizeof(mib2If) / sizeof(mib2If[0]);
244     vars[0].name.ids = mib2If;
245     ok(!SnmpUtilOidNCmp(&vars2[0].name, &vars[0].name, vars[0].name.idLength),
246         "expected 1.3.6.1.2.1.2, got %s\n", SnmpUtilOidToA(&vars2[0].name));
247     SnmpUtilVarBindFree(&vars2[0]);
248
249     /* Check the type and OIDs of the IP address table */
250     vars[0].name.idLength = sizeof(mib2IpAddr) / sizeof(mib2IpAddr[0]);
251     vars[0].name.ids = mib2IpAddr;
252     SnmpUtilOidCpy(&vars2[0].name, &vars[0].name);
253     vars2[0].value.asnType = 0;
254     list.len = 1;
255     list.list = vars2;
256     moreData = TRUE;
257     do {
258         ret = pQuery(SNMP_PDU_GETNEXT, &list, &error, &index);
259         ok(ret, "SnmpExtensionQuery failed: %d\n", GetLastError());
260         ok(error == SNMP_ERRORSTATUS_NOERROR,
261             "expected SNMP_ERRORSTATUS_NOERROR, got %d\n", error);
262         ok(index == 0, "expected index 0, got %d\n", index);
263         if (!ret)
264             moreData = FALSE;
265         else if (error)
266             moreData = FALSE;
267         else if (SnmpUtilOidNCmp(&vars2[0].name, &vars[0].name,
268             vars[0].name.idLength))
269             moreData = FALSE;
270         if (moreData)
271         {
272             /* Make sure the size of the OID is right.
273              * FIXME: don't know if IPv6 addrs are shared with this table.
274              * Don't think so, but I'm not certain.
275              */
276             ok(vars2[0].name.idLength == vars[0].name.idLength + 4,
277                 "expected length %d, got %d\n", vars[0].name.idLength + 4,
278                 vars2[0].name.idLength);
279             /* Make sure the type is right */
280             ok(vars2[0].value.asnType == ASN_IPADDRESS,
281                 "expected type ASN_IPADDRESS, got %02x\n",
282                 vars2[0].value.asnType);
283             if (vars2[0].value.asnType == ASN_IPADDRESS)
284             {
285                 UINT i;
286
287                 /* This looks uglier than it is:  the base OID for the IP
288                  * address, 1.3.6.1.2.1.4.20.1.1, is appended with the IP
289                  * address of the entry.  So e.g. the loopback address is
290                  * identified in MIB2 as 1.3.6.1.2.1.4.20.1.1.127.0.0.1
291                  */
292                 for (i = 0; i < vars2[0].value.asnValue.address.length; i++)
293                 {
294                     ok(vars2[0].value.asnValue.address.stream[i] ==
295                         vars2[0].name.ids[vars2[0].name.idLength - 4 + i],
296                         "expected ident byte %d to be %d, got %d\n", i,
297                         vars2[0].value.asnValue.address.stream[i],
298                         vars2[0].name.ids[vars2[0].name.idLength - 4 + i]);
299                 }
300             }
301         }
302     } while (moreData);
303     SnmpUtilVarBindFree(&vars2[0]);
304
305     /* Check the type and OIDs of the IP route table */
306     vars[0].name.idLength = DEFINE_SIZEOF(mib2IpRouteTable);
307     vars[0].name.ids = mib2IpRouteTable;
308     SnmpUtilOidCpy(&vars2[0].name, &vars[0].name);
309     vars2[0].value.asnType = 0;
310     list.len = 1;
311     list.list = vars2;
312     moreData = TRUE;
313     do {
314         ret = pQuery(SNMP_PDU_GETNEXT, &list, &error, &index);
315         ok(ret, "SnmpExtensionQuery failed: %d\n", GetLastError());
316         ok(error == SNMP_ERRORSTATUS_NOERROR,
317             "expected SNMP_ERRORSTATUS_NOERROR, got %d\n", error);
318         ok(index == 0, "expected index 0, got %d\n", index);
319         if (!ret)
320             moreData = FALSE;
321         else if (error)
322             moreData = FALSE;
323         else if (SnmpUtilOidNCmp(&vars2[0].name, &vars[0].name,
324             vars[0].name.idLength))
325             moreData = FALSE;
326         if (moreData)
327         {
328             /* Make sure the size of the OID is right.
329              * FIXME: don't know if IPv6 addrs are shared with this table.
330              * Don't think so, but I'm not certain.
331              */
332             ok(vars2[0].name.idLength = vars[0].name.idLength + 4,
333                 "expected length %d, got %d\n", vars[0].name.idLength + 4,
334                 vars2[0].name.idLength);
335             /* Make sure the type is right */
336             ok(vars2[0].value.asnType == ASN_IPADDRESS,
337                 "expected type ASN_IPADDRESS, got %02x\n",
338                 vars2[0].value.asnType);
339             if (vars2[0].value.asnType == ASN_IPADDRESS)
340             {
341                 UINT i;
342
343                 /* The base OID for the route table, 1.3.6.1.2.1.4.21.1.1, is
344                  * appended with the dest IP address of the entry.  So e.g. a
345                  * route entry for 224.0.0.0 is identified in MIB2 as
346                  * 1.3.6.1.2.1.4.21.1.1.224.0.0.0
347                  */
348                 for (i = 0; i < vars2[0].value.asnValue.address.length; i++)
349                 {
350                     ok(vars2[0].value.asnValue.address.stream[i] ==
351                         vars2[0].name.ids[vars2[0].name.idLength - 4 + i],
352                         "expected ident byte %d to be %d, got %d\n", i,
353                         vars2[0].value.asnValue.address.stream[i],
354                         vars2[0].name.ids[vars2[0].name.idLength - 4 + i]);
355                 }
356             }
357         }
358     } while (moreData);
359     SnmpUtilVarBindFree(&vars2[0]);
360
361     /* Check the type and OIDs of the UDP table */
362     vars[0].name.idLength = DEFINE_SIZEOF(mib2UdpTable);
363     vars[0].name.ids = mib2UdpTable;
364     SnmpUtilOidCpy(&vars2[0].name, &vars[0].name);
365     vars2[0].value.asnType = 0;
366     list.len = 1;
367     list.list = vars2;
368     moreData = TRUE;
369     do {
370         ret = pQuery(SNMP_PDU_GETNEXT, &list, &error, &index);
371         ok(ret, "SnmpExtensionQuery failed: %d\n", GetLastError());
372         /* FIXME:  error and index aren't checked here because the UDP table is
373          * the last OID currently supported by Wine, so the last GetNext fails.
374          * todo_wine is also not effective because it will succeed for all but
375          * the last GetNext.  Remove the if (0) if any later OID is supported
376          * by Wine.
377          */
378         if (0) {
379         ok(error == SNMP_ERRORSTATUS_NOERROR,
380             "expected SNMP_ERRORSTATUS_NOERROR, got %d\n", error);
381         ok(index == 0, "expected index 0, got %d\n", index);
382         }
383         if (!ret)
384             moreData = FALSE;
385         else if (error)
386             moreData = FALSE;
387         else if (SnmpUtilOidNCmp(&vars2[0].name, &vars[0].name,
388             vars[0].name.idLength))
389             moreData = FALSE;
390         if (moreData)
391         {
392             /* Make sure the size of the OID is right. */
393             ok(vars2[0].name.idLength == vars[0].name.idLength + 5,
394                 "expected length %d, got %d\n", vars[0].name.idLength + 5,
395                 vars2[0].name.idLength);
396             /* Make sure the type is right */
397             ok(vars2[0].value.asnType == ASN_IPADDRESS,
398                 "expected type ASN_IPADDRESS, got %02x\n",
399                 vars2[0].value.asnType);
400             if (vars2[0].value.asnType == ASN_IPADDRESS)
401             {
402                 UINT i;
403
404                 /* Again with the ugly:  the base OID for the UDP table,
405                  * 1.3.6.1.2.1.7.5.1, is appended with the local IP address and
406                  * port number of the entry.  So e.g. an entry for
407                  * 192.168.1.1:4000 is identified in MIB2 as
408                  * 1.3.6.1.2.1.7.5.1.192.168.1.1.4000
409                  */
410                 for (i = 0; i < vars2[0].value.asnValue.address.length; i++)
411                 {
412                     ok(vars2[0].value.asnValue.address.stream[i] ==
413                         vars2[0].name.ids[vars2[0].name.idLength - 5 + i],
414                         "expected ident byte %d to be %d, got %d\n", i,
415                         vars2[0].value.asnValue.address.stream[i],
416                         vars2[0].name.ids[vars2[0].name.idLength - 5 + i]);
417                 }
418             }
419         }
420     } while (moreData);
421     SnmpUtilVarBindFree(&vars2[0]);
422 }
423
424 START_TEST(main)
425 {
426     inetmib1 = LoadLibraryA("inetmib1");
427     testInit();
428     testQuery();
429 }