MailNews fakeserver

Fakeserver is a testing server that implements a generic text-based protocol common to all major mailnews protocols (POP, IMAP, SMTP, and NNTP) designed for use in automated tests. It can also be used in manual QA tests.

Basic Structure

Fakeserver is situated entirely under mailnews/test/fakeserver/. It is comprised of three major components: a daemon, a handler, and the server itself.

The server itself is found in mailnews/test/fakeserver/maild.js and was largely based off of the httpd fakeserver from network code. It utilizes UTF-8 as its transport mechanism and is capable of performing proper pipelining of commands.

The other two components are specific to the protocol type and found under similar files in the same directory (e.g., smtpd.js).

A daemon is information about the server that can be manipulated. An example of such a manipulation is adding a folder or adding messages. Daemons should usually persist for the entirety of the test.

The handler is the glue between the server and the daemon. This area will be the most diverse of all components, as edge cases with quirky servers being liberal with the specs will require different handlers.

Writing a new fakeserver

Since the number of protocols we use is very small, most people shouldn't have to write an entirely new daemon and handler setup by themselves. That said, someone writing a new test may have to take into account a new handler.

A handler will contain the following methods:

<caption> Handler methods </caption>
Name Arguments Returns Notes
[command] rest of sent line Server's response The name is normalized to be uppercase.
onError command, rest of sent line Server's response Called if handler does not define the command function.
onStartup none Server's response Called when a connection is made.
onMultiline sent line Server's response or nothing Called when in multi-line mode.
postCommand Server object none Called after every command.

The server is smart about the responses and will normalize all occurrences of '\n' (but not '\r') to '\r\n' for you, as well as append the '\r\n' to the response if not present. It will also properly queue all messages sent in pipelining mode and will present lines without any EOL characters. In addition, it uses UTF-8 as the transmission medium.

The server presents the following API to the handler:

<caption> Server API </caption>
Name Arguments Returns Description
closeSocket none nothing Closes the socket and stops the test.
setMultiline the new value of multiline nothing The value is a boolean, with true invoking multiline mode. It is off by default.

Extending a handler to provide different mechanisms is a process not defined here; please check the documentation of the corresponding handler for instructions.

Using fakeserver in xpcshell tests

Since there are three different components of fakeserver, a total of four objects need to be set up in a fakeserver test on top of other testing. The outline of a test looks like this:

var daemon = setupDaemon(); // Check with your local fakeserver docs
var handlers = [ /* Some set of handlers */ ];

for (var handler in handlers) {
  // This sets up the fake server
  var server = new nsMailServer(new handler(daemon));
  // Port to use. I tend to like using 1024 + default port number myself.
  // Probably best to include as a constant somewhere. Also, use something
  // larger than 1024 for best results.

  // Set up a nsIMsgIncomingServer locally
  // Nothing will be executed until the connection is closed
  // localserver.closeCachedConnections() is generally a good way to do so

  // Set up second test
  transaction = server.playTransaction();

  // Finished with tests

Currently, fakeserver provides no means to keep a persistent connection past a test, requiring connections to be closed, possibly forcibly. Alternatively, one can wait for the timeout, which occurs after 3 minutes, so not closing connections would make the tests take unbearably long.

The server provides the following api to xpcshell tests:

<caption> nsMailServer xpcshell API </caption>
Name Arguments Returns Description
performTest none nothing Runs until the test is forcibly aborted or stopped normally
isStopped none if the server is stopped helper for performTest
isTestFinished none if the test is finished helper for performTest
playTransaction none the transaction The transaction is an object with two properties: us, and them; us is an array of responses we sent, them an array of commands received
resetTest none nothing prepares the server for the next test without closing the connection
start port number nothing Starts the server listening
stop none nothing Stops the server and closes the connection

Using fakeserver in QA testing

Debug output from fakeservers

It is possible to get the fakeservers to dump to the console the commands they have sent and received.

To enable/disable debugging, call setDebugLevel on your nsMailServer instance with one of the following options:

const fsDebugNone = 0;     // Dump nothing
const fsDebugRecv = 1;     // Dump just the received commands
const fsDebugRecvSend = 2; // Dump received + sent commands, no data
const fsDebugAll = 3;      // Dump everything

Debugging is set to "None" by default.

Specific fakeserver guidelines








nntpd.js defines a few different classes of NNTP servers: compliance for RFC 977, RFC 2980, and RFC 3977, as well as a Giganews and an INN server. (Full 2980 and 3977 compliance as well as INN is not yet written).

For xpcshell tests in mailnews/news, all files in mailnews/news/test/postings/auto-add are automatically added based on the Newsgroups header. There are added in alphabetical order, which may help for purposes that rely on article key orders. The only generated header is Lines.

The following comprises the API of the news fakeserver itself:

nntpDaemon API
Name Arguments Returns Notes
[constructor] daemon flags N/A Flags are defined below
addGroup group name (string), is postable nothing Adds the group (resetting if it exists)
addArticle newsArticle object nothing Adds the message to all groups in the article's group list
addArticleToGroup newsArticle object, group (string), integral key for group nothing Adds the message to the specified group with the specified key
getGroup group name group object Group is a map key->article, with the additional properties: flags, keys (array of keys), nextKey (next key to use for new articles)
getGroupStats group name array of [size, min, max] the size is an estimate if NNTP_REAL_LENGTH is not specified in the daemon flags.
getArticle message ID newsArticle object Pretty self-explanatory
newsArticle API
Name Arguments Returns Notes
[constructor] text (as a string) N/A initializes all fields
headers (property) map of header (lower-case) -> value
body (property) text of body
messageID (property) message ID
fullText (property) Full text as message without modification except added headers.