mirror of
https://git.notmuchmail.org/git/notmuch
synced 2024-11-22 02:48:08 +01:00
test: Add test that emacs interface actually sends mail.
Rather than *reall* sending mail here, we instead have a new test program, smtp-dummy which implements (a small piece of) the server-side SMTP protocol and saves a mail message to the filename provided. This gives us reasonable test coverage of a large chunk of the notmuch+emacs code base (down to talking to an SMTP server with the final mail contents).
This commit is contained in:
parent
f30200a429
commit
102c57c825
5 changed files with 234 additions and 2 deletions
1
test/.gitignore
vendored
1
test/.gitignore
vendored
|
@ -1,2 +1,3 @@
|
||||||
test-results
|
test-results
|
||||||
corpus.mail
|
corpus.mail
|
||||||
|
smtp-dummy
|
||||||
|
|
|
@ -2,6 +2,11 @@
|
||||||
|
|
||||||
dir := test
|
dir := test
|
||||||
|
|
||||||
|
$(dir)/smtp-dummy: $(dir)/smtp-dummy.c
|
||||||
|
$(call quiet,CC) $^ -o $@
|
||||||
|
|
||||||
.PHONY: test
|
.PHONY: test
|
||||||
test: all
|
test: all $(dir)/smtp-dummy
|
||||||
@${dir}/notmuch-test $(OPTIONS)
|
@${dir}/notmuch-test $(OPTIONS)
|
||||||
|
|
||||||
|
CLEAN := $(CLEAN) $(dir)/smtp-dummy
|
||||||
|
|
|
@ -52,7 +52,7 @@ test_expect_code 2 'failure to clean up causes the test to fail' '
|
||||||
# Ensure that all tests are being run
|
# Ensure that all tests are being run
|
||||||
test_begin_subtest 'Ensure that all available tests will be run by notmuch-test'
|
test_begin_subtest 'Ensure that all available tests will be run by notmuch-test'
|
||||||
tests_in_suite=$(grep TESTS= ../notmuch-test | sed -e "s/TESTS=\"\(.*\)\"/\1/" | tr " " "\n" | sort)
|
tests_in_suite=$(grep TESTS= ../notmuch-test | sed -e "s/TESTS=\"\(.*\)\"/\1/" | tr " " "\n" | sort)
|
||||||
available=$(ls -1 ../ | grep -v -E "^(aggregate-results.sh|Makefile|Makefile.local|notmuch-test|README|test-lib.sh|test-results|tmp.*|valgrind|corpus*|emacs.expected-output)" | sort)
|
available=$(ls -1 ../ | grep -v -E "^(aggregate-results.sh|Makefile|Makefile.local|notmuch-test|README|test-lib.sh|test-results|tmp.*|valgrind|corpus*|emacs.expected-output|smtp-dummy|smtp-dummy.c)" | sort)
|
||||||
test_expect_equal "$tests_in_suite" "$available"
|
test_expect_equal "$tests_in_suite" "$available"
|
||||||
|
|
||||||
################################################################
|
################################################################
|
||||||
|
|
20
test/emacs
20
test/emacs
|
@ -56,4 +56,24 @@ test_emacs '(notmuch-search "id:\"123..456@example\"") (notmuch-test-wait) (notm
|
||||||
output=$(notmuch search 'id:"123..456@example"' | notmuch_search_sanitize)
|
output=$(notmuch search 'id:"123..456@example"' | notmuch_search_sanitize)
|
||||||
test_expect_equal "$output" "thread:XXX 2001-01-05 [1/1] Notmuch Test Suite; Message with .. in Message-Id (inbox search-add show-add)"
|
test_expect_equal "$output" "thread:XXX 2001-01-05 [1/1] Notmuch Test Suite; Message with .. in Message-Id (inbox search-add show-add)"
|
||||||
|
|
||||||
|
test_begin_subtest "Sending a message via (fake) SMTP"
|
||||||
|
../smtp-dummy sent_message &
|
||||||
|
smtp_dummy_pid=$!
|
||||||
|
test_emacs "(setq message-send-mail-function 'message-smtpmail-send-it) (setq smtpmail-smtp-server \"localhost\") (setq smtpmail-smtp-service \"25025\") (notmuch-hello) (notmuch-mua-mail) (message-goto-to) (insert \"user@example.com\") (message-goto-subject) (insert \"Testing message sent via SMTP\") (message-goto-body) (insert \"This is a test that messages are sent via SMTP\") (message-send-and-exit)" >/dev/null 2>&1
|
||||||
|
wait ${smtp_dummy_pid}
|
||||||
|
output=$(sed -e 's,^From: .*,From: XXX,' \
|
||||||
|
-e s',^User-Agent: Notmuch/.* Emacs/.*,User-Agent: Notmuch/XXX Emacs/XXX,' \
|
||||||
|
-e s',^Date:.*,Date: XXX,' \
|
||||||
|
-e s',^Message-ID: <.*>$,Message-ID: <XXX>,' < sent_message)
|
||||||
|
test_expect_equal "$output" "From: XXX
|
||||||
|
To: user@example.com
|
||||||
|
Subject: Testing message sent via SMTP
|
||||||
|
User-Agent: Notmuch/XXX Emacs/XXX
|
||||||
|
Date: XXX
|
||||||
|
Message-ID: <XXX>
|
||||||
|
MIME-Version: 1.0
|
||||||
|
Content-Type: text/plain; charset=us-ascii
|
||||||
|
|
||||||
|
This is a test that messages are sent via SMTP"
|
||||||
|
|
||||||
test_done
|
test_done
|
||||||
|
|
206
test/smtp-dummy.c
Normal file
206
test/smtp-dummy.c
Normal file
|
@ -0,0 +1,206 @@
|
||||||
|
/* smtp-dummy - Dummy SMTP server that delivers mail to the given file
|
||||||
|
*
|
||||||
|
* Copyright © 2010 Carl Worth
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see http://www.gnu.org/licenses/ .
|
||||||
|
*
|
||||||
|
* Authors: Carl Worth <cworth@cworth.org>
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* This (non-compliant) SMTP server listens on localhost, port 25025
|
||||||
|
* and delivers a mail received to the given filename, (specified as a
|
||||||
|
* command-line argument). It exists after the first client connection
|
||||||
|
* completes.
|
||||||
|
*
|
||||||
|
* It implements very little of the SMTP protocol, even less than
|
||||||
|
* specified as the minimum implementation in the SMTP RFC, (not
|
||||||
|
* implementing RSET, NOOP, nor VRFY). And it doesn't do any
|
||||||
|
* error-checking on the input.
|
||||||
|
*
|
||||||
|
* That is to say, if you use this program, you will very likely find
|
||||||
|
* cases where it doesn't do everything your SMTP client expects. You
|
||||||
|
* have been warned.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <netinet/ip.h>
|
||||||
|
#include <netdb.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#define STRNCMP_LITERAL(var, literal) \
|
||||||
|
strncmp ((var), (literal), sizeof (literal) - 1)
|
||||||
|
|
||||||
|
static void
|
||||||
|
receive_data_to_file (FILE *peer, FILE *output)
|
||||||
|
{
|
||||||
|
char *line = NULL;
|
||||||
|
size_t line_size;
|
||||||
|
ssize_t line_len;
|
||||||
|
|
||||||
|
while ((line_len = getline (&line, &line_size, peer)) != -1) {
|
||||||
|
if (STRNCMP_LITERAL (line, ".\r\n") == 0)
|
||||||
|
break;
|
||||||
|
if (line_len < 2)
|
||||||
|
continue;
|
||||||
|
if (line[line_len-1] == '\n' && line[line_len-2] == '\r') {
|
||||||
|
line[line_len-2] = '\n';
|
||||||
|
line[line_len-1] = '\0';
|
||||||
|
}
|
||||||
|
fprintf (output, "%s",
|
||||||
|
line[0] == '.' ? line + 1 : line);
|
||||||
|
}
|
||||||
|
|
||||||
|
free (line);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
process_command (FILE *peer, FILE *output, const char *command)
|
||||||
|
{
|
||||||
|
if (STRNCMP_LITERAL (command, "EHLO ") == 0) {
|
||||||
|
fprintf (peer, "502\r\n");
|
||||||
|
fflush (peer);
|
||||||
|
} else if (STRNCMP_LITERAL (command, "HELO ") == 0) {
|
||||||
|
fprintf (peer, "250 localhost\r\n");
|
||||||
|
fflush (peer);
|
||||||
|
} else if (STRNCMP_LITERAL (command, "MAIL FROM:") == 0 ||
|
||||||
|
STRNCMP_LITERAL (command, "RCPT TO:") == 0) {
|
||||||
|
fprintf (peer, "250 OK\r\n");
|
||||||
|
fflush (peer);
|
||||||
|
} else if (STRNCMP_LITERAL (command, "DATA") == 0) {
|
||||||
|
fprintf (peer, "354 End data with <CR><LF>.<CR><LF>\r\n");
|
||||||
|
fflush (peer);
|
||||||
|
receive_data_to_file (peer, output);
|
||||||
|
fprintf (peer, "250 OK\r\n");
|
||||||
|
fflush (peer);
|
||||||
|
} else if (STRNCMP_LITERAL (command, "QUIT") == 0) {
|
||||||
|
fprintf (peer, "221 BYE\r\n");
|
||||||
|
fflush (peer);
|
||||||
|
return 1;
|
||||||
|
} else {
|
||||||
|
fprintf (stderr, "Unknown command: %s\n", command);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
do_smtp_to_file (FILE *peer, FILE *output)
|
||||||
|
{
|
||||||
|
char buf[4096];
|
||||||
|
ssize_t bytes;
|
||||||
|
char greeting[] = "220 localhost smtp-dummy\r\n";
|
||||||
|
char *line = NULL;
|
||||||
|
size_t line_size;
|
||||||
|
ssize_t line_len;
|
||||||
|
|
||||||
|
fprintf (peer, "220 localhost smtp-dummy\r\n");
|
||||||
|
fflush (peer);
|
||||||
|
|
||||||
|
while ((line_len = getline (&line, &line_size, peer)) != -1) {
|
||||||
|
if (process_command (peer, output, line))
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
free (line);
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
main (int argc, char *argv[])
|
||||||
|
{
|
||||||
|
char *output_filename;
|
||||||
|
FILE *peer_file, *output;
|
||||||
|
int sock, peer, err;
|
||||||
|
struct sockaddr_in addr, peer_addr;
|
||||||
|
struct hostent *hostinfo;
|
||||||
|
socklen_t peer_addr_len;
|
||||||
|
int reuse;
|
||||||
|
|
||||||
|
if (argc != 2) {
|
||||||
|
fprintf (stderr, "Usage: %s <output-file>\n", argv[0]);
|
||||||
|
exit (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
output_filename = argv[1];
|
||||||
|
output = fopen (output_filename, "w");
|
||||||
|
if (output == NULL) {
|
||||||
|
fprintf (stderr, "Failed to open %s for writing: %s\n",
|
||||||
|
output_filename, strerror (errno));
|
||||||
|
exit (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
sock = socket (AF_INET, SOCK_STREAM, 0);
|
||||||
|
if (sock == -1) {
|
||||||
|
fprintf (stderr, "Error: socket() failed: %s\n",
|
||||||
|
strerror (errno));
|
||||||
|
exit (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
reuse = 1;
|
||||||
|
err = setsockopt (sock, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof (reuse));
|
||||||
|
if (err) {
|
||||||
|
fprintf (stderr, "Error: setsockopt() failed: %s\n",
|
||||||
|
strerror (errno));
|
||||||
|
exit (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
hostinfo = gethostbyname ("localhost");
|
||||||
|
if (hostinfo == NULL) {
|
||||||
|
fprintf (stderr, "Unknown host: localhost\n");
|
||||||
|
exit (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
addr.sin_family = AF_INET;
|
||||||
|
addr.sin_port = htons (25025);
|
||||||
|
addr.sin_addr = *(struct in_addr *) hostinfo->h_addr;
|
||||||
|
err = bind (sock, (struct sockaddr *) &addr, sizeof(addr));
|
||||||
|
if (err) {
|
||||||
|
fprintf (stderr, "Error: bind() failed: %s\n",
|
||||||
|
strerror (errno));
|
||||||
|
close (sock);
|
||||||
|
exit (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
err = listen (sock, 1);
|
||||||
|
if (err) {
|
||||||
|
fprintf (stderr, "Error: listen() failed: %s\n",
|
||||||
|
strerror (errno));
|
||||||
|
close (sock);
|
||||||
|
exit (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
peer_addr_len = sizeof (peer_addr);
|
||||||
|
peer = accept (sock, (struct sockaddr *) &peer_addr, &peer_addr_len);
|
||||||
|
if (peer == -1) {
|
||||||
|
fprintf (stderr, "Error: accept() failed: %s\n",
|
||||||
|
strerror (errno));
|
||||||
|
exit (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
peer_file = fdopen (peer, "w+");
|
||||||
|
if (peer_file == NULL) {
|
||||||
|
fprintf (stderr, "Error: fdopen() failed: %s\n",
|
||||||
|
strerror (errno));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
do_smtp_to_file (peer_file, output);
|
||||||
|
|
||||||
|
fclose (output);
|
||||||
|
fclose (peer_file);
|
||||||
|
close (sock);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
Reference in a new issue