Simplified Wrapper and Interface Generator

SWIG (simplified wrapper and interface
generator) is a software development tool that connects programs
written in C, C++ and Objective-C with a variety of high-level
programming languages. It is often used with common scripting
languages such as Perl, Python and Tcl/Tk. In addition, it has been
extended to include languages such as Java, Eiffel and Guile.

SWIG is used to create high-level interpreted programming
environments for systems integration, and as a tool for building
user interfaces and testing. It is distributed as open source and
can be downloaded from
http://www.swig.org/.

In the following sections, I will discuss some of SWIG's
features and my personal experience with it as a testing engineer
at Zero Knowledge Systems
(http://www.zeroknowledge.com/)
for the Freedom Software.

Environment and Mission

At work, I have to test e-mail, web, IRC, FTP, proxy servers
and TELNET clients. My machine is a Pentium powered by a 266MHz
processor running Red Hat Linux with 128MB of RAM. My
responsibilities include testing the source code currently being
developed—hundreds of thousands of lines of code in a
mission-critical system, with no room for errors. Given that “the
strength of a security system is the strength of its weakest
link”, there was no place for flaws. The code cannot be tested
manually, because of the distributed architecture of client and
servers. Thus, there was an urgent need for an efficient tool that
would automate testing procedures. This tool had to be
platform-independent and compliant with both C and C++.

Then There was Light!

Shopping around, I learned about SWIG from its web page. The
source can be easily compiled for Linux; it is about 2MB in size.
SWIG is multi-platform, i.e., there is no need to duplicate test
procedures for Linux and Windows; it supports C and C++; and it can
be integrated into MSVC++ (Microsoft Visual C++). SWIG proved to be
the perfect tool.

Let Us SWIG Together

SWIG accepts as input an ANSI C-like interface file that
describes the functions and objects constituting the program to be
tested. The interface file can also include SWIG directives and
documentation. SWIG wraps the functions in another C program. When
both of these programs (the source code and the wrapped source
code) get compiled, SWIG creates a library file that can be called
from the Tcl shell.

Step By Step

The Program: start by writing your C program to be
tested. One thing to note is you have to modify the name of the
“main” function. Listing 1 is an example of a C program.

SWIG helped me a lot, due to the flexibility of function
calling it provides. The company had a secure mail system to be
tested. In this system, all e-mail messages go through several
servers before they reach their final destination, and they are
encrypted each time they pass through a new routing server.

My approach toward testing this environment was to write an
e-mail generator program in C which I called GenerateMail.
GenerateMail accepts several options such as the number of To, CC
and Bcc copies, the number of file attachments, etc. It produces a
file ready to be piped to Sendmail.

The first line creates an e-mail message file. The message
has three target addresses and two carbon copies. Three binary
files were attached as MIME attachments. By default, GenerateMail
uses bitmaps that are in its current directory.

The second line calls Sendmail with the appropriate options
to accept that mail message and send it on to the wire. Doing that,
it was easy to generate a large number of mail messages. In
addition, comparing the source and destination message checksums
was very easy with the help of SWIG.

Trending Topics

Upcoming Webinar

Getting Started with DevOps - Including New Data on IT Performance from Puppet Labs 2015 State of DevOps Report

August 27, 2015
12:00 PM CDT

DevOps represents a profound change from the way most IT departments have traditionally worked: from siloed teams and high-anxiety releases to everyone collaborating on uneventful and more frequent releases of higher-quality code. It doesn't matter how large or small an organization is, or even whether it's historically slow moving or risk averse — there are ways to adopt DevOps sanely, and get measurable results in just weeks.