/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */ /* * This file is part of the LibreOffice project. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
void LockfileTest::testLOLockFileURL()
{ // Test the generated file name for LibreOffice lock files
OUString aTestODT = generateTestURL(u"testLOLockFileURL.odt");
void LockfileTest::testLOLockFileContent()
{ // Test the lockfile generated for the specified ODT document
OUString aTestODT = generateTestURL(u"testLOLockFileContent.odt");
// Set user name
SvtUserOptions aUserOpt;
aUserOpt.SetToken(UserOptToken::FirstName, u"LockFile"_ustr);
aUserOpt.SetToken(UserOptToken::LastName, u"Test"_ustr);
// Write the lock file and check the content
svt::DocumentLockFile aLockFile(aTestODT);
aLockFile.CreateOwnLockFile();
OUString sLockFileContent(readLockFile(aLockFile.GetURL()));
aLockFile.RemoveFileDirectly();
// Skip date and time because it changes after the lock file was created
nFirstChar = nNextComma + 1;
nNextComma = sLockFileContent.indexOf(',', nFirstChar);
void LockfileTest::testLOLockFileRT()
{ // Test the lockfile generated for the specified ODT document
OUString aTestODT = generateTestURL(u"testLOLockFileRT.odt");
// Set user name
SvtUserOptions aUserOpt;
aUserOpt.SetToken(UserOptToken::FirstName, u"LockFile"_ustr);
aUserOpt.SetToken(UserOptToken::LastName, u"Test"_ustr);
// Write the lock file and read it back
svt::DocumentLockFile aLockFile(aTestODT);
LockFileEntry aOrigEntry = svt::LockFileCommon::GenerateOwnEntry();
aLockFile.CreateOwnLockFile();
LockFileEntry aRTEntry = aLockFile.GetLockData();
// Check whether the lock file attributes are the same
CPPUNIT_ASSERT_EQUAL(aOrigEntry[LockFileComponent::OOOUSERNAME],
aRTEntry[LockFileComponent::OOOUSERNAME]);
CPPUNIT_ASSERT_EQUAL(aOrigEntry[LockFileComponent::SYSUSERNAME],
aRTEntry[LockFileComponent::SYSUSERNAME]);
CPPUNIT_ASSERT_EQUAL(aOrigEntry[LockFileComponent::LOCALHOST],
aRTEntry[LockFileComponent::LOCALHOST]);
CPPUNIT_ASSERT_EQUAL(aOrigEntry[LockFileComponent::USERURL],
aRTEntry[LockFileComponent::USERURL]); // LockFileComponent::EDITTIME can change
aLockFile.RemoveFileDirectly();
}
void LockfileTest::testLOLockFileUnicodeUsername()
{ // Test the lockfile generated for the specified ODT document
OUString aTestODT = generateTestURL(u"testLOLockFileUnicodeUsername.odt");
// Set user name
SvtUserOptions aUserOpt;
sal_Unicode vFirstName[] = { 2351, 2676, 3117, 5279 };
aUserOpt.SetToken(UserOptToken::FirstName, OUString(vFirstName, 4));
sal_Unicode vLastName[] = { 671, 1245, 1422, 1822 };
aUserOpt.SetToken(UserOptToken::LastName, OUString(vLastName, 4));
// Write the lock file and read it back
svt::DocumentLockFile aLockFile(aTestODT);
LockFileEntry aOrigEntry = svt::LockFileCommon::GenerateOwnEntry();
aLockFile.CreateOwnLockFile();
LockFileEntry aRTEntry = aLockFile.GetLockData();
// Check whether the lock file attributes are the same
CPPUNIT_ASSERT_EQUAL(aOrigEntry[LockFileComponent::OOOUSERNAME],
aRTEntry[LockFileComponent::OOOUSERNAME]);
CPPUNIT_ASSERT_EQUAL(OUString(aUserOpt.GetFirstName() + " " + aUserOpt.GetLastName()),
aOrigEntry[LockFileComponent::OOOUSERNAME]);
// Set user name
SvtUserOptions aUserOpt;
aUserOpt.SetToken(UserOptToken::FirstName, u"LockFile"_ustr);
aUserOpt.SetToken(UserOptToken::LastName, u"Test"_ustr);
// Write the lock file and read it back
svt::DocumentLockFile aLockFile(aTestODT);
aLockFile.CreateOwnLockFile();
// Change user name
aUserOpt.SetToken(UserOptToken::FirstName, u"LockFile2"_ustr);
aUserOpt.SetToken(UserOptToken::LastName, u"Test"_ustr);
// Check whether the lock file attributes are the same
CPPUNIT_ASSERT_EQUAL(aOrigEntry[LockFileComponent::OOOUSERNAME],
aRTEntry[LockFileComponent::OOOUSERNAME]);
CPPUNIT_ASSERT_EQUAL(aOrigEntry[LockFileComponent::SYSUSERNAME],
aRTEntry[LockFileComponent::SYSUSERNAME]);
CPPUNIT_ASSERT_EQUAL(aOrigEntry[LockFileComponent::LOCALHOST],
aRTEntry[LockFileComponent::LOCALHOST]);
CPPUNIT_ASSERT_EQUAL(aOrigEntry[LockFileComponent::USERURL],
aRTEntry[LockFileComponent::USERURL]);
aLockFile2.RemoveFileDirectly();
}
void LockfileTest::testWordLockFileURL()
{ // Test the generated file name for Word lock files
// Word specific file format
{
OUString aTestFile = generateTestURL(u"testWordLockFileURL.docx");
svt::MSODocumentLockFile aLockFile(aTestFile);
CPPUNIT_ASSERT_EQUAL(u"~$stWordLockFileURL.docx"_ustr, GetLockFileName(aLockFile));
}
// Eight character file name (cuts two characters)
{
OUString aTestFile = generateTestURL(u"12345678.docx");
svt::MSODocumentLockFile aLockFile(aTestFile);
CPPUNIT_ASSERT_EQUAL(u"~$345678.docx"_ustr, GetLockFileName(aLockFile));
}
// Seven character file name (cuts one character)
{
OUString aTestFile = generateTestURL(u"1234567.docx");
svt::MSODocumentLockFile aLockFile(aTestFile);
CPPUNIT_ASSERT_EQUAL(u"~$234567.docx"_ustr, GetLockFileName(aLockFile));
}
// Six character file name (cuts no character)
{
OUString aTestFile = generateTestURL(u"123456.docx");
svt::MSODocumentLockFile aLockFile(aTestFile);
CPPUNIT_ASSERT_EQUAL(u"~$123456.docx"_ustr, GetLockFileName(aLockFile));
}
// One character file name
{
OUString aTestFile = generateTestURL(u"1.docx");
svt::MSODocumentLockFile aLockFile(aTestFile);
CPPUNIT_ASSERT_EQUAL(u"~$1.docx"_ustr, GetLockFileName(aLockFile));
}
// Test for ODT format
{
OUString aTestFile = generateTestURL(u"12345678.odt");
svt::MSODocumentLockFile aLockFile(aTestFile);
CPPUNIT_ASSERT_EQUAL(u"~$345678.odt"_ustr, GetLockFileName(aLockFile));
}
// Test for DOC format
{
OUString aTestFile = generateTestURL(u"12345678.doc");
svt::MSODocumentLockFile aLockFile(aTestFile);
CPPUNIT_ASSERT_EQUAL(u"~$345678.doc"_ustr, GetLockFileName(aLockFile));
}
// Test for RTF format
{
OUString aTestFile = generateTestURL(u"12345678.rtf");
svt::MSODocumentLockFile aLockFile(aTestFile);
CPPUNIT_ASSERT_EQUAL(u"~$345678.rtf"_ustr, GetLockFileName(aLockFile));
}
}
void LockfileTest::testExcelLockFileURL()
{ // Test the generated file name for Excel lock files
{
OUString aTestFile = generateTestURL(u"testExcelLockFileURL.xlsx");
svt::MSODocumentLockFile aLockFile(aTestFile);
CPPUNIT_ASSERT_EQUAL(u"~$testExcelLockFileURL.xlsx"_ustr, GetLockFileName(aLockFile));
}
// Eight character file name
{
OUString aTestFile = generateTestURL(u"12345678.xlsx");
svt::MSODocumentLockFile aLockFile(aTestFile);
CPPUNIT_ASSERT_EQUAL(u"~$12345678.xlsx"_ustr, GetLockFileName(aLockFile));
}
// One character file name
{
OUString aTestFile = generateTestURL(u"1.xlsx");
svt::MSODocumentLockFile aLockFile(aTestFile);
CPPUNIT_ASSERT_EQUAL(u"~$1.xlsx"_ustr, GetLockFileName(aLockFile));
}
// Test for ODS format
{
OUString aTestFile = generateTestURL(u"12345678.ods");
svt::MSODocumentLockFile aLockFile(aTestFile);
CPPUNIT_ASSERT_EQUAL(u"~$12345678.ods"_ustr, GetLockFileName(aLockFile));
}
}
void LockfileTest::testPowerPointLockFileURL()
{ // Test the generated file name for PowerPoint lock files
{
OUString aTestFile = generateTestURL(u"testPowerPointLockFileURL.pptx");
svt::MSODocumentLockFile aLockFile(aTestFile);
CPPUNIT_ASSERT_EQUAL(u"~$testPowerPointLockFileURL.pptx"_ustr, GetLockFileName(aLockFile));
}
// Eight character file name
{
OUString aTestFile = generateTestURL(u"12345678.pptx");
svt::MSODocumentLockFile aLockFile(aTestFile);
CPPUNIT_ASSERT_EQUAL(u"~$12345678.pptx"_ustr, GetLockFileName(aLockFile));
}
// One character file name
{
OUString aTestFile = generateTestURL(u"1.pptx");
svt::MSODocumentLockFile aLockFile(aTestFile);
CPPUNIT_ASSERT_EQUAL(u"~$1.pptx"_ustr, GetLockFileName(aLockFile));
}
// Test for PPT format
{
OUString aTestFile = generateTestURL(u"12345678.ppt");
svt::MSODocumentLockFile aLockFile(aTestFile);
CPPUNIT_ASSERT_EQUAL(u"~$12345678.ppt"_ustr, GetLockFileName(aLockFile));
}
// Test for ODP format
{
OUString aTestFile = generateTestURL(u"/12345678.odp");
svt::MSODocumentLockFile aLockFile(aTestFile);
CPPUNIT_ASSERT_EQUAL(u"~$12345678.odp"_ustr, GetLockFileName(aLockFile));
}
}
void LockfileTest::testWordLockFileContent()
{ // Test the lockfile generated for the specified DOCX document
OUString aTestFile = generateTestURL(u"testWordLockFileContent.docx");
// Set user name
SvtUserOptions aUserOpt;
aUserOpt.SetToken(UserOptToken::FirstName, u"LockFile"_ustr);
aUserOpt.SetToken(UserOptToken::LastName, u"Test"_ustr);
// Write the lock file and check the content
svt::MSODocumentLockFile aLockFile(aTestFile);
aLockFile.CreateOwnLockFile();
OUString sLockFileContent(readLockFile(aLockFile.GetURL()));
aLockFile.RemoveFileDirectly();
// First character is the size of the user name
OUString sUserName = aUserOpt.GetFirstName() + " " + aUserOpt.GetLastName(); int nIndex = 0;
CPPUNIT_ASSERT_EQUAL(sUserName.getLength(), static_cast<sal_Int32>(sLockFileContent[nIndex]));
// Then we have the user name
CPPUNIT_ASSERT_EQUAL(sUserName, sLockFileContent.copy(1, sUserName.getLength()));
// We have some filling 0 bytes after the user name for (nIndex = sUserName.getLength() + 1; nIndex < MSO_USERNAME_MAX_LENGTH + 2; ++nIndex)
{
CPPUNIT_ASSERT_EQUAL(sal_Int32(0), static_cast<sal_Int32>(sLockFileContent[nIndex]));
}
// Then we have the user name's length again
CPPUNIT_ASSERT_EQUAL(sUserName.getLength(), static_cast<sal_Int32>(sLockFileContent[nIndex]));
// Then we have the user name again with 16 bit coding for (int i = 0; i < sUserName.getLength(); ++i)
{
CPPUNIT_ASSERT_EQUAL(
sUserName[i], static_cast<sal_Unicode>(static_cast<sal_Int16>(sLockFileContent[55 + i * 2])
+ static_cast<sal_Int16>(sLockFileContent[55 + i * 2 + 1])));
}
// We have some filling 0 bytes after the user name for (nIndex += sUserName.getLength() * 2 + 1; nIndex < MSO_WORD_LOCKFILE_SIZE; ++nIndex)
{
CPPUNIT_ASSERT_EQUAL(sal_Int32(0), static_cast<sal_Int32>(sLockFileContent[nIndex]));
}
// We have a fixed size lock file
CPPUNIT_ASSERT_EQUAL(sal_Int32(MSO_WORD_LOCKFILE_SIZE), sLockFileContent.getLength());
}
void LockfileTest::testExcelLockFileContent()
{ // Test the lockfile generated for the specified XLSX document
OUString aTestFile = generateTestURL(u"testExcelLockFileContent.xlsx");
// Set user name
SvtUserOptions aUserOpt;
aUserOpt.SetToken(UserOptToken::FirstName, u"LockFile"_ustr);
aUserOpt.SetToken(UserOptToken::LastName, u"Test"_ustr);
// Write the lock file and check the content
svt::MSODocumentLockFile aLockFile(aTestFile);
aLockFile.CreateOwnLockFile();
OUString sLockFileContent(readLockFile(aLockFile.GetURL()));
aLockFile.RemoveFileDirectly();
// First character is the size of the user name
OUString sUserName = aUserOpt.GetFirstName() + " " + aUserOpt.GetLastName(); int nIndex = 0;
CPPUNIT_ASSERT_EQUAL(sUserName.getLength(), static_cast<sal_Int32>(sLockFileContent[nIndex]));
// Then we have the user name
CPPUNIT_ASSERT_EQUAL(sUserName, sLockFileContent.copy(1, sUserName.getLength()));
// We have some filling 0x20 bytes after the user name for (nIndex = sUserName.getLength() + 1; nIndex < MSO_USERNAME_MAX_LENGTH + 3; ++nIndex)
{
CPPUNIT_ASSERT_EQUAL(sal_Int32(0x20), static_cast<sal_Int32>(sLockFileContent[nIndex]));
}
// Then we have the user name's length again
CPPUNIT_ASSERT_EQUAL(sUserName.getLength(), static_cast<sal_Int32>(sLockFileContent[nIndex]));
// Then we have the user name again with 16 bit coding for (int i = 0; i < sUserName.getLength(); ++i)
{
CPPUNIT_ASSERT_EQUAL(
sUserName[i], static_cast<sal_Unicode>(static_cast<sal_Int16>(sLockFileContent[56 + i * 2])
+ static_cast<sal_Int16>(sLockFileContent[56 + i * 2 + 1])));
}
// We have some filling 0 and 0x20 bytes after the user name for (nIndex += sUserName.getLength() * 2 + 2; nIndex < MSO_EXCEL_AND_POWERPOINT_LOCKFILE_SIZE;
nIndex += 2)
{
CPPUNIT_ASSERT_EQUAL(sal_Int32(0x20), static_cast<sal_Int32>(sLockFileContent[nIndex])); if (nIndex + 1 < MSO_EXCEL_AND_POWERPOINT_LOCKFILE_SIZE)
CPPUNIT_ASSERT_EQUAL(sal_Int32(0), static_cast<sal_Int32>(sLockFileContent[nIndex + 1]));
}
// We have a fixed size lock file
CPPUNIT_ASSERT_EQUAL(sal_Int32(MSO_EXCEL_AND_POWERPOINT_LOCKFILE_SIZE),
sLockFileContent.getLength());
}
void LockfileTest::testPowerPointLockFileContent()
{ // Test the lockfile generated for the specified PPTX document
OUString aTestFile = generateTestURL(u"testPowerPointLockFileContent.pptx");
// Set user name
SvtUserOptions aUserOpt;
aUserOpt.SetToken(UserOptToken::FirstName, u"LockFile"_ustr);
aUserOpt.SetToken(UserOptToken::LastName, u"Test"_ustr);
// Write the lock file and check the content
svt::MSODocumentLockFile aLockFile(aTestFile);
aLockFile.CreateOwnLockFile();
OUString sLockFileContent(readLockFile(aLockFile.GetURL()));
aLockFile.RemoveFileDirectly();
// First character is the size of the user name
OUString sUserName = aUserOpt.GetFirstName() + " " + aUserOpt.GetLastName(); int nIndex = 0;
CPPUNIT_ASSERT_EQUAL(sUserName.getLength(), static_cast<sal_Int32>(sLockFileContent[nIndex]));
// Then we have the user name
CPPUNIT_ASSERT_EQUAL(sUserName, sLockFileContent.copy(1, sUserName.getLength()));
// We have some filling bytes after the user name
nIndex = sUserName.getLength() + 1;
CPPUNIT_ASSERT_EQUAL(sal_Int32(0), static_cast<sal_Int32>(sLockFileContent[nIndex])); for (nIndex += 1; nIndex < MSO_USERNAME_MAX_LENGTH + 3; ++nIndex)
{
CPPUNIT_ASSERT_EQUAL(sal_Int32(0x20), static_cast<sal_Int32>(sLockFileContent[nIndex]));
}
// Then we have the user name's length again
CPPUNIT_ASSERT_EQUAL(sUserName.getLength(), static_cast<sal_Int32>(sLockFileContent[nIndex]));
// Then we have the user name again with 16 bit coding for (int i = 0; i < sUserName.getLength(); ++i)
{
CPPUNIT_ASSERT_EQUAL(
sUserName[i], static_cast<sal_Unicode>(static_cast<sal_Int16>(sLockFileContent[56 + i * 2])
+ static_cast<sal_Int16>(sLockFileContent[56 + i * 2 + 1])));
}
// We have some filling 0 and 0x20 bytes after the user name for (nIndex += sUserName.getLength() * 2 + 2; nIndex < MSO_EXCEL_AND_POWERPOINT_LOCKFILE_SIZE;
nIndex += 2)
{
CPPUNIT_ASSERT_EQUAL(sal_Int32(0x20), static_cast<sal_Int32>(sLockFileContent[nIndex])); if (nIndex + 1 < MSO_EXCEL_AND_POWERPOINT_LOCKFILE_SIZE)
CPPUNIT_ASSERT_EQUAL(sal_Int32(0), static_cast<sal_Int32>(sLockFileContent[nIndex + 1]));
}
// We have a fixed size lock file
CPPUNIT_ASSERT_EQUAL(sal_Int32(MSO_EXCEL_AND_POWERPOINT_LOCKFILE_SIZE),
sLockFileContent.getLength());
}
// Set user name
SvtUserOptions aUserOpt;
aUserOpt.SetToken(UserOptToken::FirstName, u"LockFile"_ustr);
aUserOpt.SetToken(UserOptToken::LastName, u"Test"_ustr);
// Write the lock file and read it back
svt::MSODocumentLockFile aLockFile(aTestODT);
LockFileEntry aOrigEntry = svt::LockFileCommon::GenerateOwnEntry();
aLockFile.CreateOwnLockFile();
LockFileEntry aRTEntry = aLockFile.GetLockData();
aLockFile.RemoveFileDirectly();
// Check whether the lock file attributes are the same
CPPUNIT_ASSERT_EQUAL(aOrigEntry[LockFileComponent::OOOUSERNAME],
aRTEntry[LockFileComponent::OOOUSERNAME]);
}
// Set user name
SvtUserOptions aUserOpt;
aUserOpt.SetToken(UserOptToken::FirstName, u"LockFile"_ustr);
aUserOpt.SetToken(UserOptToken::LastName, u"Test"_ustr);
// Write the lock file and read it back
svt::MSODocumentLockFile aLockFile(aTestODT);
LockFileEntry aOrigEntry = svt::LockFileCommon::GenerateOwnEntry();
aLockFile.CreateOwnLockFile();
LockFileEntry aRTEntry = aLockFile.GetLockData();
aLockFile.RemoveFileDirectly();
// Check whether the lock file attributes are the same
CPPUNIT_ASSERT_EQUAL(aOrigEntry[LockFileComponent::OOOUSERNAME],
aRTEntry[LockFileComponent::OOOUSERNAME]);
}
// Set user name
SvtUserOptions aUserOpt;
aUserOpt.SetToken(UserOptToken::FirstName, u"LockFile"_ustr);
aUserOpt.SetToken(UserOptToken::LastName, u"Test"_ustr);
// Write the lock file and read it back
svt::MSODocumentLockFile aLockFile(aTestODT);
LockFileEntry aOrigEntry = svt::LockFileCommon::GenerateOwnEntry();
aLockFile.CreateOwnLockFile();
LockFileEntry aRTEntry = aLockFile.GetLockData();
aLockFile.RemoveFileDirectly();
// Check whether the lock file attributes are the same
CPPUNIT_ASSERT_EQUAL(aOrigEntry[LockFileComponent::OOOUSERNAME],
aRTEntry[LockFileComponent::OOOUSERNAME]);
}
// Set user name
SvtUserOptions aUserOpt;
aUserOpt.SetToken(UserOptToken::FirstName,
u"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"_ustr);
aUserOpt.SetToken(UserOptToken::LastName,
u"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"_ustr);
// Write the lock file and read it back
svt::MSODocumentLockFile aLockFile(aTestODT);
LockFileEntry aOrigEntry = svt::LockFileCommon::GenerateOwnEntry();
aLockFile.CreateOwnLockFile();
LockFileEntry aRTEntry = aLockFile.GetLockData();
// Check whether the user name was cut to the maximum length
CPPUNIT_ASSERT_EQUAL(
aOrigEntry[LockFileComponent::OOOUSERNAME].copy(0, MSO_USERNAME_MAX_LENGTH),
aRTEntry[LockFileComponent::OOOUSERNAME]);
aLockFile.RemoveFileDirectly();
}
void LockfileTest::testMSOLockFileUnicodeUsername()
{ // Test the lockfile generated for the specified ODT document
OUString aTestODT = generateTestURL(u"testMSOLockFileUnicodeUsername.docx");
// Set user name
SvtUserOptions aUserOpt;
sal_Unicode vFirstName[] = { 2351, 2676, 3117, 5279 };
aUserOpt.SetToken(UserOptToken::FirstName, OUString(vFirstName, 4));
sal_Unicode vLastName[] = { 671, 1245, 1422, 1822 };
aUserOpt.SetToken(UserOptToken::LastName, OUString(vLastName, 4));
// Write the lock file and read it back
svt::DocumentLockFile aLockFile(aTestODT);
LockFileEntry aOrigEntry = svt::LockFileCommon::GenerateOwnEntry();
aLockFile.CreateOwnLockFile();
LockFileEntry aRTEntry = aLockFile.GetLockData();
// Check whether the user name is the same
CPPUNIT_ASSERT_EQUAL(aOrigEntry[LockFileComponent::OOOUSERNAME],
aRTEntry[LockFileComponent::OOOUSERNAME]);
CPPUNIT_ASSERT_EQUAL(OUString(aUserOpt.GetFirstName() + " " + aUserOpt.GetLastName()),
aOrigEntry[LockFileComponent::OOOUSERNAME]);
// Set user name
SvtUserOptions aUserOpt;
aUserOpt.SetToken(UserOptToken::FirstName, u"LockFile"_ustr);
aUserOpt.SetToken(UserOptToken::LastName, u"Test"_ustr);
// Write the lock file and read it back
svt::MSODocumentLockFile aLockFile(aTestODT);
aLockFile.CreateOwnLockFile();
// Change user name
aUserOpt.SetToken(UserOptToken::FirstName, u"LockFile2"_ustr);
aUserOpt.SetToken(UserOptToken::LastName, u"Test"_ustr);
// Check whether the lock file attributes are the same
CPPUNIT_ASSERT_EQUAL(aOrigEntry[LockFileComponent::OOOUSERNAME],
aRTEntry[LockFileComponent::OOOUSERNAME]);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.