public void Should_Contain_Secondary_Locks() { Guard = new ResourceLockGuard(Lock, Repository) { SecondaryLocks = this.SecondaryLocks }; Assert.AreSame(SecondaryLocks, Guard.SecondaryLocks); }
public void Should_Release_Lock_On_Dispose() { //mock.Setup(rep => rep.ReleaseReadLock(LockItem.ResourceId, LockItem.LockId)).Returns(true); //make sure putting the guard into a using construct eventually releases the lock using (var grd = new ResourceLockGuard(Lock, RepositoryMock.Object)) { } RepositoryMock.Verify(rep => rep.ReleaseWriteLock(Lock.ResourceId, Lock.LockId)); RepositoryMock.Verify(rep => rep.ReleaseReadLock(Lock.ResourceId, Lock.LockId), Times.Never()); RepositoryMock.Verify(rep => rep.ReleaseLock(Lock), Times.Never()); }
public void Should_Release_Main_Lock_Before_Secondary_Ones() { bool mainReleased = false; //set the bool to true on release RepositoryMock.Setup(rep => rep.ReleaseReadLock(Lock.ResourceId, Lock.LockId)) .Callback(() => mainReleased = true); //verify flag was switched RepositoryMock.Setup(rep => rep.ReleaseLock(It.IsAny <LockItem>())) .Callback(() => Assert.IsTrue(mainReleased)); //make sure putting the guard into a using construct eventually releases the lock using (var grd = new ResourceLockGuard(Lock, RepositoryMock.Object) { SecondaryLocks = SecondaryLocks }) { } }
/// <summary> /// Creates a transfer object for a given requested resource. /// </summary> /// <param name="submittedResourceFilePath">The resource identifier as submitted.</param> /// <param name="fileItem">Represents the requested file resource.</param> /// <param name="token">The token that is being issued for the transfer.</param> /// <param name="claims">The access rights for the resource.</param> /// <param name="lockGuard">File locks, if necessary. Can be a null reference /// if no locking takes place.</param> /// <param name="expirationJob">A scheduled job that invokes the /// <see cref="TransferHandlerBase{TFile,TToken,TTransfer}.OnTransferExpiration"/>"/> /// method once the transfer expires. May be null if the token does not expire.</param> /// <returns>A transfer object which encapsulates the information required to perform /// the transfer.</returns> protected override LocalDownloadTransfer CreateTransfer(string submittedResourceFilePath, FileItem fileItem, DownloadToken token, FileClaims claims, ResourceLockGuard lockGuard, Job <DownloadToken> expirationJob) { var transfer = new LocalDownloadTransfer(token, fileItem) { Status = TransferStatus.Starting }; if (expirationJob != null) { transfer.ExpirationNotificationJob = expirationJob; } if (lockGuard != null) { transfer.ResourceLock = lockGuard; } transfer.Owner = Config.Provider.Security.GetIdentity(); return(transfer); }
public void Should_Release_Write_Main_And_Secondardy_Locks() { bool mainReleased = false; RepositoryMock.Setup(rep => rep.ReleaseReadLock("xxx", It.IsAny <string>())) .Callback(() => mainReleased = true); RepositoryMock.Setup(rep => rep.ReleaseLock(It.IsAny <LockItem>())) .Callback(() => Assert.IsTrue(mainReleased)); //make sure putting the guard into a using construct eventually releases the lock using (var grd = new ResourceLockGuard(Lock, RepositoryMock.Object) { SecondaryLocks = SecondaryLocks }) { } RepositoryMock.Verify(rep => rep.ReleaseReadLock(Lock.ResourceId, Lock.LockId), Times.Once()); RepositoryMock.Verify(rep => rep.ReleaseLock(It.IsAny <LockItem>()), Times.Exactly(SecondaryLocks.Count)); RepositoryMock.Verify(rep => rep.ReleaseWriteLock(Lock.ResourceId, Lock.LockId), Times.Never()); }
/// <summary> /// Creates a transfer object for a given file resource. /// </summary> /// <param name="submittedResourceFilePath">The resource identifier as submitted.</param> /// <param name="fileItem">Represents the file resource to be uploaded.</param> /// <param name="parentFolder">The file's parent folder.</param> /// <param name="token">The token that is being issued for the transfer.</param> /// <param name="claims">The access rights for the resource.</param> /// <param name="lockGuard">File locks, if necessary. Can be a null reference /// if no locking takes place.</param> /// <param name="expirationJob">A scheduled job that invokes the /// <see cref="TransferHandlerBase{TFile,TToken,TTransfer}.OnTransferExpiration"/> /// method once the transfer expires. May be null if the token does not expire.</param> /// <returns>A transfer object which encapsulates the information required to perform /// the transfer.</returns> protected override ZipUploadTransfer CreateTransfer(string submittedResourceFilePath, ZipFileItem fileItem, ZipFolderItem parentFolder, UploadToken token, FileClaims claims, ResourceLockGuard lockGuard, Job <UploadToken> expirationJob) { var transfer = new ZipUploadTransfer(token, fileItem) { ParentFolder = parentFolder, Status = TransferStatus.Starting, Owner = Config.Provider.Security.GetIdentity() }; if (expirationJob != null) { transfer.ExpirationNotificationJob = expirationJob; } if (lockGuard != null) { transfer.ResourceLock = lockGuard; } return(transfer); }
/// <summary> /// Creates a transfer object for a given file resource. /// </summary> /// <param name="submittedResourceFilePath">The resource identifier as submitted.</param> /// <param name="fileItem">Represents the file resource to be uploaded.</param> /// <param name="parentFolder">The file's parent folder.</param> /// <param name="token">The token that is being issued for the transfer.</param> /// <param name="claims">The access rights for the resource.</param> /// <param name="lockGuard">File locks, if necessary. Can be a null reference /// if no locking takes place.</param> /// <param name="expirationJob">A scheduled job that invokes the /// <see cref="TransferHandlerBase{TFile,TToken,TTransfer}.OnTransferExpiration"/> /// method once the transfer expires. May be null if the token does not expire.</param> /// <returns>A transfer object which encapsulates the information required to perform /// the transfer.</returns> protected abstract TTransfer CreateTransfer(string submittedResourceFilePath, TFile fileItem, TFolder parentFolder, UploadToken token, FileClaims claims, ResourceLockGuard lockGuard, Job <UploadToken> expirationJob);
private TTransfer InitTransferImpl(string submittedResourceFilePath, bool overwrite, long resourceLength, string contentType) { const FileSystemTask context = FileSystemTask.UploadTokenRequest; //validate maximum file size var maxFileSize = GetMaxFileUploadSize(); if (maxFileSize.HasValue && maxFileSize < resourceLength) { string msg = "Upload for file [{0}] denied: Resource length of [{1}] is above the maximum upload limit of [{2}] bytes."; msg = String.Format(msg, submittedResourceFilePath, resourceLength, maxFileSize.Value); throw new ResourceAccessException(msg); } //of course, the length cannot be negative if (resourceLength < 0) { string msg = "Upload for file [{0}] denied: Resource length cannot be negative [{1}]."; msg = String.Format(msg, submittedResourceFilePath, resourceLength); throw new ResourceAccessException(msg); } TFile fileItem = CreateFileItemImpl(submittedResourceFilePath, false, context); if (fileItem.Exists && !overwrite) { AuditHelper.AuditDeniedOperation(Auditor, context, AuditEvent.FileAlreadyExists, fileItem); string msg = String.Format("Cannot upload file [{0}] without overwriting existing data - a file already exists at this location.", submittedResourceFilePath); throw new ResourceOverwriteException(msg) { IsAudited = true }; } //get authorization TFolder parentFolder = GetParentFolder(fileItem, context); //validate file system specific restrictions VerifyCanUploadFileToFileSystemLocation(submittedResourceFilePath, parentFolder, fileItem); //get parent folder and check whether files can be added FolderClaims folderClaims = GetParentFolderClaims(fileItem, parentFolder); if (!folderClaims.AllowAddFiles) { //deny adding a file to that folder AuditHelper.AuditDeniedOperation(Auditor, context, AuditEvent.CreateFileDenied, fileItem); string msg = "Cannot create file at [{0}] - adding files to the folder is not permitted."; msg = String.Format(msg, submittedResourceFilePath); throw new ResourceAccessException(msg) { IsAudited = true }; } //only overwrite a file if explicitly requested FileClaims claims = GetFileClaims(fileItem); if (fileItem.Exists) { if (!claims.AllowOverwrite) { AuditHelper.AuditDeniedOperation(Auditor, context, AuditEvent.FileDataOverwriteDenied, fileItem); string msg = "Overwriting file [{0}] was denied due to missing permission."; msg = String.Format(msg, submittedResourceFilePath); throw new ResourceOverwriteException(msg) { IsAudited = true }; } } //try to get lock ResourceLockGuard writeLock = LockResourceForUpload(fileItem); if (writeLock != null && !writeLock.IsLockEnabled) { AuditHelper.AuditDeniedOperation(Auditor, context, AuditEvent.FileReadLockDenied, fileItem); string msg = "The file [{0}] is currently locked and cannot be accessed."; msg = String.Format(msg, submittedResourceFilePath); throw new ResourceLockedException(msg) { IsAudited = true }; } //create upload token UploadToken token = CreateUploadToken(submittedResourceFilePath, fileItem, resourceLength, contentType); //create expiration job if we have an expiration time Job <UploadToken> job = null; if (token.ExpirationTime.HasValue) { job = ScheduleExpiration(token); } //create and cache transfer instance TTransfer transfer = CreateTransfer(submittedResourceFilePath, fileItem, parentFolder, token, claims, writeLock, job); TransferStore.AddTransfer(transfer); AuditHelper.AuditResourceOperation(Auditor, context, AuditEvent.UploadTokenIssued, fileItem); return(transfer); }
public void Init() { Lock = LockItem.CreateWrite("xxx", null); RepositoryMock = new Mock <IResourceLockRepository>(); Guard = new ResourceLockGuard(Lock, Repository); }
public void Init() { Lock = LockItem.CreateDenied("xxx"); RepositoryMock = new Mock <IResourceLockRepository>(); Guard = new ResourceLockGuard(Lock, Repository); }