Search in sources :

Example 1 with ImmutableUserAgent

use of nl.basjes.parse.useragent.UserAgent.ImmutableUserAgent in project yauaa by nielsbasjes.

the class TestUseragent method comparingUserAgents.

@Test
void comparingUserAgents() {
    MutableUserAgent baseAgent = new MutableUserAgent("Something 2");
    MutableUserAgent mutableAgent0 = new MutableUserAgent("Something 2");
    MutableUserAgent mutableAgent1 = new MutableUserAgent("Something 1");
    MutableUserAgent mutableAgent2 = new MutableUserAgent("Something 2");
    MutableUserAgent mutableAgent3 = new MutableUserAgent("Something 2");
    MutableUserAgent mutableAgent4 = new MutableUserAgent("Something 2");
    MutableAgentField field0 = new MutableAgentField("Foo");
    field0.setValue("One", 1);
    MutableAgentField field1 = new MutableAgentField("Foo");
    field1.setValue("One", 1);
    // Same, different value
    MutableAgentField field2 = new MutableAgentField("Foo");
    field2.setValue("Two", 1);
    // Same, different confidence
    MutableAgentField field3 = new MutableAgentField("Foo");
    field3.setValue("One", 2);
    // Same, different default
    MutableAgentField field4 = new MutableAgentField(null);
    field4.setValue("One", 1);
    // We compare the base agent with 4 variations
    baseAgent.setImmediateForTesting("Field", field0);
    // Same
    mutableAgent0.setImmediateForTesting("Field", field1);
    // Different useragent
    mutableAgent1.setImmediateForTesting("Field", field1);
    // Different field value
    mutableAgent2.setImmediateForTesting("Field", field2);
    // Different field confidence
    mutableAgent3.setImmediateForTesting("Field", field3);
    // Different field default value
    mutableAgent4.setImmediateForTesting("Field", field4);
    // Check em
    assertEquals(baseAgent, baseAgent);
    assertEquals(baseAgent, mutableAgent0);
    assertEquals(mutableAgent0, baseAgent);
    assertEquals(baseAgent.hashCode(), mutableAgent0.hashCode());
    assertNotEquals(baseAgent, mutableAgent2);
    assertNotEquals(baseAgent, mutableAgent3);
    assertNotEquals(baseAgent, mutableAgent4);
    assertNotEquals("String", mutableAgent1);
    assertNotEquals(mutableAgent1, "String");
    UserAgent immutableAgent0 = new ImmutableUserAgent(mutableAgent0);
    UserAgent immutableAgent1 = new ImmutableUserAgent(mutableAgent1);
    UserAgent immutableAgent2 = new ImmutableUserAgent(mutableAgent2);
    UserAgent immutableAgent3 = new ImmutableUserAgent(mutableAgent3);
    UserAgent immutableAgent4 = new ImmutableUserAgent(mutableAgent4);
    assertEquals(mutableAgent0.hashCode(), immutableAgent0.hashCode());
    assertEquals(mutableAgent1.hashCode(), immutableAgent1.hashCode());
    assertEquals(mutableAgent2.hashCode(), immutableAgent2.hashCode());
    assertEquals(mutableAgent3.hashCode(), immutableAgent3.hashCode());
    assertEquals(mutableAgent4.hashCode(), immutableAgent4.hashCode());
    assertEquals(mutableAgent0, immutableAgent0);
    assertEquals(mutableAgent1, immutableAgent1);
    assertEquals(mutableAgent2, immutableAgent2);
    assertEquals(mutableAgent3, immutableAgent3);
    assertEquals(mutableAgent4, immutableAgent4);
    assertEquals(immutableAgent0, mutableAgent0);
    assertEquals(immutableAgent1, mutableAgent1);
    assertEquals(immutableAgent2, mutableAgent2);
    assertEquals(immutableAgent3, mutableAgent3);
    assertEquals(immutableAgent4, mutableAgent4);
    assertEquals(mutableAgent0.toString(), immutableAgent0.toString());
    assertEquals(mutableAgent1.toString(), immutableAgent1.toString());
    assertEquals(mutableAgent2.toString(), immutableAgent2.toString());
    assertEquals(mutableAgent3.toString(), immutableAgent3.toString());
    assertEquals(mutableAgent4.toString(), immutableAgent4.toString());
    assertEquals(mutableAgent0.toXML(), immutableAgent0.toXML());
    assertEquals(mutableAgent1.toXML(), immutableAgent1.toXML());
    assertEquals(mutableAgent2.toXML(), immutableAgent2.toXML());
    assertEquals(mutableAgent3.toXML(), immutableAgent3.toXML());
    assertEquals(mutableAgent4.toXML(), immutableAgent4.toXML());
    assertEquals(mutableAgent0.toYamlTestCase(), immutableAgent0.toYamlTestCase());
    assertEquals(mutableAgent1.toYamlTestCase(), immutableAgent1.toYamlTestCase());
    assertEquals(mutableAgent2.toYamlTestCase(), immutableAgent2.toYamlTestCase());
    assertEquals(mutableAgent3.toYamlTestCase(), immutableAgent3.toYamlTestCase());
    assertEquals(mutableAgent4.toYamlTestCase(), immutableAgent4.toYamlTestCase());
}
Also used : MutableUserAgent(nl.basjes.parse.useragent.UserAgent.MutableUserAgent) ImmutableUserAgent(nl.basjes.parse.useragent.UserAgent.ImmutableUserAgent) MutableAgentField(nl.basjes.parse.useragent.AgentField.MutableAgentField) MutableUserAgent(nl.basjes.parse.useragent.UserAgent.MutableUserAgent) ImmutableUserAgent(nl.basjes.parse.useragent.UserAgent.ImmutableUserAgent) Test(org.junit.jupiter.api.Test)

Example 2 with ImmutableUserAgent

use of nl.basjes.parse.useragent.UserAgent.ImmutableUserAgent in project yauaa by nielsbasjes.

the class TestUseragent method fullToString.

@Test
void fullToString() {
    MutableUserAgent userAgent1 = new MutableUserAgent("Some'Agent");
    ((MutableAgentField) userAgent1.get("Niels")).setValue("Basjes", 42);
    ((MutableAgentField) userAgent1.get("BackToDefault")).setValue("One", 42);
    assertEquals("One", userAgent1.getValue("BackToDefault"));
    assertEquals(42, userAgent1.getConfidence("BackToDefault"));
    ((MutableAgentField) userAgent1.get("BackToDefault")).setValue("<<<null>>>", 84);
    assertTrue(userAgent1.get("BackToDefault").isDefaultValue());
    assertEquals("Unknown", userAgent1.getValue("BackToDefault"));
    assertEquals(84, userAgent1.getConfidence("BackToDefault"));
    assertEquals("Basjes", userAgent1.getValue("Niels"));
    assertEquals(42, userAgent1.getConfidence("Niels"));
    UserAgent userAgent2 = new ImmutableUserAgent(userAgent1);
    assertEquals("Basjes", userAgent2.getValue("Niels"));
    assertEquals(42, userAgent2.getConfidence("Niels"));
    // Also values that are "Default" are copied.
    assertTrue(userAgent2.get("BackToDefault").isDefaultValue());
    assertEquals("Unknown", userAgent2.getValue("BackToDefault"));
    // You may have a default value ("<<<null>>>") with a confidence.
    assertEquals(84, userAgent2.getConfidence("BackToDefault"));
    assertEquals(// You get the fields in the order you ask them!
    "  - user_agent_string  : 'Some''Agent'\n" + "    Niels              : 'Basjes'\n" + "    DeviceClass        : 'Unknown'\n", userAgent1.toString("Niels", "DeviceClass"));
    assertEquals("  - user_agent_string                : 'Some''Agent'\n" + "    DeviceClass                      : 'Unknown'\n" + "    DeviceName                       : 'Unknown'\n" + "    DeviceBrand                      : 'Unknown'\n" + "    OperatingSystemClass             : 'Unknown'\n" + "    OperatingSystemName              : 'Unknown'\n" + "    OperatingSystemVersion           : '??'\n" + "    OperatingSystemVersionMajor      : '??'\n" + "    OperatingSystemNameVersion       : 'Unknown ??'\n" + "    OperatingSystemNameVersionMajor  : 'Unknown ??'\n" + "    LayoutEngineClass                : 'Unknown'\n" + "    LayoutEngineName                 : 'Unknown'\n" + "    LayoutEngineVersion              : '??'\n" + "    LayoutEngineVersionMajor         : '??'\n" + "    LayoutEngineNameVersion          : 'Unknown ??'\n" + "    LayoutEngineNameVersionMajor     : 'Unknown ??'\n" + "    AgentClass                       : 'Unknown'\n" + "    AgentName                        : 'Unknown'\n" + "    AgentVersion                     : '??'\n" + "    AgentVersionMajor                : '??'\n" + "    AgentNameVersion                 : 'Unknown ??'\n" + "    AgentNameVersionMajor            : 'Unknown ??'\n" + "    BackToDefault                    : 'Unknown'\n" + "    Niels                            : 'Basjes'\n", userAgent1.toString());
    assertEquals(userAgent1.getAvailableFieldNamesSorted(), userAgent2.getAvailableFieldNamesSorted());
    assertEquals(userAgent1.toString(), userAgent2.toString());
    assertEquals(userAgent1.toXML(), userAgent2.toXML());
    assertEquals(userAgent1.toYamlTestCase(), userAgent2.toYamlTestCase());
}
Also used : MutableUserAgent(nl.basjes.parse.useragent.UserAgent.MutableUserAgent) ImmutableUserAgent(nl.basjes.parse.useragent.UserAgent.ImmutableUserAgent) MutableAgentField(nl.basjes.parse.useragent.AgentField.MutableAgentField) MutableUserAgent(nl.basjes.parse.useragent.UserAgent.MutableUserAgent) ImmutableUserAgent(nl.basjes.parse.useragent.UserAgent.ImmutableUserAgent) Test(org.junit.jupiter.api.Test)

Example 3 with ImmutableUserAgent

use of nl.basjes.parse.useragent.UserAgent.ImmutableUserAgent in project yauaa by nielsbasjes.

the class TestDeveloperTools method validateStringOutputsAndMatches.

@Test
void validateStringOutputsAndMatches() {
    UserAgentAnalyzerTester uaa = UserAgentAnalyzerTester.newBuilder().withField("DeviceName").build();
    MutableUserAgent useragent = new MutableUserAgent("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");
    ImmutableUserAgent parseResult = uaa.parse(useragent);
    assertTrue(parseResult.toString().contains("'Google Nexus 6'"));
    assertTrue(parseResult.toJson().contains("\"DeviceName\":\"Google Nexus 6\""));
    assertTrue(parseResult.toJson("DeviceName").contains("\"DeviceName\":\"Google Nexus 6\""));
    assertFalse(parseResult.toJson("DeviceClass").contains("\"DeviceName\":\"Google Nexus 6\""));
    assertTrue(parseResult.toXML().contains("<DeviceName>Google Nexus 6</DeviceName>"));
    assertTrue(parseResult.toXML("DeviceName").contains("<DeviceName>Google Nexus 6</DeviceName>"));
    assertFalse(parseResult.toXML("DeviceClass").contains("<DeviceName>Google Nexus 6</DeviceName>"));
    assertEquals("Google Nexus 6", parseResult.toMap().get("DeviceName"));
    assertEquals("Google Nexus 6", parseResult.toMap("DeviceName").get("DeviceName"));
    assertNull(parseResult.toMap("DeviceClass").get("DeviceName"));
    assertTrue(parseResult.toYamlTestCase(true).contains("'Google Nexus 6'"));
    boolean ok = false;
    for (Match match : uaa.getMatches()) {
        if ("agent.(1)product.(1)comments.(3)entry[3-3]".equals(match.getKey())) {
            assertEquals("Build", match.getValue());
            ok = true;
            break;
        }
    }
    assertTrue(ok, "Did not see the expected match.");
    ok = false;
    for (Match match : uaa.getUsedMatches(useragent)) {
        if ("agent.(1)product.(1)comments.(3)entry[3-3]".equals(match.getKey())) {
            assertEquals("Build", match.getValue());
            ok = true;
            break;
        }
    }
    assertTrue(ok, "Did not see the expected match.");
}
Also used : MutableUserAgent(nl.basjes.parse.useragent.UserAgent.MutableUserAgent) UserAgentAnalyzerTester(nl.basjes.parse.useragent.debug.UserAgentAnalyzerTester) ImmutableUserAgent(nl.basjes.parse.useragent.UserAgent.ImmutableUserAgent) Match(nl.basjes.parse.useragent.analyze.MatchesList.Match) Test(org.junit.jupiter.api.Test)

Example 4 with ImmutableUserAgent

use of nl.basjes.parse.useragent.UserAgent.ImmutableUserAgent in project yauaa by nielsbasjes.

the class TestCaching method testCustomCacheImplementationInline.

@Test
void testCustomCacheImplementationInline() {
    String userAgent = "Mozilla/5.0 (Linux; Android 8.1.0; Pixel Build/OPM4.171019.021.D1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.98 Mobile Safari/537.36";
    UserAgentAnalyzer uaa = UserAgentAnalyzer.newBuilder().withCacheInstantiator(new CacheInstantiator() {

        @Override
        public Map<String, ImmutableUserAgent> instantiateCache(int cacheSize) {
            // The Map MUST be synchronized
            return Collections.synchronizedMap(// This is ONLY for this test.
            new LRUMap<String, ImmutableUserAgent>(cacheSize) {

                @Override
                public ImmutableUserAgent get(Object key) {
                    LOG.info("Did a GET on {}", key);
                    return super.get(key);
                }

                @Override
                public ImmutableUserAgent put(String key, ImmutableUserAgent value) {
                    LOG.info("Did a PUT on {}", key);
                    return super.put(key, value);
                }
            });
        }
    }).withCache(10).hideMatcherLoadStats().build();
    // First time
    UserAgent agent1 = uaa.parse(userAgent);
    // Should come from cache
    UserAgent agent2 = uaa.parse(userAgent);
    // Both should be the same
    assertEquals(agent1, agent2);
}
Also used : CacheInstantiator(nl.basjes.parse.useragent.AbstractUserAgentAnalyzer.CacheInstantiator) ImmutableUserAgent(nl.basjes.parse.useragent.UserAgent.ImmutableUserAgent) ImmutableUserAgent(nl.basjes.parse.useragent.UserAgent.ImmutableUserAgent) Test(org.junit.jupiter.api.Test)

Example 5 with ImmutableUserAgent

use of nl.basjes.parse.useragent.UserAgent.ImmutableUserAgent in project yauaa by nielsbasjes.

the class AbstractUserAgentAnalyzerDirect method parse.

/**
 * Parses and analyzes the useragent string provided in the MutableUserAgent instance.
 * NOTE: This method is internally synchronized because the way the analyzer works is not reentrant.
 * @param userAgent The MutableUserAgent instance that is to be parsed and that gets all results
 * @return An ImmutableUserAgent copy of the results that is suitable for further usage and caching.
 */
@Nonnull
public ImmutableUserAgent parse(MutableUserAgent userAgent) {
    initializeMatchers();
    String useragentString = userAgent.getUserAgentString();
    if (useragentString != null && useragentString.length() > userAgentMaxLength) {
        setAsHacker(userAgent, 100);
        userAgent.setForced(HACKER_ATTACK_VECTOR, "Buffer overflow", 100);
        return new ImmutableUserAgent(hardCodedPostProcessing(userAgent));
    }
    synchronized (this) {
        // Reset all Matchers
        reset();
        if (userAgent.isDebug()) {
            for (Matcher matcher : allMatchers) {
                matcher.setVerboseTemporarily(true);
            }
        }
        try {
            userAgent = flattener.parse(userAgent);
            inform(SYNTAX_ERROR, userAgent.getValue(SYNTAX_ERROR), null);
            if (verbose) {
                LOG.info("=========== Checking all Touched Matchers: {}", touchedMatchers.size());
            }
            // Fire all Analyzers with any input
            for (Matcher matcher : touchedMatchers) {
                matcher.analyze(userAgent);
            }
            if (verbose) {
                LOG.info("=========== Checking all Zero Input Matchers: {}", zeroInputMatchers.size());
            }
            // Fire all Analyzers that should not get input
            for (Matcher matcher : zeroInputMatchers) {
                matcher.analyze(userAgent);
            }
            userAgent.processSetAll();
        } catch (RuntimeException rte) {
            // If this occurs then someone has found a previously undetected problem.
            // So this is a safety for something that 'can' but 'should not' occur.
            userAgent.reset();
            setAsHacker(userAgent, 10000);
            userAgent.setForced(HACKER_ATTACK_VECTOR, "Yauaa Exploit", 10000);
        }
    }
    return new ImmutableUserAgent(hardCodedPostProcessing(userAgent));
}
Also used : ImmutableUserAgent(nl.basjes.parse.useragent.UserAgent.ImmutableUserAgent) Matcher(nl.basjes.parse.useragent.analyze.Matcher) Nonnull(javax.annotation.Nonnull)

Aggregations

ImmutableUserAgent (nl.basjes.parse.useragent.UserAgent.ImmutableUserAgent)5 Test (org.junit.jupiter.api.Test)4 MutableUserAgent (nl.basjes.parse.useragent.UserAgent.MutableUserAgent)3 MutableAgentField (nl.basjes.parse.useragent.AgentField.MutableAgentField)2 Nonnull (javax.annotation.Nonnull)1 CacheInstantiator (nl.basjes.parse.useragent.AbstractUserAgentAnalyzer.CacheInstantiator)1 Matcher (nl.basjes.parse.useragent.analyze.Matcher)1 Match (nl.basjes.parse.useragent.analyze.MatchesList.Match)1 UserAgentAnalyzerTester (nl.basjes.parse.useragent.debug.UserAgentAnalyzerTester)1