예제 #1
0
        public void GetMostRecentHitDateDuplicateEventIds()
        {
            m_Index = new SqlErrorIndex(StackHashSqlConfiguration.Default, SqlUtils.UnitTestDatabase, m_RootCabFolder);
            m_Index.DeleteIndex();

            m_Index.Activate();

            DateTime         creationDateTime = new DateTime(2010, 04, 04, 22, 9, 0, DateTimeKind.Utc);
            DateTime         modifiedDateTime = new DateTime(2010, 05, 05, 23, 10, 0, DateTimeKind.Utc);
            StackHashProduct product1         =
                new StackHashProduct(creationDateTime, modifiedDateTime, null, 1, "TestProduct1", 20, 30, "2.10.02123.1293");

            StackHashFile file1 =
                new StackHashFile(creationDateTime, modifiedDateTime, 20, creationDateTime, "File1.dll", "2.3.4.5");

            StackHashEventSignature eventSignature = new StackHashEventSignature();

            eventSignature.Parameters = new StackHashParameterCollection();
            eventSignature.Parameters.Add(new StackHashParameter(StackHashEventSignature.ParamApplicationName, "AppName"));
            eventSignature.Parameters.Add(new StackHashParameter(StackHashEventSignature.ParamApplicationVersion, "1.2.3.4"));
            eventSignature.Parameters.Add(new StackHashParameter(StackHashEventSignature.ParamApplicationTimeStamp, creationDateTime.ToString()));
            eventSignature.Parameters.Add(new StackHashParameter(StackHashEventSignature.ParamModuleName, "ModuleName"));
            eventSignature.Parameters.Add(new StackHashParameter(StackHashEventSignature.ParamModuleVersion, "2.3.4.5"));
            eventSignature.Parameters.Add(new StackHashParameter(StackHashEventSignature.ParamModuleTimeStamp, creationDateTime.ToString()));
            eventSignature.Parameters.Add(new StackHashParameter(StackHashEventSignature.ParamExceptionCode, "1234"));
            eventSignature.Parameters.Add(new StackHashParameter(StackHashEventSignature.ParamOffset, "0x1234"));
            eventSignature.InterpretParameters();

            StackHashEvent event1 =
                new StackHashEvent(creationDateTime, modifiedDateTime, "EventTypeName1", 100, eventSignature, 20, file1.Id, "bug");

            StackHashEvent event2 =
                new StackHashEvent(creationDateTime, modifiedDateTime, "EventTypeName2", 100, eventSignature, 20, file1.Id, "bug");

            m_Index.AddProduct(product1);
            m_Index.AddFile(product1, file1);
            m_Index.AddEvent(product1, file1, event1);
            m_Index.AddEvent(product1, file1, event2);

            StackHashEventInfoCollection eventInfos1 = new StackHashEventInfoCollection();

            int numEventInfos = 3;

            for (int i = 0; i < numEventInfos; i++)
            {
                DateTime nowTime = DateTime.Now;
                DateTime date    = new DateTime(nowTime.Year, nowTime.Month, nowTime.Day, nowTime.Hour, nowTime.Minute, 0);

                int localeId = i;
                StackHashEventInfo eventInfo =
                    new StackHashEventInfo(date.AddDays(i + 2), date.AddDays(i + 1), date.AddDays(i), "US", localeId, "English", "Windows Vista" + i.ToString(), "1.2.3.4 build 7", event1.Id);

                eventInfos1.Add(eventInfo);
            }

            m_Index.AddEventInfoCollection(product1, file1, event1, eventInfos1);


            StackHashEventInfoCollection eventInfos2 = new StackHashEventInfoCollection();

            // Add some other event infos for a different event.
            for (int i = 0; i < numEventInfos; i++)
            {
                DateTime nowTime = DateTime.Now;
                DateTime date    = new DateTime(nowTime.Year, nowTime.Month, nowTime.Day, nowTime.Hour, nowTime.Minute, 0);

                int localeId = i;
                StackHashEventInfo eventInfo =
                    new StackHashEventInfo(date.AddDays(i + 2), date.AddDays(i + 1), date.AddDays(i * 2), "US", localeId, "English", "Windows Vista" + i.ToString(), "1.2.3.4 build 7", event2.Id);

                eventInfos2.Add(eventInfo);
            }


            m_Index.AddEventInfoCollection(product1, file1, event2, eventInfos2);

            StackHashEventInfoCollection eventInfosRetrieved = m_Index.LoadEventInfoList(product1, file1, event1);

            Assert.AreEqual(numEventInfos, eventInfosRetrieved.Count);

            for (int i = 0; i < numEventInfos; i++)
            {
                StackHashEventInfo eventInfo = eventInfosRetrieved.FindEventInfo(eventInfos1[i]);

                Assert.AreEqual(0, eventInfos1[i].CompareTo(eventInfo));
            }

            DateTime mostRecentHitDate = m_Index.GetMostRecentHitDate(product1, file1, event1);

            Assert.AreEqual(eventInfos1[numEventInfos - 1].HitDateLocal, mostRecentHitDate);
            Assert.AreNotEqual(eventInfos1[0].HitDateLocal, mostRecentHitDate);
            Assert.AreNotEqual(eventInfos1[1].HitDateLocal, mostRecentHitDate);

            DateTime mostRecentHitDate2 = m_Index.GetMostRecentHitDate(product1, file1, event2);

            Assert.AreEqual(eventInfos2[numEventInfos - 1].HitDateLocal, mostRecentHitDate2);
            Assert.AreNotEqual(eventInfos2[0].HitDateLocal, mostRecentHitDate2);
            Assert.AreNotEqual(eventInfos2[1].HitDateLocal, mostRecentHitDate2);
        }
예제 #2
0
        public static void CreateTestIndex(IErrorIndex index, StackHashTestIndexData testData, bool includeDuplicates)
        {
            if (testData.EventsToAssignCabs == 0)
            {
                testData.EventsToAssignCabs = 1;
            }

            Random rand       = new Random(1);
            Random scriptRand = new Random(1);

            if (index == null)
            {
                throw new ArgumentNullException("index");
            }
            if (testData == null)
            {
                throw new ArgumentNullException("testData");
            }

            DateTime now = DateTime.Now;


            int fileId    = 1;
            int eventId   = 1;
            int cabId     = 1;
            int productId = 1;
            int offset    = 10000;

            if (!s_IsTestMode)
            {
                productId = 26214;
                fileId    = 1035620;
                eventId   = 1099309964;
                cabId     = 939529168;
            }

            int initialFileId  = fileId;
            int initialEventId = eventId;
            int initialCabId   = cabId;
            int initialOffset  = offset;

            int totalEventsPerProduct = testData.NumberOfFiles * testData.NumberOfEvents;

            index.SetLastSyncTimeLocal(-2, DateTime.Now.AddDays(-14));

            for (int i = 0; i < testData.NumberOfProducts; i++)
            {
                StackHashProduct product = new StackHashProduct();
                product.DateCreatedLocal  = now.ToUniversalTime().AddDays(-1 * (rand.Next() % 180)).RoundToPreviousMinute();
                product.DateModifiedLocal = product.DateCreatedLocal.AddDays(rand.Next(-10, 10)).RoundToPreviousMinute();
                product.FilesLink         = "http://www.cucku.com";
                product.Id             = productId;
                product.Name           = "StackHash" + productId.ToString(CultureInfo.InvariantCulture);
                product.TotalEvents    = totalEventsPerProduct;
                product.TotalResponses = 1;
                product.Version        = "1.2.3." + productId.ToString(CultureInfo.InvariantCulture);

                index.AddProduct(product);
                productId++;

                if (includeDuplicates)
                {
                    fileId  = initialFileId;
                    eventId = initialEventId;
                    cabId   = initialCabId;
                    offset  = initialOffset;
                    rand    = new Random(1);
                }

                for (int j = 0; j < testData.NumberOfFiles; j++)
                {
                    StackHashFile file = new StackHashFile();

                    file.DateCreatedLocal  = now.ToUniversalTime().AddDays(-1 * (rand.Next() % 180)).RoundToPreviousMinute();
                    file.DateModifiedLocal = file.DateCreatedLocal.AddDays(rand.Next(-10, 10)).RoundToPreviousMinute();
                    file.Id            = fileId++;
                    file.LinkDateLocal = file.DateCreatedLocal.AddDays(rand.Next(-10, 10)).RoundToPreviousMinute();

                    int fileIndex = rand.Next() % s_FileNames.Length;

                    file.Name    = String.Format(CultureInfo.InvariantCulture, "{0}.{1}", s_FileNames[fileIndex], "dll");
                    file.Version = String.Format(CultureInfo.InvariantCulture, "{0}.{1}.{2}{3}.{4}",
                                                 fileId, rand.Next() % 99, rand.Next() % 366, rand.Next() % 5 + 2005, (j + 1) * 1237);

                    index.AddFile(product, file);

                    for (int k = 0; k < testData.NumberOfEvents; k++)
                    {
                        int totalHits = 0;

                        Random hitsRand = new Random(k);
                        for (int l = 0; l < testData.NumberOfEventInfos; l++)
                        {
                            if (s_IsTestMode)
                            {
                                totalHits += (l + k);
                            }
                            else
                            {
                                totalHits += hitsRand.Next(0, 50);
                            }
                        }

                        StackHashEvent theEvent = new StackHashEvent();

                        theEvent.DateCreatedLocal  = now.ToUniversalTime().AddDays(-1 * (rand.Next() % 180)).RoundToPreviousMinute();
                        theEvent.DateModifiedLocal = theEvent.DateCreatedLocal.AddDays(rand.Next(-10, 10)).RoundToPreviousMinute();

                        theEvent.EventTypeName = s_EventTypes[rand.Next(0, s_EventTypes.Length)];
                        theEvent.FileId        = file.Id;
                        theEvent.Id            = eventId;
                        theEvent.BugId         = "Bug" + eventId.ToString(CultureInfo.InvariantCulture);
                        theEvent.TotalHits     = totalHits;

                        theEvent.EventSignature = new StackHashEventSignature();
                        theEvent.EventSignature.ApplicationName      = "StackHash.exe";
                        theEvent.EventSignature.ApplicationTimeStamp = now.ToUniversalTime().AddDays(rand.Next(-180, 0));
                        theEvent.EventSignature.ApplicationVersion   = String.Format(CultureInfo.InvariantCulture, "{0}.{1}.{2}{3}.{4}",
                                                                                     eventId, rand.Next() % 99, rand.Next() % 366, rand.Next() % 5 + 2005, (Math.Abs(eventId) + 1) * 1234);
                        theEvent.EventSignature.ExceptionCode = 0xc0000000 + rand.Next(0, 16);

                        theEvent.EventSignature.ModuleName      = "Module" + k.ToString(CultureInfo.InvariantCulture);
                        theEvent.EventSignature.ModuleTimeStamp = now.ToUniversalTime().AddDays(-1 * ((rand.Next() % 200)));
                        theEvent.EventSignature.ModuleVersion   = String.Format(CultureInfo.InvariantCulture, "{0}.{1}.{2}{3}.{4}",
                                                                                eventId, rand.Next() % 99, rand.Next() % 366, rand.Next() % 5 + 2005, (Math.Abs(eventId) + 1) * 1234);

                        if (s_IsTestMode)
                        {
                            theEvent.EventSignature.Offset = offset--;   // Make these go backwards.
                        }
                        else
                        {
                            theEvent.EventSignature.Offset = rand.Next(0, 0xfffff);
                        }

                        theEvent.EventSignature.Parameters = new StackHashParameterCollection();
                        theEvent.EventSignature.Parameters.Add(new StackHashParameter("applicationName", theEvent.EventSignature.ApplicationName));
                        theEvent.EventSignature.Parameters.Add(new StackHashParameter("applicationTimeStamp", theEvent.EventSignature.ApplicationTimeStamp.ToString(CultureInfo.InvariantCulture)));
                        theEvent.EventSignature.Parameters.Add(new StackHashParameter("applicationVersion", theEvent.EventSignature.ApplicationVersion));
                        theEvent.EventSignature.Parameters.Add(new StackHashParameter("exceptionCode", String.Format(CultureInfo.InvariantCulture, "{0:X}", theEvent.EventSignature.ExceptionCode)));
                        theEvent.EventSignature.Parameters.Add(new StackHashParameter("moduleName", theEvent.EventSignature.ModuleName));
                        theEvent.EventSignature.Parameters.Add(new StackHashParameter("moduleTimeStamp", theEvent.EventSignature.ModuleTimeStamp.ToString(CultureInfo.InvariantCulture)));
                        theEvent.EventSignature.Parameters.Add(new StackHashParameter("moduleVersion", theEvent.EventSignature.ModuleVersion.ToString()));
                        theEvent.EventSignature.Parameters.Add(new StackHashParameter("offset", String.Format(CultureInfo.InvariantCulture, "{0:X}", theEvent.EventSignature.Offset)));
                        theEvent.EventSignature.InterpretParameters();
                        index.AddEvent(product, file, theEvent);

                        // Allow for some duplicate event ids.
                        if (!s_IsTestMode)
                        {
                            eventId++;
                            //if (rand.Next(0, 100) > 5)
                            //    eventId++;

                            //if (rand.Next(0, 100) > 50)
                            //    eventId = -1 * eventId;
                        }
                        else
                        {
                            eventId++;
                        }

                        hitsRand = new Random(k);
                        StackHashEventInfoCollection eventInfoCollection = new StackHashEventInfoCollection();
                        for (int l = 0; l < testData.NumberOfEventInfos; l++)
                        {
                            int languageIndex = rand.Next() % s_Languages.Length;

                            if (s_IsTestMode)
                            {
                                languageIndex = l % s_Languages.Length;
                            }

                            StackHashEventInfo eventInfo = new StackHashEventInfo();
                            eventInfo.DateCreatedLocal  = now.ToUniversalTime().AddDays(-1 * (rand.Next() % 180)).RoundToPreviousMinute();
                            eventInfo.DateModifiedLocal = eventInfo.DateCreatedLocal.AddDays(l).RoundToPreviousMinute();

                            if (s_IsTestMode)
                            {
                                eventInfo.HitDateLocal = now.ToUniversalTime().AddDays(-1 * l).RoundToPreviousMinute();
                            }
                            else
                            {
                                eventInfo.HitDateLocal = now.ToUniversalTime().AddDays(rand.Next(-180, 0)).RoundToPreviousMinute();
                            }

                            eventInfo.Language = s_Languages[languageIndex].Name;
                            eventInfo.Lcid     = s_Languages[languageIndex].Lcid;
                            eventInfo.Locale   = s_Languages[languageIndex].LocaleCode;


                            int osIndex = rand.Next(0, s_OperatingSystems.Length);
                            if (s_IsTestMode)
                            {
                                osIndex = l % s_OperatingSystems.Length;
                            }
                            eventInfo.OperatingSystemName = s_OperatingSystems[osIndex];

                            if (s_IsTestMode)
                            {
                                eventInfo.OperatingSystemVersion = s_OperatingSystemVersions[osIndex] + l.ToString(CultureInfo.InvariantCulture);
                            }
                            else
                            {
                                eventInfo.OperatingSystemVersion = s_OperatingSystemVersions[osIndex];
                            }

                            if (s_IsTestMode)
                            {
                                eventInfo.TotalHits = l + k;
                            }
                            else
                            {
                                eventInfo.TotalHits = hitsRand.Next(0, 50);
                            }

                            if (eventInfoCollection.FindEventInfo(eventInfo) == null)
                            {
                                eventInfoCollection.Add(eventInfo);
                            }
                        }

                        index.MergeEventInfoCollection(product, file, theEvent, eventInfoCollection);

                        for (int m = 0; m < testData.NumberOfCabs; m++)
                        {
                            if ((k % testData.EventsToAssignCabs) != 0)
                            {
                                break;
                            }

                            StackHashCab cab = new StackHashCab();

                            if (IsTestMode)
                            {
                                cab.DateCreatedLocal  = now.ToUniversalTime().AddDays(-1 * m).RoundToPreviousMinute();
                                cab.DateModifiedLocal = cab.DateCreatedLocal.AddDays(m).RoundToPreviousMinute();
                            }
                            else
                            {
                                cab.DateCreatedLocal  = now.ToUniversalTime().AddDays(-1 * (rand.Next() % 180)).RoundToPreviousMinute();
                                cab.DateModifiedLocal = cab.DateCreatedLocal.AddDays(m).RoundToPreviousMinute();
                            }
                            cab.EventId       = theEvent.Id;
                            cab.EventTypeName = theEvent.EventTypeName;
                            cab.FileName      = String.Format(CultureInfo.InvariantCulture, "{0}-{1}-{2}.cab", cab.EventId, cab.EventTypeName, cab.Id);
                            cab.Id            = cabId++;
                            cab.SizeInBytes   = 64123 + rand.Next(-4000, 4000); // Some random value - corrected later if a real file exists.
                            cab.CabDownloaded = true;

                            // Get the size of the cab file.
                            String sourceCabFile;
                            if (testData.CabFileName != null)
                            {
                                sourceCabFile = Path.Combine(TestSettings.TestDataFolder + @"Cabs\", testData.CabFileName);
                            }
                            else if (testData.UseLargeCab)
                            {
                                sourceCabFile = TestSettings.TestDataFolder + @"Cabs\1630796338-Crash32bit-0760025228.cab";
                            }
                            else
                            {
                                sourceCabFile = TestSettings.TestDataFolder + @"Cabs\1641909485-Crash32bit-0773522646.cab";
                            }

                            if (File.Exists(sourceCabFile))
                            {
                                FileInfo sourceCabFileInfo = new FileInfo(sourceCabFile);
                                cab.SizeInBytes = sourceCabFileInfo.Length;
                            }

                            index.AddCab(product, file, theEvent, cab, false);


                            // Copy in a test cab file.

                            String cabFolder = index.GetCabFolder(product, file, theEvent, cab);
                            if (!Directory.Exists(cabFolder))
                            {
                                Directory.CreateDirectory(cabFolder);
                            }
                            String cabFileName = index.GetCabFileName(product, file, theEvent, cab);

                            if (!File.Exists(cabFileName))
                            {
                                if (testData.CabFileName != null)
                                {
                                    File.Copy(Path.Combine(TestSettings.TestDataFolder + @"Cabs\", testData.CabFileName), cabFileName);
                                }
                                else if (testData.UseLargeCab)
                                {
                                    File.Copy(TestSettings.TestDataFolder + @"Cabs\1630796338-Crash32bit-0760025228.cab", cabFileName);
                                }
                                else
                                {
                                    File.Copy(TestSettings.TestDataFolder + @"Cabs\1641909485-Crash32bit-0773522646.cab", cabFileName);
                                }
                            }


                            // Make sure the file is not read only.
                            FileAttributes attributes = File.GetAttributes(cabFileName);
                            attributes &= ~FileAttributes.ReadOnly;
                            File.SetAttributes(cabFileName, attributes);


                            // Unwrap the cab.
                            if (testData.UnwrapCabs)
                            {
                                Cabs.ExtractCab(cabFileName, cabFolder);
                            }

                            if (testData.NumberOfScriptResults > 0)
                            {
                                String analysisFolder = index.GetCabFolder(product, file, theEvent, cab) + "\\analysis";

                                if (!Directory.Exists(analysisFolder))
                                {
                                    Directory.CreateDirectory(analysisFolder);
                                }

                                for (int scriptCount = 0; scriptCount < testData.NumberOfScriptResults; scriptCount++)
                                {
                                    CreateScriptFile(scriptCount, cab.Id, analysisFolder, scriptRand.Next(5), CultureInfo.InvariantCulture, 2, testData.ScriptFileSize);
                                }
                            }


                            StackHashNotes cabNotes = index.GetCabNotes(product, file, theEvent, cab);
                            for (int q = 0; q < testData.NumberOfCabNotes; q++)
                            {
                                StackHashNoteEntry note = new StackHashNoteEntry(now.ToUniversalTime().RoundToPreviousSecond(), "User", "MarkJ", "This is a cab note" + q.ToString(CultureInfo.InvariantCulture));

                                // Don't add duplicate cab notes in the SQL index. The XML index may contain duplicates.
                                if (index.IndexType == ErrorIndexType.Xml || !cabNotes.ContainsNote(note))
                                {
                                    index.AddCabNote(product, file, theEvent, cab, note);
                                }
                            }
                        }

                        StackHashNotes eventNotes = index.GetEventNotes(product, file, theEvent);
                        for (int p = 0; p < testData.NumberOfEventNotes; p++)
                        {
                            StackHashNoteEntry note = new StackHashNoteEntry(now.ToUniversalTime().RoundToPreviousSecond(), "User", "MarkJ", "This is an event note" + p.ToString(CultureInfo.InvariantCulture));

                            // Don't add duplicate event notes in the SQL index. The XML index may contain duplicates.
                            if (index.IndexType == ErrorIndexType.Xml || !eventNotes.ContainsNote(note))
                            {
                                index.AddEventNote(product, file, theEvent, note);
                            }
                        }
                    }
                }
                index.UpdateProductStatistics(product);
            }
        }