test-pop3-migration-plugin.c revision 29fc8f1dc678f9698363181ea599e6db105ea50f
02c335c23bf5fa225a467c19f2c063fb0dc7b8c3Timo Sirainen/* Copyright (c) 2015-2017 Dovecot authors, see the included COPYING file */
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen#include "lib.h"
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen#include "sha1.h"
0536ccb51d41e3078c3a9fa33e509fb4b2420f95Timo Sirainen#include "hex-binary.h"
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen#include "istream.h"
8cb72c59d5ea4e9e5f638d7ec840bb853f5a188eTimo Sirainen#include "test-common.h"
8cb72c59d5ea4e9e5f638d7ec840bb853f5a188eTimo Sirainen#include "pop3-migration-plugin.h"
8cb72c59d5ea4e9e5f638d7ec840bb853f5a188eTimo Sirainen
8cb72c59d5ea4e9e5f638d7ec840bb853f5a188eTimo Sirainenstatic void test_pop3_migration_get_hdr_sha1(void)
8cb72c59d5ea4e9e5f638d7ec840bb853f5a188eTimo Sirainen{
8cb72c59d5ea4e9e5f638d7ec840bb853f5a188eTimo Sirainen static const struct {
8cb72c59d5ea4e9e5f638d7ec840bb853f5a188eTimo Sirainen const char *input;
8cb72c59d5ea4e9e5f638d7ec840bb853f5a188eTimo Sirainen const char *sha1;
e3678f7bfba87b5aa1446a1a7b9928272b4f72a3Timo Sirainen bool have_eoh;
e3678f7bfba87b5aa1446a1a7b9928272b4f72a3Timo Sirainen } tests[] = {
e3678f7bfba87b5aa1446a1a7b9928272b4f72a3Timo Sirainen { "", "da39a3ee5e6b4b0d3255bfef95601890afd80709", FALSE },
e3678f7bfba87b5aa1446a1a7b9928272b4f72a3Timo Sirainen { "\n", "adc83b19e793491b1c6ea0fd8b46cd9f32e592fc", TRUE },
e3678f7bfba87b5aa1446a1a7b9928272b4f72a3Timo Sirainen { "a: \r\n", "a3871371f2d468493005286282ae10549dab2c57", FALSE },
e3678f7bfba87b5aa1446a1a7b9928272b4f72a3Timo Sirainen { "a: b\r\n", "44ef6a20971148dd54a161f79814e22e2d098ddb", FALSE },
8cb72c59d5ea4e9e5f638d7ec840bb853f5a188eTimo Sirainen { "a: b \r\n", "44ef6a20971148dd54a161f79814e22e2d098ddb", FALSE },
8cb72c59d5ea4e9e5f638d7ec840bb853f5a188eTimo Sirainen { "a: b \r\n", "44ef6a20971148dd54a161f79814e22e2d098ddb", FALSE },
8b247780e911909a9fdc47f69ce6d1478902ad98Timo Sirainen { "a: b \r\n", "44ef6a20971148dd54a161f79814e22e2d098ddb", FALSE },
8b247780e911909a9fdc47f69ce6d1478902ad98Timo Sirainen { "a: b\r\n\r\n", "938b96404495cced816e3a4f6031734eab4e71b3", TRUE },
8b247780e911909a9fdc47f69ce6d1478902ad98Timo Sirainen { "a: b\r\n\r\r\n", "44ef6a20971148dd54a161f79814e22e2d098ddb", FALSE },
8b247780e911909a9fdc47f69ce6d1478902ad98Timo Sirainen { "a: b\r\n\r\r\nc: d\r\n\r\n", "44ef6a20971148dd54a161f79814e22e2d098ddb", TRUE },
8b247780e911909a9fdc47f69ce6d1478902ad98Timo Sirainen { "a: b\r\n \r\n", "44ef6a20971148dd54a161f79814e22e2d098ddb", FALSE },
862ec874f9373e3e499e237d3b9f71fdf1413feeTimo Sirainen { "a: b\r\n \r\n", "44ef6a20971148dd54a161f79814e22e2d098ddb", FALSE },
862ec874f9373e3e499e237d3b9f71fdf1413feeTimo Sirainen { "a: b\r\n\t\r\n", "44ef6a20971148dd54a161f79814e22e2d098ddb", FALSE },
862ec874f9373e3e499e237d3b9f71fdf1413feeTimo Sirainen };
862ec874f9373e3e499e237d3b9f71fdf1413feeTimo Sirainen struct istream *input;
862ec874f9373e3e499e237d3b9f71fdf1413feeTimo Sirainen unsigned char digest[SHA1_RESULTLEN];
862ec874f9373e3e499e237d3b9f71fdf1413feeTimo Sirainen unsigned int i;
862ec874f9373e3e499e237d3b9f71fdf1413feeTimo Sirainen bool have_eoh;
862ec874f9373e3e499e237d3b9f71fdf1413feeTimo Sirainen
862ec874f9373e3e499e237d3b9f71fdf1413feeTimo Sirainen test_begin("pop3 migration get hdr sha1");
862ec874f9373e3e499e237d3b9f71fdf1413feeTimo Sirainen
862ec874f9373e3e499e237d3b9f71fdf1413feeTimo Sirainen for (i = 0; i < N_ELEMENTS(tests); i++) {
862ec874f9373e3e499e237d3b9f71fdf1413feeTimo Sirainen input = i_stream_create_from_data(tests[i].input,
862ec874f9373e3e499e237d3b9f71fdf1413feeTimo Sirainen strlen(tests[i].input));
862ec874f9373e3e499e237d3b9f71fdf1413feeTimo Sirainen test_assert_idx(pop3_migration_get_hdr_sha1(1, input, digest, &have_eoh) == 0, i);
862ec874f9373e3e499e237d3b9f71fdf1413feeTimo Sirainen test_assert_idx(strcasecmp(binary_to_hex(digest, sizeof(digest)), tests[i].sha1) == 0, i);
862ec874f9373e3e499e237d3b9f71fdf1413feeTimo Sirainen test_assert_idx(tests[i].have_eoh == have_eoh, i);
862ec874f9373e3e499e237d3b9f71fdf1413feeTimo Sirainen i_stream_unref(&input);
862ec874f9373e3e499e237d3b9f71fdf1413feeTimo Sirainen }
e2ce8d4a6ac5d82a906178148453e7613fab9ba0Timo Sirainen
e2ce8d4a6ac5d82a906178148453e7613fab9ba0Timo Sirainen test_end();
9a48c2243fe98ca8393be7908f84d20c634bcdf9Timo Sirainen}
9a48c2243fe98ca8393be7908f84d20c634bcdf9Timo Sirainen
9a48c2243fe98ca8393be7908f84d20c634bcdf9Timo Sirainenint main(void)
9a48c2243fe98ca8393be7908f84d20c634bcdf9Timo Sirainen{
9a48c2243fe98ca8393be7908f84d20c634bcdf9Timo Sirainen static void (*const test_functions[])(void) = {
9a48c2243fe98ca8393be7908f84d20c634bcdf9Timo Sirainen test_pop3_migration_get_hdr_sha1,
59e26ff34b05cd971a111f8a42fc60c13d9f688bTimo Sirainen NULL
9a48c2243fe98ca8393be7908f84d20c634bcdf9Timo Sirainen };
2d2ebe91d56e9a158de000c9d0026f65600fbcfaTimo Sirainen return test_run(test_functions);
e2ce8d4a6ac5d82a906178148453e7613fab9ba0Timo Sirainen}
f988b93c2ef773987bcdcbfb4cca39b955e3a392Timo Sirainen