xref: /aoo41x/main/store/workben/t_file.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_store.hxx"
30 
31 #include "sal/types.h"
32 #include "osl/thread.h"
33 #include "rtl/ustring.hxx"
34 
35 #include "lockbyte.hxx"
36 
37 #ifndef INCLUDED_STDIO_H
38 #include <stdio.h>
39 #define INCLUDED_STDIO_H
40 #endif
41 
42 #include "osl/file.h"
43 #include "osl/process.h"
44 
45 using namespace store;
46 
47 #define TEST_PAGESIZE 16384
48 
49 /*========================================================================
50  *
51  * main.
52  *
53  *======================================================================*/
54 int SAL_CALL main (int argc, char **argv)
55 {
56 	storeError eErrCode = store_E_None;
57 	rtl::Reference<ILockBytes> xLockBytes;
58 
59     if (argc > 1)
60     {
61         rtl::OUString aFilename (
62             argv[1], rtl_str_getLength(argv[1]),
63             osl_getThreadTextEncoding());
64 
65 #if 0   /* EXP */
66 		oslFileError result;
67 		rtl::OUString aPath;
68 
69 		result = osl_getFileURLFromSystemPath(aFilename.pData, &(aPath.pData));
70 		if (result != osl_File_E_None)
71 		{
72 			// not SystemPath, assume FileUrl.
73 			aPath = aFilename;
74 		}
75 		if (rtl_ustr_ascii_shortenedCompare_WithLength(aPath.pData->buffer, aPath.pData->length, "file://", 7) != 0)
76 		{
77 			// not FileUrl, assume relative path.
78 			rtl::OUString aBase;
79 			(void) osl_getProcessWorkingDir (&(aBase.pData));
80 
81 			// absolute FileUrl.
82 			(void) osl_getAbsoluteFileURL(aBase.pData, aPath.pData, &(aPath.pData));
83 		}
84 		aFilename = aPath;
85 #endif  /* EXP */
86 
87         eErrCode = FileLockBytes_createInstance (
88             xLockBytes, aFilename.pData, store_AccessReadWrite);
89         if (eErrCode != store_E_None)
90         {
91             // Check reason.
92             if (eErrCode != store_E_NotExists)
93             {
94                 fprintf (stderr, "t_file: create() error: %d\n", eErrCode);
95                 return eErrCode;
96             }
97 
98             // Create.
99             eErrCode = FileLockBytes_createInstance (
100                 xLockBytes, aFilename.pData, store_AccessReadCreate);
101             if (eErrCode != store_E_None)
102             {
103                 fprintf (stderr, "t_file: create() error: %d\n", eErrCode);
104                 return eErrCode;
105             }
106         }
107         fprintf (stdout, "t_file: using FileLockBytes(\"%s\") implementation.\n", argv[1]);
108     }
109     else
110     {
111         eErrCode = MemoryLockBytes_createInstance (xLockBytes);
112         if (eErrCode != store_E_None)
113         {
114             fprintf (stderr, "t_file: create() error: %d\n", eErrCode);
115             return eErrCode;
116         }
117         fprintf (stdout, "t_file: using MemoryLockBytes implementation.\n");
118     }
119 
120     rtl::Reference< PageData::Allocator > xAllocator;
121     eErrCode = xLockBytes->initialize (xAllocator, TEST_PAGESIZE);
122     if (eErrCode != store_E_None)
123     {
124         fprintf (stderr, "t_file: initialize() error: %d\n", eErrCode);
125         return eErrCode;
126     }
127 
128 	sal_Char buffer[TEST_PAGESIZE];
129 	rtl_fillMemory (buffer, sizeof(buffer), sal_uInt8('B'));
130 
131 	sal_uInt32 i, k;
132 	for (k = 0; k < 4; k++)
133 	{
134 		sal_uInt32 index = k * TEST_PAGESIZE / 4;
135 		buffer[index] = 'A';
136 	}
137 
138 	for (i = 0; i < 256; i++)
139 	{
140 		sal_uInt32 offset = i * TEST_PAGESIZE;
141 		eErrCode = xLockBytes->setSize (offset + TEST_PAGESIZE);
142 		if (eErrCode != store_E_None)
143 		{
144 			fprintf (stderr, "t_file: setSize() error: %d\n", eErrCode);
145 			return eErrCode;
146 		}
147 
148 		for (k = 0; k < 4; k++)
149 		{
150 			sal_uInt32 magic = i * 4 + k;
151 			if (magic)
152 			{
153 				sal_uInt32 verify = 0;
154 				eErrCode = xLockBytes->readAt (
155 					0, &verify, sizeof(verify));
156 				if (eErrCode != store_E_None)
157 				{
158 					fprintf (stderr, "t_file: readAt() error: %d\n", eErrCode);
159 					return eErrCode;
160 				}
161 				if (verify != magic)
162 				{
163 					// Failure.
164 					fprintf (stderr, "Expected %ld read %ld\n", (unsigned long)(magic), (unsigned long)(verify));
165 				}
166 			}
167 
168 			sal_uInt32 index = k * TEST_PAGESIZE / 4;
169 			eErrCode = xLockBytes->writeAt (
170 				offset + index, &(buffer[index]), TEST_PAGESIZE / 4);
171 			if (eErrCode != store_E_None)
172 			{
173 				fprintf (stderr, "t_file: writeAt() error: %d\n", eErrCode);
174 				return eErrCode;
175 			}
176 
177 			magic += 1;
178 			eErrCode = xLockBytes->writeAt (
179 				0, &magic, sizeof(magic));
180 			if (eErrCode != store_E_None)
181 			{
182 				fprintf (stderr, "t_file: writeAt() error: %d\n", eErrCode);
183 				return eErrCode;
184 			}
185 		}
186 	}
187 
188 	eErrCode = xLockBytes->flush();
189 	if (eErrCode != store_E_None)
190 	{
191 		fprintf (stderr, "t_file: flush() error: %d\n", eErrCode);
192 		return eErrCode;
193 	}
194 
195 	sal_Char verify[TEST_PAGESIZE];
196 	for (i = 0; i < 256; i++)
197 	{
198 		sal_uInt32 offset = i * TEST_PAGESIZE;
199 
200 		eErrCode = xLockBytes->readAt (offset, verify, TEST_PAGESIZE);
201 		if (eErrCode != store_E_None)
202 		{
203 			fprintf (stderr, "t_file: readAt() error: %d\n", eErrCode);
204 			return eErrCode;
205 		}
206 
207 		sal_uInt32 index = 0;
208 		if (offset == 0)
209 		{
210 			sal_uInt32 magic = 256 * 4;
211 			if (rtl_compareMemory (&verify[index], &magic, sizeof(magic)))
212 			{
213 				// Failure.
214 				fprintf (stderr, "t_file: Unexpected value at 0x00000000\n");
215 			}
216 			index += 4;
217 		}
218 		if (rtl_compareMemory (
219 			&verify[index], &buffer[index], TEST_PAGESIZE - index))
220 		{
221 			// Failure.
222 			fprintf (stderr, "t_file: Unexpected block at 0x%08x\n", (unsigned)(offset));
223 		}
224 	}
225 
226 	for (i = 0; i < 256; i++)
227 	{
228         PageHolder xPage;
229 		sal_uInt32 offset = i * TEST_PAGESIZE;
230 
231 		eErrCode = xLockBytes->readPageAt (xPage, offset);
232 		if (eErrCode != store_E_None)
233 		{
234 			fprintf (stderr, "t_file: readPageAt() error: %d\n", eErrCode);
235 			return eErrCode;
236 		}
237 
238         PageData * page = xPage.get();
239         (void)page; // UNUSED
240     }
241 
242 	xLockBytes.clear();
243 	return 0;
244 }
245