Next: , Previous: , Up: mtasim   [Contents][Index]


12.2 mtasim expect commands

Until now we were using mtasim interactively. However, it is often useful in shell scripts, for example the mailfromd test suite is written in shell and mtasim. To avoid the necessity to use auxiliary programs like expect or DejaGNU, mtasim contains a built-in expect feature. The administrative command \E introduces the SMTP code that the next command is expected to yield. For example,

\E250
rcpt to: <foo@bar.org>

tells mtasim that the response to RCPT TO command must begin with ‘250’ code. If it does, mtasim continues execution. Otherwise, it prints an error message and terminates with exit code 1. The error message it prints looks like:

Expected 250 but got 470

The expected code given with the \E command may have less than 3 digits. In this case it specifies the first digits of expected reply. For example, the command ‘\E2’ matches replies ‘200’, ‘220’, etc.

If \E is passed two arguments, the second one is treated as an extended regular expression. The subsequent command will then succeed if its return code matches the one supplied as the first argument, and its extended SMTP code and textual message match the supplied regular expression. If the regular expression contains whitespace, enclose it in a pair of double quotes. Within double quotes, backslash can be used to escape double quote, and backslash character.

This feature can be used to automate your tests. For example, the following script tests the greylisting functionality (see the previous section):

# Test the greylisting functionality
#
\E220
\Dclient_addr=10.10.1.13
\E250
ehlo yahoo.com
\E250
mail from: <gray@yahoo.com>
\E450
rcpt to: <gray@localhost>
\E221
quit

This example also illustrates the fact that you can use ‘#’-style comments in the mtasim input.

mtasim scripts can be used in shell programs, for example:

mtasim -Xauto --statedir -- -P../mflib test.rc < scriptfile
if $? -ne 0; then
  echo "Greylisting test failed"
fi

Next: , Previous: , Up: mtasim   [Contents][Index]