Ejemplo n.º 1
0
        public void Ctor_Create_Storage_File()
        {
            // Arrange
            var fileContents = new StringBuilder();

            InitializeMocks(fileContents, fileExists: false, dirExists: false);
            directoryMock
            .Setup(x => x.Create(Path.GetDirectoryName(TelemetryDataRepository.GetStorageFilePath())));
            fileMock
            .Setup(x => x.CreateText(TelemetryDataRepository.GetStorageFilePath()))
            .Returns(() => new StringWriter(fileContents));

            // Act
            var repository = new TelemetryDataRepository(fileMock.Object, directoryMock.Object, watcherFactoryMock.Object);

            // Assert
            RemoveLineEndings(fileContents.ToString()).Should().Be(RemoveLineEndings(@"<?xml version=""1.0"" encoding=""utf-16""?>
<TelemetryData xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">
  <IsAnonymousDataShared>true</IsAnonymousDataShared>
  <NumberOfDaysOfUse>0</NumberOfDaysOfUse>
  <InstallationDate>0001-01-01T00:00:00.0000000+00:00</InstallationDate>
  <LastSavedAnalysisDate>0001-01-01T00:00:00.0000000+00:00</LastSavedAnalysisDate>
  <LastUploadDate>0001-01-01T00:00:00.0000000+00:00</LastUploadDate>
</TelemetryData>"));

            Mock.VerifyAll(fileMock, directoryMock, watcherFactoryMock);
        }
        public void Save_SavesIntoXmlAllValuesOfData()
        {
            // Arrange
            File.Delete(TelemetryDataRepository.GetStorageFilePath());

            var repository = new TelemetryDataRepository();

            repository.Data.IsAnonymousDataShared = false;
            repository.Data.InstallationDate      = DateTime.MaxValue;
            repository.Data.LastSavedAnalysisDate = DateTime.MaxValue;
            repository.Data.NumberOfDaysOfUse     = long.MaxValue;
            repository.Data.LastUploadDate        = DateTime.MaxValue;

            // Act
            repository.Save();

            // Assert
            var stream     = File.OpenRead(TelemetryDataRepository.GetStorageFilePath());
            var serializer = new XmlSerializer(typeof(TelemetryData));
            var data       = serializer.Deserialize(stream) as TelemetryData;

            data.IsAnonymousDataShared.Should().BeFalse();
            data.InstallationDate.Should().Be(DateTime.MaxValue);
            data.LastSavedAnalysisDate.Should().Be(DateTime.MaxValue);
            data.NumberOfDaysOfUse.Should().Be(long.MaxValue);
            data.LastUploadDate.Should().Be(DateTime.MaxValue);
        }
        public void Ctor_AlwaysReadValueFromFile()
        {
            // Arrange
            File.Delete(TelemetryDataRepository.GetStorageFilePath());

            var repository = new TelemetryDataRepository();

            repository.Data.IsAnonymousDataShared = false;
            repository.Data.InstallationDate      = DateTime.MaxValue;
            repository.Data.LastSavedAnalysisDate = DateTime.MaxValue;
            repository.Data.NumberOfDaysOfUse     = long.MaxValue;
            repository.Data.LastUploadDate        = DateTime.MaxValue;
            repository.Save();
            repository.Dispose();

            // Act
            repository = new TelemetryDataRepository();

            // Assert
            repository.Data.IsAnonymousDataShared.Should().BeFalse();
            repository.Data.InstallationDate.Should().Be(DateTime.MaxValue);
            repository.Data.LastSavedAnalysisDate.Should().Be(DateTime.MaxValue);
            repository.Data.NumberOfDaysOfUse.Should().Be(long.MaxValue);
            repository.Data.LastUploadDate.Should().Be(DateTime.MaxValue);
        }
        public void Instance_AutomaticallyReadFileOnChange()
        {
            // Arrange
            RetryHelper.RetryOnException(5, TimeSpan.FromSeconds(1),
                                         () => File.Delete(TelemetryDataRepository.GetStorageFilePath()));

            var repository = new TelemetryDataRepository();

            repository.Data.IsAnonymousDataShared = false;
            repository.Data.InstallationDate      = DateTime.MaxValue;
            repository.Data.LastSavedAnalysisDate = DateTime.MaxValue;
            repository.Data.NumberOfDaysOfUse     = long.MaxValue;
            repository.Data.LastUploadDate        = DateTime.MaxValue;

            var otherRepository = new TelemetryDataRepository();

            // Act
            repository.Save();
            Task.Delay(700).Wait();

            // Assert
            otherRepository.Data.InstallationDate.Should().Be(DateTime.MaxValue);
            otherRepository.Data.LastSavedAnalysisDate.Should().Be(DateTime.MaxValue);
            otherRepository.Data.NumberOfDaysOfUse.Should().Be(long.MaxValue);
            otherRepository.Data.LastUploadDate.Should().Be(DateTime.MaxValue);
            otherRepository.Data.IsAnonymousDataShared.Should().BeFalse();
        }
Ejemplo n.º 5
0
        public void Ctor_AlwaysCreateStorageFile()
        {
            // Arrange
            var filePath = TelemetryDataRepository.GetStorageFilePath();

            RetryHelper.RetryOnException(5, TimeSpan.FromSeconds(1), () => File.Delete(filePath));
            File.Exists(filePath).Should().BeFalse(); // Sanity test

            // Act
            var repository = new TelemetryDataRepository();

            // Assert
            File.Exists(filePath).Should().BeTrue();
        }
Ejemplo n.º 6
0
        public void Ctor_AlwaysCreateStorageFileFolders()
        {
            // Arrange
            var filePath      = TelemetryDataRepository.GetStorageFilePath();
            var directoryPath = Path.GetDirectoryName(filePath);

            RetryHelper.RetryOnException(5, TimeSpan.FromSeconds(1), () => Directory.Delete(directoryPath, true));
            Directory.Exists(directoryPath).Should().BeFalse(); // Sanity test

            // Act
            var repository = new TelemetryDataRepository();

            // Assert
            Directory.Exists(directoryPath).Should().BeTrue();
        }
        public void Ctor_Create_Storage_File()
        {
            var fileContents = new StringBuilder();

            // Arrange
            InitializeMocks(fileContents, fileExists: false, dirExists: false);

            fileSystemMock
            .Setup(x => x.Directory.CreateDirectory(Path.GetDirectoryName(TelemetryDataRepository.GetStorageFilePath())))
            .Returns(null as IDirectoryInfo);

            fileSystemMock
            .Setup(x => x.File.WriteAllText(TelemetryDataRepository.GetStorageFilePath(), It.IsAny <string>()))
            .Callback((string path, string content) => fileContents.Append(content));

            // Act
            var repository = new TelemetryDataRepository(fileSystemMock.Object, watcherFactoryMock.Object);

            // Assert
            fileContents.ToString().Should().Be(@"<?xml version=""1.0"" encoding=""utf-16""?>
<TelemetryData xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">
  <IsAnonymousDataShared>true</IsAnonymousDataShared>
  <NumberOfDaysOfUse>0</NumberOfDaysOfUse>
  <InstallationDate>0001-01-01T00:00:00.0000000+00:00</InstallationDate>
  <LastSavedAnalysisDate>0001-01-01T00:00:00.0000000+00:00</LastSavedAnalysisDate>
  <LastUploadDate>0001-01-01T00:00:00.0000000+00:00</LastUploadDate>
  <Analyses />
  <ShowHotspot>
    <NumberOfRequests>0</NumberOfRequests>
  </ShowHotspot>
  <TaintVulnerabilities>
    <NumberOfIssuesInvestigatedLocally>0</NumberOfIssuesInvestigatedLocally>
    <NumberOfIssuesInvestigatedRemotely>0</NumberOfIssuesInvestigatedRemotely>
  </TaintVulnerabilities>
  <ServerNotifications>
    <IsDisabled>false</IsDisabled>
    <ServerNotificationCounters />
  </ServerNotifications>
</TelemetryData>");

            Mock.VerifyAll(fileSystemMock, watcherFactoryMock);
        }
Ejemplo n.º 8
0
        private void InitializeMocks(StringBuilder fileContents, bool fileExists, bool dirExists,
                                     IFileSystemWatcher fileSystemWatcher = null)
        {
            fileMock = new Mock <IFile>(MockBehavior.Strict);
            fileMock
            .Setup(x => x.OpenText(TelemetryDataRepository.GetStorageFilePath()))
            .Returns(() => new StringReader(fileContents.ToString()));
            fileMock
            .Setup(x => x.Exists(TelemetryDataRepository.GetStorageFilePath()))
            .Returns(fileExists);

            directoryMock = new Mock <IDirectory>(MockBehavior.Strict);
            directoryMock
            .Setup(x => x.Exists(Path.GetDirectoryName(TelemetryDataRepository.GetStorageFilePath())))
            .Returns(dirExists);

            watcherFactoryMock = new Mock <IFileSystemWatcherFactory>(MockBehavior.Strict);
            watcherFactoryMock
            .Setup(x => x.Create())
            .Returns(fileSystemWatcher ?? new Mock <IFileSystemWatcher>().Object);
        }