dnsapi: Implement and test DnsRecordSetDetach.
[wine] / dlls / dnsapi / tests / record.c
1 /*
2  * Tests for record handling functions
3  *
4  * Copyright 2006 Hans Leidekker
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include <stdarg.h>
22 #include <stdio.h>
23
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winnls.h"
27 #include "windns.h"
28
29 #include "wine/test.h"
30
31 static HMODULE dnsapi;
32
33 static BOOL        (WINAPI *pDnsRecordCompare)(PDNS_RECORD,PDNS_RECORD);
34 static BOOL        (WINAPI *pDnsRecordSetCompare)(PDNS_RECORD,PDNS_RECORD,PDNS_RECORD*,PDNS_RECORD*);
35 static DNS_RECORD* (WINAPI *pDnsRecordSetDetach)(PDNS_RECORD);
36
37 #define GETFUNCPTR(func) p##func = (void *)GetProcAddress( dnsapi, #func ); \
38     if (!p##func) return FALSE;
39
40 static BOOL init_function_ptrs( void )
41 {
42     GETFUNCPTR( DnsRecordCompare )
43     GETFUNCPTR( DnsRecordSetCompare )
44     GETFUNCPTR( DnsRecordSetDetach )
45     return TRUE;
46 }
47
48 static char name1[] = "localhost";
49 static char name2[] = "LOCALHOST";
50
51 static DNS_RECORDA r1 = { NULL, name1, DNS_TYPE_A, sizeof(DNS_A_DATA), { 0 }, 1200, 0, { { 0xffffffff } } };
52 static DNS_RECORDA r2 = { NULL, name1, DNS_TYPE_A, sizeof(DNS_A_DATA), { 0 }, 1200, 0, { { 0xffffffff } } };
53 static DNS_RECORDA r3 = { NULL, name1, DNS_TYPE_A, sizeof(DNS_A_DATA), { 0 }, 1200, 0, { { 0xffffffff } } };
54
55 static void test_DnsRecordCompare( void )
56 {
57     ok( pDnsRecordCompare( (PDNS_RECORD)&r1, (PDNS_RECORD)&r1 ) == TRUE, "failed unexpectedly\n" );
58
59     r2.pName = name2;
60     ok( pDnsRecordCompare( (PDNS_RECORD)&r1, (PDNS_RECORD)&r2 ) == TRUE, "failed unexpectedly\n" );
61
62     r2.Flags.S.CharSet = DnsCharSetUnicode;
63     ok( pDnsRecordCompare( (PDNS_RECORD)&r1, (PDNS_RECORD)&r2 ) == FALSE, "succeeded unexpectedly\n" );
64
65     r2.Flags.S.CharSet = DnsCharSetAnsi;
66     ok( pDnsRecordCompare( (PDNS_RECORD)&r1, (PDNS_RECORD)&r2 ) == FALSE, "succeeded unexpectedly\n" );
67
68     r1.Flags.S.CharSet = DnsCharSetAnsi;
69     ok( pDnsRecordCompare( (PDNS_RECORD)&r1, (PDNS_RECORD)&r2 ) == TRUE, "failed unexpectedly\n" );
70
71     r1.dwTtl = 0;
72     ok( pDnsRecordCompare( (PDNS_RECORD)&r1, (PDNS_RECORD)&r2 ) == TRUE, "failed unexpectedly\n" );
73
74     r2.Data.A.IpAddress = 0;
75     ok( pDnsRecordCompare( (PDNS_RECORD)&r1, (PDNS_RECORD)&r2 ) == FALSE, "succeeded unexpectedly\n" );
76 }
77
78 static void test_DnsRecordSetCompare( void )
79 {
80     DNS_RECORD *diff1;
81     DNS_RECORD *diff2;
82     DNS_RRSET rr1, rr2;
83
84     r1.Flags.DW = 0x2019;
85     r2.Flags.DW = 0x2019;
86     r2.Data.A.IpAddress = 0xffffffff;
87
88     DNS_RRSET_INIT( rr1 );
89     DNS_RRSET_INIT( rr2 );
90
91     DNS_RRSET_ADD( rr1, &r1 );
92     DNS_RRSET_ADD( rr2, &r2 );
93
94     DNS_RRSET_TERMINATE( rr1 );
95     DNS_RRSET_TERMINATE( rr2 );
96
97     ok( pDnsRecordSetCompare( NULL, NULL, NULL, NULL ) == FALSE, "succeeded unexpectedly\n" );
98     ok( pDnsRecordSetCompare( rr1.pFirstRR, NULL, NULL, NULL ) == FALSE, "succeeded unexpectedly\n" );
99     ok( pDnsRecordSetCompare( NULL, rr2.pFirstRR, NULL, NULL ) == FALSE, "succeeded unexpectedly\n" );
100
101     diff1 = NULL;
102     diff2 = NULL;
103
104     ok( pDnsRecordSetCompare( NULL, NULL, &diff1, &diff2 ) == FALSE, "succeeded unexpectedly\n" );
105     ok( diff1 == NULL && diff2 == NULL, "unexpected result: %p, %p\n", diff1, diff2 );
106
107     ok( pDnsRecordSetCompare( rr1.pFirstRR, NULL, &diff1, &diff2 ) == FALSE, "succeeded unexpectedly\n" );
108     ok( diff1 != NULL && diff2 == NULL, "unexpected result: %p, %p\n", diff1, diff2 );
109
110     ok( pDnsRecordSetCompare( NULL, rr2.pFirstRR, &diff1, &diff2 ) == FALSE, "succeeded unexpectedly\n" );
111     ok( diff1 == NULL && diff2 != NULL, "unexpected result: %p, %p\n", diff1, diff2 );
112
113     ok( pDnsRecordSetCompare( rr1.pFirstRR, rr2.pFirstRR, NULL, &diff2 ) == TRUE, "failed unexpectedly\n" );
114     ok( diff2 == NULL, "unexpected result: %p\n", diff2 );
115
116     ok( pDnsRecordSetCompare( rr1.pFirstRR, rr2.pFirstRR, &diff1, NULL ) == TRUE, "failed unexpectedly\n" );
117     ok( diff1 == NULL, "unexpected result: %p\n", diff1 );
118
119     ok( pDnsRecordSetCompare( rr1.pFirstRR, rr2.pFirstRR, &diff1, &diff2 ) == TRUE, "failed unexpectedly\n" );
120     ok( diff1 == NULL && diff2 == NULL, "unexpected result: %p, %p\n", diff1, diff2 );
121
122     r2.Data.A.IpAddress = 0;
123
124     ok( pDnsRecordSetCompare( rr1.pFirstRR, rr2.pFirstRR, NULL, &diff2 ) == FALSE, "succeeded unexpectedly\n" );
125     ok( pDnsRecordSetCompare( rr1.pFirstRR, rr2.pFirstRR, &diff1, NULL ) == FALSE, "succeeded unexpectedly\n" );
126     ok( pDnsRecordSetCompare( rr1.pFirstRR, rr2.pFirstRR, &diff1, &diff2 ) == FALSE, "succeeded unexpectedly\n" );
127 }
128
129 static void test_DnsRecordSetDetach( void )
130 {
131     DNS_RRSET rr;
132     DNS_RECORDA *r, *s;
133
134     DNS_RRSET_INIT( rr );
135     DNS_RRSET_ADD( rr, &r1 );
136     DNS_RRSET_ADD( rr, &r2 );
137     DNS_RRSET_ADD( rr, &r3 );
138     DNS_RRSET_TERMINATE( rr );
139
140     ok( !pDnsRecordSetDetach( NULL ), "succeeded unexpectedly\n" );
141
142     r = rr.pFirstRR;
143     s = pDnsRecordSetDetach( r );
144
145     ok( s == &r3, "failed unexpectedly: got %p, expected %p\n", s, &r3 );
146     ok( r == &r1, "failed unexpectedly: got %p, expected %p\n", r, &r1 );
147     ok( !r2.pNext, "failed unexpectedly\n" );
148 }
149
150 START_TEST(record)
151 {
152     dnsapi = LoadLibraryA( "dnsapi.dll" );
153     if (!dnsapi) return;
154
155     if (!init_function_ptrs())
156     {
157         FreeLibrary( dnsapi );
158         return;
159     }
160
161     test_DnsRecordCompare();
162     test_DnsRecordSetCompare();
163     test_DnsRecordSetDetach();
164
165     FreeLibrary( dnsapi );
166 }