public static bool IsMachineInformationFromAVirtualMachine(CSSDataContext db, MachineInformation machineInformation, Login login)
        {
            var virtualMachineRecord = machineInformation.MachineValues.FirstOrDefault(p => db.VirtualMachineMarkers.Count(q => System.Data.Linq.SqlClient.SqlMethods.Like(p.Value, q.IdentifierMask) && (DeviceType) q.RecordTypeId == p.Type) > 0);
            if (virtualMachineRecord != null)
            {
                Log.Write(db, Enumerations.LogType.AuthenticationServer, "LoginID: " + login.Id + ", Name: " + login.Username + ", Virtual Machine Detected: name: " + virtualMachineRecord.Name + ", type: " + virtualMachineRecord.Type + ", value: " + virtualMachineRecord.Value + ".");
                return true;
            }

            return false;
        }
        static void Main(string[] args)
        {
            using (CSSDataContext db = new CSSDataContext())
            {
                var linkedLogin = db.Logins.FirstOrDefault(p => p.Id == 134);

                var linkedMachineRecords = db.MachineRecords.Where(p => linkedLogin.Identity.Logins.Contains(p.Login));
                foreach (var machineRecord in linkedMachineRecords)
                {
                    Console.WriteLine("loginID: " + machineRecord.LoginId + ", machineRecord: " + machineRecord.Id + ", " + machineRecord.Identifier);
                }

                var machineRecords = db.MachineRecords.Where(p => p.LoginId == 135);
                List<DeviceInfo> deviceInfos = new List<DeviceInfo>();

                foreach (MachineRecord machineRecord in machineRecords)
                {
                    deviceInfos.Add(new DeviceInfo()
                    {
                        Name = machineRecord.DeviceType.ToString(),
                        Type = machineRecord.DeviceType,
                        Value = machineRecord.Identifier
                    });
                }

                MachineInformation mi = new MachineInformation()
                {
                    MachineValues = deviceInfos,
                    Token = "Test Token"
                };

                var login = db.Logins.FirstOrDefault(p => p.Id == 135);

                bool wasMerged;
                Identity identity;
                Identity.MatchIdentity(db, (Login)login, mi, out identity, out wasMerged);
            }
        }
        public void TestMatchingForDisparateMachineRecord()
        {
            using (CSSDataContext db = new CSSDataContext())
            {
                db.MachineRecords.DeleteAllOnSubmit(db.MachineRecords);
                db.SubmitChanges();

                LoadTestMachineRecords();

                //// Remove any existing links for login 15.
                //var linkedItemsToDelete = db.LinkedItems.Where(p => p.TargetId == 15 && p.LinkedItemTypeId == 1);
                //db.LinkedItems.DeleteAllOnSubmit(linkedItemsToDelete);
                //db.Links.DeleteAllOnSubmit(linkedItemsToDelete.Select(p => p.Link));
                //db.SubmitChanges();

                //// Remove any existing links for login 16.
                //linkedItemsToDelete = db.LinkedItems.Where(p => p.TargetId == 16 && p.LinkedItemTypeId == 1);
                //db.LinkedItems.DeleteAllOnSubmit(linkedItemsToDelete);
                //db.Links.DeleteAllOnSubmit(linkedItemsToDelete.Select(p => p.Link));
                //db.SubmitChanges();

                //// Remove any existing links for login 17.
                //linkedItemsToDelete = db.LinkedItems.Where(p => p.TargetId == 17 && p.LinkedItemTypeId == 1);
                //db.LinkedItems.DeleteAllOnSubmit(linkedItemsToDelete);
                //db.Links.DeleteAllOnSubmit(linkedItemsToDelete.Select(p => p.Link));
                //db.SubmitChanges();

                MachineInformation machineInformation = new MachineInformation();
                machineInformation.MachineValues = new List<DeviceInfo>();

                //-------------------------------
                var machineRecords = db.MachineRecords.Where(p => p.LoginId == 15);
                foreach (var machineRecord in machineRecords)
                {
                    machineInformation.MachineValues.Add(new DeviceInfo()
                    {
                        Name = machineRecord.MachineRecordType.Name,
                        Type = (DeviceType)machineRecord.MachineRecordType.Id,
                        Value = machineRecord.Identifier
                    });
                }

                var login15 = db.Logins.FirstOrDefault(p => p.Id == 15);

                Identity identity;
                bool wasMerged;
                Identity.MatchIdentity(db, login15, machineInformation, out identity, out wasMerged);

                //-------------------------------

                machineInformation = new MachineInformation();
                machineInformation.MachineValues = new List<DeviceInfo>();

                machineRecords = db.MachineRecords.Where(p => p.LoginId == 16);
                foreach (var machineRecord in machineRecords)
                {
                    machineInformation.MachineValues.Add(new DeviceInfo()
                    {
                        Name = machineRecord.MachineRecordType.Name,
                        Type = (DeviceType)machineRecord.MachineRecordType.Id,
                        Value = machineRecord.Identifier
                    });
                }

                var login16 = db.Logins.FirstOrDefault(p => p.Id == 16);
                Identity.MatchIdentity(db, login16, machineInformation, out identity, out wasMerged);

                //-----------------------------

                machineInformation = new MachineInformation();
                machineInformation.MachineValues = new List<DeviceInfo>();

                machineRecords = db.MachineRecords.Where(p => p.LoginId == 17);
                foreach (var machineRecord in machineRecords)
                {
                    machineInformation.MachineValues.Add(new DeviceInfo()
                    {
                        Name = machineRecord.MachineRecordType.Name,
                        Type = (DeviceType)machineRecord.MachineRecordType.Id,
                        Value = machineRecord.Identifier
                    });
                }

                var login17 = db.Logins.FirstOrDefault(p => p.Id == 17);

                Identity.MatchIdentity(db, login17, machineInformation, out identity, out wasMerged);

                Assert.AreEqual(5, login15.Identity.MachineRecords.Count());
                Assert.AreEqual(5, login16.Identity.MachineRecords.Count());
                Assert.AreEqual(5, login17.Identity.MachineRecords.Count());
            }
        }
        public void TestVirtualMachineDetection()
        {
            Login login1;
            Login login2;
            Login login3;

            CreateTestData(out login1, out login2, out login3);

            MachineInformation machineInformation = new MachineInformation();

            machineInformation.MachineValues = new List<DeviceInfo>();

            using (CSSDataContext db = new CSSDataContext())
            {

                // Get all machine records for login 1.
                var machineRecords = db.MachineRecords.Where(p => p.LoginId == 1);
                foreach (var machineRecord in machineRecords)
                {
                    machineInformation.MachineValues.Add(new DeviceInfo()
                    {
                        Name = machineRecord.MachineRecordType.Name,
                        Type = (DeviceType)machineRecord.MachineRecordType.Id,
                        Value = machineRecord.Identifier
                    });
                }

                Assert.IsFalse(VirtualMachineMarker.IsMachineInformationFromAVirtualMachine(db, machineInformation, login1));

                machineInformation.MachineValues.Add(new DeviceInfo()
                {
                    Name = "HardDisk",
                    Type = DeviceType.HardDisk,
                    Value = "3951160193|Virtual HD|0|Virtual HD"
                });

                Assert.IsTrue(VirtualMachineMarker.IsMachineInformationFromAVirtualMachine(db, machineInformation, login1));
            }
        }
        public void TestMatching_UniqueRecords()
        {
            LoadTestMachineRecords();

            MachineInformation machineInformation = new MachineInformation();
            machineInformation.MachineValues = new List<DeviceInfo>();

            using (CSSDataContext db = new CSSDataContext())
            {
                // Get all machine records for login 1.
                var machineRecords = db.MachineRecords.Where(p => p.LoginId == 1);
                foreach (var machineRecord in machineRecords)
                {
                    machineInformation.MachineValues.Add(new DeviceInfo()
                    {
                        Name = machineRecord.MachineRecordType.Name,
                        Type = (DeviceType)machineRecord.MachineRecordType.Id,
                        Value = machineRecord.Identifier
                    });
                }

                var login = db.Logins.FirstOrDefault(p => p.Id == 1);

                Identity identity;
                bool wasMerged;
                Identity.MatchIdentity(db, login, machineInformation, out identity, out wasMerged);

                db.SubmitChanges();

                Assert.IsTrue(login.Identity == identity);

                // Shouldn't be linked to any other accounts.
                Assert.AreEqual(0, login.Identity.Logins.Where(p => p.Id != login.Id).Count());
                Assert.AreEqual(0, login.Identity.MachineRecords.Where(p => p.LoginId != login.Id).Count());
                Assert.AreEqual(0, login.Identity.PollVotes.Where(p => p.LoginId != login.Id).Count());
                Assert.AreEqual(0, login.Identity.Bans.Where(p => p.LoginId != login.Id).Count());
            }
        }
        public void TestMatching_DuplicateNetworkAdapterRecords()
        {
            LoadTestMachineRecords();

            MachineInformation machineInformation = new MachineInformation();
            machineInformation.MachineValues = new List<DeviceInfo>();

            using (CSSDataContext db = new CSSDataContext())
            {
                // Get all machine records for login 21.
                var machineRecords = db.MachineRecords.Where(p => p.LoginId == 21);
                foreach (var machineRecord in machineRecords)
                {
                    machineInformation.MachineValues.Add(new DeviceInfo()
                    {
                        Name = machineRecord.MachineRecordType.Name,
                        Type = (DeviceType)machineRecord.MachineRecordType.Id,
                        Value = machineRecord.Identifier
                    });
                }

                //machineInformation.MachineValues.Add(new DeviceInfo()
                //{
                //    Name = DeviceType.HardDisk.ToString(),
                //    Type = DeviceType.HardDisk,
                //    Value = "This is a test harddisk!"
                //});

                var login = db.Logins.FirstOrDefault(p => p.Id == 21);

                Identity identity;
                bool wasMerged;
                Identity.MatchIdentity(db, login, machineInformation, out identity, out wasMerged);

                db.SubmitChanges();

                Assert.IsTrue(login.Identity == identity);

                // Should NOT be linked to account 22.
                Assert.AreEqual(1, db.Logins.FirstOrDefault(p => p.Id == 21).Identity.Logins.Count);
            }
        }
        public void TestMatching_DuplicateHardDriveRecords()
        {
            LoadTestMachineRecords();

            MachineInformation machineInformation = new MachineInformation();
            machineInformation.MachineValues = new List<DeviceInfo>();

            using (CSSDataContext db = new CSSDataContext())
            {
                // Get all machine records for login 12.
                var machineRecords = db.MachineRecords.Where(p => p.LoginId == 12);
                foreach (var machineRecord in machineRecords)
                {
                    machineInformation.MachineValues.Add(new DeviceInfo()
                    {
                        Name = machineRecord.MachineRecordType.Name,
                        Type = (DeviceType)machineRecord.MachineRecordType.Id,
                        Value = machineRecord.Identifier
                    });
                }

                machineInformation.MachineValues.Add(new DeviceInfo()
                {
                    Name = DeviceType.HardDisk.ToString(),
                    Type = DeviceType.HardDisk,
                    Value = "This is a test harddisk!"
                });

                var login12 = db.Logins.FirstOrDefault(p => p.Id == 12);
                var login13 = db.Logins.FirstOrDefault(p => p.Id == 13);

                Identity identity;
                bool wasMerged;
                Identity.MatchIdentity(db, login12, machineInformation, out identity, out wasMerged);

                db.SubmitChanges();

                Assert.IsTrue(login12.Identity == identity);

                // Should be linked to account 13.
                Assert.AreEqual(2, db.Logins.FirstOrDefault(p => p.Id == 12).Identity.Logins.Count);

                Assert.AreEqual(1, login12.Identity.Logins.Where(p => p.Id != login12.Id).Count());
                Assert.AreEqual(1, login12.Identity.MachineRecords.Where(p => p.LoginId != login12.Id).Count());
                Assert.AreEqual(0, login12.Identity.PollVotes.Where(p => p.LoginId != login12.Id).Count());
                Assert.AreEqual(0, login12.Identity.Bans.Where(p => p.LoginId != login12.Id).Count());

                Assert.IsTrue(Identity.IsLinkedToAnOlderAccount(db, login13.Username));

                Assert.AreEqual(login12.Username, Identity.GetOldestLinkedAcccountUsername(db, login13.Username));

            }
        }
        public void TestMatchingOnLoginWithNewMatchingHardDiskMachineInfo()
        {
            using (CSSDataContext db = new CSSDataContext())
            {
                db.MachineRecords.DeleteAllOnSubmit(db.MachineRecords);
                db.SubmitChanges();

                LoadTestMachineRecords();

                var login23 = db.Logins.FirstOrDefault(p => p.Id == 23);
                var login17 = db.Logins.FirstOrDefault(p => p.Id == 17);
                var hardDiskRecord = login23.MachineRecords.FirstOrDefault(p => p.DeviceType == DeviceType.HardDisk);

                List<DeviceInfo> machineValues = new List<DeviceInfo>();
                machineValues.Add(new DeviceInfo()
                {
                    Name = hardDiskRecord.DeviceType.ToString(),
                    Type = hardDiskRecord.DeviceType,
                    Value = hardDiskRecord.Identifier
                });

                var machineInformation = new MachineInformation()
                {
                    MachineValues = machineValues
                };

                Assert.AreEqual(1, login17.Identity.Logins.Count());

                Identity identity;
                bool wasMerged;
                Identity.MatchIdentity(db, login17, machineInformation, out identity, out wasMerged);

                Assert.AreEqual(2, login17.Identity.Logins.Count());
                Assert.IsTrue(wasMerged);

                Identity.MatchIdentity(db, login17, machineInformation, out identity, out wasMerged);

                Assert.AreEqual(2, login17.Identity.Logins.Count());
                Assert.IsFalse(wasMerged);
            }
        }