Exemple #1
0
        /// <include file="Tools/.Docs/.ManifestUninstaller.xml" path="docs/method[@name='Install(string, ManifestComponents, bool)']/*"/>
        public static bool Uninstall(string AppDirectory, ManifestComponents Components, bool ThrowExceptions)
        {
            foreach (var Component in Components.Values)
            {
                var Path = PathService.CombinePath(AppDirectory, Component.FileName);

                try
                {
                    if (FileService.Exists(Path))
                    {
                        FileDeleter.Delete(Path);
                    }
                }
                catch
                {
                    if (ThrowExceptions)
                    {
                        throw;
                    }
                    else
                    {
                        return(false);
                    }
                }
            }

            return(true);
        }
        public void IndicateNullsProperly()
        {
            using (FileDeleter fd = new FileDeleter(Extensions.GetTempDBFile()))
            {
                Database db = new Database(new Context(fd.Fi));
                using (SQLiteConnection conn = db.Connection)
                {
                    conn.Open();
                    Initializer init = new Initializer(null);
                    init.Initialize(db);

                    Tuple <List <DeviceInfo>, DateTimeOffset> devices = LoadDevices(db, conn);

                    string sql = "SELECT Name, LastCollectionAttempt, LastCollectedAt, NextCollectionTime FROM Collectors WHERE IsEnabled = 1";
                    using (SQLiteCommand command = new SQLiteCommand(sql, conn))
                        using (SQLiteDataReader reader = command.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                Assert.False(reader.IsDBNull(0));
                                Assert.True(reader.IsDBNull(1));
                                Assert.True(reader.IsDBNull(2));
                                Assert.True(reader.IsDBNull(3));
                            }
                        }
                }
            }
        }
        static void Main(string[] args)
        {
            Log.Logger = new LoggerConfiguration()
                         .Enrich.FromLogContext()
                         .WriteTo.Console()
                         .WriteTo.File($"{AppDomain.CurrentDomain.BaseDirectory}logs\\{AppDomain.CurrentDomain.FriendlyName}-.log", rollingInterval: RollingInterval.Day)
                         .CreateLogger();

            using (ServiceProvider serviceProvider = new ServiceCollection()
                                                     .AddLogging(l => l.AddSerilog(dispose: true))
                                                     .BuildServiceProvider())
            {
                ILogger <Program> logger = serviceProvider.GetService <ILoggerFactory>()
                                           .CreateLogger <Program>();

                try
                {
                    logger.LogInformation("Starting application.");
                    FileDeleter fileDeleter = new FileDeleter(logger);
                    fileDeleter.CleanUpAllDrives();
                    logger.LogInformation("Application completed succesfully.");
                }
                catch (Exception e)
                {
                    logger.LogCritical("Application failed", e);
                }
            }

#if DEBUG
            Console.WriteLine("Press \"Enter\" to exit.");
            Console.ReadLine();
#endif
        }
        private static async Task <FileObject> PerformOneIterationOfPhaseTwoAsync(
            AnalysisIterationContextPhaseTwo iterationContext)
        {
            iterationContext.ThrowIfNull(nameof(iterationContext));

            _logger.Info("Preparing to run one iteration of phase two.");

            var fileDeleter = new FileDeleter(iterationContext.FinalOutputFile);

            // Contract: the analysis program is located in the same directory as our app.
            var processLaunchContext = ProcessLaunchContext.Create(
                file: iterationContext.Args.AnalysisProgramName,
                args: iterationContext.AnalysisInputArgs,
                showWindow: iterationContext.LaunchContext.ShowAnalysisWindow
                );

            _logger.Info(
                $"Starting analysis program. Launch context: {processLaunchContext.ToLogString()}"
                );
            using (var analysisRunner = ProgramRunner.RunProgram(processLaunchContext))
            {
                _logger.Info("Waiting to finish one iteration of phase two.");
                await analysisRunner.WaitAsync();
            }

            DataObject <OutputFileData> data =
                iterationContext.FileWorker.ReadDataFile(iterationContext.FinalOutputFile);

            _logger.Info("Finished one iteration of phase two.");
            return(new FileObject(fileDeleter, data));
        }
Exemple #5
0
        public static void Main(string[] args)
        {
            if (!args.Any())
            {
                UsageInformation.Print();
                return;
            }

            var fileName = args.FirstOrDefault();

            Console.WriteLine(Resources.ConfirmationLine, fileName);
            var keyInfo = Console.ReadKey();

            if (keyInfo.Key != ConsoleKey.Y && keyInfo.Key != ConsoleKey.Enter)
            {
                return;
            }

            if (!FileDeleter.Delete(fileName))
            {
                var lastError = Marshal.GetLastWin32Error();
                Console.WriteLine();
                Console.WriteLine($"Error: {new Win32Exception(lastError).Message}");
            }
            else
            {
                ProgressTracker.Instance.Stop();
            }
        }
        public void UseDBCollectionTimeRetrieverProperly()
        {
            using (FileDeleter fd = new FileDeleter(Extensions.GetTempDBFile()))
            {
                Database db = new Database(new Context(fd.Fi));
                using (SQLiteConnection conn = db.Connection)
                {
                    conn.Open();
                    Initializer init = new Initializer(null);
                    init.Initialize(db);

                    Tuple <List <DeviceInfo>, DateTimeOffset> devices = LoadDevices(db, conn);

                    int    count = -1;
                    string sql   = "SELECT COUNT(*) FROM Collectors WHERE IsEnabled = 1 AND FrequencyInMinutes > 0";
                    using (SQLiteCommand command = new SQLiteCommand(sql, conn))
                        using (SQLiteDataReader reader = command.ExecuteReader())
                        {
                            if (reader.Read())
                            {
                                count = reader.GetInt32(0);
                            }
                        }

                    DBCollectionTimeRetriever retriever = new DBCollectionTimeRetriever(conn);
                    Assert.Equal(count, retriever.AllIDs.Count);
                }
            }
        }
        public void Setup()
        {
            _mockFileProvider   = new Mock <IFileProvider>();
            _mockThreadProvider = new Mock <IThreadProvider>();

            cut = new FileDeleter(_mockFileProvider.Object, _mockThreadProvider.Object);
        }
Exemple #8
0
        public static void Main(string[] args)
        {
            var parsedArgs = CmdLineArgsParser.Parse(args);

            if (!args.Any() || string.IsNullOrEmpty(parsedArgs.FileName))
            {
                UsageInformation.Print();
                return;
            }

            //If silent mode is not specified
            if (!parsedArgs.SilentModeEnabled)
            {
                Console.WriteLine(Resources.ConfirmationLine, parsedArgs.FileName);
                var keyInfo = Console.ReadKey();
                if (keyInfo.Key != ConsoleKey.Y && keyInfo.Key != ConsoleKey.Enter)
                {
                    return;
                }
            }

            if (!FileDeleter.Delete(parsedArgs.FileName))
            {
                var lastError = Marshal.GetLastWin32Error();
                Console.WriteLine();
                Console.WriteLine($"Error: {new Win32Exception(lastError).Message}");
            }
            else
            {
                ProgressTracker.Instance.Stop();
            }
        }
 public DuplicateFilesRemover(string currentDirectoryPath, DirectoryContentRetriever directoryContentRetriever, Md5Generator md5Generator, DuplicateFilesIdentifier duplicateFilesIdentifier, FileDeleter fileDeleter)
 {
     _currentDirectoryPath = currentDirectoryPath;
     _directoryContentRetriever = directoryContentRetriever;
     _md5Generator = md5Generator;
     _duplicateFilesIdentifier = duplicateFilesIdentifier;
     _fileDeleter = fileDeleter;
 }
        public void CanDeleteFile()
        {
            var deleter = new FileDeleter();
            MakeSureFileIsNotPresentThanCreateIt();

            deleter.Delete(new List<string> { FILE_TO_DELETE });

            Assert.IsFalse(File.Exists(FILE_TO_DELETE));
        }
Exemple #11
0
 public DeletionManager(StateSaveConcurrentQueue <string> queue, NetworkPorts ports,
                        ConcurrentDictionary <string, Peer> peers, ConcurrentDictionary <string, P2PFile> locations)
 {
     this._queue      = queue;
     this._ports      = ports;
     this._waitHandle = new ManualResetEvent(false);
     this._queue.ElementAddedToQueue += QueueElementAddedToQueue;
     this._filesList          = locations;
     Peer.PeerSwitchedOnline += PeerWentOnline;
     _fileDeleter             = new FileDeleter(peers, _ports);
 }
Exemple #12
0
        public static void Main(string[] args)
        {
            ParsedCmdLineArgs parsedArgs;

            try
            {
                parsedArgs = CmdLineArgsParser.Parse(args);
            }
            catch (CmdLineArgsParser.InvalidCmdLineException e)
            {
                CmdLineArgsParser.PrintUsage(e);
                return;
            }

            try
            {
                // get the full path for confirmation
                string filename = FileDeleter.GetFullPath(parsedArgs.FileName);

                //If silent mode is not specified
                if (!parsedArgs.SilentModeEnabled)
                {
                    Console.WriteLine(Resources.ConfirmationLine, filename);
                    var keyInfo = Console.ReadKey();
                    if (keyInfo.Key != ConsoleKey.Y && keyInfo.Key != ConsoleKey.Enter)
                    {
                        return;
                    }
                }

                FileDeleter.Delete(filename, parsedArgs.BypassAcl);
            }
            catch (Exception e)
            {
                Console.WriteLine();

                if (parsedArgs.PrintStackTrace)
                {
                    Console.WriteLine($"Error: {e.ToString()}");
                }
                else
                {
                    Console.WriteLine($"Error: {e.Message}");
                }
            }
            finally
            {
                ProgressTracker.Instance.Stop();
            }
        }
        public void HandleUpdateRequestProperly(string section, SystemErrorsUpdateRequest.EType type)
        {
            using (FileDeleter fd = new FileDeleter(Extensions.GetTempDBFile()))
            {
                Database    db   = new Database(new Context(fd.Fi));
                Initializer init = new Initializer(null);
                init.Initialize(db);

                RequestBus        bus       = new RequestBus();
                EventLogResponder responder = new EventLogResponder()
                {
                    DB = db
                };
                bus.Subscribe(responder);

                using (SQLiteConnection conn = db.Connection)
                {
                    conn.Open();

                    string new_path = $"machine_name.{section}.max_event_log";

                    System.DateTime dt  = GenerateTime(2019, 2, 20, 12, 23, 34, 456789);
                    EventLogData    eld = new EventLogData();
                    eld.Insert(dt, 22334455);
                    eld.Insert(dt, 22334456);

                    SystemErrorsUpdateRequest req = new SystemErrorsUpdateRequest("machine_name", type);
                    req.LogData.Assign(eld);
                    bus.MakeRequest(req);
                    Assert.True(req.IsHandled);

                    Attribute attr = new Attribute();
                    string    x    = attr.Get(new_path, conn);
                    Assert.False(string.IsNullOrEmpty(x));

                    EventLogData eld2 = JsonConvert.DeserializeObject <EventLogData>(x);
                    Assert.NotNull(eld);
                    Assert.False(eld2.MaxRecordNumber.HasValue);
                    Assert.Equal(dt, eld2.MaxDate);

                    List <ulong> record_numbers = eld2.MaxDateToRecordNumbers[dt];
                    Assert.NotNull(record_numbers);
                    Assert.Equal(2, record_numbers.Count);
                    // Sort just in case they got put in the list in a different order
                    record_numbers.Sort();
                    Assert.Equal((ulong)22334455, record_numbers[0]);
                    Assert.Equal((ulong)22334456, record_numbers[1]);
                }
            }
        }
Exemple #14
0
        public void CreateTableOK()
        {
            using (FileDeleter fd = new FileDeleter(Extensions.GetTempDBFile()))
            {
                Database db = new Database(new Context(fd.Fi));
                using (SQLiteConnection conn = db.Connection)
                {
                    conn.Open();

                    conn.ExecuteNonQuery(CreateTable);
                    Assert.True(conn.DoesTableExist("Temp"));
                    Assert.False(conn.DoesTableExist("NonExistentTable"));
                }
            }
        }
        public async static Task PerformFullAnalysisForPhaseTwoAsync(ParametersPack args,
                                                                     AnalysisLaunchContext launchContext, LocalFileWorker fileWorker,
                                                                     Func <FileObject, Task> asyncCallback)
        {
            args.ThrowIfNull(nameof(args));
            launchContext.ThrowIfNull(nameof(launchContext));
            fileWorker.ThrowIfNull(nameof(fileWorker));
            asyncCallback.ThrowIfNull(nameof(asyncCallback));

            _logger.Info("Preparing to run full analysis for phase two.");

            // Contract: output files are located in the same directory as our app.
            IReadOnlyList <FileInfo> finalOutputFiles = args.GetOutputFilenames(phaseNumber: 2);

            using var fileDeleter = new FileDeleter(finalOutputFiles);

            IReadOnlyList <string> analysisInputArgsCollection =
                args.CollectionPackAsInputArgumentsForPhaseTwo();

            var limitedScheduler =
                new LimitedConcurrencyLevelTaskScheduler(launchContext.MaxDegreeOfParallelism);

            var processingTasks = new List <Task <FileObject> >(analysisInputArgsCollection.Count);

            // The last is common analysis data file.
            // We don't need to read/use the last one.
            for (int index = 0; index < analysisInputArgsCollection.Count; ++index)
            {
                var iterationContext = new AnalysisIterationContextPhaseTwo(
                    args: args,
                    launchContext: launchContext,
                    fileWorker: fileWorker,
                    analysisInputArgs: analysisInputArgsCollection[index],
                    finalOutputFile: finalOutputFiles[index]
                    );

                Task <FileObject> processingTask = TaskHelper.StartNew(
                    () => PerformOneIterationOfPhaseTwoAsync(iterationContext),
                    limitedScheduler
                    );
                processingTasks.Add(processingTask);
            }

            await Task.WhenAll(
                processingTasks.Select(task => AwaitAndProcessAsync(task, asyncCallback))
                );
        }
Exemple #16
0
        public void ProperlyHandleLongs()
        {
            using (FileDeleter fd = new FileDeleter(Extensions.GetTempDBFile()))
            {
                Database db = new Database(new Context(fd.Fi));
                using (SQLiteConnection conn = db.Connection)
                {
                    conn.Open();

                    conn.ExecuteNonQuery("CREATE TABLE Temp(A INTEGER NOT NULL PRIMARY KEY, B INTEGER NOT NULL)");
                    Assert.True(conn.DoesTableExist("Temp"));

                    Dictionary <int, long> dict = new Dictionary <int, long>();
                    Random r     = new Random();
                    int    count = 100;
                    for (int i = 0; i < count; ++i)
                    {
                        int  a = r.Next();
                        long b = r.NextLong();

                        Inserter ins = new Inserter("Temp", conn);
                        ins.Set("A", a);
                        ins.Set("B", b);
                        ins.Execute();

                        dict[a] = b;
                    }

                    foreach (int a in dict.Keys)
                    {
                        string sql = $"SELECT B FROM Temp WHERE A = {a}";
                        using (SQLiteCommand command = new SQLiteCommand(sql, conn))
                            using (SQLiteDataReader reader = command.ExecuteReader())
                            {
                                bool read = reader.Read();
                                Assert.True(read);
                                if (read)
                                {
                                    long b = reader.GetInt64(0);
                                    Assert.Equal(dict[a], b);
                                }
                            }
                    }
                }
            }
        }
Exemple #17
0
        public void ProperlyHandleDates()
        {
            using (FileDeleter fd = new FileDeleter(Extensions.GetTempDBFile()))
            {
                Database db = new Database(new Context(fd.Fi));
                using (SQLiteConnection conn = db.Connection)
                {
                    conn.Open();

                    conn.ExecuteNonQuery("CREATE TABLE Temp(A INTEGER NOT NULL PRIMARY KEY, B TEXT NOT NULL)");
                    Assert.True(conn.DoesTableExist("Temp"));

                    Dictionary <int, DateTimeOffset> dict = new Dictionary <int, DateTimeOffset>();
                    Random r     = new Random();
                    int    count = 100;
                    for (int i = 0; i < count; ++i)
                    {
                        int            a = r.Next();
                        DateTimeOffset b = new DateTimeOffset(2018, r.Next(1, 12), r.Next(1, 28), r.Next(0, 23), r.Next(0, 59), r.Next(0, 59), r.Next(0, 999), TimeSpan.FromHours(r.Next(-12, 12)));

                        Inserter ins = new Inserter("Temp", conn);
                        ins.Set("A", a);
                        ins.Set("B", b);
                        ins.Execute();

                        dict[a] = b;
                    }

                    foreach (int a in dict.Keys)
                    {
                        string sql = $"SELECT B FROM Temp WHERE A = {a}";
                        using (SQLiteCommand command = new SQLiteCommand(sql, conn))
                            using (SQLiteDataReader reader = command.ExecuteReader())
                            {
                                bool read = reader.Read();
                                Assert.True(read);
                                if (read)
                                {
                                    DateTimeOffset b = DateTimeOffset.Parse(reader.GetString(0));
                                    Assert.Equal(dict[a], b);
                                }
                            }
                    }
                }
            }
        }
        public void ConvertProperly(string section, SystemErrorsInfoRequest.EType type)
        {
            using (FileDeleter fd = new FileDeleter(Extensions.GetTempDBFile()))
            {
                Database    db   = new Database(new Context(fd.Fi));
                Initializer init = new Initializer(null);
                init.Initialize(db);

                RequestBus        bus       = new RequestBus();
                EventLogResponder responder = new EventLogResponder()
                {
                    DB = db
                };
                bus.Subscribe(responder);

                using (SQLiteConnection conn = db.Connection)
                {
                    conn.Open();

                    string orig_path  = $"machine_name.{section}.max_record_number";
                    string new_path   = $"machine_name.{section}.max_event_log";
                    ulong  record_num = 234567;

                    Attribute attr = new Attribute();
                    attr.Set(orig_path, record_num.ToString(), conn);

                    SystemErrorsInfoRequest req = new SystemErrorsInfoRequest("machine_name", type);
                    bus.MakeRequest(req);
                    Assert.True(req.IsHandled);
                    Assert.True(req.LogData.MaxRecordNumber.HasValue);
                    Assert.Equal(record_num, req.LogData.MaxRecordNumber.Value);

                    string x = attr.Get(orig_path, conn);
                    Assert.True(string.IsNullOrEmpty(x));

                    x = attr.Get(new_path, conn);
                    Assert.False(string.IsNullOrEmpty(x));

                    EventLogData eld = JsonConvert.DeserializeObject <EventLogData>(x);
                    Assert.NotNull(eld);
                    Assert.True(eld.MaxRecordNumber.HasValue);
                    Assert.Equal(record_num, eld.MaxRecordNumber.Value);
                }
            }
        }
Exemple #19
0
        public void NotBeNullable()
        {
            using (FileDeleter fd = new FileDeleter(Extensions.GetTempDBFile()))
            {
                Database db = new Database(new Context(fd.Fi));
                using (SQLiteConnection conn = db.Connection)
                {
                    conn.Open();

                    conn.ExecuteNonQuery("CREATE TABLE Temp(A INTEGER NOT NULL PRIMARY KEY, B TEXT NOT NULL)");
                    Assert.True(conn.DoesTableExist("Temp"));

                    Inserter i = new Inserter("Temp", conn);
                    i.Set("A", 1);
                    i.SetNull("B");

                    Assert.Throws <SQLiteException>(() => i.Execute());
                }
            }
        }
Exemple #20
0
        public void UpdateOK()
        {
            using (FileDeleter fd = new FileDeleter(Extensions.GetTempDBFile()))
            {
                Dictionary <int, string> dict = new Dictionary <int, string>();
                Database db = new Database(new Context(fd.Fi));

                using (SQLiteConnection conn = db.Connection)
                {
                    conn.Open();

                    conn.ExecuteNonQuery(CreateTable);
                    Assert.True(conn.DoesTableExist("Temp"));

                    Insert(conn, 100, dict);
                    dict = Update(conn, dict);
                    Verify(conn, dict);
                }
            }
        }
        public static async Task <FileObject> PerformOneIterationOfPhaseOneAsync(ParametersPack args,
                                                                                 AnalysisLaunchContext launchContext, LocalFileWorker fileWorker)
        {
            args.ThrowIfNull(nameof(args));
            launchContext.ThrowIfNull(nameof(launchContext));
            fileWorker.ThrowIfNull(nameof(fileWorker));

            _logger.Info("Preparing to run one iteration of phase one.");

            // Contract: output files are located in the same directory as our app.
            IReadOnlyList <FileInfo> finalOutputFiles = args.GetOutputFilenames(phaseNumber: 1);

            CheckExpectedFilenamesNumber(upperBound: 2, finalOutputFiles);

            var fileDeleter = new FileDeleter(finalOutputFiles);

            // Contract: the analysis program is located in the same directory as our app.
            var processLaunchContext = ProcessLaunchContext.Create(
                file: args.AnalysisProgramName,
                args: args.PackAsInputArgumentsForPhaseOne(),
                showWindow: launchContext.ShowAnalysisWindow
                );

            _logger.Info(
                $"Starting analysis program. Launch context: {processLaunchContext.ToLogString()}"
                );
            using (var analysisRunner = ProgramRunner.RunProgram(processLaunchContext))
            {
                _logger.Info("Waiting to finish one iteration of phase one.");
                await analysisRunner.WaitAsync();
            }

            // The first data file is iteration result, the last is common analysis data file.
            // We don't need to read/use the last one.
            FileInfo finalOutputFile = finalOutputFiles.First();

            DataObject <OutputFileData> data = fileWorker.ReadDataFile(finalOutputFile);

            _logger.Info("Finished one iteration of phase one.");
            return(new FileObject(fileDeleter, data));
        }
        public void CollectNowProperly()
        {
            using (FileDeleter fd = new FileDeleter(Extensions.GetTempDBFile()))
            {
                Database db = new Database(new Context(fd.Fi));
                using (SQLiteConnection conn = db.Connection)
                {
                    conn.Open();
                    Initializer init = new Initializer(null);
                    init.Initialize(db);

                    Tuple <List <DeviceInfo>, DateTimeOffset> devices = LoadDevices(db, conn);
                    DBCollectionTimeRetriever retriever = new DBCollectionTimeRetriever(conn);
                    List <long> collector_ids           = retriever.AllIDs;

                    // At first, all of the collectors will have a NULL value, so we need to
                    // get them to a non-NULL value.
                    collector_ids.ForEach(c => new BeingCollected(c, conn).Dispose());

                    // Now lets go through each of them and make sure that when we collect now
                    // the next time changes to NULL, and re-appears
                    CollectionTime ct = new CollectionTime(conn);
                    foreach (long collector_id in collector_ids)
                    {
                        retriever = new DBCollectionTimeRetriever(conn);
                        Assert.DoesNotContain(collector_id, retriever.AllIDs);

                        CollectorInfo collector_info = ct.CollectNow(collector_id);
                        Assert.Equal(collector_id, collector_info.id);

                        retriever = new DBCollectionTimeRetriever(conn);
                        Assert.Contains(collector_id, retriever.AllIDs);

                        Assert.Null(collector_info.nextCollectionTime);
                    }
                }
            }
        }
Exemple #23
0
        public void DeleteOK()
        {
            using (FileDeleter fd = new FileDeleter(Extensions.GetTempDBFile()))
            {
                Dictionary <int, string> dict = new Dictionary <int, string>();
                Database db = new Database(new Context(fd.Fi));

                using (SQLiteConnection conn = db.Connection)
                {
                    conn.Open();

                    conn.ExecuteNonQuery(CreateTable);
                    Assert.True(conn.DoesTableExist("Temp"));

                    Insert(conn, 100, dict);
                    Verify(conn, dict);

                    foreach (int i in dict.Keys)
                    {
                        Deleter d = new Deleter("Temp", $"A = {i}", conn);
                        d.Execute();
                    }

                    foreach (int i in dict.Keys)
                    {
                        string sql = $"SELECT B FROM Temp WHERE A = {i}";
                        using (SQLiteCommand command = new SQLiteCommand(sql, conn))
                            using (SQLiteDataReader reader = command.ExecuteReader())
                            {
                                bool read = reader.Read();
                                Assert.False(read);
                            }
                    }
                }
            }
        }
Exemple #24
0
        public DeleteFile(string filePath)
        {
            var filePathInfo = new FileInfo(HostingEnvironment.MapPath(filePath));

            _command = new FileDeleter(filePathInfo);
        }
Exemple #25
0
        public static void DeleteOversizedTest()
        {
            FileDeleter del = new FileDeleter();

            del.DeleteOversized();
        }
        public void WorkFrom131ToCurrent()
        {
            // The queries that created the DB as it existed for COMMON 1.3.1
            List <string> sql = new List <string>(new string[] {
                @"CREATE TABLE Configuration (
    ConfigurationID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
    Path TEXT NOT NULL,
    Value TEXT,
    DateAdded TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
    IsValid INTEGER NOT NULL DEFAULT 0
);",
                @"CREATE TABLE DeviceTypes(
    DeviceType INTEGER NOT NULL,
    Description TEXT NOT NULL,
    PRIMARY KEY(DeviceType)
);",
                @"CREATE TABLE CollectorTypes(
    CollectorType INTEGER NOT NULL,
    Description TEXT NOT NULL,
    PRIMARY KEY(CollectorType)
);",
                @"CREATE TABLE Devices(
    DeviceID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
    Name TEXT NOT NULL,
    Type INTEGER NOT NULL,
    IPAddress TEXT,
    Username TEXT,
    Password TEXT,
    DateActivated TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
    DateDisabled TEXT,
    FOREIGN KEY(Type) REFERENCES DeviceTypes(DeviceType)
);",
                @"CREATE TABLE Collectors(
    CollectorID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
    Name TEXT NOT NULL,
    DeviceID INTEGER NOT NULL,
    CollectorType INTEGER NOT NULL,
    IsEnabled INTEGER NOT NULL DEFAULT 1,
    FrequencyInMinutes INTEGER NOT NULL DEFAULT 360,
    FOREIGN KEY(DeviceID) REFERENCES Devices(DeviceID),
    FOREIGN KEY(CollectorType) REFERENCES CollectorTypes(CollectorType)
);",
                @"CREATE TABLE Data(
    DataID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
    CollectorID INTEGER NOT NULL,
    Value TEXT NOT NULL,
    TimeStamp TEXT NOT NULL,
    WrittenToDailyFile INTEGER NOT NULL DEFAULT 0,
    FOREIGN KEY(CollectorID) REFERENCES Collectors(CollectorID)
);",
                @"CREATE TABLE MostRecentDataPerCollector(
    CollectorID INTEGER NOT NULL,
    DataID INTEGER NOT NULL,
    PRIMARY KEY(CollectorID, DataID)
            
    FOREIGN KEY(CollectorID) REFERENCES Collectors(CollectorID),
    FOREIGN KEY(DataID) REFERENCES Data(DataID)
);",
                @"CREATE TABLE NetworkStatus(
    IPAddress TEXT NOT NULL,
    Name TEXT NOT NULL,
    SuccessfulPing INTEGER NOT NULL,
    DateSuccessfulPingOccurred TEXT NOT NULL,
    DatePingAttempted TEXT NOT NULL,
    PRIMARY KEY(IPAddress)
);",
                @"CREATE TABLE StatusTypes(
    StatusType INTEGER NOT NULL,
    Description TEXT NOT NULL,
    PRIMARY KEY(StatusType)
);",
                @"CREATE TABLE DeviceStatus(
    DeviceID INTEGER NOT NULL,
    StatusType INTEGER NOT NULL,
    IsAlarm INTEGER NOT NULL,
    Date TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY(DeviceID) REFERENCES Devices(DeviceID),
    FOREIGN KEY(StatusType) REFERENCES StatusTypes(StatusType)
);",
                @"CREATE TABLE Attributes(
    Path TEXT NOT NULL,
    Value TEXT,
    PRIMARY KEY(Path)
);",
                @"CREATE INDEX Data_WrittenToDailyFile ON Data(WrittenToDailyFile);",
                @"CREATE INDEX Data_TimeStamp ON Data(TimeStamp);",
                @"CREATE INDEX Data_CollectorID ON Data(CollectorID);",
                @"CREATE INDEX Configuration_IsValid ON Configuration(IsValid);",
                @"CREATE INDEX Collectors_DeviceID_CollectorType ON Collectors(DeviceID, CollectorType);",
                @"CREATE INDEX MostRecentDataPerCollector_CollectorID ON MostRecentDataPerCollector(CollectorID);",
                @"CREATE INDEX MostRecentDataPerCollector_DataID ON MostRecentDataPerCollector(DataID);",
                @"INSERT INTO DeviceTypes(DeviceType, Description) VALUES
(0, 'Server'),
(1, 'Workstation'),
(2, 'Camera'),
(3, 'RPM'),
(4, 'System');",
                @"INSERT INTO CollectorTypes(CollectorType, Description) VALUES
(-1, 'Unknown'),
(0, 'Memory'),
(1, 'Disk'),
(2, 'CPUUsage'),
(3, 'NICUsage'),
(4, 'Uptime'),
(5, 'LastBootTime'),
(6, 'Processes'),
(7, 'Ping'),
(8, 'InstalledApplications'),
(9, 'Services'),
(11, 'SystemErrors'),
(12, 'ApplicationErrors'),
(13, 'DatabaseSize'),
(14, 'UPS'),
(15, 'DiskSpeed');",
                @"INSERT INTO Configuration(Path, Value, IsValid) VALUES
('site.name', 'Test Site Name', 1);",
                @"INSERT INTO Devices(Name, Type) VALUES('System', 4);",
                @"INSERT INTO Collectors(Name, DeviceID, CollectorType, IsEnabled, FrequencyInMinutes) SELECT 'System.Ping', S.seq, 7, 1, 360 FROM sqlite_sequence AS S WHERE S.name = 'Devices';",
                @"INSERT INTO Devices(Name, Type) VALUES('Server', 0);",
                @"INSERT INTO Collectors(Name, DeviceID, CollectorType, IsEnabled, FrequencyInMinutes) SELECT 'Server.Memory', S.seq, 0, 1, 360 FROM sqlite_sequence AS S WHERE S.name = 'Devices';",
                @"INSERT INTO Collectors(Name, DeviceID, CollectorType, IsEnabled, FrequencyInMinutes) SELECT 'Server.Disk', S.seq, 1, 1, 360 FROM sqlite_sequence AS S WHERE S.name = 'Devices';",
                @"INSERT INTO Collectors(Name, DeviceID, CollectorType, IsEnabled, FrequencyInMinutes) SELECT 'Server.CPUUsage', S.seq, 2, 1, 360 FROM sqlite_sequence AS S WHERE S.name = 'Devices';",
                @"INSERT INTO Collectors(Name, DeviceID, CollectorType, IsEnabled, FrequencyInMinutes) SELECT 'Server.NICUsage', S.seq, 3, 1, 360 FROM sqlite_sequence AS S WHERE S.name = 'Devices';",
                @"INSERT INTO Collectors(Name, DeviceID, CollectorType, IsEnabled, FrequencyInMinutes) SELECT 'Server.Uptime', S.seq, 4, 1, 360 FROM sqlite_sequence AS S WHERE S.name = 'Devices';",
                @"INSERT INTO Collectors(Name, DeviceID, CollectorType, IsEnabled, FrequencyInMinutes) SELECT 'Server.LastBootTime', S.seq, 5, 1, 360 FROM sqlite_sequence AS S WHERE S.name = 'Devices';",
                @"INSERT INTO Collectors(Name, DeviceID, CollectorType, IsEnabled, FrequencyInMinutes) SELECT 'Server.Processes', S.seq, 6, 1, 360 FROM sqlite_sequence AS S WHERE S.name = 'Devices';",
                @"INSERT INTO Collectors(Name, DeviceID, CollectorType, IsEnabled, FrequencyInMinutes) SELECT 'Server.SystemErrors', S.seq, 11, 1, 360 FROM sqlite_sequence AS S WHERE S.name = 'Devices';",
                @"INSERT INTO Collectors(Name, DeviceID, CollectorType, IsEnabled, FrequencyInMinutes) SELECT 'Server.ApplicationErrors', S.seq, 12, 1, 360 FROM sqlite_sequence AS S WHERE S.name = 'Devices';",
            });

            using (FileDeleter fd = new FileDeleter(Extensions.GetTempDBFile()))
            {
                Database db = new Database(new Context(fd.Fi));
                using (SQLiteConnection conn = db.Connection)
                {
                    conn.Open();

                    // Create the DB as it existed at 1.3.1, then make sure the tables are all there
                    sql.ForEach(s => conn.ExecuteNonQuery(s));

                    Assert.True(conn.DoesTableExist("Configuration"));
                    Assert.True(conn.DoesTableExist("DeviceTypes"));
                    Assert.True(conn.DoesTableExist("CollectorTypes"));
                    Assert.True(conn.DoesTableExist("Devices"));
                    Assert.True(conn.DoesTableExist("Collectors"));
                    Assert.True(conn.DoesTableExist("Data"));
                    Assert.True(conn.DoesTableExist("MostRecentDataPerCollector"));
                    Assert.True(conn.DoesTableExist("NetworkStatus"));
                    Assert.True(conn.DoesTableExist("StatusTypes"));
                    Assert.True(conn.DoesTableExist("DeviceStatus"));
                    Assert.True(conn.DoesTableExist("Attributes"));

                    // Create an initializer and initialize the DB. Then we can check that the new
                    // columns and tables exist.
                    Initializer init = new Initializer(null);
                    init.Initialize(db);

                    Assert.True(conn.DoesColumnExist("Collectors", "LastCollectionAttempt"));
                    Assert.True(conn.DoesColumnExist("Collectors", "LastCollectedAt"));
                    Assert.True(conn.DoesColumnExist("Collectors", "NextCollectionTime"));
                    Assert.True(conn.DoesColumnExist("Collectors", "SuccessfullyCollected"));
                    Assert.True(conn.DoesColumnExist("Collectors", "CurrentlyBeingCollected"));

                    Assert.True(conn.DoesTableExist("DeviceGroups"));

                    Assert.True(conn.DoesColumnExist("DeviceStatus", "Message"));
                    Assert.True(conn.DoesColumnExist("DeviceStatus", "IsValid"));

                    Assert.True(conn.DoesColumnExist("Devices", "GroupID"));

                    Assert.True(conn.DoesTableExist("FileTypes"));
                    Assert.True(conn.DoesTableExist("Files"));
                }
            }
        }
        public void UpdateBeingCollectedProperly()
        {
            using (FileDeleter fd = new FileDeleter(Extensions.GetTempDBFile()))
            {
                Database db = new Database(new Context(fd.Fi));
                using (SQLiteConnection conn = db.Connection)
                {
                    conn.Open();
                    Initializer init = new Initializer(null);
                    init.Initialize(db);

                    Tuple <List <DeviceInfo>, DateTimeOffset> devices = LoadDevices(db, conn);

                    TimeSpan one_second = TimeSpan.FromSeconds(1);
                    DBCollectionTimeRetriever retriever = new DBCollectionTimeRetriever(conn);

                    foreach (long collector_id in retriever.AllIDs)
                    {
                        string sql = $"SELECT LastCollectionAttempt, LastCollectedAt, NextCollectionTime, CurrentlyBeingCollected, FrequencyInMinutes FROM Collectors WHERE CollectorID = {collector_id}";
                        using (SQLiteCommand command = new SQLiteCommand(sql, conn))
                            using (SQLiteDataReader reader = command.ExecuteReader())
                            {
                                if (reader.Read())
                                {
                                    // Before being collected, the CurrentlyBeingCollected column should have 0
                                    Assert.True(reader.IsDBNull(0));
                                    Assert.True(reader.IsDBNull(1));
                                    Assert.True(reader.IsDBNull(2));
                                    Assert.False(reader.IsDBNull(3));
                                    Assert.Equal(0, reader.GetInt32(3));
                                    Assert.False(reader.IsDBNull(4));
                                }
                            }

                        using (BeingCollected bc = new BeingCollected(collector_id, conn))
                        {
                            using (SQLiteCommand command = new SQLiteCommand(sql, conn))
                                using (SQLiteDataReader reader = command.ExecuteReader())
                                {
                                    if (reader.Read())
                                    {
                                        // Now that it's being collected, the LastCollectionAttempt and NextCollectionTime columns
                                        // should have something there, and the CurrentlyBeingCollected column should have 1
                                        Assert.False(reader.IsDBNull(0));
                                        Assert.True(reader.IsDBNull(1));
                                        Assert.False(reader.IsDBNull(2));
                                        Assert.False(reader.IsDBNull(3));
                                        Assert.Equal(1, reader.GetInt32(3));

                                        if (DateTimeOffset.TryParse(reader.GetString(0), out DateTimeOffset lca))
                                        {
                                            // Make sure it just happened...let's say within 1 second of now
                                            DateTimeOffset now   = DateTimeOffset.Now;
                                            DateTimeOffset lower = now - one_second;
                                            Assert.InRange(lca, lower, now);

                                            // And make sure NextCollectionTime is correct--lets just see if the time is
                                            // in the future.
                                            int frequency = reader.GetInt32(4);
                                            if (frequency > 0 && DateTimeOffset.TryParse(reader.GetString(2), out DateTimeOffset next))
                                            {
                                                Assert.True(next >= now);
                                            }
                                        }
                                        else
                                        {
                                            Assert.True(false);
                                        }
                                    }
                                }
                        }

                        using (SQLiteCommand command = new SQLiteCommand(sql, conn))
                            using (SQLiteDataReader reader = command.ExecuteReader())
                            {
                                if (reader.Read())
                                {
                                    // And after being collected, the CurrentlyBeingCollected column should have 0 again
                                    Assert.False(reader.IsDBNull(0));
                                    Assert.True(reader.IsDBNull(1));
                                    Assert.False(reader.IsDBNull(2));
                                    Assert.False(reader.IsDBNull(3));
                                    Assert.Equal(0, reader.GetInt32(3));
                                }
                            }

                        // The LastCollectedAt column should remain NULL because it's set when the data is stored and
                        // we didn't store anything here.
                    }

                    // Now make sure none of them show as being collected
                    foreach (long collector_id in retriever.AllIDs)
                    {
                        string sql = $"SELECT CurrentlyBeingCollected FROM Collectors WHERE CollectorID = {collector_id}";
                        using (SQLiteCommand command = new SQLiteCommand(sql, conn))
                            using (SQLiteDataReader reader = command.ExecuteReader())
                            {
                                if (reader.Read())
                                {
                                    Assert.Equal(0, reader.GetInt32(0));
                                }
                            }
                    }

                    // And make sure that if an exception occurs while marking it as being collected that it shows as
                    // no longer being collected
                    foreach (long collector_id in retriever.AllIDs)
                    {
                        string sql = $"SELECT CurrentlyBeingCollected FROM Collectors WHERE CollectorID = {collector_id}";
                        try
                        {
                            using (BeingCollected bc = new BeingCollected(collector_id, conn))
                            {
                                using (SQLiteCommand command = new SQLiteCommand(sql, conn))
                                    using (SQLiteDataReader reader = command.ExecuteReader())
                                    {
                                        if (reader.Read())
                                        {
                                            Assert.Equal(1, reader.GetInt32(0));
                                        }
                                    }
                                throw new Exception($"Exception with collector_id {collector_id}");
                            }
                        }
                        catch (Exception)
                        {
                        }

                        using (SQLiteCommand command = new SQLiteCommand(sql, conn))
                            using (SQLiteDataReader reader = command.ExecuteReader())
                            {
                                if (reader.Read())
                                {
                                    Assert.Equal(0, reader.GetInt32(0));
                                }
                            }
                    }
                }
            }
        }
Exemple #28
0
        public static void DeleteOutdatedTest()
        {
            FileDeleter del = new FileDeleter();

            del.DeleteOutdated();
        }
Exemple #29
0
        public void InsertNullOK()
        {
            using (FileDeleter fd = new FileDeleter(Extensions.GetTempDBFile()))
            {
                Database db = new Database(new Context(fd.Fi));
                using (SQLiteConnection conn = db.Connection)
                {
                    conn.Open();

                    conn.ExecuteNonQuery(CreateTable);
                    Assert.True(conn.DoesTableExist("Temp"));

                    int    count = 100;
                    Random r     = new Random();
                    Dictionary <int, string> dict = new Dictionary <int, string>();
                    for (int i = 0; i < count; ++i)
                    {
                        Inserter inserter = new Inserter("Temp", conn);
                        int      a        = r.Next();
                        inserter.Set("A", a);

                        // Make every other one null
                        if (i % 2 == 0)
                        {
                            string b = Guid.NewGuid().ToString();
                            inserter.Set("B", b, false);
                            dict[a] = b;
                        }
                        else
                        {
                            inserter.SetNull("B");
                            dict[a] = null;
                        }

                        inserter.Execute();
                    }

                    foreach (int a in dict.Keys)
                    {
                        string sql = $"SELECT B FROM Temp WHERE A = {a}";
                        using (SQLiteCommand command = new SQLiteCommand(sql, conn))
                            using (SQLiteDataReader reader = command.ExecuteReader())
                            {
                                bool read = reader.Read();
                                Assert.True(read);
                                if (read)
                                {
                                    if (dict[a] != null)
                                    {
                                        Assert.False(reader.IsDBNull(0));
                                        string b = reader.GetString(0);
                                        Assert.Equal(dict[a], b);
                                    }
                                    else
                                    {
                                        Assert.True(reader.IsDBNull(0));
                                        Assert.Throws <InvalidCastException>(() => reader.GetString(0));
                                    }
                                }
                            }
                    }
                }
            }
        }