Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.

Channels ▼

Community Voices

Dr. Dobb's Bloggers

Insights into Router Design: Unit Testing of Networking Protocols

September 06, 2009


Unit testing is a software validation methodology through which a programmer tests individual modules or units of source code. If the programmer has been responsible for developing a networking protocol in a device such as a router or a switch, e.g. for a new signaling protocol for VoIP, the protocol implementation or its constituent parts have to be unit tested. A networking protocol, by its very nature, provides some aids to unit testing. This article, through some practical recommendations, discusses these aspects of protocol development and how it is possible to write software that is friendly to unit testing.


What is a Networking Protocol?


A network protocol is a specification of a contract between two or more networking elements, which may not all be manufactured by the same vendor. The contract specifies the external behavior of these entities on the introduction of some external triggers or events. These events are usually administrative actions or network packets or timer events.


Code both sides


Thus a networking protocol has an important characteristic that can be leveraged for effective unit testing. It has at least 2 sides to most transactions, at least to the ones that involve packets on the wire. One side is often called the network side, which is the direction towards the core of the network. The other side is called the host or user side, which is the direction towards the edge of the network. And, even if only side of the transaction, say the network side, is to be implemented on the device, it may make sense to also implement in a hidden way, the other side, say the host side, concurrently. Coding both sides of any protocol transaction ensures that even without any other device, the implementation at any stage is always testable. This little subtlety is often overlooked in the interest of time to completion, with the result that it is only after the entire one-sided protocol has completed development can it be reasonably verified. And even at that point, the verification can happen only against a dissimilar device, thereby throwing interoperability into the mix while attempting to validate functionality or conformance.


Test it on a Host


It should be possible to test the functioning of the protocol without having to run it on the target hardware. Any effort spent to simulate a test network on a standard Linux or FreeBSD or other host system is very worthwhile in the long run. Host-based unit testing saves a lot of time for software developers, and consequently for test engineers, and for the whole lifecycle of the project. In a few minutes, a test can be run, a problem can be identified and a fix found and applied, and the test re-run. It is typically not possible to do these activities at the same speed on a real router/switch system. Besides, target (real) router/switch hardware, even that belonging to the same company, is often expensive. Regular computers (hosts) are much less so, and are often used for software development by telecommuting engineers working from home. It is very convenient if they can simulate a good deal of the system on their computer, so they can develop software and unit test and fix bugs on it wherever they are, instead of being tied to the real hardware which is often in the lab of an office building. The best thing about being able to unit test something on one's own computer is that it requires no network connectivity at all.


If the entire router/switch infrastructure has a corresponding infrastructure available on the host, that is excellent. But even in the absence of such an arrangement, for instance, in the early days of a startup environment, it is often possible to rig up something entirely specific to the job at hand.


Make it a Library


It makes sense to write a protocol implementation in a very minimalist environment that uses, but is not tied to, the software interfaces that are necessary in the actual router/switch software infrastructure. This makes it possible to also write test code using the same APIs, that can then be checked into the repository and maintained. The protocol code should be unaware of the target hardware and should act through carefully structured interfaces. In short, it should be a library with well-defined APIs for administrative action, packet transfer, timer events, and interactions with other software modules. The library should allow the registration of callbacks for easy notification to other modules of events happening within the protocol. The library can thus be linked into both the target and test programs, so it runs appropriately in a real or test situation. The packet transfer functions, for instance, may be merely implemented in the test program as socket calls. On a Linux host, it is possible to run software like Ethereal, for example, to look at these packet decodes and catch protocol errors.


Hack to Simulate


A useful and often misunderstood aspect of effective development is that hacks can often be placed in the software that, when compiled in through the use of specific compile-time flags, can simulate some important effect normally not present, such as network connectivity to a peer. For instance, it may be possible to send a packet out of the system, but get it back through a hack, on a different interface. This can mean to two instances of the protocol that these interfaces are connected, without there being any actual physical connection. And so, the protocol can be made to believe that there is a peer, which can then imply that several interactions can be tested. The advantage with doing this sort of non-standard thing is the speed with which something can be tested during development.


Remember to test on the target


Of course, the limitations of a simulation or unit testing environment must be well understood. It is often only on the real device that various race conditions to do with different processors etc. can be debugged. Realistic stress testing usually cannot be attempted on a host. Interoperability testing with real line cards probably cannot be done unless the target interfaces are regular Ethernet or appropriate line cards are available for the host.


About the author --- Rajesh Kumar Venkateswaran has been developing software in the data networking and telecommunications industry for many years. He has led teams, architected solutions, and created new products in this space.

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.