Kaydet (Commit) 5db1e20b authored tarafından Tamás Zolnai's avatar Tamás Zolnai

Introduce new lockfile handler for MSO like lockfiles

* Implement writing of MSO lockfiles
* Grab the already implemented parsing code (tryMSOwnerFile method)
and put it together into one class
* Add tests about the generated URL for lockfiles and the lockfile content
* MSO lockfiles are not written yet by LO, next step is to integrate
this code into the locking mechanism.

Change-Id: I3b0ed1975cd57dfd006d4e1890b23c307890de5c
Reviewed-on: https://gerrit.libreoffice.org/69582
Tested-by: Jenkins
Reviewed-by: 's avatarTamás Zolnai <tamas.zolnai@collabora.com>
üst 0ed7e590
......@@ -29,23 +29,41 @@ namespace com { namespace sun { namespace star { namespace io { class XOutputStr
namespace svt {
class SVL_DLLPUBLIC DocumentLockFile : public LockFileCommon
/// Generalized class for LO and MSO lockfile handling.
class SVL_DLLPUBLIC GenDocumentLockFile : public LockFileCommon
{
css::uno::Reference< css::io::XInputStream > OpenStream();
void WriteEntryToStream( const LockFileEntry& aEntry, const css::uno::Reference< css::io::XOutputStream >& xStream );
public:
DocumentLockFile( const OUString& aOrigURL );
~DocumentLockFile();
/// Specify the lockfile URL directly
GenDocumentLockFile( const OUString& aURL );
/// Let the object generate and own URL based on the original file's URL and a prefix
GenDocumentLockFile( const OUString& aOrigURL, const OUString& aPrefix );
virtual ~GenDocumentLockFile() override;
bool CreateOwnLockFile();
LockFileEntry GetLockData();
bool OverwriteOwnLockFile();
/// Delete the Lockfile, if current user is the owner
void RemoveFile();
virtual void RemoveFile();
/// Only delete lockfile, disregarding ownership
void RemoveFileDirectly();
virtual LockFileEntry GetLockData() = 0;
protected:
virtual void WriteEntryToStream( const LockFileEntry& aEntry, const css::uno::Reference< css::io::XOutputStream >& xStream ) = 0;
css::uno::Reference< css::io::XInputStream > OpenStream();
};
/// Class implementing reading and writing LO lockfiles.
class SVL_DLLPUBLIC DocumentLockFile : public GenDocumentLockFile
{
protected:
virtual void WriteEntryToStream( const LockFileEntry& aEntry, const css::uno::Reference< css::io::XOutputStream >& xStream ) override;
public:
DocumentLockFile( const OUString& aOrigURL );
virtual ~DocumentLockFile() override;
virtual LockFileEntry GetLockData() override;
};
}
......
......@@ -38,18 +38,27 @@ typedef o3tl::enumarray<LockFileComponent,OUString> LockFileEntry;
namespace svt {
// This is a general implementation that is used in document lock file implementation and in sharing control file implementation
/// This is a general implementation that is used in document lock file implementation and in sharing control file implementation
class SVL_DLLPUBLIC LockFileCommon
{
protected:
::osl::Mutex m_aMutex;
private:
OUString m_aURL;
INetURLObject ResolveLinks( const INetURLObject& aDocURL ) const;
protected:
::osl::Mutex m_aMutex;
public:
/// Specify the lockfile URL directly
LockFileCommon( const OUString& aURL );
/// Let the object generate and own URL based on the original file's URL and a prefix
LockFileCommon( const OUString& aOrigURL, const OUString& aPrefix );
~LockFileCommon();
virtual ~LockFileCommon();
const OUString& GetURL() const;
void SetURL(const OUString& aURL);
/// This method generates the URL of the lock file based on the document URL and the specified prefix.
virtual OUString GenerateURL( const OUString& aOrigURL, const OUString& aPrefix );
static void ParseList( const css::uno::Sequence< sal_Int8 >& aBuffer, std::vector< LockFileEntry > &rOutput );
static LockFileEntry ParseEntry( const css::uno::Sequence< sal_Int8 >& aBuffer, sal_Int32& o_nCurPos );
......@@ -58,6 +67,8 @@ public:
static OUString GetOOOUserName();
static OUString GetCurrentLocalTime();
static LockFileEntry GenerateOwnEntry();
INetURLObject ResolveLinks( const INetURLObject& aDocURL ) const;
};
}
......
/* -*- 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/.
*/
#ifndef INCLUDED_SVL_MSODOCUMENTLOCKFILE_HXX
#define INCLUDED_SVL_MSODOCUMENTLOCKFILE_HXX
#include <svl/svldllapi.h>
#include <svl/lockfilecommon.hxx>
#include <svl/documentlockfile.hxx>
#include <com/sun/star/lang/XComponent.hpp>
namespace com
{
namespace sun
{
namespace star
{
namespace io
{
class XInputStream;
}
}
}
}
namespace com
{
namespace sun
{
namespace star
{
namespace io
{
class XOutputStream;
}
}
}
}
#define MSO_WORD_LOCKFILE_SIZE 162
#define MSO_EXCEL_AND_POWERPOINT_LOCKFILE_SIZE 165
#define MSO_USERNAME_MAX_LENGTH 52
namespace svt
{
/// Class implementing reading and writing MSO lockfiles.
class SVL_DLLPUBLIC MSODocumentLockFile : public GenDocumentLockFile
{
private:
OUString m_sOrigURL;
bool isWordFormat(const OUString& aOrigURL) const;
bool isExcelFormat(const OUString& aOrigURL) const;
bool isPowerPointFormat(const OUString& aOrigURL) const;
protected:
virtual void
WriteEntryToStream(const LockFileEntry& aEntry,
const css::uno::Reference<css::io::XOutputStream>& xStream) override;
public:
MSODocumentLockFile(const OUString& aOrigURL);
virtual ~MSODocumentLockFile() override;
/// Need to generate different lock file name for MSO.
virtual OUString GenerateURL(const OUString& aOrigURL, const OUString& aPrefix) override;
virtual LockFileEntry GetLockData() override;
virtual void RemoveFile() override;
};
}
#endif
/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
......@@ -53,7 +53,7 @@ public:
// The constructor will throw exception in case the stream can not be opened
ShareControlFile( const OUString& aOrigURL );
~ShareControlFile();
virtual ~ShareControlFile() override;
std::vector< LockFileEntry > GetUsersData();
void SetUsersDataAndStore( const std::vector< LockFileEntry >& aUserNames );
......
# -*- Mode: makefile-gmake; tab-width: 4; indent-tabs-mode: t; 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/.
#
$(eval $(call gb_CppunitTest_CppunitTest,svl_lockfiles))
$(eval $(call gb_CppunitTest_use_sdk_api,svl_lockfiles))
$(eval $(call gb_CppunitTest_use_api,svl_lockfiles,\
udkapi \
offapi \
oovbaapi \
))
$(eval $(call gb_CppunitTest_use_ure,svl_lockfiles))
$(eval $(call gb_CppunitTest_use_vcl,svl_lockfiles))
$(eval $(call gb_CppunitTest_add_exception_objects,svl_lockfiles, \
svl/qa/unit/lockfiles/test_lockfiles \
))
$(eval $(call gb_CppunitTest_use_libraries,svl_lockfiles, \
comphelper \
cppu \
cppuhelper \
tl \
sal \
svl \
svt \
sw \
test \
unotest \
utl \
vcl \
))
$(eval $(call gb_CppunitTest_use_rdb,svl_lockfiles,services))
$(eval $(call gb_CppunitTest_use_custom_headers,svl_lockfiles,\
officecfg/registry \
))
$(eval $(call gb_CppunitTest_use_configuration,svl_lockfiles))
# vim: set noet sw=4 ts=4:
......@@ -158,7 +158,8 @@ $(eval $(call gb_Library_add_exception_objects,svl,\
svl/source/misc/PasswordHelper \
svl/source/misc/adrparse \
$(if $(filter DESKTOP,$(BUILD_TYPE)),\
svl/source/misc/documentlockfile) \
svl/source/misc/documentlockfile \
svl/source/misc/msodocumentlockfile) \
svl/source/misc/filenotation \
svl/source/misc/fstathelper \
svl/source/misc/getstringresource \
......
......@@ -34,6 +34,7 @@ $(eval $(call gb_Module_add_check_targets,svl,\
CppunitTest_svl_itempool \
CppunitTest_svl_items \
CppunitTest_svl_lngmisc \
CppunitTest_svl_lockfiles \
CppunitTest_svl_notify \
CppunitTest_svl_qa_cppunit \
CppunitTest_svl_urihelper \
......
/* -*- 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/.
*/
#include <sal/config.h>
#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/plugin/TestPlugIn.h>
#include <test/bootstrapfixture.hxx>
#include <unotest/directories.hxx>
#include <svl/lockfilecommon.hxx>
#include <svl/documentlockfile.hxx>
#include <svl/msodocumentlockfile.hxx>
#include <unotools/useroptions.hxx>
#include <comphelper/sequence.hxx>
#include <tools/stream.hxx>
#include <rtl/strbuf.hxx>
#include <osl/security.hxx>
#include <osl/socket.hxx>
#include <unotools/bootstrap.hxx>
namespace
{
class LockfileTest : public test::BootstrapFixture
{
public:
void testLOLockFileURL();
void testLOLockFileContent();
void testLOLockFileRT();
void testLOLockFileUnicodeUsername();
void testLOLockFileOverwrite();
void testWordLockFileURL();
void testExcelLockFileURL();
void testPowerPointLockFileURL();
void testWordLockFileContent();
void testExcelLockFileContent();
void testPowerPointLockFileContent();
void testWordLockFileRT();
void testExcelLockFileRT();
void testPowerPointLockFileRT();
void testMSOLockFileLongUserName();
void testMSOLockFileUnicodeUsername();
void testMSOLockFileOverwrite();
private:
CPPUNIT_TEST_SUITE(LockfileTest);
CPPUNIT_TEST(testLOLockFileURL);
CPPUNIT_TEST(testLOLockFileContent);
CPPUNIT_TEST(testLOLockFileRT);
CPPUNIT_TEST(testLOLockFileUnicodeUsername);
CPPUNIT_TEST(testLOLockFileOverwrite);
CPPUNIT_TEST(testWordLockFileURL);
CPPUNIT_TEST(testExcelLockFileURL);
CPPUNIT_TEST(testPowerPointLockFileURL);
CPPUNIT_TEST(testWordLockFileContent);
CPPUNIT_TEST(testExcelLockFileContent);
CPPUNIT_TEST(testPowerPointLockFileContent);
CPPUNIT_TEST(testWordLockFileRT);
CPPUNIT_TEST(testExcelLockFileRT);
CPPUNIT_TEST(testPowerPointLockFileRT);
CPPUNIT_TEST(testMSOLockFileLongUserName);
CPPUNIT_TEST(testMSOLockFileUnicodeUsername);
CPPUNIT_TEST(testMSOLockFileOverwrite);
CPPUNIT_TEST_SUITE_END();
};
OUString readLockFile(const OUString& aSource)
{
SvFileStream aFileStream(aSource, StreamMode::READ);
std::size_t nSize = aFileStream.remainingSize();
std::unique_ptr<sal_Int8[]> pBuffer(new sal_Int8[nSize]);
aFileStream.ReadBytes(pBuffer.get(), nSize);
css::uno::Sequence<sal_Int8> aData(pBuffer.get(), nSize);
OStringBuffer aResult;
for (sal_Int8 nByte : aData)
{
aResult.append(static_cast<sal_Char>(nByte));
}
return OStringToOUString(aResult.makeStringAndClear(), RTL_TEXTENCODING_UTF8);
}
OUString GetLockFileName(const svt::GenDocumentLockFile& rLockFile)
{
INetURLObject aDocURL = rLockFile.ResolveLinks(INetURLObject(rLockFile.GetURL()));
return aDocURL.GetName();
}
void LockfileTest::testLOLockFileURL()
{
// Test the generated file name for LibreOffice lock files
OUString aTestODT
= m_directories.getURLFromSrc("/svl/qa/unit/lockfiles/data/testLOLockFileURL.odt");
svt::DocumentLockFile aLockFile(aTestODT);
CPPUNIT_ASSERT_EQUAL(OUString(".~lock.testLOLockFileURL.odt%23"), GetLockFileName(aLockFile));
}
void LockfileTest::testLOLockFileContent()
{
// Test the lockfile generated for the specified ODT document
OUString aTestODT
= m_directories.getURLFromSrc("/svl/qa/unit/lockfiles/data/testLOLockFileContent.odt");
// Set user name
SvtUserOptions aUserOpt;
aUserOpt.SetToken(UserOptToken::FirstName, "LockFile");
aUserOpt.SetToken(UserOptToken::LastName, "Test");
// Write the lock file and check the content
svt::DocumentLockFile aLockFile(aTestODT);
aLockFile.CreateOwnLockFile();
OUString sLockFileContent(readLockFile(aLockFile.GetURL()));
aLockFile.RemoveFileDirectly();
// User name
sal_Int32 nFirstChar = 0;
sal_Int32 nNextComma = sLockFileContent.indexOf(',', nFirstChar);
OUString sUserName;
sUserName += aUserOpt.GetFirstName() + " ";
sUserName += aUserOpt.GetLastName();
CPPUNIT_ASSERT_EQUAL(sUserName, sLockFileContent.copy(nFirstChar, nNextComma - nFirstChar));
// System user name
nFirstChar = nNextComma + 1;
nNextComma = sLockFileContent.indexOf(',', nFirstChar);
::osl::Security aSecurity;
OUString sSysUserName;
aSecurity.getUserName(sSysUserName);
CPPUNIT_ASSERT_EQUAL(sSysUserName, sLockFileContent.copy(nFirstChar, nNextComma - nFirstChar));
// Local host
nFirstChar = nNextComma + 1;
nNextComma = sLockFileContent.indexOf(',', nFirstChar);
CPPUNIT_ASSERT_EQUAL(::osl::SocketAddr::getLocalHostname(),
sLockFileContent.copy(nFirstChar, nNextComma - nFirstChar));
// Skip date and time because it changes after the lock file was created
nFirstChar = nNextComma + 1;
nNextComma = sLockFileContent.indexOf(',', nFirstChar);
// user url
nFirstChar = nNextComma + 1;
OUString aUserInstDir;
::utl::Bootstrap::locateUserInstallation(aUserInstDir);
CPPUNIT_ASSERT_EQUAL(
aUserInstDir,
sLockFileContent.copy(nFirstChar, sLockFileContent.getLength() - nFirstChar - 1));
}
void LockfileTest::testLOLockFileRT()
{
// Test the lockfile generated for the specified ODT document
OUString aTestODT
= m_directories.getURLFromSrc("/svl/qa/unit/lockfiles/data/testLOLockFileRT.odt");
// Set user name
SvtUserOptions aUserOpt;
aUserOpt.SetToken(UserOptToken::FirstName, "LockFile");
aUserOpt.SetToken(UserOptToken::LastName, "Test");
// 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 = m_directories.getURLFromSrc(
"/svl/qa/unit/lockfiles/data/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]);
aLockFile.RemoveFileDirectly();
}
void LockfileTest::testLOLockFileOverwrite()
{
OUString aTestODT
= m_directories.getURLFromSrc("/svl/qa/unit/lockfiles/data/testLOLockFileOverwrite.odt");
// Set user name
SvtUserOptions aUserOpt;
aUserOpt.SetToken(UserOptToken::FirstName, "LockFile");
aUserOpt.SetToken(UserOptToken::LastName, "Test");
// Write the lock file and read it back
svt::DocumentLockFile aLockFile(aTestODT);
aLockFile.CreateOwnLockFile();
// Change user name
aUserOpt.SetToken(UserOptToken::FirstName, "LockFile2");
aUserOpt.SetToken(UserOptToken::LastName, "Test");
// Overwrite lockfile
svt::DocumentLockFile aLockFile2(aTestODT);
LockFileEntry aOrigEntry = svt::LockFileCommon::GenerateOwnEntry();
aLockFile2.OverwriteOwnLockFile();
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]);
aLockFile2.RemoveFileDirectly();
}
void LockfileTest::testWordLockFileURL()
{
// Test the generated file name for Word lock files
// Word specific file format
{
OUString aTestFile
= m_directories.getURLFromSrc("/svl/qa/unit/lockfiles/data/testWordLockFileURL.docx");
svt::MSODocumentLockFile aLockFile(aTestFile);
CPPUNIT_ASSERT_EQUAL(OUString("~$stWordLockFileURL.docx"), GetLockFileName(aLockFile));
}
// Eight character file name (cuts two characters)
{
OUString aTestFile
= m_directories.getURLFromSrc("/svl/qa/unit/lockfiles/data/12345678.docx");
svt::MSODocumentLockFile aLockFile(aTestFile);
CPPUNIT_ASSERT_EQUAL(OUString("~$345678.docx"), GetLockFileName(aLockFile));
}
// Seven character file name (cuts one character)
{
OUString aTestFile
= m_directories.getURLFromSrc("/svl/qa/unit/lockfiles/data/1234567.docx");
svt::MSODocumentLockFile aLockFile(aTestFile);
CPPUNIT_ASSERT_EQUAL(OUString("~$234567.docx"), GetLockFileName(aLockFile));
}
// Six character file name (cuts no character)
{
OUString aTestFile = m_directories.getURLFromSrc("/svl/qa/unit/lockfiles/data/123456.docx");
svt::MSODocumentLockFile aLockFile(aTestFile);
CPPUNIT_ASSERT_EQUAL(OUString("~$123456.docx"), GetLockFileName(aLockFile));
}
// One character file name
{
OUString aTestFile = m_directories.getURLFromSrc("/svl/qa/unit/lockfiles/data/1.docx");
svt::MSODocumentLockFile aLockFile(aTestFile);
CPPUNIT_ASSERT_EQUAL(OUString("~$1.docx"), GetLockFileName(aLockFile));
}
// Test for ODT format
{
OUString aTestFile
= m_directories.getURLFromSrc("/svl/qa/unit/lockfiles/data/12345678.odt");
svt::MSODocumentLockFile aLockFile(aTestFile);
CPPUNIT_ASSERT_EQUAL(OUString("~$345678.odt"), GetLockFileName(aLockFile));
}
// Test for DOC format
{
OUString aTestFile
= m_directories.getURLFromSrc("/svl/qa/unit/lockfiles/data/12345678.doc");
svt::MSODocumentLockFile aLockFile(aTestFile);
CPPUNIT_ASSERT_EQUAL(OUString("~$345678.doc"), GetLockFileName(aLockFile));
}
// Test for RTF format
{
OUString aTestFile
= m_directories.getURLFromSrc("/svl/qa/unit/lockfiles/data/12345678.rtf");
svt::MSODocumentLockFile aLockFile(aTestFile);
CPPUNIT_ASSERT_EQUAL(OUString("~$345678.rtf"), GetLockFileName(aLockFile));
}
}
void LockfileTest::testExcelLockFileURL()
{
// Test the generated file name for Excel lock files
{
OUString aTestFile
= m_directories.getURLFromSrc("/svl/qa/unit/lockfiles/data/testExcelLockFileURL.xlsx");
svt::MSODocumentLockFile aLockFile(aTestFile);
CPPUNIT_ASSERT_EQUAL(OUString("~$testExcelLockFileURL.xlsx"), GetLockFileName(aLockFile));
}
// Eight character file name
{
OUString aTestFile
= m_directories.getURLFromSrc("/svl/qa/unit/lockfiles/data/12345678.xlsx");
svt::MSODocumentLockFile aLockFile(aTestFile);
CPPUNIT_ASSERT_EQUAL(OUString("~$12345678.xlsx"), GetLockFileName(aLockFile));
}
// One character file name
{
OUString aTestFile = m_directories.getURLFromSrc("/svl/qa/unit/lockfiles/data/1.xlsx");
svt::MSODocumentLockFile aLockFile(aTestFile);
CPPUNIT_ASSERT_EQUAL(OUString("~$1.xlsx"), GetLockFileName(aLockFile));
}
// Test for ODS format
{
OUString aTestFile
= m_directories.getURLFromSrc("/svl/qa/unit/lockfiles/data/12345678.ods");
svt::MSODocumentLockFile aLockFile(aTestFile);
CPPUNIT_ASSERT_EQUAL(OUString("~$12345678.ods"), GetLockFileName(aLockFile));
}
}
void LockfileTest::testPowerPointLockFileURL()
{
// Test the generated file name for PowerPoint lock files
{
OUString aTestFile = m_directories.getURLFromSrc(
"/svl/qa/unit/lockfiles/data/testPowerPointLockFileURL.pptx");
svt::MSODocumentLockFile aLockFile(aTestFile);
CPPUNIT_ASSERT_EQUAL(OUString("~$testPowerPointLockFileURL.pptx"),
GetLockFileName(aLockFile));
}
// Eight character file name
{
OUString aTestFile
= m_directories.getURLFromSrc("/svl/qa/unit/lockfiles/data/12345678.pptx");
svt::MSODocumentLockFile aLockFile(aTestFile);
CPPUNIT_ASSERT_EQUAL(OUString("~$12345678.pptx"), GetLockFileName(aLockFile));
}
// One character file name
{
OUString aTestFile = m_directories.getURLFromSrc("/svl/qa/unit/lockfiles/data/1.pptx");
svt::MSODocumentLockFile aLockFile(aTestFile);
CPPUNIT_ASSERT_EQUAL(OUString("~$1.pptx"), GetLockFileName(aLockFile));
}
// Test for PPT format
{
OUString aTestFile
= m_directories.getURLFromSrc("/svl/qa/unit/lockfiles/data/12345678.ppt");
svt::MSODocumentLockFile aLockFile(aTestFile);
CPPUNIT_ASSERT_EQUAL(OUString("~$12345678.ppt"), GetLockFileName(aLockFile));
}
// Test for ODP format
{
OUString aTestFile
= m_directories.getURLFromSrc("/svl/qa/unit/lockfiles/data/12345678.odp");
svt::MSODocumentLockFile aLockFile(aTestFile);
CPPUNIT_ASSERT_EQUAL(OUString("~$12345678.odp"), GetLockFileName(aLockFile));
}
}
void LockfileTest::testWordLockFileContent()
{
// Test the lockfile generated for the specified DOCX document
OUString aTestFile
= m_directories.getURLFromSrc("/svl/qa/unit/lockfiles/data/testWordLockFileContent.docx");
// Set user name
SvtUserOptions aUserOpt;
aUserOpt.SetToken(UserOptToken::FirstName, "LockFile");
aUserOpt.SetToken(UserOptToken::LastName, "Test");
// 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;
sUserName += aUserOpt.GetFirstName() + " ";
sUserName += 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
= m_directories.getURLFromSrc("/svl/qa/unit/lockfiles/data/testExcelLockFileContent.xlsx");
// Set user name
SvtUserOptions aUserOpt;