b6df8dc95d2f07fd0c9dd2b286fb3a0ddfe7e6f4
[tridge/openchange.git] / trunk / utils / mapitest / modules / module_nspi.c
1 /*
2    Stand-alone MAPI testsuite
3
4    OpenChange Project - NSPI tests
5
6    Copyright (C) Julien Kerihuel 2008
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "utils/mapitest/mapitest.h"
23 #include "utils/mapitest/proto.h"
24
25 /**
26    \file module_nspi.c
27
28    \brief NSPI tests
29  */
30
31
32 /**
33    \details Test the NspiUpdateStat RPC operation (0x02)
34
35    \param mt pointer on the top-level mapitest structure
36
37    \return true on success, otherwise false
38  */
39 _PUBLIC_ bool mapitest_nspi_UpdateStat(struct mapitest *mt)
40 {
41         TALLOC_CTX              *mem_ctx;
42         enum MAPISTATUS         retval;
43         struct nspi_context     *nspi_ctx;
44         uint32_t                plDelta = 1;
45         struct SRowSet          *SRowSet;
46
47         mem_ctx = talloc_named(NULL, 0, "mapitest_nspi_UpdateStat");
48         nspi_ctx = (struct nspi_context *) mt->session->nspi->ctx;
49
50         SRowSet = talloc_zero(mem_ctx, struct SRowSet);
51         retval = nspi_GetSpecialTable(nspi_ctx, mem_ctx, 0x2, &SRowSet);
52         MAPIFreeBuffer(SRowSet);
53         if (retval != MAPI_E_SUCCESS) {
54                 talloc_free(mem_ctx);
55                 return false;
56         }
57
58         retval = nspi_UpdateStat(nspi_ctx, mem_ctx, &plDelta);
59         mapitest_print_retval(mt, "NspiUpdateStat");
60         if (retval != MAPI_E_SUCCESS) {
61                 talloc_free(mem_ctx);
62                 return false;
63         }
64         mapitest_print(mt, "* %-35s: %d\n", "plDelta", plDelta);
65         talloc_free(mem_ctx);
66
67         return true;
68 }
69
70
71 /**
72    \details Test the NspiQueryRows RPC operation (0x3)
73    
74    \param mt pointer on the top-level mapitest structure
75
76    \return true on success, otherwise false
77  */
78 _PUBLIC_ bool mapitest_nspi_QueryRows(struct mapitest *mt)
79 {
80         TALLOC_CTX              *mem_ctx;
81         enum MAPISTATUS         retval;
82         struct nspi_context     *nspi_ctx;
83         struct SPropTagArray    *MIds;
84         struct SRowSet          *SRowSet;
85         struct SPropTagArray    *SPropTagArray;
86         struct SPropValue       *lpProp;
87         struct Restriction_r    Filter;
88
89         mem_ctx = talloc_named(NULL, 0, "mapitest_nspi_QueryRows");
90         nspi_ctx = (struct nspi_context *) mt->session->nspi->ctx;
91
92         /* Build the array of columns we want to retrieve */
93         SPropTagArray = set_SPropTagArray(mem_ctx, 0x2, PR_DISPLAY_NAME,
94                                           PR_DISPLAY_TYPE);
95
96         /* Build the restriction we want for NspiGetMatches */
97         lpProp = talloc_zero(mem_ctx, struct SPropValue);
98         lpProp->ulPropTag = PR_ACCOUNT;
99         lpProp->dwAlignPad = 0;
100         lpProp->value.lpszA = mt->mapi_ctx->session->profile->username;
101
102         Filter.rt = RES_PROPERTY;
103         Filter.res.resProperty.relop = RES_PROPERTY;
104         Filter.res.resProperty.ulPropTag = PR_ACCOUNT;
105         Filter.res.resProperty.lpProp = lpProp;
106
107         SRowSet = talloc_zero(mem_ctx, struct SRowSet);
108         MIds = talloc_zero(mem_ctx, struct SPropTagArray);
109         retval = nspi_GetMatches(nspi_ctx, mem_ctx, SPropTagArray, &Filter, &SRowSet, &MIds);
110         MAPIFreeBuffer(lpProp);
111         MAPIFreeBuffer(SRowSet);
112         MAPIFreeBuffer(SPropTagArray);
113         mapitest_print_retval_clean(mt, "NspiGetMatches", retval);
114         if (retval != MAPI_E_SUCCESS) {
115                 MAPIFreeBuffer(MIds);
116                 talloc_free(mem_ctx);
117                 return false;
118         }
119
120         /* Query the rows */
121         SRowSet = talloc_zero(mem_ctx, struct SRowSet);
122         retval = nspi_QueryRows(nspi_ctx, mem_ctx, NULL, MIds, 1, &SRowSet);
123         MAPIFreeBuffer(SRowSet);
124         mapitest_print_retval_clean(mt, "NspiQueryRows", retval);
125         if (retval != MAPI_E_SUCCESS) {
126                 MAPIFreeBuffer(MIds);
127                 talloc_free(mem_ctx);
128                 return false;
129         }
130
131         talloc_free(mem_ctx);
132
133         return true;
134 }
135
136
137 /**
138    \details Test the NspiSeekEntries RPC operation (0x04)
139
140    \param mt pointer on the top-level mapitest structure
141
142    \return true on success, otherwise false
143  */
144 _PUBLIC_ bool mapitest_nspi_SeekEntries(struct mapitest *mt)
145 {
146         TALLOC_CTX              *mem_ctx;
147         enum MAPISTATUS         retval;
148         struct nspi_context     *nspi_ctx;
149         struct SPropValue       pTarget;
150         struct SPropTagArray    *pPropTags;
151         struct SRowSet          *SRowSet;
152         struct emsmdb_context   *emsmdb;
153         bool                    ret = true;
154
155         mem_ctx = talloc_named(NULL, 0, "mapitest_nspi_SeekEntries");
156         nspi_ctx = (struct nspi_context *) mt->session->nspi->ctx;
157
158         emsmdb = (struct emsmdb_context *) mt->session->emsmdb->ctx;
159         SRowSet = talloc_zero(mem_ctx, struct SRowSet);
160         
161         pTarget.ulPropTag = PR_DISPLAY_NAME;
162         pTarget.dwAlignPad = 0x0;
163         pTarget.value.lpszA = emsmdb->info.szDisplayName;
164
165         pPropTags = set_SPropTagArray(mem_ctx, 0x1, PR_ACCOUNT);
166         retval = nspi_SeekEntries(nspi_ctx, mem_ctx, SortTypeDisplayName, &pTarget, pPropTags, NULL, &SRowSet);
167         if (retval != MAPI_E_SUCCESS) {
168                 ret = false;
169         }
170
171         mapitest_print_retval_clean(mt, "NspiSeekEntries", retval);
172         MAPIFreeBuffer(SRowSet);
173         MAPIFreeBuffer(pPropTags);
174
175         SRowSet = talloc_zero(mem_ctx, struct SRowSet);
176
177         pTarget.ulPropTag = PR_DISPLAY_NAME_UNICODE;
178         pTarget.dwAlignPad = 0x0;
179         pTarget.value.lpszA = emsmdb->info.szDisplayName;
180
181         pPropTags = set_SPropTagArray(mem_ctx, 0x1, PR_ACCOUNT);
182         retval = nspi_SeekEntries(nspi_ctx, mem_ctx, SortTypeDisplayName, &pTarget, pPropTags, NULL, &SRowSet);
183         if (retval != MAPI_E_SUCCESS) {
184                 ret = false;
185         }
186
187         mapitest_print_retval_clean(mt, "NspiSeekEntries", retval);
188         MAPIFreeBuffer(SRowSet);
189         MAPIFreeBuffer(pPropTags);
190
191         talloc_free(mem_ctx);
192
193         return ret;
194 }
195
196
197 /**
198    \details Test the NspiGetMatches RPC operation (0x5)
199
200    \param mt pointer on the top-level mapitest structure
201
202    \return true on success, otherwise false
203  */
204 _PUBLIC_ bool mapitest_nspi_GetMatches(struct mapitest *mt)
205 {
206         TALLOC_CTX              *mem_ctx;
207         enum MAPISTATUS         retval;
208         struct nspi_context     *nspi_ctx;
209         struct SPropTagArray    *MIds;
210         struct SRowSet          *SRowSet;
211         struct SPropTagArray    *SPropTagArray;
212         struct SPropValue       *lpProp;
213         struct Restriction_r    Filter;
214         bool                    ret = true;
215
216         mem_ctx = talloc_named(NULL, 0, "mapitest_nspi_GetMatches");
217         nspi_ctx = (struct nspi_context *) mt->session->nspi->ctx;
218
219         /* Build the array of columns we want to retrieve */
220         SPropTagArray = set_SPropTagArray(mem_ctx, 0x2, PR_DISPLAY_NAME,
221                                           PR_DISPLAY_TYPE);
222
223         /* Build the restriction we want for NspiGetMatches */
224         lpProp = talloc_zero(mem_ctx, struct SPropValue);
225         lpProp->ulPropTag = PR_ACCOUNT;
226         lpProp->dwAlignPad = 0;
227         lpProp->value.lpszA = mt->mapi_ctx->session->profile->username;
228
229         Filter.rt = RES_PROPERTY;
230         Filter.res.resProperty.relop = RES_PROPERTY;
231         Filter.res.resProperty.ulPropTag = PR_ACCOUNT;
232         Filter.res.resProperty.lpProp = lpProp;
233
234         SRowSet = talloc_zero(mem_ctx, struct SRowSet);
235         MIds = talloc_zero(mem_ctx, struct SPropTagArray);
236         retval = nspi_GetMatches(nspi_ctx, mem_ctx, SPropTagArray, &Filter, &SRowSet, &MIds);
237         MAPIFreeBuffer(lpProp);
238         MAPIFreeBuffer(SRowSet);
239         MAPIFreeBuffer(SPropTagArray);
240         MAPIFreeBuffer(MIds);
241         mapitest_print_retval_clean(mt, "NspiGetMatches", retval);
242         if (retval != MAPI_E_SUCCESS) {
243                 ret = false;
244         }
245
246         talloc_free(mem_ctx);
247         return ret;
248 }
249
250
251 /**
252    \details Test the NspiResortRestriction RPC operation (0x6)
253
254    \param mt pointer on the top-level mapitest structure
255
256    \return true on success, otherwise false
257  */
258 _PUBLIC_ bool mapitest_nspi_ResortRestriction(struct mapitest *mt)
259 {
260         TALLOC_CTX              *mem_ctx;
261         enum MAPISTATUS         retval;
262         struct nspi_context     *nspi_ctx;
263         struct Restriction_r    Filter;
264         struct SRowSet          *SRowSet = NULL;
265         struct SPropTagArray    *SPropTagArray = NULL;
266         struct SPropValue       *lpProp = NULL;
267         struct SPropTagArray    *MIds = NULL;
268         struct SPropTagArray    *ppMIds = NULL;
269         bool                    ret = true;
270
271         mem_ctx = talloc_named(NULL, 0, "mapitest_nspi_ResortRestriction");
272         nspi_ctx = (struct nspi_context *) mt->session->nspi->ctx;
273
274         /* Build the array of columns we want to retrieve */
275         SPropTagArray = set_SPropTagArray(mem_ctx, 0xb,
276                                           PR_DISPLAY_NAME,
277                                           PR_OFFICE_TELEPHONE_NUMBER,
278                                           PR_OFFICE_LOCATION,
279                                           PR_TITLE,
280                                           PR_COMPANY_NAME,
281                                           PR_ACCOUNT,
282                                           PR_ADDRTYPE,
283                                           PR_ENTRYID,
284                                           PR_DISPLAY_TYPE,
285                                           PR_INSTANCE_KEY,
286                                           PR_EMAIL_ADDRESS
287                                           );
288
289         /* Build the restriction we want for NspiGetMatches */
290         lpProp = talloc_zero(mem_ctx, struct SPropValue);
291         lpProp->ulPropTag = PR_OBJECT_TYPE;
292         lpProp->dwAlignPad = 0;
293         lpProp->value.l = 6;
294
295         Filter.rt = RES_PROPERTY;
296         Filter.res.resProperty.relop = RES_PROPERTY;
297         Filter.res.resProperty.ulPropTag = PR_OBJECT_TYPE;
298         Filter.res.resProperty.lpProp = lpProp;
299
300         SRowSet = talloc_zero(mem_ctx, struct SRowSet);
301         MIds = talloc_zero(mem_ctx, struct SPropTagArray);
302         retval = nspi_GetMatches(nspi_ctx, mem_ctx, SPropTagArray, &Filter, &SRowSet, &MIds);
303         MAPIFreeBuffer(lpProp);
304         MAPIFreeBuffer(SPropTagArray);
305         MAPIFreeBuffer(SRowSet);
306         mapitest_print_retval_clean(mt, "NspiGetMatches", retval);
307         if (retval != MAPI_E_SUCCESS) {
308                 MAPIFreeBuffer(MIds);
309                 talloc_free(mem_ctx);
310                 return false;
311         }
312
313         ppMIds = talloc_zero(mem_ctx, struct SPropTagArray);
314         retval = nspi_ResortRestriction(nspi_ctx, mem_ctx, SortTypeDisplayName, MIds, &ppMIds);
315         mapitest_print_retval_clean(mt, "NspiResortRestriction", retval);
316         if (retval != MAPI_E_SUCCESS) {
317                 ret = false;
318         }
319
320         MAPIFreeBuffer(MIds);
321         MAPIFreeBuffer(ppMIds);
322         talloc_free(mem_ctx);
323
324         return ret;
325 }
326
327
328 /**
329    \details Test the NspiDNToMId RPC operation (0x7)
330
331    \param mt pointer on the top-level mapitest structure
332
333    \return true on success, otherwise false
334  */
335 _PUBLIC_ bool mapitest_nspi_DNToMId(struct mapitest *mt)
336 {
337         TALLOC_CTX              *mem_ctx;
338         enum MAPISTATUS         retval;
339         struct nspi_context     *nspi_ctx;
340         struct StringsArray_r   pNames;
341         struct SPropTagArray    *MId;
342
343         mem_ctx = talloc_named(NULL, 0, "mapitest_nspi_DNToMId");
344         nspi_ctx = (struct nspi_context *) mt->session->nspi->ctx;
345
346         pNames.Count = 0x1;
347         pNames.Strings = (const char **) talloc_array(mem_ctx, char **, 1);
348         pNames.Strings[0] = mt->mapi_ctx->session->profile->homemdb;
349
350         MId = talloc_zero(mem_ctx, struct SPropTagArray);
351
352         retval = nspi_DNToMId(nspi_ctx, mem_ctx, &pNames, &MId);
353         MAPIFreeBuffer((char **)pNames.Strings);
354         MAPIFreeBuffer(MId);
355         talloc_free(mem_ctx);
356
357         mapitest_print_retval_clean(mt, "NspiDNToMId", retval);
358
359         if (retval == MAPI_E_SUCCESS) {
360               return true;
361         } else {
362               return false;
363         }
364 }
365
366
367 /**
368    \details Test the NspiGetPropList RPC operation (0x08)
369
370    \param mt pointer on the top-level mapitest structure
371
372    \return true on success, otherwise false
373  */
374 _PUBLIC_ bool mapitest_nspi_GetPropList(struct mapitest *mt)
375 {
376         TALLOC_CTX              *mem_ctx;
377         enum MAPISTATUS         retval;
378         struct nspi_context     *nspi_ctx;
379         struct SPropTagArray    *pPropTags = 0;
380         struct SPropTagArray    *MIds;
381         struct SPropValue       *lpProp;
382         struct Restriction_r    Filter;
383         struct SPropTagArray    *SPropTagArray;
384         struct SRowSet          *SRowSet;
385
386         mem_ctx = talloc_named(NULL, 0, "mapitest_nspi_GetPropList");
387         nspi_ctx = (struct nspi_context *) mt->session->nspi->ctx;
388
389         /* Step 1. Query for current profile username */
390         SPropTagArray = set_SPropTagArray(mem_ctx, 0x1, PR_DISPLAY_NAME);
391         lpProp = talloc_zero(mem_ctx, struct SPropValue);
392         lpProp->ulPropTag = PR_ANR_UNICODE;
393         lpProp->dwAlignPad = 0;
394         lpProp->value.lpszW = mt->mapi_ctx->session->profile->username;
395
396         Filter.rt = RES_PROPERTY;
397         Filter.res.resProperty.relop = RES_PROPERTY;
398         Filter.res.resProperty.ulPropTag = PR_ANR_UNICODE;
399         Filter.res.resProperty.lpProp = lpProp;
400
401         SRowSet = talloc_zero(mem_ctx, struct SRowSet);
402         MIds = talloc_zero(mem_ctx, struct SPropTagArray);
403         retval = nspi_GetMatches(nspi_ctx, mem_ctx, SPropTagArray, &Filter, &SRowSet, &MIds);
404         MAPIFreeBuffer(SPropTagArray);
405         MAPIFreeBuffer(lpProp);
406         MAPIFreeBuffer(SRowSet);
407         if (retval != MAPI_E_SUCCESS) {
408                 MAPIFreeBuffer(MIds);
409                 talloc_free(mem_ctx);
410                 return retval;
411         }
412
413
414         /* Step 2. Call NspiGetPropList using the MId returned by NspiGetMatches */
415         pPropTags = talloc_zero(mt->mem_ctx, struct SPropTagArray);
416         retval = nspi_GetPropList(nspi_ctx, mem_ctx, 0, MIds->aulPropTag[0], &pPropTags);
417         MAPIFreeBuffer(MIds);
418         mapitest_print_retval(mt, "NspiGetPropList");
419
420         if (retval != MAPI_E_SUCCESS) {
421                 MAPIFreeBuffer(pPropTags);
422                 talloc_free(mem_ctx);
423                 return false;
424         }
425
426         if (pPropTags) {
427                 mapitest_print(mt, "* %-35s: %d\n", "Properties number", pPropTags->cValues);
428                 MAPIFreeBuffer(pPropTags);
429         }
430         talloc_free(mem_ctx);
431
432         return true;
433 }
434
435
436 /**
437    \details Test the NspiGetProps RPC operation (0x09)
438
439    \param mt pointer to the top-level mapitest structure
440
441    \return true on success, otherwise false
442  */
443 _PUBLIC_ bool mapitest_nspi_GetProps(struct mapitest *mt)
444 {
445         TALLOC_CTX              *mem_ctx;
446         enum MAPISTATUS         retval;
447         struct nspi_context     *nspi_ctx;
448         struct StringsArray_r   pNames;
449         struct SPropTagArray    *MId;
450         struct SPropTagArray    *SPropTagArray;
451         struct SRowSet          *SRowSet;
452
453         mem_ctx = talloc_named(NULL, 0, "mapitest_nspi_GetProps");
454         nspi_ctx = (struct nspi_context *) mt->session->nspi->ctx;
455
456         pNames.Count = 0x1;
457         pNames.Strings = (const char **) talloc_array(mem_ctx, char **, 1);
458         pNames.Strings[0] = mt->mapi_ctx->session->profile->homemdb;
459
460         MId = talloc_zero(mem_ctx, struct SPropTagArray);
461
462         retval = nspi_DNToMId(nspi_ctx, mem_ctx, &pNames, &MId);
463         MAPIFreeBuffer((char **)pNames.Strings);
464         mapitest_print_retval(mt, "NspiDNToMId");
465
466         if (retval != MAPI_E_SUCCESS) {
467                 MAPIFreeBuffer(MId);
468                 talloc_free(mem_ctx);
469                 return false;
470         }
471
472         SRowSet = talloc_zero(mem_ctx, struct SRowSet);
473         SPropTagArray = set_SPropTagArray(mem_ctx, 0x1, PR_EMS_AB_NETWORK_ADDRESS);
474         retval = nspi_GetProps(nspi_ctx, mem_ctx, SPropTagArray, MId, &SRowSet);
475         mapitest_print_retval_clean(mt, "NspiGetProps", retval);
476         MAPIFreeBuffer(SPropTagArray);
477         MAPIFreeBuffer(MId);
478         MAPIFreeBuffer(SRowSet);
479
480         talloc_free(mem_ctx);
481
482         if (retval == MAPI_E_SUCCESS) {
483               return true;
484         } else {
485               return false;
486         }
487 }
488
489
490 /**
491    \details Test the NspiCompareMIds RPC operation (0x0a)
492
493    \param mt pointer to the top-level mapitest structure
494
495    \return true on success, otherwise false
496  */
497 _PUBLIC_ bool mapitest_nspi_CompareMIds(struct mapitest *mt)
498 {
499         TALLOC_CTX              *mem_ctx;
500         enum MAPISTATUS         retval;
501         struct nspi_context     *nspi_ctx;
502         uint32_t                plResult;
503         struct SPropTagArray    *MIds;
504         struct SRowSet          *SRowSet;
505         struct SPropTagArray    *SPropTagArray;
506         struct SPropValue       *lpProp;
507         struct Restriction_r    Filter;
508
509         mem_ctx = talloc_named(NULL, 0, "mapitest_nspi_CompareMIds");
510         nspi_ctx = (struct nspi_context *) mt->session->nspi->ctx;
511
512         /* Build the array of columns we want to retrieve */
513         SPropTagArray = set_SPropTagArray(mem_ctx, 0x1, PR_DISPLAY_NAME);
514
515         /* Build the restriction we want for NspiGetMatches */
516         lpProp = talloc_zero(mem_ctx, struct SPropValue);
517         lpProp->ulPropTag = PR_OBJECT_TYPE;
518         lpProp->dwAlignPad = 0;
519         lpProp->value.l = 6;
520
521         Filter.rt = RES_PROPERTY;
522         Filter.res.resProperty.relop = RES_PROPERTY;
523         Filter.res.resProperty.ulPropTag = PR_OBJECT_TYPE;
524         Filter.res.resProperty.lpProp = lpProp;
525
526         SRowSet = talloc_zero(mem_ctx, struct SRowSet);
527         MIds = talloc_zero(mem_ctx, struct SPropTagArray);
528         retval = nspi_GetMatches(nspi_ctx, mem_ctx, SPropTagArray, &Filter, &SRowSet, &MIds);
529         MAPIFreeBuffer(lpProp);
530         MAPIFreeBuffer(SPropTagArray);
531         MAPIFreeBuffer(SRowSet);
532         mapitest_print_retval_clean(mt, "NspiGetMatches", retval);
533         if (retval != MAPI_E_SUCCESS) {
534                 MAPIFreeBuffer(MIds);
535                 talloc_free(mem_ctx);
536                 return false;
537         }
538
539         /* Ensure we have at least two result to compare */
540         if (MIds->cValues < 2) {
541                 mapitest_print(mt, "* Only one result found, can't compare\n");
542                 MAPIFreeBuffer(MIds);
543                 talloc_free(mem_ctx);
544                 return false;
545         }
546
547         retval = nspi_CompareMIds(nspi_ctx, mem_ctx, MIds->aulPropTag[0], MIds->aulPropTag[1], &plResult);
548         mapitest_print_retval_clean(mt, "NspiCompareMIds", retval);
549         MAPIFreeBuffer(MIds);
550         if (retval != MAPI_E_SUCCESS) {
551                 talloc_free(mem_ctx);
552                 return false;
553         }
554
555         mapitest_print(mt, "* %-35s: %d\n", "value of the comparison", plResult);
556         talloc_free(mem_ctx);
557
558         return true;
559 }
560
561
562 /**
563    \details Test the NspiModProps RPC operation (0xb)
564  
565    \param mt pointer on the top-level mapitest structure
566
567    \return true on success, otherwise false
568 */
569 _PUBLIC_ bool mapitest_nspi_ModProps(struct mapitest *mt)
570 {
571         TALLOC_CTX              *mem_ctx;
572         enum MAPISTATUS         retval;
573         struct nspi_context     *nspi_ctx;
574         struct SRow             *pRow;
575         struct SPropTagArray    *pPropTags;
576         struct SPropValue       modProp;
577         struct SPropTagArray    *MIds;
578         struct SRowSet          *SRowSet;
579         struct SPropTagArray    *SPropTagArray;
580         struct SPropValue       *lpProp;
581         struct Restriction_r    Filter;
582         const char              *original_office_location;
583         bool                    ret = true;
584
585         mem_ctx = talloc_named(NULL, 0, "mapitest_nspi_ModProps");
586         nspi_ctx = (struct nspi_context *) mt->session->nspi->ctx;
587
588         /* Build the array of columns we want to retrieve */
589         SPropTagArray = set_SPropTagArray(mem_ctx, 0x2, PR_DISPLAY_NAME,
590                                           PR_DISPLAY_TYPE);
591
592         /* Build the restriction we want for NspiGetMatches */
593         lpProp = talloc_zero(mem_ctx, struct SPropValue);
594         lpProp->ulPropTag = PR_ACCOUNT;
595         lpProp->dwAlignPad = 0;
596         lpProp->value.lpszA = mt->mapi_ctx->session->profile->username;
597
598         Filter.rt = RES_PROPERTY;
599         Filter.res.resProperty.relop = RES_PROPERTY;
600         Filter.res.resProperty.ulPropTag = PR_ACCOUNT;
601         Filter.res.resProperty.lpProp = lpProp;
602
603         SRowSet = talloc_zero(mem_ctx, struct SRowSet);
604         MIds = talloc_zero(mem_ctx, struct SPropTagArray);
605         retval = nspi_GetMatches(nspi_ctx, mem_ctx, SPropTagArray, &Filter, &SRowSet, &MIds);
606         MAPIFreeBuffer(lpProp);
607         MAPIFreeBuffer(SRowSet);
608         MAPIFreeBuffer(SPropTagArray);
609         mapitest_print_retval_clean(mt, "nspi_GetMatches", retval);
610         if (retval != MAPI_E_SUCCESS) {
611                 MAPIFreeBuffer(MIds);
612                 talloc_free(mem_ctx);
613                 return false;
614         }
615
616         /* Query the rows */
617         SRowSet = talloc_zero(mem_ctx, struct SRowSet);
618         retval = nspi_QueryRows(nspi_ctx, mem_ctx, NULL, MIds, 1, &SRowSet);
619         mapitest_print_retval_clean(mt, "nspi_QueryRows", retval);
620         if (retval != MAPI_E_SUCCESS) {
621                 MAPIFreeBuffer(MIds);
622                 MAPIFreeBuffer(SRowSet);
623                 talloc_free(mem_ctx);
624                 return false;
625         }
626         if (SRowSet->cRows != 1) {
627                 mapitest_print(mem_ctx, "unexpected number of rows: %i\n", SRowSet->cRows);
628                 MAPIFreeBuffer(MIds);
629                 MAPIFreeBuffer(SRowSet);
630                 talloc_free(mem_ctx);
631                 return false;
632         }
633         original_office_location = (const char *)find_SPropValue_data(&(SRowSet->aRow[0]), PR_OFFICE_LOCATION);
634         mapitest_print(mt, "original PR_OFFICE_LOCATION value: %s\n", original_office_location);
635
636         /* Build the SRow and SPropTagArray for NspiModProps */
637         pRow = talloc_zero(mem_ctx, struct SRow);
638         modProp.ulPropTag = PR_OFFICE_LOCATION;
639         modProp.value.lpszA = "[MT] office location";
640         SRow_addprop(pRow, modProp);
641
642         pPropTags = set_SPropTagArray(mem_ctx, 0x1, PR_OFFICE_LOCATION);
643         retval = nspi_ModProps(nspi_ctx, mem_ctx, MIds->aulPropTag[0], pPropTags, pRow);
644         mapitest_print_retval_clean(mt, "nspi_ModProps", retval);
645         MAPIFreeBuffer(pRow);
646
647         if (retval != MAPI_E_SUCCESS) {
648                 MAPIFreeBuffer(MIds);
649                 MAPIFreeBuffer(pPropTags);
650                 talloc_free(mem_ctx);
651                 return false;
652         }
653
654         /* Check that the property was set correctly */
655         SRowSet = talloc_zero(mem_ctx, struct SRowSet);
656         retval = nspi_QueryRows(nspi_ctx, mem_ctx, NULL, MIds, 1, &SRowSet);
657         mapitest_print_retval_clean(mt, "nspi_QueryRows", retval);
658         if (retval != MAPI_E_SUCCESS) {
659                 MAPIFreeBuffer(MIds);
660                 MAPIFreeBuffer(SRowSet);
661                 talloc_free(mem_ctx);
662                 return false;
663         }
664         if (SRowSet->cRows != 1) {
665                 mapitest_print(mem_ctx, "unexpected number of rows: %i\n", SRowSet->cRows);
666                 MAPIFreeBuffer(MIds);
667                 MAPIFreeBuffer(SRowSet);
668                 talloc_free(mem_ctx);
669                 return false;
670         }
671         if (strcmp((const char *)find_SPropValue_data(&(SRowSet->aRow[0]), PR_OFFICE_LOCATION), "[MT] office location") != 0) {
672                 mapitest_print(mt, "PR_OFFICE_LOCATION string value mismatch: %s", (const char *)find_SPropValue_data(&(SRowSet->aRow[0]), PR_OFFICE_LOCATION));
673                 ret = false;
674         } else {
675                 mapitest_print(mt, "correctly set PR_OFFICE_LOCATION\n");
676         }
677
678         /* try to reset the office location back to the original value */
679         pRow = talloc_zero(mem_ctx, struct SRow);
680         modProp.ulPropTag = PR_OFFICE_LOCATION;
681         modProp.value.lpszA = original_office_location;
682         SRow_addprop(pRow, modProp);
683
684         retval = nspi_ModProps(nspi_ctx, mem_ctx, MIds->aulPropTag[0], pPropTags, pRow);
685         mapitest_print_retval_clean(mt, "nspi_ModProps (reset original value)", retval);
686         if (retval != MAPI_E_SUCCESS) {
687                 ret = false;
688         }
689
690         MAPIFreeBuffer(MIds);
691         MAPIFreeBuffer(pPropTags);
692         MAPIFreeBuffer(pRow);
693
694         talloc_free(mem_ctx);
695         
696         return ret;
697 }
698
699
700 /**
701    \details Test the NspiGetSpecialTable RPC operation (0x0c)
702
703    \param mt pointer on the top-level mapitest structure
704
705    \return true on success, otherwise false
706  */
707 _PUBLIC_ bool mapitest_nspi_GetSpecialTable(struct mapitest *mt)
708 {
709         TALLOC_CTX              *mem_ctx;
710         enum MAPISTATUS         retval;
711         struct nspi_context     *nspi_ctx;
712         struct SRowSet          *SRowSet;
713
714         mem_ctx = talloc_named(NULL, 0, "mapitest_nspi_GetSpecialTable");
715         nspi_ctx = (struct nspi_context *) mt->session->nspi->ctx;
716
717         SRowSet = talloc_zero(mem_ctx, struct SRowSet);
718         retval = nspi_GetSpecialTable(nspi_ctx, mem_ctx, 0x0, &SRowSet);
719         MAPIFreeBuffer(SRowSet);
720         mapitest_print_retval_clean(mt, "NspiGetSpecialTable (Hierarchy Table)", retval);
721
722         if (retval != MAPI_E_SUCCESS) {
723                 talloc_free(mem_ctx);
724                 return false;
725         }
726
727         SRowSet = talloc_zero(mt->mem_ctx, struct SRowSet);
728         retval = nspi_GetSpecialTable(nspi_ctx, mem_ctx, 0x2, &SRowSet);
729         MAPIFreeBuffer(SRowSet);
730         mapitest_print_retval_clean(mt, "NspiGetSpecialTable (Address Creation Template)", retval);
731         talloc_free(mem_ctx);
732
733         if (retval == MAPI_E_SUCCESS) {
734               return true;
735         } else {
736               return false;
737         }
738 }
739
740
741 /**
742    \details Test the NspiGetTemplateInfo RPC operation (0x0d)
743
744    \param mt pointer on the top-level mapitest structure
745
746    \return true on success, otherwise false
747  */
748 _PUBLIC_ bool mapitest_nspi_GetTemplateInfo(struct mapitest *mt)
749 {
750         TALLOC_CTX              *mem_ctx;
751         enum MAPISTATUS         retval;
752         struct nspi_context     *nspi_ctx;
753         struct SRow             *ppData = NULL;
754
755         mem_ctx = talloc_named(NULL, 0, "mapitest_nspi_GetTemplateInfo");
756         nspi_ctx = (struct nspi_context *) mt->session->nspi->ctx;
757
758         ppData = talloc_zero(mem_ctx, struct SRow);
759         retval = nspi_GetTemplateInfo(nspi_ctx, mem_ctx,
760                                       TI_TEMPLATE|TI_SCRIPT|TI_EMT|TI_HELPFILE_NAME|TI_HELPFILE_CONTENTS,
761                                       0, NULL, &ppData);
762         mapitest_print_retval_clean(mt, "NspiGetTemplateInfo", retval);
763         MAPIFreeBuffer(ppData);
764         talloc_free(mem_ctx);
765
766         if (retval == MAPI_E_SUCCESS) {
767               return true;
768         } else {
769               return false;
770         }
771 }
772
773
774 /**
775    \details Test the NspiModLinkAtt RPC operation (0x0e)
776
777    \param mt pointer on the top-level mapitest structure
778
779    \return true on success, otherwise false
780  */
781 _PUBLIC_ bool mapitest_nspi_ModLinkAtt(struct mapitest *mt)
782 {
783         enum MAPISTATUS         retval;
784         struct nspi_context     *nspi_ctx;
785 /*      struct SPropTagArray    *MIds; */
786         struct BinaryArray_r    *lpEntryIds;
787
788         nspi_ctx = (struct nspi_context *) mt->session->nspi->ctx;
789
790         lpEntryIds = talloc_zero(mt->mem_ctx, struct BinaryArray_r);
791         lpEntryIds->cValues = 0;
792         lpEntryIds->lpbin = NULL;
793
794         retval = nspi_ModLinkAtt(nspi_ctx, false, PR_EMS_AB_REPORTS, 0x0, lpEntryIds);
795         mapitest_print_retval_clean(mt, "NspiModLinkAtt", retval);
796         MAPIFreeBuffer(lpEntryIds);
797
798         if (retval == MAPI_E_SUCCESS) {
799               return true;
800         } else {
801               return false;
802         }
803 }
804
805
806
807 /**
808    \details Test the NspiQueryColumns RPC operation (0x10)
809
810    \param mt pointer on the top-level mapitest structure
811
812    \return true on success, otherwise false
813  */
814 _PUBLIC_ bool mapitest_nspi_QueryColumns(struct mapitest *mt)
815 {
816         TALLOC_CTX              *mem_ctx;
817         enum MAPISTATUS         retval;
818         struct nspi_context     *nspi_ctx;
819         struct SPropTagArray    *SPropTagArray = NULL;
820         
821         mem_ctx = talloc_named(NULL, 0, "mapitest_nspi_QueryColumns");
822         nspi_ctx = (struct nspi_context *) mt->session->nspi->ctx;
823         
824         SPropTagArray = talloc_zero(mem_ctx, struct SPropTagArray);
825
826         retval = nspi_QueryColumns(nspi_ctx, mem_ctx, true, &SPropTagArray);
827         if (retval != MAPI_E_SUCCESS) {
828                 mapitest_print_retval_clean(mt, "NspiQueryColumns", retval);
829                 MAPIFreeBuffer(SPropTagArray);
830                 talloc_free(mem_ctx);
831                 return false;
832         }
833
834         if (SPropTagArray) {
835                 mapitest_print(mt, "* %d columns returned\n", SPropTagArray->cValues);
836                 mapitest_print_retval_clean(mt, "NspiQueryColumns", retval);
837                 MAPIFreeBuffer(SPropTagArray);
838         }
839         talloc_free(mem_ctx);
840
841         return true;
842 }
843
844
845 /**
846    \details Test the NspiGetNamesFromIDs RPC operation (0x11)
847
848    \param mt pointer on the top-level mapitest structure
849
850    \return true on success, otherwise false
851  */
852 _PUBLIC_ bool mapitest_nspi_GetNamesFromIDs(struct mapitest *mt)
853 {
854         TALLOC_CTX                      *mem_ctx;
855         enum MAPISTATUS                 retval;
856         struct nspi_context             *nspi_ctx;
857         struct SPropTagArray            *ppReturnedPropTags;
858         struct PropertyNameSet_r        *ppNames;
859
860         mem_ctx = talloc_named(NULL, 0, "mapitest_nspi_GetNamesFromIDs");
861         nspi_ctx = (struct nspi_context *) mt->session->nspi->ctx;
862
863
864         ppReturnedPropTags = talloc_zero(mem_ctx, struct SPropTagArray);
865         ppNames = talloc_zero(mem_ctx, struct PropertyNameSet_r);
866         retval = nspi_GetNamesFromIDs(nspi_ctx, mem_ctx, NULL, NULL, &ppReturnedPropTags, &ppNames);
867         mapitest_print_retval_clean(mt, "NspiGetNamesFromIDs", retval);
868         MAPIFreeBuffer(ppReturnedPropTags);
869         MAPIFreeBuffer(ppNames);
870         talloc_free(mem_ctx);
871
872         if (retval == MAPI_E_SUCCESS) {
873               return true;
874         } else {
875               return false;
876         }
877 }
878
879
880 /**
881    \details Test the NspiGetIDsFromNames RPC operation (0x12)
882
883    \param mt pointer on the top-level mapitest structure
884
885    \return true on success, otherwise false
886  */
887 _PUBLIC_ bool mapitest_nspi_GetIDsFromNames(struct mapitest *mt)
888 {
889         TALLOC_CTX                      *mem_ctx;
890         enum MAPISTATUS                 retval;
891         struct nspi_context             *nspi_ctx;
892         struct SPropTagArray            *ppReturnedPropTags;
893         struct PropertyNameSet_r        *ppNames;
894
895         mem_ctx = talloc_named(NULL, 0, "mapitest_nspi_GetIDsFromNames");
896         nspi_ctx = (struct nspi_context *) mt->session->nspi->ctx;
897
898
899         ppReturnedPropTags = talloc_zero(mem_ctx, struct SPropTagArray);
900         ppNames = talloc_zero(mem_ctx, struct PropertyNameSet_r);
901         retval = nspi_GetNamesFromIDs(nspi_ctx, mem_ctx, NULL, NULL, &ppReturnedPropTags, &ppNames);
902         mapitest_print_retval_clean(mt, "NspiGetNamesFromIDs", retval);
903         MAPIFreeBuffer(ppReturnedPropTags);
904
905         if ( (retval != MAPI_E_SUCCESS) || !ppNames ) {
906                 MAPIFreeBuffer(ppNames);
907                 talloc_free(mem_ctx);
908                 return false;
909         }
910
911         ppReturnedPropTags = talloc_zero(mem_ctx, struct SPropTagArray);
912         retval = nspi_GetIDsFromNames(nspi_ctx, mem_ctx, true, ppNames->cNames, ppNames->aNames, &ppReturnedPropTags);
913         mapitest_print_retval_clean(mt, "NspiGetIDsFromNames", retval);
914         MAPIFreeBuffer(ppReturnedPropTags);
915         MAPIFreeBuffer(ppNames);
916         talloc_free(mem_ctx);
917
918         if (retval == MAPI_E_SUCCESS) {
919               return true;
920         } else {
921               return false;
922         }
923 }
924
925
926 /**
927    \details Test the NspiResolveNames and NspiResolveNamesW RPC
928    operations (0x13 and 0x14)
929
930    \param mt pointer on the top-level mapitest structure
931    
932    \return true on success, otherwise false
933  */
934 _PUBLIC_ bool mapitest_nspi_ResolveNames(struct mapitest *mt)
935 {
936         enum MAPISTATUS                 retval;
937         struct nspi_context             *nspi_ctx;
938         struct SPropTagArray            *SPropTagArray = NULL;
939         struct SRowSet                  *SRowSet = NULL;
940         struct PropertyTagArray_r       *flaglist = NULL;
941         const char                      *username[2];
942         const char                      *username_err[2];
943
944         nspi_ctx = (struct nspi_context *) mt->session->nspi->ctx;
945
946         /* Build the username array */
947         username[0] = (const char *)mt->profile->mailbox;
948         username[1] = NULL;
949         /* Build the err username array */
950         username_err[0] = talloc_asprintf(mt->mem_ctx, "%s%s", mt->info.szDNPrefix, "nspi_resolve_testcase");
951         username_err[1] = NULL;
952
953         SPropTagArray = set_SPropTagArray(mt->mem_ctx, 0xd,
954                                           PR_ENTRYID,
955                                           PR_DISPLAY_NAME,
956                                           PR_ADDRTYPE,
957                                           PR_GIVEN_NAME,
958                                           PR_SMTP_ADDRESS,
959                                           PR_OBJECT_TYPE,
960                                           PR_DISPLAY_TYPE,
961                                           PR_EMAIL_ADDRESS,
962                                           PR_SEND_INTERNET_ENCODING,
963                                           PR_SEND_RICH_INFO,
964                                           PR_SEARCH_KEY,
965                                           PR_TRANSMITTABLE_DISPLAY_NAME,
966                                           PR_7BIT_DISPLAY_NAME);
967
968         /* Test with existing username */
969         /* NspiResolveNames (0x13) */
970         retval = ResolveNames(mt->session, (const char **)username, SPropTagArray, &SRowSet, &flaglist, 0);
971         mapitest_print_retval_clean(mt, "NspiResolveNames - existing", retval);
972         if (flaglist->aulPropTag[0] != MAPI_RESOLVED) {
973                 mapitest_print(mt, "Expected 2 (MAPI_RESOLVED), but NspiResolveNames returned: %i\n", flaglist->aulPropTag[0]);
974         } else {
975                 mapitest_print(mt, "\tGot expected resolution flag\n");
976         }
977         talloc_free(flaglist);
978         talloc_free(SRowSet);
979         if (retval != MAPI_E_SUCCESS) {
980                 MAPIFreeBuffer(SPropTagArray);
981                 return false;
982         }
983
984         /* NspiResolveNamesW (0x14) */
985         retval = ResolveNames(mt->session, (const char **)username, SPropTagArray, &SRowSet, &flaglist, MAPI_UNICODE);
986         mapitest_print_retval_clean(mt, "NspiResolveNamesW - existing", retval);
987         if (flaglist->aulPropTag[0] != MAPI_RESOLVED) {
988                 mapitest_print(mt, "Expected 2 (MAPI_RESOLVED), but NspiResolveNamesW returned: %i\n", flaglist->aulPropTag[0]);
989         } else {
990                 mapitest_print(mt, "\tGot expected resolution flag\n");
991         }
992         talloc_free(flaglist);
993         talloc_free(SRowSet);
994         if (retval != MAPI_E_SUCCESS) {
995                 MAPIFreeBuffer(SPropTagArray);
996                 return false;
997         }
998
999         /* Test with non-existant username */
1000         /* NspiResolveNames (0x13) */
1001         retval = ResolveNames(mt->session, (const char **)username_err, SPropTagArray, &SRowSet, &flaglist, 0);
1002         mapitest_print_retval_clean(mt, "NspiResolveNames - non existant user name", retval);
1003         if (flaglist->aulPropTag[0] != MAPI_UNRESOLVED) {
1004                 mapitest_print(mt, "Expected 0 (MAPI_UNRESOLVED), but NspiResolveNames returned: %i\n", flaglist->aulPropTag[0]);
1005         } else {
1006                 mapitest_print(mt, "\tGot expected resolution flag\n");
1007         }
1008         talloc_free(flaglist);
1009         talloc_free(SRowSet);
1010         if (retval != MAPI_E_SUCCESS) {
1011                 MAPIFreeBuffer(SPropTagArray);
1012                 return false;
1013         }
1014
1015         /* NspiResolveNamesW (0x14) */
1016         retval = ResolveNames(mt->session, (const char **)username_err, SPropTagArray, &SRowSet, &flaglist, MAPI_UNICODE);
1017         mapitest_print_retval_clean(mt, "NspiResolveNamesW - non existant user name", retval);
1018         if (flaglist->aulPropTag[0] != MAPI_UNRESOLVED) {
1019                 mapitest_print(mt, "Expected 0 (MAPI_UNRESOLVED), but NspiResolveNamesW returned: %i\n", flaglist->aulPropTag[0]);
1020         } else {
1021                 mapitest_print(mt, "\tGot expected resolution flag\n");
1022         }
1023         talloc_free(flaglist);
1024         talloc_free(SRowSet);
1025         if (retval != MAPI_E_SUCCESS) {
1026                 MAPIFreeBuffer(SPropTagArray);
1027                 return false;
1028         }
1029         MAPIFreeBuffer(SPropTagArray);
1030         return true;
1031 }
1032
1033 /**
1034    \details Test the GetGALTable function
1035
1036    \param mt pointer to the top-level mapitest structure
1037    
1038    \return true on success, otherwise false
1039  */
1040 _PUBLIC_ bool mapitest_nspi_GetGALTable(struct mapitest *mt)
1041 {
1042         struct SPropTagArray    *SPropTagArray;
1043         struct SRowSet          *SRowSet;
1044         enum MAPISTATUS         retval;
1045         uint32_t                i;
1046         uint32_t                count;
1047         uint8_t                 ulFlags;
1048         uint32_t                rowsFetched = 0;
1049         uint32_t                totalRowsFetched = 0;
1050         bool                    ret = true;
1051
1052         SPropTagArray = set_SPropTagArray(mt->mem_ctx, 0xc,
1053                                           PR_INSTANCE_KEY,
1054                                           PR_ENTRYID,
1055                                           PR_DISPLAY_NAME_UNICODE,
1056                                           PR_EMAIL_ADDRESS_UNICODE,
1057                                           PR_DISPLAY_TYPE,
1058                                           PR_OBJECT_TYPE,
1059                                           PR_ADDRTYPE_UNICODE,
1060                                           PR_OFFICE_TELEPHONE_NUMBER_UNICODE,
1061                                           PR_OFFICE_LOCATION_UNICODE,
1062                                           PR_TITLE_UNICODE,
1063                                           PR_COMPANY_NAME_UNICODE,
1064                                           PR_ACCOUNT_UNICODE);
1065
1066         count = 0x20;
1067         ulFlags = TABLE_START;
1068         do {
1069                 retval = GetGALTable(mt->session, SPropTagArray, &SRowSet, count, ulFlags);
1070                 mapitest_print_retval_clean(mt, "GetGALTable", retval);
1071                 if ((!SRowSet) || (!(SRowSet->aRow))) {
1072                         ret = false;
1073                         goto cleanup;
1074                 }
1075                 rowsFetched = SRowSet->cRows;
1076                 totalRowsFetched += rowsFetched;
1077                 if (rowsFetched) {
1078                         for (i = 0; i < rowsFetched; i++) {
1079                                 mapitest_print_PAB_entry(mt, &SRowSet->aRow[i]);
1080                         }
1081                 }
1082                 ulFlags = TABLE_CUR;
1083                 MAPIFreeBuffer(SRowSet);
1084         } while (rowsFetched == count);
1085
1086         if (totalRowsFetched < 1) {
1087                 /* We should always have at least ourselves in the list */
1088                 /* So if we got no rows at all, there is a problem */
1089                 ret = false;
1090         }
1091 cleanup:
1092         MAPIFreeBuffer(SPropTagArray);
1093
1094         return ret;
1095 }