|
1 /* |
|
2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: FileTestUtils |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include <f32file.h> |
|
20 #include <fstream> |
|
21 #include <iostream> |
|
22 |
|
23 #include <DRMCommon.h> |
|
24 #include <DRMMessageParser.h> |
|
25 #include <DRMRightsClient.h> |
|
26 |
|
27 #include "filetestutils.h" |
|
28 |
|
29 using namespace std; |
|
30 using namespace java::filetest; |
|
31 |
|
32 void FileTestUtils::createFile(const wstring &aPath, int aFlags, int aMode) |
|
33 { |
|
34 int fd = wopen(aPath.c_str(), aFlags, aMode); |
|
35 FileTestUtils::checkError(aPath,fd); |
|
36 close(fd); |
|
37 } |
|
38 |
|
39 |
|
40 |
|
41 HBufC* stringToDes(const wchar_t* str) |
|
42 { |
|
43 HBufC* resultBuf = 0; |
|
44 try |
|
45 { |
|
46 if (str) |
|
47 { |
|
48 int len = wcslen(str); |
|
49 resultBuf = HBufC::New(len + 1); |
|
50 TPtr ptr = resultBuf->Des(); |
|
51 |
|
52 TPtr16 ptr16((TUint16 *)str, len); |
|
53 ptr16.SetLength(len); |
|
54 ptr.Copy(ptr16); |
|
55 ptr.ZeroTerminate(); |
|
56 } |
|
57 } |
|
58 catch (...) |
|
59 { |
|
60 |
|
61 } |
|
62 return resultBuf; |
|
63 } |
|
64 |
|
65 |
|
66 /** |
|
67 * |
|
68 */ |
|
69 void ReplaceCharacters(TPtr& aPtr,TUint8 aReplacedChar, |
|
70 TUint8 aNewChar,TBool aOnlyFirstMatch) |
|
71 { |
|
72 for (TInt i = 0; i < aPtr.Length(); ++i) |
|
73 { |
|
74 if (aReplacedChar == aPtr[i]) |
|
75 { |
|
76 aPtr[i] = aNewChar; |
|
77 if (aOnlyFirstMatch) |
|
78 return; |
|
79 } |
|
80 } |
|
81 } |
|
82 |
|
83 void ReplaceCharacters(TPtr& aPtr,TUint8 aReplacedChar, |
|
84 TUint8 aNewChar) |
|
85 { |
|
86 ReplaceCharacters(aPtr,aReplacedChar,aNewChar,EFalse); |
|
87 } |
|
88 |
|
89 |
|
90 |
|
91 void FileTestUtils::createSystemFile(const wstring &aPath) |
|
92 { |
|
93 FileTestUtils::createNormalFile(aPath); |
|
94 wchar_t* filePtr = const_cast<wchar_t*>(aPath.c_str()); |
|
95 HBufC* tmpBuf = stringToDes(filePtr); |
|
96 TPtr tmpBufPtr(tmpBuf->Des()); |
|
97 ReplaceCharacters(tmpBufPtr,'/','\\'); |
|
98 |
|
99 RFs aRFs; |
|
100 aRFs.Connect(); |
|
101 int err = aRFs.SetAtt(*tmpBuf,KEntryAttSystem,0); |
|
102 aRFs.Close(); |
|
103 //Donot know what to do in linux |
|
104 checkError(aPath, err); |
|
105 } |
|
106 |
|
107 void FileTestUtils::createHiddenDir(const wstring &aPath, const wstring& aFile) |
|
108 { |
|
109 FileTestUtils::createDir(aPath+aFile); |
|
110 |
|
111 wstring fileName = (aPath+aFile); |
|
112 |
|
113 wchar_t* filePtr = const_cast<wchar_t*>(fileName.c_str()); |
|
114 HBufC* tmpBuf = stringToDes(filePtr); |
|
115 TPtr tmpBufPtr(tmpBuf->Des()); |
|
116 ReplaceCharacters(tmpBufPtr,'/','\\'); |
|
117 |
|
118 RFs aRFs; |
|
119 aRFs.Connect(); |
|
120 int error = aRFs.SetAtt(*tmpBuf ,KEntryAttHidden,0); |
|
121 aRFs.Close(); |
|
122 checkError(aPath+aFile, error); |
|
123 } |
|
124 |
|
125 void FileTestUtils::createSystemDir(const wstring &aPath) |
|
126 { |
|
127 FileTestUtils::createDir(aPath); |
|
128 wchar_t* filePtr = const_cast<wchar_t*>(aPath.c_str()); |
|
129 HBufC* tmpBuf = stringToDes(filePtr); |
|
130 TPtr tmpBufPtr(tmpBuf->Des()); |
|
131 ReplaceCharacters(tmpBufPtr,'/','\\'); |
|
132 |
|
133 RFs aRFs; |
|
134 aRFs.Connect(); |
|
135 int err = aRFs.SetAtt(*tmpBuf,KEntryAttSystem,0); |
|
136 aRFs.Close(); |
|
137 //Donot know what to do in linux |
|
138 checkError(aPath, err); |
|
139 } |
|
140 |
|
141 |
|
142 void FileTestUtils::createDirMode(const wstring& aPath, const int aMode) |
|
143 { |
|
144 // R W X |
|
145 // 4 2 1 |
|
146 int error = wmkdir(aPath.c_str(), aMode); |
|
147 FileTestUtils::checkError(aPath, error); |
|
148 } |
|
149 |
|
150 void FileTestUtils::writeDataToFile(const wstring& aPath, const string& aData) |
|
151 { |
|
152 //Linux, just use ofstream |
|
153 int fd = wopen(aPath.c_str(), O_RDWR); |
|
154 |
|
155 checkError(aPath, fd); |
|
156 write(fd, aData.c_str(), aData.length()); |
|
157 close(fd); |
|
158 } |
|
159 |
|
160 void FileTestUtils::writeDataInLoop(const wstring& aPath, const int aData, const int aTimes) |
|
161 { |
|
162 char* chr = new char[aPath.size()+1]; |
|
163 sprintf(chr,"%ls",aPath.c_str()); |
|
164 wofstream file(chr, ios::out|ios::app|ios::binary); |
|
165 |
|
166 for (int i=0 ; i<aTimes; i++) |
|
167 { |
|
168 file<<(char)aData; |
|
169 } |
|
170 file.close(); |
|
171 |
|
172 } |
|
173 |
|
174 void FileTestUtils::appendDataToFile(const wstring& aPath, const int aData) |
|
175 { |
|
176 char* chr = new char[aPath.size()+1]; |
|
177 sprintf(chr,"%ls",aPath.c_str()); |
|
178 wofstream file(chr, ios::out|ios::app|ios::binary); |
|
179 file<<(char)aData; |
|
180 file.close(); |
|
181 } |
|
182 |
|
183 void FileTestUtils::appendDataToFile(const wstring& aPath, const wstring& aData) |
|
184 { |
|
185 char* chr = new char[aPath.size()+1]; |
|
186 sprintf(chr,"%ls",aPath.c_str()); |
|
187 wofstream file(chr, ios::out|ios::app|ios::binary); |
|
188 file<<(char*)aData.c_str(); |
|
189 file.close(); |
|
190 } |
|
191 |
|
192 void FileTestUtils::setFileHidden(const wstring &aPath, const wstring &aFile) |
|
193 { |
|
194 wstring fileName = (aPath+aFile); |
|
195 |
|
196 wchar_t* filePtr = const_cast<wchar_t*>(fileName.c_str()); |
|
197 HBufC* tmpBuf = stringToDes(filePtr); |
|
198 TPtr tmpBufPtr(tmpBuf->Des()); |
|
199 ReplaceCharacters(tmpBufPtr,'/','\\'); |
|
200 |
|
201 RFs aRFs; |
|
202 aRFs.Connect(); |
|
203 int error = aRFs.SetAtt(*tmpBuf ,KEntryAttHidden,0); |
|
204 aRFs.Close(); |
|
205 checkError(aPath+aFile, error); |
|
206 } |
|
207 |
|
208 void FileTestUtils::setFileReadOnly(const wstring &aPath) |
|
209 { |
|
210 wchmod(aPath.c_str(), 0444); |
|
211 } |
|
212 |
|
213 void FileTestUtils::setDirHidden(const wstring &aPath, const wstring &aName) |
|
214 { |
|
215 wstring fileName = (aPath+aName); |
|
216 |
|
217 wchar_t* filePtr = const_cast<wchar_t*>(fileName.c_str()); |
|
218 HBufC* tmpBuf = stringToDes(filePtr); |
|
219 TPtr tmpBufPtr(tmpBuf->Des()); |
|
220 ReplaceCharacters(tmpBufPtr,'/','\\'); |
|
221 |
|
222 RFs aRFs; |
|
223 aRFs.Connect(); |
|
224 int error = aRFs.SetAtt(*tmpBuf ,KEntryAttHidden,0); |
|
225 aRFs.Close(); |
|
226 checkError(aPath+aName, error); |
|
227 } |
|
228 |
|
229 |
|
230 /******************************************************************************* |
|
231 |
|
232 |
|
233 DRM Methods: Specific to S60 |
|
234 |
|
235 ******************************************************************************/ |
|
236 |
|
237 void InitDrFile(RFs& aRfs,const TDesC& aFileName) |
|
238 { |
|
239 //_LIT( KTempFile, "c:\\mainTestDir\\readme.dr"); |
|
240 TInt size; |
|
241 RFile file; |
|
242 //CDRMRights* rightsDetail = NULL; |
|
243 RPointerArray<CDRMRights> rightsDetails; |
|
244 |
|
245 //DRMAuthenticated *drm = DRMAuthenticated::NewL(); |
|
246 CDRMMessageParser* msgParser = CDRMMessageParser::NewL(); |
|
247 CleanupStack::PushL(msgParser); |
|
248 |
|
249 User::LeaveIfError(file.Open(aRfs,aFileName,EFileRead)); |
|
250 User::LeaveIfError(file.Size(size)); |
|
251 HBufC8* buffer = HBufC8::NewLC(size); |
|
252 TPtr8 ptr(buffer->Des()); |
|
253 |
|
254 User::LeaveIfError(file.Read(ptr, size)); |
|
255 CleanupStack::Pop(buffer); |
|
256 |
|
257 User::LeaveIfError(msgParser->ProcessRightsObject(ptr, rightsDetails)); |
|
258 |
|
259 delete buffer; |
|
260 //delete rightsDetail; |
|
261 rightsDetails.ResetAndDestroy(); |
|
262 |
|
263 CleanupStack::PopAndDestroy(); // drm |
|
264 file.Close(); |
|
265 } |
|
266 |
|
267 |
|
268 |
|
269 //We will have to unzip winscw.zip in ..\doc folder to \epoc32\winscw\ for this |
|
270 //to work |
|
271 void FileTestUtils::doInitDRMContents() |
|
272 { |
|
273 RFs aRFs; |
|
274 aRFs.Connect(); |
|
275 RDRMRightsClient rightsClient; |
|
276 User::LeaveIfError(rightsClient.Connect()); |
|
277 TInt err = rightsClient.DeleteAll(); |
|
278 rightsClient.Close(); |
|
279 User::LeaveIfError(err); |
|
280 |
|
281 //InitDrFile(aRFs,_L("c:\\data\\Videos\\drmFiles\\drmfile002DecExecuteCount2.dr")); |
|
282 //InitDrFile(aRFs,_L("c:\\data\\Videos\\drmFiles\\readme2.dr")); |
|
283 |
|
284 InitDrFile(aRFs,_L("c:\\data\\Videos\\drmFiles\\drmfile001PlayPreview.dr")); |
|
285 InitDrFile(aRFs,_L("c:\\data\\Videos\\drmFiles\\drmfile001DisplayPreview.dr")); |
|
286 InitDrFile(aRFs,_L("c:\\data\\Videos\\drmFiles\\drmfile001PrintPreview.dr")); |
|
287 InitDrFile(aRFs,_L("c:\\data\\Videos\\drmFiles\\drmfile001ExecutePreview.dr")); |
|
288 InitDrFile(aRFs,_L("c:\\data\\Videos\\drmFiles\\drmfile002DecDisplayCount2.dr")); |
|
289 InitDrFile(aRFs,_L("c:\\data\\Videos\\drmFiles\\drmfile002DecPlayCount2.dr")); |
|
290 InitDrFile(aRFs,_L("c:\\data\\Videos\\drmFiles\\drmfile002DecPrintCount2.dr")); |
|
291 InitDrFile(aRFs,_L("c:\\data\\Videos\\drmFiles\\drmfile002DecExecuteCount2.dr")); |
|
292 InitDrFile(aRFs,_L("c:\\data\\Videos\\drmFiles\\drmfile002EncCount1.dr")); |
|
293 InitDrFile(aRFs,_L("c:\\data\\Videos\\drmFiles\\drmfile003_01DecPlayCount2.dr")); |
|
294 InitDrFile(aRFs,_L("c:\\data\\Videos\\drmFiles\\drmfile003_02DecPlayCount2.dr")); |
|
295 InitDrFile(aRFs,_L("c:\\data\\Videos\\drmFiles\\drmfile003_03DecPlayCount2.dr")); |
|
296 InitDrFile(aRFs,_L("c:\\data\\Videos\\drmFiles\\drmfile003_04DecPlayCount50.dr")); |
|
297 InitDrFile(aRFs,_L("c:\\data\\Videos\\drmFiles\\drmfile004DecPlayCount1.dr")); |
|
298 InitDrFile(aRFs,_L("c:\\data\\Videos\\drmFiles\\drmfile006_6DecPlayCount1.dr")); |
|
299 InitDrFile(aRFs,_L("c:\\data\\Videos\\drmFiles\\drmfile006_7DecPlayCount10.dr")); |
|
300 InitDrFile(aRFs,_L("c:\\data\\Videos\\drmFiles\\drmfile006DecPlayCount1.dr")); |
|
301 InitDrFile(aRFs,_L("c:\\data\\Videos\\drmFiles\\drmfile007DecPlayCount2.dr")); |
|
302 InitDrFile(aRFs,_L("c:\\data\\Videos\\drmFiles\\drmfile008DecPlayCount2.dr")); |
|
303 InitDrFile(aRFs,_L("c:\\data\\Videos\\drmFiles\\drmfile009DecPlayCount1.dr")); |
|
304 InitDrFile(aRFs,_L("c:\\data\\Videos\\drmFiles\\drmfile010DecPlayCount2.dr")); |
|
305 InitDrFile(aRFs,_L("c:\\data\\Videos\\drmFiles\\drmfile011DecPlayCount4.dr")); |
|
306 InitDrFile(aRFs,_L("c:\\data\\Videos\\drmFiles\\drmfile012DecPlayCount4.dr")); |
|
307 InitDrFile(aRFs,_L("c:\\data\\Videos\\drmFiles\\drmfile013DecPlayCount1.dr")); |
|
308 InitDrFile(aRFs,_L("c:\\data\\Videos\\drmFiles\\drmfile014DecPlayCount2.dr")); |
|
309 InitDrFile(aRFs,_L("c:\\data\\Videos\\drmFiles\\drmfile015DecPlayCount3.dr")); |
|
310 InitDrFile(aRFs,_L("c:\\data\\Videos\\drmFiles\\testDir001\\drmfile015_001DecPlayCount3.dr")); |
|
311 InitDrFile(aRFs,_L("c:\\data\\Videos\\drmFiles\\drmfile016DecPlayCount2.dr")); |
|
312 InitDrFile(aRFs,_L("c:\\data\\Videos\\drmFiles\\drmfile017DecPlayCount10.dr")); |
|
313 InitDrFile(aRFs,_L("c:\\data\\Videos\\drmFiles\\drmfile018DecPlayCount10.dr")); |
|
314 InitDrFile(aRFs,_L("c:\\data\\Videos\\drmFiles\\drmfile019DecPlayCount2.dr")); |
|
315 InitDrFile(aRFs,_L("c:\\data\\Videos\\drmFiles\\drmfile020DecPlayCount3.dr")); |
|
316 InitDrFile(aRFs,_L("c:\\data\\Videos\\drmFiles\\drmfile021DecPlayCount2.dr")); |
|
317 InitDrFile(aRFs,_L("c:\\data\\Videos\\drmFiles\\drmfile022DecPlayCount2.dr")); |
|
318 InitDrFile(aRFs,_L("c:\\data\\Videos\\drmFiles\\drmfile023DecPlayCount10.dr")); |
|
319 InitDrFile(aRFs,_L("c:\\data\\Videos\\drmFiles\\drmfile024DecPlayCount50.dr")); |
|
320 InitDrFile(aRFs,_L("c:\\data\\Videos\\drmFiles\\drmfile024DecDisplayCount50.dr")); |
|
321 InitDrFile(aRFs,_L("c:\\data\\Videos\\drmFiles\\drmfile024DecPrintCount50.dr")); |
|
322 InitDrFile(aRFs,_L("c:\\data\\Videos\\drmFiles\\drmfile024DecExecuteCount50.dr")); |
|
323 InitDrFile(aRFs,_L("c:\\data\\Videos\\drmFiles\\drmfile025DecPlayCount3.dr")); |
|
324 InitDrFile(aRFs,_L("c:\\data\\Images\\myDir\\drmfile026DecPlayCount50.dr")); |
|
325 |
|
326 InitDrFile(aRFs,_L("e:\\drmfile003DecPlayCount1.dr")); |
|
327 InitDrFile(aRFs,_L("e:\\drmfile006_61DecPlayCount1.dr")); |
|
328 InitDrFile(aRFs,_L("e:\\drmfile025DecPrintCount50.dr")); |
|
329 InitDrFile(aRFs,_L("e:\\drmfile003_05DecPlayCount50.dr")); |
|
330 } |