xref: /aoo4110/main/store/workben/t_file.cxx (revision b1cdbd2c)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_store.hxx"
26 
27 #include "sal/types.h"
28 #include "osl/thread.h"
29 #include "rtl/ustring.hxx"
30 
31 #include "lockbyte.hxx"
32 
33 #ifndef INCLUDED_STDIO_H
34 #include <stdio.h>
35 #define INCLUDED_STDIO_H
36 #endif
37 
38 #include "osl/file.h"
39 #include "osl/process.h"
40 
41 using namespace store;
42 
43 #define TEST_PAGESIZE 16384
44 
45 /*========================================================================
46  *
47  * main.
48  *
49  *======================================================================*/
main(int argc,char ** argv)50 int SAL_CALL main (int argc, char **argv)
51 {
52 	storeError eErrCode = store_E_None;
53 	rtl::Reference<ILockBytes> xLockBytes;
54 
55     if (argc > 1)
56     {
57         rtl::OUString aFilename (
58             argv[1], rtl_str_getLength(argv[1]),
59             osl_getThreadTextEncoding());
60 
61 #if 0   /* EXP */
62 		oslFileError result;
63 		rtl::OUString aPath;
64 
65 		result = osl_getFileURLFromSystemPath(aFilename.pData, &(aPath.pData));
66 		if (result != osl_File_E_None)
67 		{
68 			// not SystemPath, assume FileUrl.
69 			aPath = aFilename;
70 		}
71 		if (rtl_ustr_ascii_shortenedCompare_WithLength(aPath.pData->buffer, aPath.pData->length, "file://", 7) != 0)
72 		{
73 			// not FileUrl, assume relative path.
74 			rtl::OUString aBase;
75 			(void) osl_getProcessWorkingDir (&(aBase.pData));
76 
77 			// absolute FileUrl.
78 			(void) osl_getAbsoluteFileURL(aBase.pData, aPath.pData, &(aPath.pData));
79 		}
80 		aFilename = aPath;
81 #endif  /* EXP */
82 
83         eErrCode = FileLockBytes_createInstance (
84             xLockBytes, aFilename.pData, store_AccessReadWrite);
85         if (eErrCode != store_E_None)
86         {
87             // Check reason.
88             if (eErrCode != store_E_NotExists)
89             {
90                 fprintf (stderr, "t_file: create() error: %d\n", eErrCode);
91                 return eErrCode;
92             }
93 
94             // Create.
95             eErrCode = FileLockBytes_createInstance (
96                 xLockBytes, aFilename.pData, store_AccessReadCreate);
97             if (eErrCode != store_E_None)
98             {
99                 fprintf (stderr, "t_file: create() error: %d\n", eErrCode);
100                 return eErrCode;
101             }
102         }
103         fprintf (stdout, "t_file: using FileLockBytes(\"%s\") implementation.\n", argv[1]);
104     }
105     else
106     {
107         eErrCode = MemoryLockBytes_createInstance (xLockBytes);
108         if (eErrCode != store_E_None)
109         {
110             fprintf (stderr, "t_file: create() error: %d\n", eErrCode);
111             return eErrCode;
112         }
113         fprintf (stdout, "t_file: using MemoryLockBytes implementation.\n");
114     }
115 
116     rtl::Reference< PageData::Allocator > xAllocator;
117     eErrCode = xLockBytes->initialize (xAllocator, TEST_PAGESIZE);
118     if (eErrCode != store_E_None)
119     {
120         fprintf (stderr, "t_file: initialize() error: %d\n", eErrCode);
121         return eErrCode;
122     }
123 
124 	sal_Char buffer[TEST_PAGESIZE];
125 	rtl_fillMemory (buffer, sizeof(buffer), sal_uInt8('B'));
126 
127 	sal_uInt32 i, k;
128 	for (k = 0; k < 4; k++)
129 	{
130 		sal_uInt32 index = k * TEST_PAGESIZE / 4;
131 		buffer[index] = 'A';
132 	}
133 
134 	for (i = 0; i < 256; i++)
135 	{
136 		sal_uInt32 offset = i * TEST_PAGESIZE;
137 		eErrCode = xLockBytes->setSize (offset + TEST_PAGESIZE);
138 		if (eErrCode != store_E_None)
139 		{
140 			fprintf (stderr, "t_file: setSize() error: %d\n", eErrCode);
141 			return eErrCode;
142 		}
143 
144 		for (k = 0; k < 4; k++)
145 		{
146 			sal_uInt32 magic = i * 4 + k;
147 			if (magic)
148 			{
149 				sal_uInt32 verify = 0;
150 				eErrCode = xLockBytes->readAt (
151 					0, &verify, sizeof(verify));
152 				if (eErrCode != store_E_None)
153 				{
154 					fprintf (stderr, "t_file: readAt() error: %d\n", eErrCode);
155 					return eErrCode;
156 				}
157 				if (verify != magic)
158 				{
159 					// Failure.
160 					fprintf (stderr, "Expected %ld read %ld\n", (unsigned long)(magic), (unsigned long)(verify));
161 				}
162 			}
163 
164 			sal_uInt32 index = k * TEST_PAGESIZE / 4;
165 			eErrCode = xLockBytes->writeAt (
166 				offset + index, &(buffer[index]), TEST_PAGESIZE / 4);
167 			if (eErrCode != store_E_None)
168 			{
169 				fprintf (stderr, "t_file: writeAt() error: %d\n", eErrCode);
170 				return eErrCode;
171 			}
172 
173 			magic += 1;
174 			eErrCode = xLockBytes->writeAt (
175 				0, &magic, sizeof(magic));
176 			if (eErrCode != store_E_None)
177 			{
178 				fprintf (stderr, "t_file: writeAt() error: %d\n", eErrCode);
179 				return eErrCode;
180 			}
181 		}
182 	}
183 
184 	eErrCode = xLockBytes->flush();
185 	if (eErrCode != store_E_None)
186 	{
187 		fprintf (stderr, "t_file: flush() error: %d\n", eErrCode);
188 		return eErrCode;
189 	}
190 
191 	sal_Char verify[TEST_PAGESIZE];
192 	for (i = 0; i < 256; i++)
193 	{
194 		sal_uInt32 offset = i * TEST_PAGESIZE;
195 
196 		eErrCode = xLockBytes->readAt (offset, verify, TEST_PAGESIZE);
197 		if (eErrCode != store_E_None)
198 		{
199 			fprintf (stderr, "t_file: readAt() error: %d\n", eErrCode);
200 			return eErrCode;
201 		}
202 
203 		sal_uInt32 index = 0;
204 		if (offset == 0)
205 		{
206 			sal_uInt32 magic = 256 * 4;
207 			if (rtl_compareMemory (&verify[index], &magic, sizeof(magic)))
208 			{
209 				// Failure.
210 				fprintf (stderr, "t_file: Unexpected value at 0x00000000\n");
211 			}
212 			index += 4;
213 		}
214 		if (rtl_compareMemory (
215 			&verify[index], &buffer[index], TEST_PAGESIZE - index))
216 		{
217 			// Failure.
218 			fprintf (stderr, "t_file: Unexpected block at 0x%08x\n", (unsigned)(offset));
219 		}
220 	}
221 
222 	for (i = 0; i < 256; i++)
223 	{
224         PageHolder xPage;
225 		sal_uInt32 offset = i * TEST_PAGESIZE;
226 
227 		eErrCode = xLockBytes->readPageAt (xPage, offset);
228 		if (eErrCode != store_E_None)
229 		{
230 			fprintf (stderr, "t_file: readPageAt() error: %d\n", eErrCode);
231 			return eErrCode;
232 		}
233 
234         PageData * page = xPage.get();
235         (void)page; // UNUSED
236     }
237 
238 	xLockBytes.clear();
239 	return 0;
240 }
241