public void Initialize_ShouldCreateStorageOfSizeAndFileNameLength(int size, int fileNameLength) { // Arrange var storageBuffer = new byte[size]; var serviceProvider = CreateServiceProvider(storageBuffer); var storageInitializer = serviceProvider.GetRequiredService <IStorageInitializer>(); var filesystemDescriptorSerializer = serviceProvider.GetRequiredService <ISerializer <FilesystemDescriptor> >(); var entryDescriptorSerializer = serviceProvider.GetRequiredService <ISerializer <EntryDescriptor> >(); var expectedFilesystemDescriptor = new FilesystemDescriptor(0, 1, fileNameLength + EntryDescriptor.BytesWithoutFilename); var expectedRootDirectoryName = PathConstants.RootDirectoryName; // Act storageInitializer.Initialize(size, fileNameLength); // Arrange using var newStorageStream = new MemoryStream(storageBuffer); newStorageStream.Seek(-FilesystemDescriptor.BytesTotal, SeekOrigin.End); var filesystemDescriptorBuffer = new byte[FilesystemDescriptor.BytesTotal]; newStorageStream.Read(filesystemDescriptorBuffer); var filesystemDescriptor = filesystemDescriptorSerializer.FromBytes(filesystemDescriptorBuffer); newStorageStream.Seek(-FilesystemDescriptor.BytesTotal - fileNameLength - EntryDescriptor.BytesWithoutFilename, SeekOrigin.End); var rootEntryDescriptorBuffer = new byte[fileNameLength + EntryDescriptor.BytesWithoutFilename]; newStorageStream.Read(rootEntryDescriptorBuffer); var rootEntryDescriptor = entryDescriptorSerializer.FromBytes(rootEntryDescriptorBuffer); Assert.Equal(size, newStorageStream.Length); Assert.Equal(expectedFilesystemDescriptor, filesystemDescriptor); Assert.Equal(rootEntryDescriptor.Id, rootEntryDescriptor.ParentId); Assert.Equal(expectedRootDirectoryName, rootEntryDescriptor.Name); }
public void Update_ShouldReturnUpdatedDescriptor(int bufferSize, int fileDescriptorLength) { // Arrange var buffer = new byte[bufferSize]; var storageStreamProvider = StorageStreamProviderMockFactory.Create(buffer); var logger = new LoggerConfiguration().CreateLogger(); var storageConnection = new StorageConnection(storageStreamProvider, 4096, logger); var filesystemSerializer = new FilesystemDescriptorSerializer(logger); var expectedDescriptor = new FilesystemDescriptor(0, 0, fileDescriptorLength); var filesystemDescriptorBytes = filesystemSerializer.ToBytes(expectedDescriptor); var filesystemDescriptorCursor = new Cursor(-FilesystemDescriptor.BytesTotal, SeekOrigin.End); storageConnection.PerformWrite(filesystemDescriptorCursor, filesystemDescriptorBytes); var filesystemDescriptorAccessor = new FilesystemDescriptorAccessor(storageConnection, filesystemSerializer, logger); // Act filesystemDescriptorAccessor.Update(_ => 0, _ => 0, _ => fileDescriptorLength); // Assert var returnedDescriptorBytes = storageConnection.PerformRead(filesystemDescriptorCursor, FilesystemDescriptor.BytesTotal); var returnedDescriptor = filesystemSerializer.FromBytes(returnedDescriptorBytes); Assert.Equal(expectedDescriptor, returnedDescriptor); }
private void UpdateInternal(FilesystemDescriptor descriptor) { _logger.Information("Trying to update filesystem descriptor"); var offset = -FilesystemDescriptor.BytesTotal; var origin = SeekOrigin.End; var data = _serializer.ToBytes(descriptor); _connection.PerformWrite(new Cursor(offset, origin), data); _logger.Information("Filesystem descriptor updated"); }
/// <inheritdoc /> public FilesystemDescriptor Update( Func <int, int> filesDataLengthUpdater = null, Func <int, int> entryDescriptorsCountUpdater = null, Func <int, int> entryDescriptorLengthUpdater = null) { lock (_locker) { var descriptor = Read(); var newFilesDataLength = filesDataLengthUpdater?.Invoke(descriptor.FilesDataLength) ?? descriptor.FilesDataLength; var newEntryDescriptorsCount = entryDescriptorsCountUpdater?.Invoke(descriptor.EntryDescriptorsCount) ?? descriptor.EntryDescriptorsCount; var newEntryDescriptorLength = entryDescriptorLengthUpdater?.Invoke(descriptor.EntryDescriptorLength) ?? descriptor.EntryDescriptorLength; var updatedDescriptor = new FilesystemDescriptor(newFilesDataLength, newEntryDescriptorsCount, newEntryDescriptorLength); UpdateInternal(updatedDescriptor); return(updatedDescriptor); } }
public void ToBuffer_ShouldCreatedCorrectBufferWithData(int fileDescriptorLength, string fileName, long createdOn, long updatedOn, int dataOffset, int dataLength, EntryType type) { // Arrange var id = Guid.NewGuid(); var parentId = Guid.NewGuid(); var filesystemDescriptor = new FilesystemDescriptor(0, 0, fileDescriptorLength); var fileDescriptor = new EntryDescriptor(id, parentId, fileName, type, createdOn, updatedOn, dataOffset, dataLength); var logger = new LoggerConfiguration().CreateLogger(); var filesystemDescriptorAccessor = CreateFilesystemDescriptorAccessor(filesystemDescriptor); var serializer = new EntryDescriptorSerializer(filesystemDescriptorAccessor, logger); // Act var buffer = serializer.ToBytes(fileDescriptor); // Assert using var memoryStream = new MemoryStream(buffer); using var reader = new BinaryReader(memoryStream); var writtenIdBytes = reader.ReadGuidBytes(); var writtenId = new Guid(writtenIdBytes); var writtenParentIdBytes = reader.ReadGuidBytes(); var writtenParentId = new Guid(writtenParentIdBytes); var writtenStringLength = reader.ReadInt32(); var writtenEntryNameBytes = reader.ReadBytes(writtenStringLength); var writtenEntryName = Encoding.UTF8.GetString(writtenEntryNameBytes); memoryStream.Seek(filesystemDescriptor.EntryDescriptorLength - writtenStringLength - EntryDescriptor.BytesWithoutFilename, SeekOrigin.Current); var writtenType = (EntryType)reader.ReadByte(); var writtenCreatedOn = reader.ReadInt64(); var writtenUpdatedOn = reader.ReadInt64(); var writtenDataOffset = reader.ReadInt32(); var writtenDataLength = reader.ReadInt32(); Assert.Equal(id, writtenId); Assert.Equal(parentId, writtenParentId); Assert.Equal(fileDescriptorLength, buffer.Length); Assert.Equal(fileName, writtenEntryName); Assert.Equal(type, writtenType); Assert.Equal(createdOn, writtenCreatedOn); Assert.Equal(updatedOn, writtenUpdatedOn); Assert.Equal(dataOffset, writtenDataOffset); Assert.Equal(dataLength, writtenDataLength); }
public void FromBuffer_ShouldCreatedCorrectModelFromBuffer(int fileDescriptorLength, string entryName, long createdOn, long updatedOn, int dataOffset, int dataLength, EntryType type) { // Arrange var filesystemDescriptor = new FilesystemDescriptor(0, 0, fileDescriptorLength); var logger = new LoggerConfiguration().CreateLogger(); var filesystemDescriptorAccessor = CreateFilesystemDescriptorAccessor(filesystemDescriptor); var serializer = new EntryDescriptorSerializer(filesystemDescriptorAccessor, logger); var buffer = new byte[fileDescriptorLength]; var id = Guid.NewGuid(); var parentId = Guid.NewGuid(); using var memoryStream = new MemoryStream(buffer); using var writer = new BinaryWriter(memoryStream); writer.Write(id.ToByteArray()); writer.Write(parentId.ToByteArray()); var entryNameBytes = Encoding.UTF8.GetBytes(entryName); writer.Write(entryName.Length); writer.Write(entryNameBytes); writer.Seek(filesystemDescriptor.EntryDescriptorLength - entryNameBytes.Length - EntryDescriptor.BytesWithoutFilename, SeekOrigin.Current); writer.Write((byte)type); writer.Write(createdOn); writer.Write(updatedOn); writer.Write(dataOffset); writer.Write(dataLength); // Act var entryDescriptor = serializer.FromBytes(buffer); // Assert Assert.Equal(id, entryDescriptor.Id); Assert.Equal(parentId, entryDescriptor.ParentId); Assert.Equal(entryNameBytes.Length, entryDescriptor.NameLength); Assert.Equal(entryNameBytes.Length, entryDescriptor.NameLength); Assert.Equal(entryName, entryDescriptor.Name); Assert.Equal(type, entryDescriptor.Type); Assert.Equal(createdOn, entryDescriptor.CreatedOn); Assert.Equal(updatedOn, entryDescriptor.UpdatedOn); Assert.Equal(dataOffset, entryDescriptor.DataOffset); Assert.Equal(dataLength, entryDescriptor.DataLength); }
public void ToBuffer_ShouldCreatedCorrectBufferWithData(int filesDataLength, int fileDescriptorsCount, int fileDescriptorLength) { // Arrange var filesystemDescriptor = new FilesystemDescriptor(filesDataLength, fileDescriptorsCount, fileDescriptorLength); var logger = new LoggerConfiguration().CreateLogger(); var serializer = new FilesystemDescriptorSerializer(logger); // Act var buffer = serializer.ToBytes(filesystemDescriptor); // Assert using var memoryStream = new MemoryStream(buffer); using var reader = new BinaryReader(memoryStream); var writtenFilesDataLength = reader.ReadInt32(); var writtenFileDescriptorsCount = reader.ReadInt32(); var writtenFileDescriptorLength = reader.ReadInt32(); Assert.Equal(FilesystemDescriptor.BytesTotal, buffer.Length); Assert.Equal(filesDataLength, writtenFilesDataLength); Assert.Equal(fileDescriptorsCount, writtenFileDescriptorsCount); Assert.Equal(fileDescriptorLength, writtenFileDescriptorLength); }
private static IFilesystemDescriptorAccessor CreateFilesystemDescriptorAccessor(FilesystemDescriptor stub) { var filesystemDescriptorAccessorMock = new Mock <IFilesystemDescriptorAccessor>(); filesystemDescriptorAccessorMock.Setup(serializer => serializer.Value).Returns(stub); return(filesystemDescriptorAccessorMock.Object); }