示例#1
0
        public void Local1ByteFileAdded([Values(true, false)] bool withExtendedAttributes)
        {
            this.SetUpMocks(withExtendedAttributes);
            Mock <IFileInfo> fileInfo = new Mock <IFileInfo>();

            fileInfo.Setup(f => f.Length).Returns(1);
            var fileContent = new byte[1];

            using (var localFileStream = new MemoryStream(fileContent)) {
                byte[] hash = SHA1Managed.Create().ComputeHash(fileContent);

                fileInfo.Setup(f => f.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete)).Returns(localFileStream);

                Mock <IDocument> document;
                this.SetupSolveFile(this.localObjectName, this.remoteObjectId, this.parentId, this.lastChangeToken, this.withExtendedAttributes, fileInfo, out document);
                this.RunSolveFile(fileInfo);
                this.storage.VerifySavedMappedObject(MappedObjectType.File, this.remoteObjectId, this.localObjectName, this.parentId, this.lastChangeToken, Times.Exactly(2), this.withExtendedAttributes, null, null, hash, 1);
                this.VerifyCreateDocument();
                if (this.withExtendedAttributes)
                {
                    fileInfo.VerifySet(f => f.Uuid = It.Is <Guid?>(uuid => uuid != null), Times.Once());
                }
                else
                {
                    fileInfo.VerifySet(f => f.Uuid = It.IsAny <Guid?>(), Times.Never());
                }

                fileInfo.VerifyThatLocalFileObjectLastWriteTimeUtcIsNeverModified();
                document.Verify(d => d.SetContentStream(It.IsAny <IContentStream>(), true, true), Times.Once());
                document.VerifyUpdateLastModificationDate(fileInfo.Object.LastWriteTimeUtc, true);
            }
        }
示例#2
0
        public void LocalEmptyFileAddedWithExtAttrAndAlreadySetUUID()
        {
            string fileName           = "fileName";
            string fileId             = "fileId";
            string parentId           = "parentId";
            string lastChangeToken    = "token";
            bool   extendedAttributes = true;
            Guid   uuid = Guid.NewGuid();

            Mock <IFileInfo> fileInfo = new Mock <IFileInfo>();

            fileInfo.Setup(f => f.Length).Returns(0);
            fileInfo.SetupGuid(uuid);

            Mock <IDocument> document;

            this.RunSolveFile(fileName, fileId, parentId, lastChangeToken, extendedAttributes, fileInfo, out document);
            this.storage.VerifySavedMappedObject(MappedObjectType.File, fileId, fileName, parentId, lastChangeToken, extendedAttributes, checksum: this.emptyhash, contentSize: 0);
            this.storage.Verify(s => s.SaveMappedObject(It.Is <IMappedObject>(o => o.Guid == uuid)));
            this.session.Verify(
                s => s.CreateDocument(
                    It.Is <IDictionary <string, object> >(p => p.ContainsKey("cmis:name")),
                    It.Is <IObjectId>(o => o.Id == parentId),
                    It.Is <IContentStream>(st => this.VerifyEmptyStream(st)),
                    null,
                    null,
                    null,
                    null),
                Times.Once());
            fileInfo.Verify(d => d.SetExtendedAttribute(It.IsAny <string>(), It.IsAny <string>(), true), Times.Never());
            fileInfo.VerifyThatLocalFileObjectLastWriteTimeUtcIsNeverModified();
            document.Verify(d => d.AppendContentStream(It.IsAny <IContentStream>(), It.IsAny <bool>(), It.IsAny <bool>()), Times.Never());
        }
示例#3
0
        public void LocalFolderChanged()
        {
            var modificationDate = DateTime.UtcNow;
            var localDirectory   = new Mock <IDirectoryInfo>();

            localDirectory.Setup(f => f.LastWriteTimeUtc).Returns(modificationDate.AddMinutes(1));
            localDirectory.Setup(f => f.Exists).Returns(true);

            var mappedObject = new MappedObject(
                "name",
                "remoteId",
                MappedObjectType.Folder,
                "parentId",
                "changeToken")
            {
                Guid = Guid.NewGuid(),
                LastRemoteWriteTimeUtc = modificationDate.AddMinutes(1)
            };

            this.storage.AddMappedFolder(mappedObject);

            this.underTest.Solve(localDirectory.Object, Mock.Of <IFolder>());

            this.storage.VerifySavedMappedObject(
                MappedObjectType.Folder,
                "remoteId",
                mappedObject.Name,
                mappedObject.ParentId,
                mappedObject.LastChangeToken,
                true,
                localDirectory.Object.LastWriteTimeUtc);
            this.queue.Verify(q => q.AddEvent(It.IsAny <ISyncEvent>()), Times.Never());
            localDirectory.VerifyThatLocalFileObjectLastWriteTimeUtcIsNeverModified();
            this.manager.Verify(m => m.AddTransmission(It.IsAny <FileTransmissionEvent>()), Times.Never());
        }
示例#4
0
        public void LocalFileIsUsedByAnotherProcess()
        {
            string    fileName           = "fileName";
            string    fileId             = "fileId";
            string    parentId           = "parentId";
            string    lastChangeToken    = "token";
            bool      extendedAttributes = true;
            Exception exception          = new ExtendedAttributeException();

            Mock <IFileInfo> fileInfo = new Mock <IFileInfo>();

            fileInfo.Setup(f => f.Length).Returns(0);
            fileInfo.Setup(f => f.SetExtendedAttribute(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <bool>())).Throws(exception);

            try {
                Mock <IDocument> document;
                this.RunSolveFile(fileName, fileId, parentId, lastChangeToken, extendedAttributes, fileInfo, out document);
            } catch (RetryException e) {
                Assert.That(e.InnerException, Is.EqualTo(exception));
                fileInfo.Verify(d => d.SetExtendedAttribute(It.IsAny <string>(), It.IsAny <string>(), true), Times.Once());
                this.storage.Verify(s => s.SaveMappedObject(It.IsAny <IMappedObject>()), Times.Never());
                this.queue.Verify(q => q.AddEvent(It.IsAny <FileTransmissionEvent>()), Times.Never());
                fileInfo.VerifyThatLocalFileObjectLastWriteTimeUtcIsNeverModified();
                throw;
            }
        }
        public void LocalAndRemoteFileDatesAreChangedAndLocalDateIsNewerUpdatesRemoteDate()
        {
            this.InitMocks();
            string   fileName = "fileName";
            DateTime lastLocalModification  = DateTime.UtcNow.AddDays(1);
            DateTime lastRemoteModification = DateTime.UtcNow.AddHours(1);
            var      localFile = new Mock <IFileInfo>();

            byte[] contentHash;
            using (var stream = this.SetUpFileWithContent(localFile, "content", out contentHash, lastLocalModification)) {
                long length       = stream.Length;
                var  remoteFile   = this.CreateRemoteDocument(lastRemoteModification, length, contentHash);
                var  mappedObject = new MappedObject(fileName, this.remoteId, MappedObjectType.File, this.parentId, this.oldChangeToken, length)
                {
                    Guid                   = Guid.NewGuid(),
                    LastChecksum           = contentHash,
                    LastLocalWriteTimeUtc  = DateTime.UtcNow,
                    LastRemoteWriteTimeUtc = DateTime.UtcNow,
                    ChecksumAlgorithmName  = "SHA-1"
                };
                this.storage.AddMappedFile(mappedObject);

                this.underTest.Solve(localFile.Object, remoteFile.Object, ContentChangeType.NONE, ContentChangeType.NONE);

                remoteFile.VerifyUpdateLastModificationDate(lastLocalModification, Times.Once(), true);
                localFile.VerifyThatLocalFileObjectLastWriteTimeUtcIsNeverModified();
                this.storage.VerifySavedMappedObject(MappedObjectType.File, this.remoteId, fileName, this.parentId, this.newChangeToken, lastLocalModification: lastLocalModification, lastRemoteModification: lastRemoteModification, checksum: contentHash, contentSize: length);
            }
        }
示例#6
0
        public void LocalFileModificationDateChanged()
        {
            string path             = "path";
            var    modificationDate = DateTime.UtcNow;
            int    fileLength       = 20;

            byte[] content      = new byte[fileLength];
            byte[] expectedHash = SHA1Managed.Create().ComputeHash(content);
            var    localFile    = new Mock <IFileInfo>();

            localFile.SetupProperty(f => f.LastWriteTimeUtc, modificationDate.AddMinutes(1));
            localFile.Setup(f => f.Length).Returns(fileLength);
            localFile.Setup(f => f.FullName).Returns(path);
            localFile.Setup(f => f.Exists).Returns(true);

            using (var stream = new MemoryStream(content)) {
                localFile.Setup(
                    f =>
                    f.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete)).Returns(stream);

                var mappedObject = new MappedObject(
                    "name",
                    "remoteId",
                    MappedObjectType.File,
                    "parentId",
                    "changeToken",
                    fileLength)
                {
                    Guid = Guid.NewGuid(),
                    LastRemoteWriteTimeUtc = modificationDate.AddMinutes(1),
                    LastLocalWriteTimeUtc  = modificationDate,
                    LastChecksum           = expectedHash,
                    ChecksumAlgorithmName  = "SHA-1"
                };

                this.storage.AddMappedFile(mappedObject, path);

                this.underTest.Solve(localFile.Object, Mock.Of <IDocument>());

                this.storage.VerifySavedMappedObject(
                    MappedObjectType.File,
                    "remoteId",
                    mappedObject.Name,
                    mappedObject.ParentId,
                    mappedObject.LastChangeToken,
                    true,
                    localFile.Object.LastWriteTimeUtc,
                    mappedObject.LastRemoteWriteTimeUtc,
                    expectedHash,
                    fileLength);
                this.queue.Verify(q => q.AddEvent(It.IsAny <ISyncEvent>()), Times.Never());
                this.manager.Verify(m => m.AddTransmission(It.IsAny <FileTransmissionEvent>()), Times.Never());
            }

            localFile.VerifyThatLocalFileObjectLastWriteTimeUtcIsNeverModified();
        }
示例#7
0
        public void IgnoreChangesOnNonExistingLocalObject()
        {
            var localDirectory = new Mock <IDirectoryInfo>();

            localDirectory.Setup(f => f.Exists).Returns(false);

            Assert.Throws <ArgumentException>(() => this.underTest.Solve(localDirectory.Object, Mock.Of <IFolder>()));

            this.storage.Verify(s => s.SaveMappedObject(It.IsAny <IMappedObject>()), Times.Never());
            this.queue.Verify(q => q.AddEvent(It.IsAny <ISyncEvent>()), Times.Never());
            localDirectory.VerifyThatLocalFileObjectLastWriteTimeUtcIsNeverModified();
            this.manager.Verify(m => m.AddTransmission(It.IsAny <FileTransmissionEvent>()), Times.Never());
        }
示例#8
0
        public void DoNotWriteLastWriteTimeUtcIfNotNecessary()
        {
            this.SetUpMocks(true);

            Mock <IFileInfo> fileInfo = new Mock <IFileInfo>();

            fileInfo.Setup(f => f.Length).Returns(0);

            Mock <IDocument> document;

            this.RunSolveFile(this.localObjectName, this.remoteObjectId, this.parentId, lastChangeToken, this.withExtendedAttributes, fileInfo, out document, false);

            fileInfo.VerifyThatLocalFileObjectLastWriteTimeUtcIsNeverModified();
        }
示例#9
0
        public void LocalObjectChangeFailsIfObjectIsNotAvailableInStorage()
        {
            var modificationDate = DateTime.UtcNow;
            var localDirectory   = new Mock <IDirectoryInfo>();

            localDirectory.Setup(f => f.LastWriteTimeUtc).Returns(modificationDate.AddMinutes(1));
            localDirectory.Setup(f => f.Exists).Returns(true);

            Assert.Throws <ArgumentException>(() => this.underTest.Solve(localDirectory.Object, Mock.Of <IFolder>()));

            this.storage.Verify(s => s.SaveMappedObject(It.IsAny <IMappedObject>()), Times.Never());
            localDirectory.VerifyThatLocalFileObjectLastWriteTimeUtcIsNeverModified();
            this.manager.Verify(m => m.AddTransmission(It.IsAny <FileTransmissionEvent>()), Times.Never());
        }
示例#10
0
        public void LocalFileModificationDateNotWritableShallNotThrow()
        {
            var modificationDate    = DateTime.UtcNow;
            var newModificationDate = modificationDate.AddHours(1);
            var newChangeToken      = "newChangeToken";
            int fileLength          = 20;

            byte[] content = new byte[fileLength];

            var localFile = new Mock <IFileInfo>();

            localFile.SetupProperty(f => f.LastWriteTimeUtc, modificationDate.AddMinutes(1));
            localFile.SetupSet(f => f.LastWriteTimeUtc = It.IsAny <DateTime>()).Throws(new IOException());
            localFile.Setup(f => f.Length).Returns(fileLength);
            localFile.Setup(f => f.FullName).Returns("path");
            localFile.Setup(f => f.Exists).Returns(true);
            using (var uploadedContent = new MemoryStream()) {
                localFile.Setup(
                    f =>
                    f.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete)).Returns(() => { return(new MemoryStream(content)); });

                var mappedObject = new MappedObject(
                    "name",
                    "remoteId",
                    MappedObjectType.File,
                    "parentId",
                    "changeToken",
                    fileLength)
                {
                    Guid = Guid.NewGuid(),
                    LastRemoteWriteTimeUtc = modificationDate.AddMinutes(1),
                    LastLocalWriteTimeUtc  = modificationDate,
                    LastChecksum           = new byte[20],
                    ChecksumAlgorithmName  = "SHA-1"
                };
                this.storage.AddMappedFile(mappedObject, "path");
                var remoteFile = MockOfIDocumentUtil.CreateRemoteDocumentMock(null, "remoteId", "name", "parentId", fileLength, new byte[20]);
                remoteFile.Setup(r => r.SetContentStream(It.IsAny <IContentStream>(), true, true)).Callback <IContentStream, bool, bool>(
                    (s, o, r) =>
                    { s.Stream.CopyTo(uploadedContent);
                      remoteFile.Setup(f => f.LastModificationDate).Returns(newModificationDate);
                      remoteFile.Setup(f => f.ChangeToken).Returns(newChangeToken); });

                this.underTest.Solve(localFile.Object, remoteFile.Object);
            }

            localFile.VerifyThatLocalFileObjectLastWriteTimeUtcIsNeverModified();
            this.manager.Verify(m => m.AddTransmission(It.IsAny <FileTransmissionEvent>()), Times.Once());
        }
示例#11
0
        public void LocalEmptyFileAddedWithExtAttr()
        {
            this.SetUpMocks(true);

            Mock <IFileInfo> fileInfo = new Mock <IFileInfo>();

            fileInfo.Setup(f => f.Length).Returns(0);

            Mock <IDocument> document;

            this.RunSolveFile(this.localObjectName, this.remoteObjectId, this.parentId, lastChangeToken, this.withExtendedAttributes, fileInfo, out document);
            this.storage.VerifySavedMappedObject(MappedObjectType.File, this.remoteObjectId, this.localObjectName, this.parentId, lastChangeToken, this.withExtendedAttributes, checksum: this.emptyhash, contentSize: 0);
            this.VerifyCreateDocument(isEmpty: true);
            fileInfo.VerifySet(f => f.Uuid = It.Is <Guid?>(uuid => uuid != null), Times.Once());
            fileInfo.VerifyThatLocalFileObjectLastWriteTimeUtcIsNeverModified();
            document.Verify(d => d.AppendContentStream(It.IsAny <IContentStream>(), It.IsAny <bool>(), It.IsAny <bool>()), Times.Never());
        }
示例#12
0
        public void DoNotWriteLastWriteTimeUtcIfNotNecessary()
        {
            string fileName           = "fileName";
            string fileId             = "fileId";
            string parentId           = "parentId";
            string lastChangeToken    = "token";
            bool   extendedAttributes = true;

            Mock <IFileInfo> fileInfo = new Mock <IFileInfo>();

            fileInfo.Setup(f => f.Length).Returns(0);

            Mock <IDocument> document;

            this.RunSolveFile(fileName, fileId, parentId, lastChangeToken, extendedAttributes, fileInfo, out document, false);

            fileInfo.VerifyThatLocalFileObjectLastWriteTimeUtcIsNeverModified();
        }
示例#13
0
        public void LocalFileIsUsedByAnotherProcessOnOpenFile()
        {
            this.SetUpMocks();
            Mock <IFileInfo> fileInfo = new Mock <IFileInfo>();

            fileInfo.Setup(f => f.Length).Returns(10);
            fileInfo.Setup(f => f.Open(It.IsAny <FileMode>())).Throws(new IOException("Alread in use by another process"));
            fileInfo.Setup(f => f.Open(It.IsAny <FileMode>(), It.IsAny <FileAccess>())).Throws(new IOException("Alread in use by another process"));
            fileInfo.Setup(f => f.Open(It.IsAny <FileMode>(), It.IsAny <FileAccess>(), It.IsAny <FileShare>())).Throws(new IOException("Alread in use by another process"));

            Mock <IDocument> document;

            Assert.Throws <IOException>(() => this.RunSolveFile(this.localObjectName, this.remoteObjectId, this.parentId, lastChangeToken, true, fileInfo, out document));
            fileInfo.VerifySet(f => f.Uuid = It.Is <Guid?>(uuid => uuid != null), Times.Once());
            this.storage.VerifySavedMappedObject(MappedObjectType.File, this.remoteObjectId, this.localObjectName, this.parentId, lastChangeToken, true, checksum: this.emptyhash, contentSize: 0);
            this.VerifyCreateDocument(isEmpty: false);
            fileInfo.VerifyThatLocalFileObjectLastWriteTimeUtcIsNeverModified();
        }
示例#14
0
        public void LocalFolderAddingFailsBecauseUtf8Character()
        {
            this.SetUpMocks();
            var transmissionManager = new ActiveActivitiesManager();
            var solver  = new LocalObjectAdded(this.session.Object, this.storage.Object, transmissionManager);
            var dirInfo = new Mock <IDirectoryInfo>();

            dirInfo.Setup(d => d.Name).Returns(@"ä".Normalize(System.Text.NormalizationForm.FormD));
            var parentDirInfo = this.SetupParentFolder(parentId);

            dirInfo.Setup(d => d.Parent).Returns(parentDirInfo);
            this.session.Setup(s => s.CreateFolder(It.IsAny <IDictionary <string, object> >(), It.IsAny <IObjectId>())).Throws(new CmisConstraintException("Conflict"));
            Assert.Throws <InteractionNeededException>(() => solver.Solve(dirInfo.Object, null));

            this.storage.VerifyThatNoObjectIsManipulated();
            this.session.Verify(s => s.CreateFolder(It.Is <IDictionary <string, object> >(p => p.ContainsKey("cmis:name")), It.Is <IObjectId>(o => o.Id == this.parentId)), Times.Once());
            dirInfo.VerifyThatLocalFileObjectLastWriteTimeUtcIsNeverModified();
            dirInfo.VerifySet(d => d.Uuid = It.IsAny <Guid?>(), Times.Never());
        }
示例#15
0
        public void LocalFileIsUsedByAnotherProcess()
        {
            this.SetUpMocks(true);
            var exception = new ExtendedAttributeException();

            Mock <IFileInfo> fileInfo = new Mock <IFileInfo>();

            fileInfo.Setup(f => f.Length).Returns(0);
            fileInfo.SetupSet(f => f.Uuid = It.IsAny <Guid?>()).Throws(exception);
            Mock <IDocument> document;

            this.SetupSolveFile(this.localObjectName, this.remoteObjectId, this.parentId, this.lastChangeToken, this.withExtendedAttributes, fileInfo, out document);

            var e = Assert.Throws <RetryException>(() => this.RunSolveFile(fileInfo));

            Assert.That(e.InnerException, Is.EqualTo(exception));
            fileInfo.VerifySet(f => f.Uuid = It.Is <Guid?>(uuid => uuid != null), Times.Once());
            this.storage.Verify(s => s.SaveMappedObject(It.IsAny <IMappedObject>()), Times.Never());
            fileInfo.VerifyThatLocalFileObjectLastWriteTimeUtcIsNeverModified();
        }
示例#16
0
        public void StorageExceptionOnUploadLeadsToSavedEmptyState()
        {
            this.SetUpMocks(true);

            Mock <IFileInfo> fileInfo = new Mock <IFileInfo>();

            fileInfo.Setup(f => f.Length).Returns(1);
            var fileContent     = new byte[1];
            var localFileStream = new MemoryStream(fileContent);

            fileInfo.Setup(f => f.Open(FileMode.Open, FileAccess.Read)).Returns(localFileStream);

            Mock <IDocument> document;

            this.RunSolveFile(this.localObjectName, this.remoteObjectId, this.parentId, lastChangeToken, this.withExtendedAttributes, fileInfo, out document, failsOnUploadContent: true);
            this.storage.VerifySavedMappedObject(MappedObjectType.File, this.remoteObjectId, this.localObjectName, this.parentId, lastChangeToken, this.withExtendedAttributes, checksum: this.emptyhash, contentSize: 0);
            this.VerifyCreateDocument();
            fileInfo.VerifySet(f => f.Uuid = It.Is <Guid?>(uuid => uuid != null), Times.Once());
            fileInfo.VerifyThatLocalFileObjectLastWriteTimeUtcIsNeverModified();
            document.Verify(d => d.SetContentStream(It.IsAny <IContentStream>(), true, true), Times.Once());
            document.Verify(d => d.UpdateProperties(It.IsAny <IDictionary <string, object> >()), Times.Never());
        }
示例#17
0
        public void Local1ByteFileAddedWithoutExtAttr()
        {
            string fileName           = "fileName";
            string fileId             = "fileId";
            string parentId           = "parentId";
            string lastChangeToken    = "token";
            bool   extendedAttributes = false;

            Mock <IFileInfo> fileInfo = new Mock <IFileInfo>();

            fileInfo.Setup(f => f.Length).Returns(1);
            var fileContent     = new byte[1];
            var localFileStream = new MemoryStream(fileContent);

            byte[] hash = SHA1Managed.Create().ComputeHash(fileContent);

            fileInfo.Setup(f => f.Open(FileMode.Open, FileAccess.Read)).Returns(localFileStream);

            Mock <IDocument> document;

            this.RunSolveFile(fileName, fileId, parentId, lastChangeToken, extendedAttributes, fileInfo, out document);
            this.storage.VerifySavedMappedObject(MappedObjectType.File, fileId, fileName, parentId, lastChangeToken, Times.Exactly(2), extendedAttributes, null, null, hash, 1);
            this.session.Verify(
                s =>
                s.CreateDocument(
                    It.Is <IDictionary <string, object> >(p => p.ContainsKey("cmis:name")),
                    It.Is <IObjectId>(o => o.Id == parentId),
                    It.Is <IContentStream>(st => st == null),
                    null,
                    null,
                    null,
                    null),
                Times.Once());
            fileInfo.Verify(d => d.SetExtendedAttribute(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <bool>()), Times.Never());
            fileInfo.VerifyThatLocalFileObjectLastWriteTimeUtcIsNeverModified();
            document.Verify(d => d.SetContentStream(It.IsAny <IContentStream>(), true, true), Times.Once());
            document.VerifyUpdateLastModificationDate(fileInfo.Object.LastWriteTimeUtc, true);
        }
示例#18
0
        public void LocalEmptyFileAdded(bool withExtendedAttributes, bool withAlreadySetUuid)
        {
            this.SetUpMocks(withExtendedAttributes);
            Guid             uuid     = Guid.NewGuid();
            Mock <IFileInfo> fileInfo = new Mock <IFileInfo>();

            fileInfo.Setup(f => f.Length).Returns(0);
            if (withAlreadySetUuid)
            {
                fileInfo.SetupGuid(uuid);
            }

            Mock <IDocument> document;

            this.SetupSolveFile(this.localObjectName, this.remoteObjectId, this.parentId, this.lastChangeToken, this.withExtendedAttributes, fileInfo, out document);
            this.RunSolveFile(fileInfo);

            this.storage.VerifySavedMappedObject(MappedObjectType.File, this.remoteObjectId, this.localObjectName, this.parentId, this.lastChangeToken, this.withExtendedAttributes, checksum: this.emptyhash, contentSize: 0);
            if (withAlreadySetUuid)
            {
                this.storage.Verify(s => s.SaveMappedObject(It.Is <IMappedObject>(o => o.Guid == uuid)));
            }

            this.VerifyCreateDocument(isEmpty: true);
            if (this.withExtendedAttributes && !withAlreadySetUuid)
            {
                fileInfo.VerifySet(f => f.Uuid = It.Is <Guid?>(u => u != null), Times.Once());
            }
            else
            {
                fileInfo.VerifySet(f => f.Uuid = It.IsAny <Guid?>(), Times.Never());
            }

            fileInfo.VerifyThatLocalFileObjectLastWriteTimeUtcIsNeverModified();
            document.Verify(d => d.AppendContentStream(It.IsAny <IContentStream>(), It.IsAny <bool>(), It.IsAny <bool>()), Times.Never());
        }
        public void IgnoreChangesOnNonExistingLocalObject() {
            this.SetUpMocks();
            var localDirectory = new Mock<IDirectoryInfo>();
            localDirectory.Setup(f => f.Exists).Returns(false);

            Assert.Throws<ArgumentException>(() => this.underTest.Solve(localDirectory.Object, Mock.Of<IFolder>()));

            this.storage.Verify(s => s.SaveMappedObject(It.IsAny<IMappedObject>()), Times.Never());
            localDirectory.VerifyThatLocalFileObjectLastWriteTimeUtcIsNeverModified();
            this.manager.VerifyThatNoTransmissionIsCreated();
        }
        public void LocalEmptyFileAdded(bool withExtendedAttributes, bool withAlreadySetUuid) {
            this.SetUpMocks(withExtendedAttributes);
            Guid uuid = Guid.NewGuid();
            Mock<IFileInfo> fileInfo = new Mock<IFileInfo>();
            fileInfo.Setup(f => f.Length).Returns(0);
            if (withAlreadySetUuid) {
                fileInfo.SetupGuid(uuid);
            }

            Mock<IDocument> document;

            this.SetupSolveFile(this.localObjectName, this.remoteObjectId, this.parentId, this.lastChangeToken, this.withExtendedAttributes, fileInfo, out document);
            this.RunSolveFile(fileInfo);

            this.storage.VerifySavedMappedObject(MappedObjectType.File, this.remoteObjectId, this.localObjectName, this.parentId, this.lastChangeToken, this.withExtendedAttributes, checksum: this.emptyhash, contentSize: 0);
            if (withAlreadySetUuid) {
                this.storage.Verify(s => s.SaveMappedObject(It.Is<IMappedObject>(o => o.Guid == uuid)));
            }

            this.VerifyCreateDocument(isEmpty: true);
            if (this.withExtendedAttributes && !withAlreadySetUuid) {
                fileInfo.VerifySet(f => f.Uuid = It.Is<Guid?>(u => u != null), Times.Once());
            } else {
                fileInfo.VerifySet(f => f.Uuid = It.IsAny<Guid?>(), Times.Never());
            }

            fileInfo.VerifyThatLocalFileObjectLastWriteTimeUtcIsNeverModified();
            document.Verify(d => d.AppendContentStream(It.IsAny<IContentStream>(), It.IsAny<bool>(), It.IsAny<bool>()), Times.Never());
        }
        public void LocalFolderAddingFailsBecauseUtf8Character() {
            this.SetUpMocks();
            var transmissionManager = new TransmissionManager();
            var solver = new LocalObjectAdded(this.session.Object, this.storage.Object, this.transmissionStorage.Object, transmissionManager);
            var dirInfo = new Mock<IDirectoryInfo>();
            dirInfo.Setup(d => d.Exists).Returns(true);
            dirInfo.Setup(d => d.Name).Returns(@"ä".Normalize(System.Text.NormalizationForm.FormD));
            var parentDirInfo = this.SetupParentFolder(this.parentId);
            dirInfo.Setup(d => d.Parent).Returns(parentDirInfo);
            this.session.Setup(s => s.CreateFolder(It.IsAny<IDictionary<string, object>>(), It.IsAny<IObjectId>())).Throws(new CmisConstraintException("Conflict"));
            Assert.Throws<InteractionNeededException>(() => solver.Solve(dirInfo.Object, null));

            this.storage.VerifyThatNoObjectIsManipulated();
            this.session.Verify(s => s.CreateFolder(It.Is<IDictionary<string, object>>(p => p.ContainsKey("cmis:name")), It.Is<IObjectId>(o => o.Id == this.parentId)), Times.Once());
            dirInfo.VerifyThatLocalFileObjectLastWriteTimeUtcIsNeverModified();
            dirInfo.VerifySet(d => d.Uuid = It.IsAny<Guid?>(), Times.Never());
        }
        public void LocalFileIsUsedByAnotherProcess() {
            this.SetUpMocks(true);
            var exception = new ExtendedAttributeException();

            Mock<IFileInfo> fileInfo = new Mock<IFileInfo>();
            fileInfo.Setup(f => f.Length).Returns(0);
            fileInfo.SetupSet(f => f.Uuid = It.IsAny<Guid?>()).Throws(exception);
            Mock<IDocument> document;
            this.SetupSolveFile(this.localObjectName, this.remoteObjectId, this.parentId, this.lastChangeToken, this.withExtendedAttributes, fileInfo, out document);

            var e = Assert.Throws<RetryException>(() => this.RunSolveFile(fileInfo));

            Assert.That(e.InnerException, Is.EqualTo(exception));
            fileInfo.VerifySet(f => f.Uuid = It.Is<Guid?>(uuid => uuid != null), Times.Once());
            this.storage.Verify(s => s.SaveMappedObject(It.IsAny<IMappedObject>()), Times.Never());
            fileInfo.VerifyThatLocalFileObjectLastWriteTimeUtcIsNeverModified();
        }
        public void LocalFileIsUsedByAnotherProcessOnOpenFile() {
            this.SetUpMocks();
            Mock<IFileInfo> fileInfo = new Mock<IFileInfo>();
            fileInfo.Setup(f => f.Length).Returns(10);
            fileInfo.SetupOpenThrows(new IOException("Already in use by another process"));

            Mock<IDocument> document;
            Assert.Throws<IOException>(() => {
                this.SetupSolveFile(this.localObjectName, this.remoteObjectId, this.parentId, lastChangeToken, true, fileInfo, out document);
                this.RunSolveFile(fileInfo);
            });
            fileInfo.VerifySet(f => f.Uuid = It.Is<Guid?>(uuid => uuid != null), Times.Once());
            this.storage.VerifySavedMappedObject(MappedObjectType.File, this.remoteObjectId, this.localObjectName, this.parentId, this.lastChangeToken, true, checksum: this.emptyhash, contentSize: 0);
            this.VerifyCreateDocument(isEmpty: false);
            fileInfo.VerifyThatLocalFileObjectLastWriteTimeUtcIsNeverModified();
        }
        public void DoNotWriteLastWriteTimeUtcIfNotNecessary() {
            this.SetUpMocks(true);

            Mock<IFileInfo> fileInfo = new Mock<IFileInfo>();
            fileInfo.Setup(f => f.Length).Returns(0);

            Mock<IDocument> document;
            this.SetupSolveFile(this.localObjectName, this.remoteObjectId, this.parentId, this.lastChangeToken, this.withExtendedAttributes, fileInfo, out document, false);
            this.RunSolveFile(fileInfo);

            fileInfo.VerifyThatLocalFileObjectLastWriteTimeUtcIsNeverModified();
        }
        public void StorageExceptionOnUploadLeadsToSavedEmptyState() {
            this.SetUpMocks();

            Mock<IFileInfo> fileInfo = new Mock<IFileInfo>();
            fileInfo.Setup(f => f.Length).Returns(1);
            var fileContent = new byte[1];
            var localFileStream = new MemoryStream(fileContent);

            fileInfo.Setup(f => f.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete)).Returns(localFileStream);

            Mock<IDocument> document;
            this.SetupSolveFile(this.localObjectName, this.remoteObjectId, this.parentId, this.lastChangeToken, this.withExtendedAttributes, fileInfo, out document, failsOnUploadContent: true);
            this.RunSolveFile(fileInfo);
            this.storage.VerifySavedMappedObject(MappedObjectType.File, this.remoteObjectId, this.localObjectName, this.parentId, this.lastChangeToken, this.withExtendedAttributes, checksum: this.emptyhash, contentSize: 0);
            this.VerifyCreateDocument();
            fileInfo.VerifySet(f => f.Uuid = It.Is<Guid?>(uuid => uuid != null), Times.Once());
            fileInfo.VerifyThatLocalFileObjectLastWriteTimeUtcIsNeverModified();
            document.Verify(d => d.SetContentStream(It.IsAny<IContentStream>(), true, true), Times.Once());
            document.Verify(d => d.UpdateProperties(It.IsAny<IDictionary<string, object>>()), Times.Never());
        }
        public void Local1ByteFileAdded([Values(true, false)]bool withExtendedAttributes) {
            this.SetUpMocks(withExtendedAttributes);
            Mock<IFileInfo> fileInfo = new Mock<IFileInfo>();
            fileInfo.Setup(f => f.Length).Returns(1);
            var fileContent = new byte[1];
            using (var localFileStream = new MemoryStream(fileContent)) {
                byte[] hash = SHA1Managed.Create().ComputeHash(fileContent);

                fileInfo.Setup(f => f.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete)).Returns(localFileStream);

                Mock<IDocument> document;
                this.SetupSolveFile(this.localObjectName, this.remoteObjectId, this.parentId, this.lastChangeToken, this.withExtendedAttributes, fileInfo, out document);
                this.RunSolveFile(fileInfo);
                this.storage.VerifySavedMappedObject(MappedObjectType.File, this.remoteObjectId, this.localObjectName, this.parentId, this.lastChangeToken, Times.Exactly(2), this.withExtendedAttributes, null, null, hash, 1);
                this.VerifyCreateDocument();
                if (this.withExtendedAttributes) {
                    fileInfo.VerifySet(f => f.Uuid = It.Is<Guid?>(uuid => uuid != null), Times.Once());
                } else {
                    fileInfo.VerifySet(f => f.Uuid = It.IsAny<Guid?>(), Times.Never());
                }

                fileInfo.VerifyThatLocalFileObjectLastWriteTimeUtcIsNeverModified();
                document.Verify(d => d.SetContentStream(It.IsAny<IContentStream>(), true, true), Times.Once());
                document.VerifyUpdateLastModificationDate(fileInfo.Object.LastWriteTimeUtc, true);
            }
        }
        public void LocalAndRemoteFileDatesAreChangedAndLocalDateIsNewerUpdatesRemoteDate() {
            this.InitMocks();
            string fileName = "fileName";
            DateTime lastLocalModification = DateTime.UtcNow.AddDays(1);
            DateTime lastRemoteModification = DateTime.UtcNow.AddHours(1);
            var localFile = new Mock<IFileInfo>();
            byte[] contentHash;
            using (var stream = this.SetUpFileWithContent(localFile, "content", out contentHash, lastLocalModification)) {
                long length = stream.Length;
                var remoteFile = this.CreateRemoteDocument(lastRemoteModification, length, contentHash);
                var mappedObject = new MappedObject(fileName, this.remoteId, MappedObjectType.File, this.parentId, this.oldChangeToken, length) {
                    Guid = Guid.NewGuid(),
                    LastChecksum = contentHash,
                    LastLocalWriteTimeUtc = DateTime.UtcNow,
                    LastRemoteWriteTimeUtc = DateTime.UtcNow,
                    ChecksumAlgorithmName = "SHA-1"
                };
                this.storage.AddMappedFile(mappedObject);

                this.underTest.Solve(localFile.Object, remoteFile.Object, ContentChangeType.NONE, ContentChangeType.NONE);

                remoteFile.VerifyUpdateLastModificationDate(lastLocalModification, Times.Once(), true);
                localFile.VerifyThatLocalFileObjectLastWriteTimeUtcIsNeverModified();
                this.storage.VerifySavedMappedObject(MappedObjectType.File, this.remoteId, fileName, this.parentId, this.newChangeToken, lastLocalModification: lastLocalModification, lastRemoteModification: lastRemoteModification, checksum: contentHash, contentSize: length);
            }
        }