public void ValidateStringOutputsAndMatches()
        {
            var uaa       = UserAgentAnalyzerTester.NewBuilder().WithField("DeviceName").Build() as UserAgentAnalyzerTester;
            var useragent = uaa.Parse("Mozilla/5.0 (Linux; Android 7.0; Nexus 6 Build/NBD90Z) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.124 Mobile Safari/537.36");

            useragent.ToString().Contains("'Google Nexus 6'").Should().BeTrue();
            useragent.ToJson().Contains("\"DeviceName\":\"Google Nexus 6\"").Should().BeTrue();
            useragent.ToYamlTestCase(true).Contains("'Google Nexus 6'").Should().BeTrue();

            var ok = false;

            foreach (var match in uaa.GetMatches())
            {
                if ("agent.(1)product.(1)comments.(3)entry[3-3]".Equals(match.Key))
                {
                    match.Value.Should().Be("Build");
                    ok = true;
                    break;
                }
            }
            ok.Should().BeTrue("Did not see the expected match.");

            ok = false;
            foreach (var match in uaa.GetUsedMatches(useragent))
            {
                if ("agent.(1)product.(1)comments.(3)entry[3-3]".Equals(match.Key))
                {
                    match.Value.Should().Be("Build");
                    ok = true;
                    break;
                }
            }
            ok.Should().BeTrue("Did not see the expected match.");
        }
示例#2
0
        public UserAgentAnalyzerTester SerializeAndDeserializeUAA()
        {
            var uaa = UserAgentAnalyzerTester.NewBuilder()
                      .HideMatcherLoadStats()
                      .DropDefaultResources()
                      .AddResources("YamlResources/UserAgents", "GoogleChrome.yaml")
                      .WithFields(this.singleFieldList)
                      .ImmediateInitialization()
                      .Build();

            byte[] bytes;

            using (var memoryStream = new MemoryStream())
            {
                var formatter = new BinaryFormatter();
                formatter.Serialize(memoryStream, uaa);
                bytes = memoryStream.ToArray();
            }
            using (var memoryStream = new MemoryStream(bytes))
            {
                var formatter = new BinaryFormatter();
                var obj       = formatter.Deserialize(memoryStream);
                obj.Should().BeOfType <UserAgentAnalyzerTester>();
                uaa = obj as UserAgentAnalyzerTester;
            }

            return(uaa as UserAgentAnalyzerTester);
        }
 private UserAgentAnalyzerTester CreateTester(string filename)
 {
     return(UserAgentAnalyzerTester
            .NewBuilder()
            .DropDefaultResources()
            .KeepTests()
            .AddResources(Config.RESOURCES_PATH, filename).Build() as UserAgentAnalyzerTester);
 }
        public void RunDocumentationExample()
        {
            var uaa = UserAgentAnalyzerTester
                      .NewBuilder()
                      .DropDefaultResources()
                      .AddResources(Config.RESOURCES_PATH, "DocumentationExample.yaml")
                      .Build() as UserAgentAnalyzerTester;

            uaa.RunTests(false, true).Should().BeTrue();
        }
示例#5
0
        /// <summary>
        /// The SerializeAndDeserializeUAA
        /// </summary>
        /// <param name="delay">The delay<see cref="bool"/></param>
        /// <returns>The <see cref="UserAgentAnalyzerTester"/></returns>
        public UserAgentAnalyzerTester SerializeAndDeserializeUAA(bool delay)
        {
            LOG.Info("==============================================================");
            LOG.Info("Create");
            LOG.Info("--------------------------------------------------------------");
            var uaab = UserAgentAnalyzerTester
                       .NewBuilder()
                       .KeepTests()
                       .DropDefaultResources()
                       .AddResources(Config.RESOURCES_PATH, "AllSteps.yaml")
                       .AddResources(Config.RESOURCES_PATH, "AllFields-tests.yaml")
                       .AddResources(Config.RESOURCES_PATH, "AllPossibleSteps.yaml")
                       .AddResources(Config.RESOURCES_PATH, "DocumentationExample.yaml")
                       .HideMatcherLoadStats();

            if (delay)
            {
                uaab.DelayInitialization();
            }
            else
            {
                uaab.ImmediateInitialization();
            }

            var uaa = uaab.Build() as UserAgentAnalyzerTester;

            LOG.Info("--------------------------------------------------------------");
            LOG.Info("Serialize");
            byte[] bytes;

            using (var memoryStream = new MemoryStream())
            {
                var formatter = new BinaryFormatter();
                formatter.Serialize(memoryStream, uaa);
                bytes = memoryStream.ToArray();
            }

            LOG.Info(string.Format("The UserAgentAnalyzer was serialized into {0} bytes", bytes.LongLength));
            LOG.Info("--------------------------------------------------------------");
            LOG.Info("Deserialize");

            using (var memoryStream = new MemoryStream(bytes))
            {
                var formatter = new BinaryFormatter();
                var obj       = formatter.Deserialize(memoryStream);
                obj.Should().BeOfType <UserAgentAnalyzerTester>();
                uaa = obj as UserAgentAnalyzerTester;
            }

            LOG.Info("Done");
            LOG.Info("==============================================================");

            return(uaa);
        }
示例#6
0
        public void ValidateAllPredefinedBrowsersPerformance()
        {
            Skip.If(!Config.ENABLE_PROFILING);
            UserAgentAnalyzerTester uaa =
                UserAgentAnalyzerTester.NewBuilder()
                .ShowMatcherLoadStats()
                .ImmediateInitialization()
                .Build() as UserAgentAnalyzerTester;

            uaa.RunTests(false, false, null, true, true).Should().BeTrue();
        }
示例#7
0
        public void ValidateAllPredefinedBrowsers()
        {
            UserAgentAnalyzerTester uaa;

            uaa = UserAgentAnalyzerTester
                  .NewBuilder()
                  .ImmediateInitialization()
                  .Build() as UserAgentAnalyzerTester;
            LOG.Info("==============================================================");
            LOG.Info("Validating when getting all fields");
            LOG.Info("--------------------------------------------------------------");
            uaa.RunTests(false, true, null, false, true).Should().BeTrue();
        }
        /// <summary>
        /// This method is an helper that runs the tests passed in yaml definitions and check if they throw an <see cref="InvalidParserConfigurationException"/> with the expected message.
        /// </summary>
        /// <param name="directory">The directory where yaml definitions are contained</param>
        /// <param name="file">The name of file you want load</param>
        /// <param name="expectedMessage">The expected message thwrown by the exception<see cref="string"/></param>
        private void RunTest(string directory, string file, string expectedMessage)
        {
            var a = new Action(() =>
            {
                var uaa = UserAgentAnalyzerTester.NewBuilder()
                          .DropDefaultResources()
                          .KeepTests()
                          .AddResources(directory, file)
                          .Build() as UserAgentAnalyzerTester;
                uaa.RunTests(false, false);
            });

            a.Should().Throw <InvalidParserConfigurationException>().Where(e => e.Message.Contains(expectedMessage));
        }
        public void ValidateNewTestcaseSituationOutput()
        {
            var uaa = UserAgentAnalyzerTester
                      .NewBuilder()
                      .DelayInitialization()
                      .HideMatcherLoadStats()
                      .DropTests()
                      .Build() as UserAgentAnalyzerTester;

            uaa.SetShowMatcherStats(true);
            uaa.KeepTests();
            uaa.LoadResources(Config.RESOURCES_PATH, "CheckNewTestcaseOutput.yaml");
            uaa.RunTests(false, true).Should().BeTrue();
        }
        public void ValidateErrorSituationOutput()
        {
            var uaa = UserAgentAnalyzerTester
                      .NewBuilder()
                      .HideMatcherLoadStats()
                      .DelayInitialization()
                      .DropTests()
                      .Build() as UserAgentAnalyzerTester;

            uaa.SetShowMatcherStats(true);
            uaa.KeepTests();
            uaa.LoadResources(Config.RESOURCES_PATH, "CheckErrorOutput.yaml");
            uaa.RunTests(false, true).Should().BeFalse();  // This test must return an error state
        }
示例#11
0
        /// <summary>
        /// The ValidateAllPredefinedBrowsersMultipleFields
        /// </summary>
        /// <param name="fields">The fields<see cref="ICollection{string}"/></param>
        private void ValidateAllPredefinedBrowsersMultipleFields(ICollection <string> fields)
        {
            LOG.Info("==============================================================");
            LOG.Info(string.Format("Validating when ONLY asking for {0}", fields.ToString()));
            LOG.Info("--------------------------------------------------------------");
            UserAgentAnalyzerTester userAgentAnalyzer =
                UserAgentAnalyzerTester
                .NewBuilder()
                .WithoutCache()
                .WithFields(fields)
                .HideMatcherLoadStats()
                .Build() as UserAgentAnalyzerTester;

            userAgentAnalyzer.Should().NotBeNull();
            userAgentAnalyzer.RunTests(false, true, fields, false, false).Should().BeTrue();
        }
示例#12
0
        private static IList <string> GetFields(Options commandlineOptions, UserAgentAnalyzerTester uaa)
        {
            IList <string> fields;

            if (commandlineOptions.Fields == null || !commandlineOptions.Fields.Any())
            {
                fields = uaa.GetAllPossibleFieldNamesSorted();
                fields.Add(DefaultUserAgentFields.USERAGENT_FIELDNAME);
            }
            else
            {
                fields = commandlineOptions.Fields;
            }

            return(fields);
        }
示例#13
0
        public void MakeSureWeDoNotHaveDuplicateTests()
        {
            UserAgentAnalyzerTester uaa = UserAgentAnalyzerTester.NewBuilder().Build() as UserAgentAnalyzerTester;

            IDictionary <string, IList <string> > allTestInputs = new Dictionary <string, IList <string> >(2000);
            HashSet <string> duplicates = new HashSet <string>();

            foreach (IDictionary <string, IDictionary <string, string> > testCase in uaa.TestCases)
            {
                string         input    = testCase["input"]["user_agent_string"];
                string         location = testCase["metaData"]["filename"] + ":" + testCase["metaData"]["fileline"];
                IList <string> locations;
                if (allTestInputs.ContainsKey(input))
                {
                    locations = allTestInputs[input];
                }
                else
                {
                    locations = new List <string>();
                }
                locations.Add(location);

                if (locations.Count > 1)
                {
                    duplicates.Add(input);
                }
                allTestInputs[input] = locations;
            }

            duplicates.Count.Should().Be(0);
            if (duplicates.Count > 0)
            {
                StringBuilder sb = new StringBuilder(1024);
                foreach (string duplicate in duplicates)
                {
                    sb.Append("======================================================\n")
                    .Append("Testcase > ").Append(duplicate).Append("\n");
                    int count = 0;
                    foreach (string location in allTestInputs[duplicate])
                    {
                        sb.Append(">Location ").Append(++count).Append(".(").Append(location).Append(")\n");
                    }
                }
                LOG.Info("Found " + duplicates.Count + " testcases multiple times: \n" + sb.ToString());
            }
        }
示例#14
0
        private static UserAgentAnalyzerTester CreateAndConfigureParser(Options commandlineOptions)
        {
            var builder = UserAgentAnalyzerTester.NewBuilder();

            builder.HideMatcherLoadStats();
            builder.WithCache(commandlineOptions.CacheSize);
            if (commandlineOptions.Fields != null)
            {
                foreach (var field in commandlineOptions.Fields)
                {
                    builder.WithField(field);
                }
            }
            var uaa = builder.Build() as UserAgentAnalyzerTester;

            return(uaa);
        }
        public void ValidateAllPredefinedBrowsersForField(string fieldName)
        {
            ICollection <string> singleFieldList = new string[] { fieldName };

            LOG.Info("==============================================================");
            LOG.Info(string.Format("Validating when ONLY asking for {0}", fieldName));
            LOG.Info("--------------------------------------------------------------");
            UserAgentAnalyzerTester userAgentAnalyzer =
                UserAgentAnalyzerTester
                .NewBuilder()
                .WithoutCache()
                .WithField(fieldName)
                .HideMatcherLoadStats()
                .Build() as UserAgentAnalyzerTester;

            userAgentAnalyzer.Should().NotBeNull();
            userAgentAnalyzer.RunTests(false, true, singleFieldList, false, false).Should().BeTrue();
        }
示例#16
0
        //[Fact]
        public void TestError()
        {
            var fieldName         = "HackerAttackVector";
            var userAgentAnalyzer =
                UserAgentAnalyzerTester
                .NewBuilder()
                .WithoutCache()
                //.WithFields(fieldName)
                .HideMatcherLoadStats()
                //.DropDefaultResources()
                //.AddResources("YamlResources/UserAgents", "Hackers.yaml")
                //.AddResources("YamlResources/UserAgents", "MobileBrands.yaml")
                .Build() as UserAgentAnalyzerTester;

            userAgentAnalyzer.Should().NotBeNull();
            //userAgentAnalyzer.RunTests(false, true, singleFieldList, false, false).Should().BeTrue();
            var userAgent = userAgentAnalyzer.Parse("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36{${sleep(20)}}");
            var field     = userAgent.Get(fieldName);

            field.GetValue().Should().Be("Unknown");
        }