Avoid potential negative array index access to cached text.
[LibreOffice.git] / svl / qa / unit / lockfiles / test_lockfiles.cxx
blobd66c301be4e793581ce8e54f507e7987f6fd2bcc
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 #include <sal/config.h>
12 #include <string_view>
14 #include <cppunit/extensions/HelperMacros.h>
15 #include <cppunit/plugin/TestPlugIn.h>
16 #include <test/bootstrapfixture.hxx>
18 #include <o3tl/cppunittraitshelper.hxx>
19 #include <unotest/directories.hxx>
20 #include <svl/lockfilecommon.hxx>
21 #include <svl/documentlockfile.hxx>
22 #include <svl/msodocumentlockfile.hxx>
23 #include <unotools/useroptions.hxx>
24 #include <tools/stream.hxx>
25 #include <rtl/strbuf.hxx>
26 #include <osl/security.hxx>
27 #include <osl/socket.hxx>
28 #include <unotools/bootstrap.hxx>
30 namespace
32 class LockfileTest : public test::BootstrapFixture
34 OUString generateTestURL(std::u16string_view sFileName) const;
36 public:
37 void testLOLockFileURL();
38 void testLOLockFileContent();
39 void testLOLockFileRT();
40 void testLOLockFileUnicodeUsername();
41 void testLOLockFileOverwrite();
42 void testWordLockFileURL();
43 void testExcelLockFileURL();
44 void testPowerPointLockFileURL();
45 void testWordLockFileContent();
46 void testExcelLockFileContent();
47 void testPowerPointLockFileContent();
48 void testWordLockFileRT();
49 void testExcelLockFileRT();
50 void testPowerPointLockFileRT();
51 void testMSOLockFileLongUserName();
52 void testMSOLockFileUnicodeUsername();
53 void testMSOLockFileOverwrite();
55 private:
56 CPPUNIT_TEST_SUITE(LockfileTest);
57 CPPUNIT_TEST(testLOLockFileURL);
58 CPPUNIT_TEST(testLOLockFileContent);
59 CPPUNIT_TEST(testLOLockFileRT);
60 CPPUNIT_TEST(testLOLockFileUnicodeUsername);
61 CPPUNIT_TEST(testLOLockFileOverwrite);
62 CPPUNIT_TEST(testWordLockFileURL);
63 CPPUNIT_TEST(testExcelLockFileURL);
64 CPPUNIT_TEST(testPowerPointLockFileURL);
65 CPPUNIT_TEST(testWordLockFileContent);
66 CPPUNIT_TEST(testExcelLockFileContent);
67 CPPUNIT_TEST(testPowerPointLockFileContent);
68 CPPUNIT_TEST(testWordLockFileRT);
69 CPPUNIT_TEST(testExcelLockFileRT);
70 CPPUNIT_TEST(testPowerPointLockFileRT);
71 CPPUNIT_TEST(testMSOLockFileLongUserName);
72 CPPUNIT_TEST(testMSOLockFileUnicodeUsername);
73 CPPUNIT_TEST(testMSOLockFileOverwrite);
74 CPPUNIT_TEST_SUITE_END();
77 OUString readLockFile(const OUString& aSource)
79 SvFileStream aFileStream(aSource, StreamMode::READ);
80 std::size_t nSize = aFileStream.remainingSize();
81 std::unique_ptr<sal_Int8[]> pBuffer(new sal_Int8[nSize]);
82 aFileStream.ReadBytes(pBuffer.get(), nSize);
84 const css::uno::Sequence<sal_Int8> aData(pBuffer.get(), nSize);
85 OStringBuffer aResult(static_cast<int>(nSize));
86 for (sal_Int8 nByte : aData)
88 aResult.append(static_cast<char>(nByte));
90 return OStringToOUString(aResult.makeStringAndClear(), RTL_TEXTENCODING_UTF8);
93 OUString GetLockFileName(const svt::GenDocumentLockFile& rLockFile)
95 INetURLObject aDocURL = svt::LockFileCommon::ResolveLinks(INetURLObject(rLockFile.GetURL()));
96 return aDocURL.GetLastName();
99 OUString LockfileTest::generateTestURL(std::u16string_view sFileName) const
101 return m_directories.getURLFromWorkdir(u"/CppunitTest/svl_lockfiles.test.user/") + sFileName;
104 void LockfileTest::testLOLockFileURL()
106 // Test the generated file name for LibreOffice lock files
107 OUString aTestODT = generateTestURL(u"testLOLockFileURL.odt");
109 svt::DocumentLockFile aLockFile(aTestODT);
110 CPPUNIT_ASSERT_EQUAL(OUString(".~lock.testLOLockFileURL.odt%23"), GetLockFileName(aLockFile));
113 void LockfileTest::testLOLockFileContent()
115 // Test the lockfile generated for the specified ODT document
116 OUString aTestODT = generateTestURL(u"testLOLockFileContent.odt");
118 // Set user name
119 SvtUserOptions aUserOpt;
120 aUserOpt.SetToken(UserOptToken::FirstName, "LockFile");
121 aUserOpt.SetToken(UserOptToken::LastName, "Test");
123 // Write the lock file and check the content
124 svt::DocumentLockFile aLockFile(aTestODT);
125 aLockFile.CreateOwnLockFile();
126 OUString sLockFileContent(readLockFile(aLockFile.GetURL()));
127 aLockFile.RemoveFileDirectly();
129 // User name
130 sal_Int32 nFirstChar = 0;
131 sal_Int32 nNextComma = sLockFileContent.indexOf(',', nFirstChar);
132 OUString sUserName = aUserOpt.GetFirstName() + " " + aUserOpt.GetLastName();
133 CPPUNIT_ASSERT_EQUAL(sUserName, sLockFileContent.copy(nFirstChar, nNextComma - nFirstChar));
135 // System user name
136 nFirstChar = nNextComma + 1;
137 nNextComma = sLockFileContent.indexOf(',', nFirstChar);
138 ::osl::Security aSecurity;
139 OUString sSysUserName;
140 aSecurity.getUserName(sSysUserName);
141 CPPUNIT_ASSERT_EQUAL(sSysUserName, sLockFileContent.copy(nFirstChar, nNextComma - nFirstChar));
143 // Local host
144 nFirstChar = nNextComma + 1;
145 nNextComma = sLockFileContent.indexOf(',', nFirstChar);
146 CPPUNIT_ASSERT_EQUAL(::osl::SocketAddr::getLocalHostname(),
147 sLockFileContent.copy(nFirstChar, nNextComma - nFirstChar));
149 // Skip date and time because it changes after the lock file was created
150 nFirstChar = nNextComma + 1;
151 nNextComma = sLockFileContent.indexOf(',', nFirstChar);
153 // user url
154 nFirstChar = nNextComma + 1;
155 OUString aUserInstDir;
156 ::utl::Bootstrap::locateUserInstallation(aUserInstDir);
157 CPPUNIT_ASSERT_EQUAL(
158 aUserInstDir,
159 sLockFileContent.copy(nFirstChar, sLockFileContent.getLength() - nFirstChar - 1));
162 void LockfileTest::testLOLockFileRT()
164 // Test the lockfile generated for the specified ODT document
165 OUString aTestODT = generateTestURL(u"testLOLockFileRT.odt");
167 // Set user name
168 SvtUserOptions aUserOpt;
169 aUserOpt.SetToken(UserOptToken::FirstName, "LockFile");
170 aUserOpt.SetToken(UserOptToken::LastName, "Test");
172 // Write the lock file and read it back
173 svt::DocumentLockFile aLockFile(aTestODT);
174 LockFileEntry aOrigEntry = svt::LockFileCommon::GenerateOwnEntry();
175 aLockFile.CreateOwnLockFile();
176 LockFileEntry aRTEntry = aLockFile.GetLockData();
178 // Check whether the lock file attributes are the same
179 CPPUNIT_ASSERT_EQUAL(aOrigEntry[LockFileComponent::OOOUSERNAME],
180 aRTEntry[LockFileComponent::OOOUSERNAME]);
181 CPPUNIT_ASSERT_EQUAL(aOrigEntry[LockFileComponent::SYSUSERNAME],
182 aRTEntry[LockFileComponent::SYSUSERNAME]);
183 CPPUNIT_ASSERT_EQUAL(aOrigEntry[LockFileComponent::LOCALHOST],
184 aRTEntry[LockFileComponent::LOCALHOST]);
185 CPPUNIT_ASSERT_EQUAL(aOrigEntry[LockFileComponent::USERURL],
186 aRTEntry[LockFileComponent::USERURL]);
187 // LockFileComponent::EDITTIME can change
189 aLockFile.RemoveFileDirectly();
192 void LockfileTest::testLOLockFileUnicodeUsername()
194 // Test the lockfile generated for the specified ODT document
195 OUString aTestODT = generateTestURL(u"testLOLockFileUnicodeUsername.odt");
197 // Set user name
198 SvtUserOptions aUserOpt;
199 sal_Unicode vFirstName[] = { 2351, 2676, 3117, 5279 };
200 aUserOpt.SetToken(UserOptToken::FirstName, OUString(vFirstName, 4));
201 sal_Unicode vLastName[] = { 671, 1245, 1422, 1822 };
202 aUserOpt.SetToken(UserOptToken::LastName, OUString(vLastName, 4));
204 // Write the lock file and read it back
205 svt::DocumentLockFile aLockFile(aTestODT);
206 LockFileEntry aOrigEntry = svt::LockFileCommon::GenerateOwnEntry();
207 aLockFile.CreateOwnLockFile();
208 LockFileEntry aRTEntry = aLockFile.GetLockData();
210 // Check whether the lock file attributes are the same
211 CPPUNIT_ASSERT_EQUAL(aOrigEntry[LockFileComponent::OOOUSERNAME],
212 aRTEntry[LockFileComponent::OOOUSERNAME]);
213 CPPUNIT_ASSERT_EQUAL(OUString(aUserOpt.GetFirstName() + " " + aUserOpt.GetLastName()),
214 aOrigEntry[LockFileComponent::OOOUSERNAME]);
216 aLockFile.RemoveFileDirectly();
219 void LockfileTest::testLOLockFileOverwrite()
221 OUString aTestODT = generateTestURL(u"testLOLockFileOverwrite.odt");
223 // Set user name
224 SvtUserOptions aUserOpt;
225 aUserOpt.SetToken(UserOptToken::FirstName, "LockFile");
226 aUserOpt.SetToken(UserOptToken::LastName, "Test");
228 // Write the lock file and read it back
229 svt::DocumentLockFile aLockFile(aTestODT);
230 aLockFile.CreateOwnLockFile();
232 // Change user name
233 aUserOpt.SetToken(UserOptToken::FirstName, "LockFile2");
234 aUserOpt.SetToken(UserOptToken::LastName, "Test");
236 // Overwrite lockfile
237 svt::DocumentLockFile aLockFile2(aTestODT);
238 LockFileEntry aOrigEntry = svt::LockFileCommon::GenerateOwnEntry();
239 aLockFile2.OverwriteOwnLockFile();
241 LockFileEntry aRTEntry = aLockFile.GetLockData();
243 // Check whether the lock file attributes are the same
244 CPPUNIT_ASSERT_EQUAL(aOrigEntry[LockFileComponent::OOOUSERNAME],
245 aRTEntry[LockFileComponent::OOOUSERNAME]);
246 CPPUNIT_ASSERT_EQUAL(aOrigEntry[LockFileComponent::SYSUSERNAME],
247 aRTEntry[LockFileComponent::SYSUSERNAME]);
248 CPPUNIT_ASSERT_EQUAL(aOrigEntry[LockFileComponent::LOCALHOST],
249 aRTEntry[LockFileComponent::LOCALHOST]);
250 CPPUNIT_ASSERT_EQUAL(aOrigEntry[LockFileComponent::USERURL],
251 aRTEntry[LockFileComponent::USERURL]);
253 aLockFile2.RemoveFileDirectly();
256 void LockfileTest::testWordLockFileURL()
258 // Test the generated file name for Word lock files
260 // Word specific file format
262 OUString aTestFile = generateTestURL(u"testWordLockFileURL.docx");
263 svt::MSODocumentLockFile aLockFile(aTestFile);
264 CPPUNIT_ASSERT_EQUAL(OUString("~$stWordLockFileURL.docx"), GetLockFileName(aLockFile));
267 // Eight character file name (cuts two characters)
269 OUString aTestFile = generateTestURL(u"12345678.docx");
270 svt::MSODocumentLockFile aLockFile(aTestFile);
271 CPPUNIT_ASSERT_EQUAL(OUString("~$345678.docx"), GetLockFileName(aLockFile));
274 // Seven character file name (cuts one character)
276 OUString aTestFile = generateTestURL(u"1234567.docx");
277 svt::MSODocumentLockFile aLockFile(aTestFile);
278 CPPUNIT_ASSERT_EQUAL(OUString("~$234567.docx"), GetLockFileName(aLockFile));
281 // Six character file name (cuts no character)
283 OUString aTestFile = generateTestURL(u"123456.docx");
284 svt::MSODocumentLockFile aLockFile(aTestFile);
285 CPPUNIT_ASSERT_EQUAL(OUString("~$123456.docx"), GetLockFileName(aLockFile));
288 // One character file name
290 OUString aTestFile = generateTestURL(u"1.docx");
291 svt::MSODocumentLockFile aLockFile(aTestFile);
292 CPPUNIT_ASSERT_EQUAL(OUString("~$1.docx"), GetLockFileName(aLockFile));
295 // Test for ODT format
297 OUString aTestFile = generateTestURL(u"12345678.odt");
298 svt::MSODocumentLockFile aLockFile(aTestFile);
299 CPPUNIT_ASSERT_EQUAL(OUString("~$345678.odt"), GetLockFileName(aLockFile));
302 // Test for DOC format
304 OUString aTestFile = generateTestURL(u"12345678.doc");
305 svt::MSODocumentLockFile aLockFile(aTestFile);
306 CPPUNIT_ASSERT_EQUAL(OUString("~$345678.doc"), GetLockFileName(aLockFile));
309 // Test for RTF format
311 OUString aTestFile = generateTestURL(u"12345678.rtf");
312 svt::MSODocumentLockFile aLockFile(aTestFile);
313 CPPUNIT_ASSERT_EQUAL(OUString("~$345678.rtf"), GetLockFileName(aLockFile));
317 void LockfileTest::testExcelLockFileURL()
319 // Test the generated file name for Excel lock files
321 OUString aTestFile = generateTestURL(u"testExcelLockFileURL.xlsx");
322 svt::MSODocumentLockFile aLockFile(aTestFile);
323 CPPUNIT_ASSERT_EQUAL(OUString("~$testExcelLockFileURL.xlsx"), GetLockFileName(aLockFile));
326 // Eight character file name
328 OUString aTestFile = generateTestURL(u"12345678.xlsx");
329 svt::MSODocumentLockFile aLockFile(aTestFile);
330 CPPUNIT_ASSERT_EQUAL(OUString("~$12345678.xlsx"), GetLockFileName(aLockFile));
333 // One character file name
335 OUString aTestFile = generateTestURL(u"1.xlsx");
336 svt::MSODocumentLockFile aLockFile(aTestFile);
337 CPPUNIT_ASSERT_EQUAL(OUString("~$1.xlsx"), GetLockFileName(aLockFile));
340 // Test for ODS format
342 OUString aTestFile = generateTestURL(u"12345678.ods");
343 svt::MSODocumentLockFile aLockFile(aTestFile);
344 CPPUNIT_ASSERT_EQUAL(OUString("~$12345678.ods"), GetLockFileName(aLockFile));
348 void LockfileTest::testPowerPointLockFileURL()
350 // Test the generated file name for PowerPoint lock files
352 OUString aTestFile = generateTestURL(u"testPowerPointLockFileURL.pptx");
353 svt::MSODocumentLockFile aLockFile(aTestFile);
354 CPPUNIT_ASSERT_EQUAL(OUString("~$testPowerPointLockFileURL.pptx"),
355 GetLockFileName(aLockFile));
358 // Eight character file name
360 OUString aTestFile = generateTestURL(u"12345678.pptx");
361 svt::MSODocumentLockFile aLockFile(aTestFile);
362 CPPUNIT_ASSERT_EQUAL(OUString("~$12345678.pptx"), GetLockFileName(aLockFile));
365 // One character file name
367 OUString aTestFile = generateTestURL(u"1.pptx");
368 svt::MSODocumentLockFile aLockFile(aTestFile);
369 CPPUNIT_ASSERT_EQUAL(OUString("~$1.pptx"), GetLockFileName(aLockFile));
372 // Test for PPT format
374 OUString aTestFile = generateTestURL(u"12345678.ppt");
375 svt::MSODocumentLockFile aLockFile(aTestFile);
376 CPPUNIT_ASSERT_EQUAL(OUString("~$12345678.ppt"), GetLockFileName(aLockFile));
379 // Test for ODP format
381 OUString aTestFile = generateTestURL(u"/12345678.odp");
382 svt::MSODocumentLockFile aLockFile(aTestFile);
383 CPPUNIT_ASSERT_EQUAL(OUString("~$12345678.odp"), GetLockFileName(aLockFile));
387 void LockfileTest::testWordLockFileContent()
389 // Test the lockfile generated for the specified DOCX document
390 OUString aTestFile = generateTestURL(u"testWordLockFileContent.docx");
392 // Set user name
393 SvtUserOptions aUserOpt;
394 aUserOpt.SetToken(UserOptToken::FirstName, "LockFile");
395 aUserOpt.SetToken(UserOptToken::LastName, "Test");
397 // Write the lock file and check the content
398 svt::MSODocumentLockFile aLockFile(aTestFile);
399 aLockFile.CreateOwnLockFile();
400 OUString sLockFileContent(readLockFile(aLockFile.GetURL()));
401 aLockFile.RemoveFileDirectly();
403 // First character is the size of the user name
404 OUString sUserName = aUserOpt.GetFirstName() + " " + aUserOpt.GetLastName();
405 int nIndex = 0;
406 CPPUNIT_ASSERT_EQUAL(sUserName.getLength(), static_cast<sal_Int32>(sLockFileContent[nIndex]));
408 // Then we have the user name
409 CPPUNIT_ASSERT_EQUAL(sUserName, sLockFileContent.copy(1, sUserName.getLength()));
411 // We have some filling 0 bytes after the user name
412 for (nIndex = sUserName.getLength() + 1; nIndex < MSO_USERNAME_MAX_LENGTH + 2; ++nIndex)
414 CPPUNIT_ASSERT_EQUAL(sal_Int32(0), static_cast<sal_Int32>(sLockFileContent[nIndex]));
417 // Then we have the user name's length again
418 CPPUNIT_ASSERT_EQUAL(sUserName.getLength(), static_cast<sal_Int32>(sLockFileContent[nIndex]));
420 // Then we have the user name again with 16 bit coding
421 for (int i = 0; i < sUserName.getLength(); ++i)
423 CPPUNIT_ASSERT_EQUAL(
424 sUserName[i],
425 static_cast<sal_Unicode>(static_cast<sal_Int16>(sLockFileContent[55 + i * 2])
426 + static_cast<sal_Int16>(sLockFileContent[55 + i * 2 + 1])));
429 // We have some filling 0 bytes after the user name
430 for (nIndex += sUserName.getLength() * 2 + 1; nIndex < MSO_WORD_LOCKFILE_SIZE; ++nIndex)
432 CPPUNIT_ASSERT_EQUAL(sal_Int32(0), static_cast<sal_Int32>(sLockFileContent[nIndex]));
435 // We have a fixed size lock file
436 CPPUNIT_ASSERT_EQUAL(sal_Int32(MSO_WORD_LOCKFILE_SIZE), sLockFileContent.getLength());
439 void LockfileTest::testExcelLockFileContent()
441 // Test the lockfile generated for the specified XLSX document
442 OUString aTestFile = generateTestURL(u"testExcelLockFileContent.xlsx");
444 // Set user name
445 SvtUserOptions aUserOpt;
446 aUserOpt.SetToken(UserOptToken::FirstName, "LockFile");
447 aUserOpt.SetToken(UserOptToken::LastName, "Test");
449 // Write the lock file and check the content
450 svt::MSODocumentLockFile aLockFile(aTestFile);
451 aLockFile.CreateOwnLockFile();
452 OUString sLockFileContent(readLockFile(aLockFile.GetURL()));
453 aLockFile.RemoveFileDirectly();
455 // First character is the size of the user name
456 OUString sUserName = aUserOpt.GetFirstName() + " " + aUserOpt.GetLastName();
457 int nIndex = 0;
458 CPPUNIT_ASSERT_EQUAL(sUserName.getLength(), static_cast<sal_Int32>(sLockFileContent[nIndex]));
460 // Then we have the user name
461 CPPUNIT_ASSERT_EQUAL(sUserName, sLockFileContent.copy(1, sUserName.getLength()));
463 // We have some filling 0x20 bytes after the user name
464 for (nIndex = sUserName.getLength() + 1; nIndex < MSO_USERNAME_MAX_LENGTH + 3; ++nIndex)
466 CPPUNIT_ASSERT_EQUAL(sal_Int32(0x20), static_cast<sal_Int32>(sLockFileContent[nIndex]));
469 // Then we have the user name's length again
470 CPPUNIT_ASSERT_EQUAL(sUserName.getLength(), static_cast<sal_Int32>(sLockFileContent[nIndex]));
472 // Then we have the user name again with 16 bit coding
473 for (int i = 0; i < sUserName.getLength(); ++i)
475 CPPUNIT_ASSERT_EQUAL(
476 sUserName[i],
477 static_cast<sal_Unicode>(static_cast<sal_Int16>(sLockFileContent[56 + i * 2])
478 + static_cast<sal_Int16>(sLockFileContent[56 + i * 2 + 1])));
481 // We have some filling 0 and 0x20 bytes after the user name
482 for (nIndex += sUserName.getLength() * 2 + 2; nIndex < MSO_EXCEL_AND_POWERPOINT_LOCKFILE_SIZE;
483 nIndex += 2)
485 CPPUNIT_ASSERT_EQUAL(sal_Int32(0x20), static_cast<sal_Int32>(sLockFileContent[nIndex]));
486 if (nIndex + 1 < MSO_EXCEL_AND_POWERPOINT_LOCKFILE_SIZE)
487 CPPUNIT_ASSERT_EQUAL(sal_Int32(0),
488 static_cast<sal_Int32>(sLockFileContent[nIndex + 1]));
491 // We have a fixed size lock file
492 CPPUNIT_ASSERT_EQUAL(sal_Int32(MSO_EXCEL_AND_POWERPOINT_LOCKFILE_SIZE),
493 sLockFileContent.getLength());
496 void LockfileTest::testPowerPointLockFileContent()
498 // Test the lockfile generated for the specified PPTX document
499 OUString aTestFile = generateTestURL(u"testPowerPointLockFileContent.pptx");
501 // Set user name
502 SvtUserOptions aUserOpt;
503 aUserOpt.SetToken(UserOptToken::FirstName, "LockFile");
504 aUserOpt.SetToken(UserOptToken::LastName, "Test");
506 // Write the lock file and check the content
507 svt::MSODocumentLockFile aLockFile(aTestFile);
508 aLockFile.CreateOwnLockFile();
509 OUString sLockFileContent(readLockFile(aLockFile.GetURL()));
510 aLockFile.RemoveFileDirectly();
512 // First character is the size of the user name
513 OUString sUserName = aUserOpt.GetFirstName() + " " + aUserOpt.GetLastName();
514 int nIndex = 0;
515 CPPUNIT_ASSERT_EQUAL(sUserName.getLength(), static_cast<sal_Int32>(sLockFileContent[nIndex]));
517 // Then we have the user name
518 CPPUNIT_ASSERT_EQUAL(sUserName, sLockFileContent.copy(1, sUserName.getLength()));
520 // We have some filling bytes after the user name
521 nIndex = sUserName.getLength() + 1;
522 CPPUNIT_ASSERT_EQUAL(sal_Int32(0), static_cast<sal_Int32>(sLockFileContent[nIndex]));
523 for (nIndex += 1; nIndex < MSO_USERNAME_MAX_LENGTH + 3; ++nIndex)
525 CPPUNIT_ASSERT_EQUAL(sal_Int32(0x20), static_cast<sal_Int32>(sLockFileContent[nIndex]));
528 // Then we have the user name's length again
529 CPPUNIT_ASSERT_EQUAL(sUserName.getLength(), static_cast<sal_Int32>(sLockFileContent[nIndex]));
531 // Then we have the user name again with 16 bit coding
532 for (int i = 0; i < sUserName.getLength(); ++i)
534 CPPUNIT_ASSERT_EQUAL(
535 sUserName[i],
536 static_cast<sal_Unicode>(static_cast<sal_Int16>(sLockFileContent[56 + i * 2])
537 + static_cast<sal_Int16>(sLockFileContent[56 + i * 2 + 1])));
540 // We have some filling 0 and 0x20 bytes after the user name
541 for (nIndex += sUserName.getLength() * 2 + 2; nIndex < MSO_EXCEL_AND_POWERPOINT_LOCKFILE_SIZE;
542 nIndex += 2)
544 CPPUNIT_ASSERT_EQUAL(sal_Int32(0x20), static_cast<sal_Int32>(sLockFileContent[nIndex]));
545 if (nIndex + 1 < MSO_EXCEL_AND_POWERPOINT_LOCKFILE_SIZE)
546 CPPUNIT_ASSERT_EQUAL(sal_Int32(0),
547 static_cast<sal_Int32>(sLockFileContent[nIndex + 1]));
550 // We have a fixed size lock file
551 CPPUNIT_ASSERT_EQUAL(sal_Int32(MSO_EXCEL_AND_POWERPOINT_LOCKFILE_SIZE),
552 sLockFileContent.getLength());
555 void LockfileTest::testWordLockFileRT()
557 OUString aTestODT = generateTestURL(u"testWordLockFileRT.docx");
559 // Set user name
560 SvtUserOptions aUserOpt;
561 aUserOpt.SetToken(UserOptToken::FirstName, "LockFile");
562 aUserOpt.SetToken(UserOptToken::LastName, "Test");
564 // Write the lock file and read it back
565 svt::MSODocumentLockFile aLockFile(aTestODT);
566 LockFileEntry aOrigEntry = svt::LockFileCommon::GenerateOwnEntry();
567 aLockFile.CreateOwnLockFile();
568 LockFileEntry aRTEntry = aLockFile.GetLockData();
569 aLockFile.RemoveFileDirectly();
571 // Check whether the lock file attributes are the same
572 CPPUNIT_ASSERT_EQUAL(aOrigEntry[LockFileComponent::OOOUSERNAME],
573 aRTEntry[LockFileComponent::OOOUSERNAME]);
576 void LockfileTest::testExcelLockFileRT()
578 OUString aTestODT = generateTestURL(u"testExcelLockFileRT.xlsx");
580 // Set user name
581 SvtUserOptions aUserOpt;
582 aUserOpt.SetToken(UserOptToken::FirstName, "LockFile");
583 aUserOpt.SetToken(UserOptToken::LastName, "Test");
585 // Write the lock file and read it back
586 svt::MSODocumentLockFile aLockFile(aTestODT);
587 LockFileEntry aOrigEntry = svt::LockFileCommon::GenerateOwnEntry();
588 aLockFile.CreateOwnLockFile();
589 LockFileEntry aRTEntry = aLockFile.GetLockData();
590 aLockFile.RemoveFileDirectly();
592 // Check whether the lock file attributes are the same
593 CPPUNIT_ASSERT_EQUAL(aOrigEntry[LockFileComponent::OOOUSERNAME],
594 aRTEntry[LockFileComponent::OOOUSERNAME]);
597 void LockfileTest::testPowerPointLockFileRT()
599 OUString aTestODT = generateTestURL(u"testPowerPointLockFileRT.pptx");
601 // Set user name
602 SvtUserOptions aUserOpt;
603 aUserOpt.SetToken(UserOptToken::FirstName, "LockFile");
604 aUserOpt.SetToken(UserOptToken::LastName, "Test");
606 // Write the lock file and read it back
607 svt::MSODocumentLockFile aLockFile(aTestODT);
608 LockFileEntry aOrigEntry = svt::LockFileCommon::GenerateOwnEntry();
609 aLockFile.CreateOwnLockFile();
610 LockFileEntry aRTEntry = aLockFile.GetLockData();
611 aLockFile.RemoveFileDirectly();
613 // Check whether the lock file attributes are the same
614 CPPUNIT_ASSERT_EQUAL(aOrigEntry[LockFileComponent::OOOUSERNAME],
615 aRTEntry[LockFileComponent::OOOUSERNAME]);
618 void LockfileTest::testMSOLockFileLongUserName()
620 OUString aTestODT = generateTestURL(u"testMSOLockFileLongUserName.docx");
622 // Set user name
623 SvtUserOptions aUserOpt;
624 aUserOpt.SetToken(UserOptToken::FirstName,
625 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
626 aUserOpt.SetToken(UserOptToken::LastName,
627 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
629 // Write the lock file and read it back
630 svt::MSODocumentLockFile aLockFile(aTestODT);
631 LockFileEntry aOrigEntry = svt::LockFileCommon::GenerateOwnEntry();
632 aLockFile.CreateOwnLockFile();
633 LockFileEntry aRTEntry = aLockFile.GetLockData();
635 // Check whether the user name was cut to the maximum length
636 CPPUNIT_ASSERT_EQUAL(
637 aOrigEntry[LockFileComponent::OOOUSERNAME].copy(0, MSO_USERNAME_MAX_LENGTH),
638 aRTEntry[LockFileComponent::OOOUSERNAME]);
640 aLockFile.RemoveFileDirectly();
643 void LockfileTest::testMSOLockFileUnicodeUsername()
645 // Test the lockfile generated for the specified ODT document
646 OUString aTestODT = generateTestURL(u"testMSOLockFileUnicodeUsername.docx");
648 // Set user name
649 SvtUserOptions aUserOpt;
650 sal_Unicode vFirstName[] = { 2351, 2676, 3117, 5279 };
651 aUserOpt.SetToken(UserOptToken::FirstName, OUString(vFirstName, 4));
652 sal_Unicode vLastName[] = { 671, 1245, 1422, 1822 };
653 aUserOpt.SetToken(UserOptToken::LastName, OUString(vLastName, 4));
655 // Write the lock file and read it back
656 svt::DocumentLockFile aLockFile(aTestODT);
657 LockFileEntry aOrigEntry = svt::LockFileCommon::GenerateOwnEntry();
658 aLockFile.CreateOwnLockFile();
659 LockFileEntry aRTEntry = aLockFile.GetLockData();
661 // Check whether the user name is the same
662 CPPUNIT_ASSERT_EQUAL(aOrigEntry[LockFileComponent::OOOUSERNAME],
663 aRTEntry[LockFileComponent::OOOUSERNAME]);
664 CPPUNIT_ASSERT_EQUAL(OUString(aUserOpt.GetFirstName() + " " + aUserOpt.GetLastName()),
665 aOrigEntry[LockFileComponent::OOOUSERNAME]);
667 aLockFile.RemoveFileDirectly();
670 void LockfileTest::testMSOLockFileOverwrite()
672 OUString aTestODT = generateTestURL(u"testMSOLockFileOverwrite.docx");
674 // Set user name
675 SvtUserOptions aUserOpt;
676 aUserOpt.SetToken(UserOptToken::FirstName, "LockFile");
677 aUserOpt.SetToken(UserOptToken::LastName, "Test");
679 // Write the lock file and read it back
680 svt::MSODocumentLockFile aLockFile(aTestODT);
681 aLockFile.CreateOwnLockFile();
683 // Change user name
684 aUserOpt.SetToken(UserOptToken::FirstName, "LockFile2");
685 aUserOpt.SetToken(UserOptToken::LastName, "Test");
687 // Overwrite lockfile
688 svt::MSODocumentLockFile aLockFile2(aTestODT);
689 LockFileEntry aOrigEntry = svt::LockFileCommon::GenerateOwnEntry();
690 aLockFile2.OverwriteOwnLockFile();
692 LockFileEntry aRTEntry = aLockFile.GetLockData();
694 // Check whether the lock file attributes are the same
695 CPPUNIT_ASSERT_EQUAL(aOrigEntry[LockFileComponent::OOOUSERNAME],
696 aRTEntry[LockFileComponent::OOOUSERNAME]);
698 aLockFile2.RemoveFileDirectly();
701 CPPUNIT_TEST_SUITE_REGISTRATION(LockfileTest);
702 } // namespace
704 CPPUNIT_PLUGIN_IMPLEMENT();
706 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */