package org.stagemonitor.web.servlet;

import org.junit.Test;
import org.stagemonitor.web.servlet.useragent.UserAgentParser;

import io.opentracing.Span;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;

public class UserAgentParserTest {

	private UserAgentParser userAgentParser = new UserAgentParser();

	@Test
	public void setUserAgentInformation() throws Exception {
		final Span span = mock(Span.class);
		final String userAgentHeader = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36";
		userAgentParser.setUserAgentInformation(span, userAgentHeader);
		verify(span).setTag("user_agent.type", "Browser");
		verify(span).setTag("user_agent.device", "Personal computer");
		verify(span).setTag("user_agent.os", "OS X");
		verify(span).setTag("user_agent.os_family", "OS X");
		verify(span).setTag("user_agent.os_version", "10.12.2");
		verify(span).setTag("user_agent.browser", "Chrome");
		verify(span).setTag("user_agent.browser_version", "55.0.2883.95");
	}

	@Test
	public void testPreventReDos() throws Exception {
		final Span span = mock(Span.class);
		final String userAgentHeader = "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500 OWASMIME/4.0500";
		userAgentParser.setUserAgentInformation(span, userAgentHeader);
		verify(span, never()).setTag(any(String.class), anyString());
	}

}
