From 25342134e74a516ebe530cbc43cb84d924967394 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Axel=20D=C3=B6rfler?= Date: Sun, 30 Oct 2011 11:00:09 +0000 Subject: [PATCH] * Started a very simple test application for the IMAP add-on. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@42995 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/tests/add-ons/Jamfile | 1 + src/tests/add-ons/mail/Jamfile | 3 + src/tests/add-ons/mail/imap/Jamfile | 29 +++ src/tests/add-ons/mail/imap/imap_tester.cpp | 198 ++++++++++++++++++++ 4 files changed, 231 insertions(+) create mode 100644 src/tests/add-ons/mail/Jamfile create mode 100644 src/tests/add-ons/mail/imap/Jamfile create mode 100644 src/tests/add-ons/mail/imap/imap_tester.cpp diff --git a/src/tests/add-ons/Jamfile b/src/tests/add-ons/Jamfile index e94c3e0eb1..26a4111a69 100644 --- a/src/tests/add-ons/Jamfile +++ b/src/tests/add-ons/Jamfile @@ -3,6 +3,7 @@ SubDir HAIKU_TOP src tests add-ons ; SubInclude HAIKU_TOP src tests add-ons input_server ; SubInclude HAIKU_TOP src tests add-ons index_server ; SubInclude HAIKU_TOP src tests add-ons kernel ; +SubInclude HAIKU_TOP src tests add-ons mail ; SubInclude HAIKU_TOP src tests add-ons media ; SubInclude HAIKU_TOP src tests add-ons opengl ; SubInclude HAIKU_TOP src tests add-ons print ; diff --git a/src/tests/add-ons/mail/Jamfile b/src/tests/add-ons/mail/Jamfile new file mode 100644 index 0000000000..8008d13b1f --- /dev/null +++ b/src/tests/add-ons/mail/Jamfile @@ -0,0 +1,3 @@ +SubDir HAIKU_TOP src tests add-ons mail ; + +SubInclude HAIKU_TOP src tests add-ons mail imap ; diff --git a/src/tests/add-ons/mail/imap/Jamfile b/src/tests/add-ons/mail/imap/Jamfile new file mode 100644 index 0000000000..ebee62a704 --- /dev/null +++ b/src/tests/add-ons/mail/imap/Jamfile @@ -0,0 +1,29 @@ +SubDir HAIKU_TOP src tests add-ons mail imap ; + +SetSubDirSupportedPlatformsBeOSCompatible ; + +UsePrivateHeaders mail shared ; +SubDirHdrs [ FDirName $(HAIKU_TOP) src tests add-ons kernel file_systems + fs_shell ] ; +SubDirHdrs [ FDirName $(HAIKU_TOP) src add-ons mail_daemon inbound_protocols + imap imap_lib ] ; + +local libSources = IMAPFolders.cpp IMAPHandler.cpp IMAPMailbox.cpp + IMAPParser.cpp IMAPProtocol.cpp IMAPStorage.cpp ; + +SimpleTest imap_tester : + imap_tester.cpp + $(libSources) + + # from fs_shell + argv.c + + : be $(TARGET_LIBSUPC++) mail +; + +SEARCH on [ FGristFiles $(libSources) ] + = [ FDirName $(HAIKU_TOP) src add-ons mail_daemon inbound_protocols imap + imap_lib ] ; + +SEARCH on [ FGristFiles argv.c ] = [ FDirName $(HAIKU_TOP) src tests add-ons + kernel file_systems fs_shell ] ; diff --git a/src/tests/add-ons/mail/imap/imap_tester.cpp b/src/tests/add-ons/mail/imap/imap_tester.cpp new file mode 100644 index 0000000000..188178f2e3 --- /dev/null +++ b/src/tests/add-ons/mail/imap/imap_tester.cpp @@ -0,0 +1,198 @@ +#include "IMAPFolders.h" +#include "IMAPMailbox.h" +#include "IMAPStorage.h" + +#include "argv.h" + + +struct cmd_entry { + char* name; + void (*func)(int argc, char **argv); + char* help; +}; + + +static void do_help(int argc, char** argv); + + +extern const char* __progname; +static const char* kProgramName = __progname; + +static IMAPStorage sStorage; +static IMAPMailbox sMailbox(sStorage); + + +static void +error(const char* context, status_t status) +{ + fprintf(stderr, "Error during %s: %s\n", context, strerror(status)); +} + + +static void +usage() +{ + printf("Usage: %s \n", kProgramName); + exit(1); +} + + +// #pragma mark - + + +static void +do_select(int argc, char** argv) +{ + const char* folder = "INBOX"; + if (argc > 1) + folder = argv[1]; + + status_t status = sMailbox.SelectMailbox(folder); + if (status != B_OK) + error("select", status); +} + + +static void +do_folders(int argc, char** argv) +{ + IMAPFolders folder(sMailbox); + FolderList folders; + status_t status = folder.GetFolders(folders); + if (status != B_OK) + error("folders", status); + + for (size_t i = 0; i < folders.size(); i++) { + printf(" %s %s\n", folders[i].subscribed ? "*" : " ", + folders[i].folder.String()); + } +} + + +static void +do_raw(int argc, char** argv) +{ + // build command back again + char command[4096]; + command[0] = '\0'; + + for (int i = 1; i < argc; i++) { + if (i > 1) + strlcat(command, " ", sizeof(command)); + strlcat(command, argv[i], sizeof(command)); + } + + class RawCommand : public IMAPCommand { + public: + RawCommand(const char* command) + : + fCommand(command) + { + } + + BString Command() + { + return fCommand; + } + + bool Handle(const BString& response) + { + return false; + } + + private: + const char* fCommand; + }; + RawCommand rawCommand(command); + status_t status = sMailbox.ProcessCommand(&rawCommand, 60 * 1000); + if (status != B_OK) + error("raw", status); +} + + +static cmd_entry sBuiltinCommands[] = { + {"select", do_select, "Selects a mailbox, defaults to INBOX"}, + {"folders", do_folders, "List of existing folders"}, + {"raw", do_raw, "Issue a raw command to the server"}, + {"help", do_help, "prints this help text"}, + {"quit", NULL, "exits the application"}, + {NULL, NULL, NULL}, +}; + + +static void +do_help(int argc, char** argv) +{ + printf("Available commands:\n"); + + for (cmd_entry* command = sBuiltinCommands; command->name != NULL; + command++) { + printf("%8s - %s\n", command->name, command->help); + } +} + + +// #pragma mark - + + +int +main(int argc, char** argv) +{ + if (argc < 4) + usage(); + + const char* server = argv[1]; + const char* user = argv[2]; + const char* password = argv[3]; + bool useSSL = argc > 4; + uint16 port = useSSL ? 995 : 143; + + printf("Connecting to \"%s\" as %s\n", server, user); + + status_t status = sMailbox.Connect(server, user, password, useSSL, port); + if (status != B_OK) { + error("connect", status); + return 1; + } + + while (true) { + printf("> "); + fflush(stdout); + + char line[1024]; + if (fgets(line, sizeof(line), stdin) == NULL) + break; + + argc = 0; + argv = build_argv(line, &argc); + if (argv == NULL || argc == 0) + continue; + + if (!strcmp(argv[0], "quit") + || !strcmp(argv[0], "exit") + || !strcmp(argv[0], "q")) + break; + + int length = strlen(argv[0]); + bool found = false; + + for (cmd_entry* command = sBuiltinCommands; command->name != NULL; + command++) { + if (!strncmp(command->name, argv[0], length)) { + command->func(argc, argv); + found = true; + break; + } + } + + if (!found) { + fprintf(stderr, "Unknown command \"%s\". Type \"help\" for a " + "list of commands.\n", argv[0]); + } + + free(argv); + } + + + return 0; +}