using System;
using skyscraper5.Docsis;
using skyscraper5.Docsis.AnnexC;
using skyscraper5.Docsis.MacManagement;
using System.Net.NetworkInformation;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace skyscraper8.Tests.ResourceTests
{
	[TestClass]
	public class DocsisTests : Feyllure
	{
		[TestMethod]
		public void ModemCapabilitiesEncoding()
		{
			byte[] buffer = Resources1.ModemCapabilitiesEncodingTest;
			ModemCapabilitiesEncoding modemCapabilitiesEncoding = new ModemCapabilitiesEncoding(buffer);
			Assert.IsTrue(modemCapabilitiesEncoding.Valid);
		}

		[TestMethod]
		public void MultipartRegistrationResponse()
		{
			Random rng = new Random();

			byte[] sourceBuffer = new byte[6];
			rng.NextBytes(sourceBuffer);
			PhysicalAddress source = new PhysicalAddress(sourceBuffer);

			byte[] targetBuffer = new byte[6];
			rng.NextBytes(targetBuffer);
			PhysicalAddress target = new PhysicalAddress(targetBuffer);

			byte[] buffer = Resources1.MultipartRegistrationResponseTest;
			T45_V4_MultipartRegistrationResponse test = new T45_V4_MultipartRegistrationResponse(source, target, buffer);
			Assert.IsTrue(test.Valid);
		}

		[TestMethod]
		public void MacManagement_4_45()
		{
			NullDocsisEventHandler docsisEventHandler = new NullDocsisEventHandler();
			DocsisEnvironment environment = new DocsisEnvironment(docsisEventHandler);

			byte[] testPayload = Resources1.PushMacManagementMessage_Version4_Type45;

			Random rng = new Random();
			byte[] sourceBuffer = new byte[6];
			rng.NextBytes(sourceBuffer);
			PhysicalAddress source = new PhysicalAddress(sourceBuffer);

			byte[] targetBuffer = new byte[6];
			rng.NextBytes(targetBuffer);
			PhysicalAddress target = new PhysicalAddress(targetBuffer);

			environment.PushMacManagementMessage(testPayload, 4, 45, source, target, testPayload);
		}

		[TestMethod]
		public void InvalidUpstreamChannelDescriptorTest()
		{
			byte[] testPayload = Resources1.UpstreamChannelDescriptorTest;

			Random rng = new Random();
			byte[] sourceBuffer = new byte[6];
			rng.NextBytes(sourceBuffer);
			PhysicalAddress source = new PhysicalAddress(sourceBuffer);

			byte[] targetBuffer = new byte[6];
			rng.NextBytes(targetBuffer);
			PhysicalAddress target = new PhysicalAddress(targetBuffer);

			UpstreamChannelDescriptor ucd = new UpstreamChannelDescriptor(source, target, testPayload);
			Assert.IsFalse(ucd.Valid);
		}

		[TestMethod]
		public void TransmitChannelConfigurationObjectTest()
		{
			byte[] testPayload = Resources1.TransmitChannelConfigurationObject;

			CommonTlvEncodingObject.TransmitChannelConfigurationObject child = new CommonTlvEncodingObject.TransmitChannelConfigurationObject(testPayload);
			Assert.IsTrue(child.Valid);
		}

		[TestMethod]
		public void MultipartRegistrationResponse2()
		{
			Random rng = new Random();

			byte[] sourceBuffer = new byte[6];
			rng.NextBytes(sourceBuffer);
			PhysicalAddress source = new PhysicalAddress(sourceBuffer);

			byte[] targetBuffer = new byte[6];
			rng.NextBytes(targetBuffer);
			PhysicalAddress target = new PhysicalAddress(targetBuffer);

			byte[] buffer = Resources1.MultipartRegistrationResponseTest2;
			T45_V4_MultipartRegistrationResponse test = new T45_V4_MultipartRegistrationResponse(source, target, buffer);
			Assert.IsTrue(test.Valid);
		}
		
		[TestMethod]
		public void OffsetBreakerTest()
		{
			byte[] blob = Resources1.ranging_response_test;
			Random rng = new Random();
			
			byte[] sourceMacBuffer = new byte[6];
			rng.NextBytes(sourceMacBuffer);
			PhysicalAddress sourceAddress = new PhysicalAddress(sourceMacBuffer);

			byte[] targetMacBuffer = new byte[6];
			rng.NextBytes(targetMacBuffer);
			PhysicalAddress targetAddress = new PhysicalAddress(targetMacBuffer);

			RangingResponse rangingResponse = new RangingResponse(sourceAddress, targetAddress, blob);
			Assert.IsTrue(rangingResponse.Valid);
		}
	}
}
