Replace deprecated talloc_init calls with talloc_named
[jelmer/openchange.git] / torture / mapi_sorttable.c
1 /*
2    OpenChange MAPI torture suite implementation.
3
4    Tables related operations torture
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 <libmapi/libmapi.h>
23 #include <gen_ndr/ndr_exchange.h>
24 #include <param.h>
25 #include <credentials.h>
26 #include <torture/mapi_torture.h>
27 #include <torture.h>
28 #include <torture/torture_proto.h>
29 #include <samba/popt.h>
30
31 bool torture_rpc_mapi_sorttable(struct torture_context *torture)
32 {
33         enum MAPISTATUS         retval;
34         TALLOC_CTX              *mem_ctx;
35         mapi_object_t           obj_store;
36         mapi_object_t           obj_folder;
37         mapi_object_t           obj_ctable;
38         mapi_id_t               id_folder;
39         struct SPropTagArray    *SPropTagArray;
40         struct SRowSet          SRowSet;
41         struct SSortOrderSet    criteria;
42         struct mapi_session     *session;
43         uint32_t                count;
44         uint32_t                i;
45
46         /* init torture test */
47         mem_ctx = talloc_named(NULL, 0, "torture_rpc_mapi_sorttable");
48         if ((session = torture_init_mapi(mem_ctx, torture->lp_ctx)) == NULL) return false;
49
50         /* Open Message Store*/
51         mapi_object_init(&obj_store);
52         retval = OpenMsgStore(session, &obj_store);
53         mapi_errstr("OpenMsgStore", GetLastError());
54         if (retval != MAPI_E_SUCCESS) return false;
55
56         /* Retrieve Inbox folder ID */
57         retval = GetDefaultFolder(&obj_store, &id_folder, olFolderInbox);
58         mapi_errstr("GetDefaultFolder", GetLastError());
59         if (retval != MAPI_E_SUCCESS) return false;
60
61         /* Open Inbox Folder */
62         mapi_object_init(&obj_folder);
63         retval = OpenFolder(&obj_store, id_folder, &obj_folder);
64         mapi_errstr("OpenFolder", GetLastError());
65         if (retval != MAPI_E_SUCCESS) return false;
66
67         /* Retrieve the Contents Table */
68         mapi_object_init(&obj_ctable);
69         retval = GetContentsTable(&obj_folder, &obj_ctable, 0, &count);
70         mapi_errstr("GetContentsTable", GetLastError());
71         if (retval != MAPI_E_SUCCESS) return false;
72
73         /* Customize the contents table view */
74         SPropTagArray = set_SPropTagArray(mem_ctx, 0x2, PR_SUBJECT, PR_LAST_MODIFICATION_TIME);
75         retval = SetColumns(&obj_ctable, SPropTagArray);
76         MAPIFreeBuffer(SPropTagArray);
77         mapi_errstr("SetColumns", GetLastError());
78         if (retval != MAPI_E_SUCCESS) return false;
79
80         /* Browse and print table content */
81         printf("\nBefore SortTable ASCENDING:\n");
82         while (((retval = QueryRows(&obj_ctable, count, TBL_ADVANCE, &SRowSet)) != MAPI_E_NOT_FOUND) &&
83                SRowSet.cRows) {
84                 count -= SRowSet.cRows;
85                 
86                 for (i = 0; i < SRowSet.cRows; i++) {
87                         printf("\t[%d] %s\n", i, SRowSet.aRow[i].lpProps[0].value.lpszA);
88                 }
89         }
90
91         /* SortTable */
92         memset(&criteria, 0x0, sizeof (struct SSortOrderSet));
93         criteria.cSorts = 1;
94         criteria.aSort = talloc_array(mem_ctx, struct SSortOrder, criteria.cSorts);
95         criteria.aSort[0].ulPropTag = PR_LAST_MODIFICATION_TIME;
96         criteria.aSort[0].ulOrder = TABLE_SORT_ASCEND;
97         retval = SortTable(&obj_ctable, &criteria);
98         mapi_errstr("SortTable", GetLastError());
99
100         /* SeekRow at beginning */
101         retval = SeekRow(&obj_ctable, BOOKMARK_BEGINNING, 0, &count);
102         mapi_errstr("SeekRow", GetLastError());
103
104         /* Browse and print table content */
105         printf("\nAfter SortTable ASCENDING:\n");
106         while (((retval = QueryRows(&obj_ctable, count, TBL_ADVANCE, &SRowSet)) != MAPI_E_NOT_FOUND) &&
107                SRowSet.cRows) {
108                 count -= SRowSet.cRows;
109                 
110                 for (i = 0; i < SRowSet.cRows; i++) {
111                         printf("\t[%d] %s\n", i, SRowSet.aRow[i].lpProps[0].value.lpszA);
112                 }
113         }
114
115
116         mapi_object_release(&obj_ctable);
117         mapi_object_release(&obj_folder);
118         mapi_object_release(&obj_store);
119
120         return true;
121 }