Click here to Skip to main content
15,885,537 members
Articles / Programming Languages / C#

C# .NET DNS query component

Rate me:
Please Sign up or sign in to vote.
4.89/5 (83 votes)
25 Oct 2005CPOL9 min read 735.9K   12.3K   195  
A reusable component for performing DNS queries.
using System;
using System.Net;
using Bdev.Net.Dns;
using NUnit.Framework;

namespace Bdev.Net.Dns.NUnit
{
	/// <summary>
	/// Summary description for Class1.
	/// </summary>
	[TestFixture]
	public class Exceptions
	{
		[Test]
		[ExpectedException(typeof(ArgumentNullException))]
		public void MXLookupNullFirstParameter()
		{
			MXRecord[] records = Resolver.MXLookup(null, IPAddress.Parse("127.0.0.1"));
		}

		[Test]
		[ExpectedException(typeof(ArgumentNullException))]
		public void MXLookupNullSecondParameter()
		{
			MXRecord[] records = Resolver.MXLookup("codeproject.com", null);
		}

		[Test]
		[ExpectedException(typeof(ArgumentNullException))]
		public void MXLookupNullBothParameters()
		{
			MXRecord[] records = Resolver.MXLookup(null, null);
		}

		[Test]
		[ExpectedException(typeof(ArgumentException))]
		public void MXLookupBadDomainName()
		{
			MXRecord[] records = Resolver.MXLookup("!�$%^&*()", IPAddress.Parse("127.0.0.1"));
		}

		[Test]
		[ExpectedException(typeof(ArgumentException))]
		public void MXLookupEmptyDomainName()
		{
			MXRecord[] records = Resolver.MXLookup(string.Empty, IPAddress.Parse("127.0.0.1"));
		}


		// --------------------------   

		[Test]
		[ExpectedException(typeof(ArgumentNullException))]
		public void LookupNullFirstParameter()
		{
			Response response = Resolver.Lookup(null, IPAddress.Parse("127.0.0.1"));
		}

		[Test]
		[ExpectedException(typeof(ArgumentNullException))]
		public void LookupNullSecondParameter()
		{
			Response response = Resolver.Lookup(new Request(), null);
		}

		[Test]
		[ExpectedException(typeof(ArgumentNullException))]
		public void LookupNullBothParameters()
		{
			Response response = Resolver.Lookup(null, null);
		}

		[Test]
		[ExpectedException(typeof(ArgumentNullException))]
		public void NewQuestionDomainNull()
		{
			Question question = new Question(null, DnsType.MX, DnsClass.IN);
		}

		[Test]
		[ExpectedException(typeof(ArgumentException))]
		public void NewQuestionDomainBad()
		{
			Question question = new Question("$$$$$.com", DnsType.MX, DnsClass.IN);
		}

		[Test]
		[ExpectedException(typeof(ArgumentOutOfRangeException))]
		public void NewQuestionBadType()
		{
			Question question = new Question("codeproject.com", (DnsType)1999, DnsClass.IN);
		}

		[Test]
		[ExpectedException(typeof(ArgumentOutOfRangeException))]
		public void NewQuestionBadClass()
		{
			Question question = new Question("codeproject.com", DnsType.ANAME, (DnsClass)1999);
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
United Kingdom United Kingdom
I am a .NET architect/developer based in London working mostly on financial trading systems. My love of computers started at an early age with BASIC on a 3KB VIC20 and progressed onto a 32KB BBC Micro using BASIC and 6502 assembly language. From there I moved on to the blisteringly fast Acorn Archimedes using BASIC and ARM assembly.

I started developing with C++ since 1990, where it was introduced to me in my first year studying for a Computer Science degree at the University of Nottingham. I started professionally with Visual C++ version 1.51 in 1993.

I moved over to C# and .NET in early 2004 after a long period of denial that anything could improve upon C++.

Recently I did a bit of work in my old language of C++ and I now realise that frankly, it's a total pain in the arse.

Comments and Discussions