PEP 55 Update license on source files to current license text and date
[tpot/pegasus/.git] / src / Pegasus / Repository / tests / Inst / Inst.cpp
1 //%2003////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2000, 2001, 2002  BMC Software, Hewlett-Packard Development
4 // Company, L. P., IBM Corp., The Open Group, Tivoli Systems.
5 // Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L. P.;
6 // IBM Corp.; EMC Corporation, The Open Group.
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to
10 // deal in the Software without restriction, including without limitation the
11 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
12 // sell copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 // 
15 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
16 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
17 // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
18 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
19 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 //
24 //==============================================================================
25 //
26 // Author: Mike Brasher (mbrasher@bmc.com)
27 //
28 // Modified By:  Carol Ann Krug Graves, Hewlett-Packard Company
29 //               (carolann_graves@hp.com)
30 //
31 //%/////////////////////////////////////////////////////////////////////////////
32
33 #include <Pegasus/Common/Config.h>
34 #include <Pegasus/Common/Destroyer.h>
35 #include <iostream>
36 #include <cstring>
37 #include <cassert>
38 #include <Pegasus/Repository/InstanceIndexFile.h>
39 #include <Pegasus/Repository/InstanceDataFile.h>
40
41 PEGASUS_USING_PEGASUS;
42 PEGASUS_USING_STD;
43
44 static const char* tmpDir;
45
46 void _Test01()
47 {
48     String indexPath (tmpDir);
49     indexPath.append("/X.idx");
50     CString ipd = indexPath.getCString();
51     const char* PATH = ipd;
52     Uint32 index;
53     Uint32 size;
54     Uint32 freeCount = 0;
55
56     // create 5 entries
57     size = 1427;
58     index = 0;
59     Boolean result = InstanceIndexFile::createEntry(PATH, 
60         CIMObjectPath("X.key1=1001,key2=\"Hello World 1\""), index, size);
61     assert(result);
62
63     size = 1433;
64     index = 1427;
65     result = InstanceIndexFile::createEntry(PATH, 
66         CIMObjectPath("X.key1=1002,key2=\"Hello World 2\""), index, size);
67     assert(result);
68
69     size = 1428;
70     index = 2860;
71     result = InstanceIndexFile::createEntry(PATH, 
72         CIMObjectPath("X.key1=1003,key2=\"Hello World 3\""), index, size);
73     assert(result);
74
75     size = 1433;
76     index = 4288;
77     result = InstanceIndexFile::createEntry(PATH, 
78         CIMObjectPath("X.key1=1004,key2=\"Hello World 4\""), index, size);
79     assert(result);
80
81     size = 1431;
82     index = 5721;
83     result = InstanceIndexFile::createEntry(PATH, 
84         CIMObjectPath("X.key1=1005,key2=\"Hello World 5\""), index, size);
85     assert(result);
86
87     // delete the 3rd entry
88     result = InstanceIndexFile::deleteEntry(PATH, 
89           CIMObjectPath("X.key2=\"Hello World 3\",key1=1003"), freeCount);
90     assert(result);
91
92     // create a new entry
93     size = 1428;
94     index = 2860;
95     result = InstanceIndexFile::createEntry(
96         PATH, CIMObjectPath("X.key1=1003,key2=\"Hello World 3\""), 
97         index, size);
98     assert(result);
99
100     // delete the newly created entry
101     result = InstanceIndexFile::deleteEntry(PATH, 
102           CIMObjectPath("X.key2=\"Hello World 3\",key1=1003"), freeCount);
103     assert(result);
104
105     // delete the first entry
106     result = InstanceIndexFile::deleteEntry(PATH, 
107         CIMObjectPath("X.key1=1001,key2=\"Hello World 1\""), freeCount);
108     assert(result);
109
110     // modify the 5th entry
111     size = 9999;
112     index = 8888;
113     result = InstanceIndexFile::modifyEntry(PATH, 
114         CIMObjectPath("X.key1=1005,key2=\"Hello World 5\""), 
115         index, size, freeCount);
116     assert(result);
117
118     //
119     // Iterate the entries in the file:
120     //   There should be 7 entries
121     //   The 1st, 3rd, 5th and 6th entries should be marked as 'free'.
122     //
123
124     {
125         Array<Uint32> freeFlags;
126         Array<Uint32> indices;
127         Array<Uint32> sizes;
128         Array<CIMObjectPath> instanceNames;
129
130         Boolean flag = InstanceIndexFile::enumerateEntries(
131             PATH, freeFlags, indices, sizes, instanceNames, true);
132
133         assert(flag);
134
135         assert(freeFlags.size() == indices.size());
136         assert(indices.size() == sizes.size());
137         assert(sizes.size() == instanceNames.size());
138
139         assert( freeFlags[0] == 1 &&
140                 freeFlags[2] == 1 &&
141                 freeFlags[4] == 1 &&
142                 freeFlags[5] == 1); 
143     }
144
145     //
146     // Now attempt to compact:
147     //
148     assert(InstanceIndexFile::compact(PATH));
149
150     //
151     // Verify the result:
152     //   There should be 3 entries and no 'free' entries
153     //
154     {
155         Array<Uint32> freeFlags;
156         Array<Uint32> indices;
157         Array<Uint32> sizes;
158         Array<CIMObjectPath> instanceNames;
159
160         Boolean flag = InstanceIndexFile::enumerateEntries(
161             PATH, freeFlags, indices, sizes, instanceNames, true);
162
163         assert(flag);
164
165         assert(freeFlags.size() == 3);
166         assert(freeFlags.size() == indices.size());
167         assert(indices.size() == sizes.size());
168         assert(sizes.size() == instanceNames.size());
169
170         for (Uint32 i = 0; i < freeFlags.size(); i++)
171         {
172             assert(freeFlags[i] == 0);
173         }
174     }
175 }
176
177 void _Test02()
178 {
179     String instancesPath (tmpDir);
180     instancesPath.append("/X.instances");
181     CString ipd = instancesPath.getCString();
182     const char* PATH = ipd;
183
184     //
185     // Append some instances:
186     //
187
188     Array<Sint8> data;
189     Uint32 index;
190
191     data.append("AAAAAAAA", 8);
192     InstanceDataFile::appendInstance(PATH, data, index);
193     data.clear();
194
195     data.append("BBBBBBBB", 8);
196     InstanceDataFile::appendInstance(PATH, data, index);
197     data.clear();
198
199     data.append("CCCCCCCC", 8);
200     InstanceDataFile::appendInstance(PATH, data, index);
201     data.clear();
202
203     //
204     // Load some instances:
205     //
206
207     InstanceDataFile::loadInstance(PATH, 8, 8, data);
208     assert(memcmp(data.getData(), "BBBBBBBB", 8) == 0);
209     data.clear();
210
211     InstanceDataFile::loadInstance(PATH, 0, 8, data);
212     assert(memcmp(data.getData(), "AAAAAAAA", 8) == 0);
213     data.clear();
214
215     InstanceDataFile::loadInstance(PATH, 16, 8, data);
216     assert(memcmp(data.getData(), "CCCCCCCC", 8) == 0);
217     data.clear();
218
219     //
220     // Load all instances:
221     //
222
223     InstanceDataFile::loadAllInstances(PATH, data);
224     assert(memcmp(data.getData(), "AAAAAAAABBBBBBBBCCCCCCCC", 24) == 0);
225     assert(data.size() == 3 * 8);
226     data.clear();
227
228     //
229     // Now attempt rollback:
230     //
231
232     assert(InstanceDataFile::beginTransaction(PATH));
233
234     data.append("ZZZZZZZZ", 8);
235     InstanceDataFile::appendInstance(PATH, data, index);
236     data.clear();
237
238     assert(InstanceDataFile::rollbackTransaction(PATH));
239
240     //
241     // Now attempt commit:
242     //
243
244     assert(InstanceDataFile::beginTransaction(PATH));
245
246     data.append("DDDDDDDD", 8);
247     InstanceDataFile::appendInstance(PATH, data, index);
248     data.clear();
249
250     assert(InstanceDataFile::commitTransaction(PATH));
251
252     //
253     // Verify the result:
254     //
255
256     InstanceDataFile::loadAllInstances(PATH, data);
257     assert(memcmp(data.getData(), "AAAAAAAABBBBBBBBCCCCCCCCDDDDDDDD", 32) == 0);
258     assert(data.size() == 4 * 8);
259     data.clear();
260
261     //
262     // Now attempt to compact:
263     //
264
265     Array<Uint32> freeFlags;
266     Array<Uint32> indices;
267     Array<Uint32> sizes;
268
269     freeFlags.append(0);
270     indices.append(0);
271     sizes.append(8);
272
273     freeFlags.append(0);
274     indices.append(16);
275     sizes.append(8);
276
277     assert(InstanceDataFile::compact(PATH, freeFlags, indices, sizes));
278
279     //
280     // Verify the result:
281     //
282
283     InstanceDataFile::loadAllInstances(PATH, data);
284     assert(memcmp(data.getData(), "AAAAAAAACCCCCCCC", 16) == 0);
285     assert(data.size() == 2 * 8);
286     data.clear();
287 }
288
289 int main(int argc, char** argv)
290 {
291     tmpDir = getenv ("PEGASUS_TMP");
292     if (tmpDir == NULL)
293     {
294         tmpDir = ".";
295     }
296
297     try
298     {
299         _Test01();
300         _Test02();
301     }
302
303     catch (Exception& e)
304     {
305         cerr << "Error: " << e.getMessage() << endl;
306         exit(1);
307     }
308
309     cout << "+++++ passed all tests" << endl;
310
311     return 0;
312 }