forked from cpp-netlib/cpp-netlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmime-roundtrip.cpp
98 lines (75 loc) · 2.95 KB
/
mime-roundtrip.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
Read in a mime structure, parse it, and write it back to a file
with the same name as the input file, but with "-Results" appended to the name
We don't just write to stdout, because we want to read/write binary data,
and stdout on some systems eats CRLF, and turns them into newlines.
Returns 0 for success, non-zero for failure
*/
#include <boost/mime.hpp>
// #include <boost/bind.hpp>
#include <functional>
#include <boost/test/included/unit_test.hpp>
#include <fstream>
#include <iostream>
#include <iterator>
#include <sstream>
#include <algorithm>
#include <exception>
namespace {
std::string readfile ( const char *fileName ) {
std::ifstream in ( fileName );
if ( !in ) {
std::cerr << std::string ( "Can't open file: " ) + fileName << std::endl;
throw std::runtime_error ( std::string ( "Can't open file: " ) + fileName );
}
std::istreambuf_iterator<char> src(in);
std::istreambuf_iterator<char> eof;
std::string retVal;
in >> std::noskipws;
std::copy(src, eof, std::back_inserter(retVal));
return retVal;
}
struct my_traits {
typedef std::string string_type;
// typedef std::pair < std::string, string_type > header_type;
typedef std::string body_type;
};
//using namespace boost::mime;
typedef boost::mime::basic_mime<my_traits> mime_part;
typedef boost::shared_ptr<mime_part> smp;
smp to_mime ( const char *fileName ) {
std::ifstream in ( fileName );
if ( !in ) {
std::cerr << std::string ( "Can't open file: " ) + fileName << std::endl;
throw std::runtime_error ( std::string ( "Can't open file: " ) + fileName );
}
in >> std::noskipws;
return mime_part::parse_mime ( in );
}
std::string from_mime ( smp mp ) {
std::ostringstream oss;
oss << *mp;
return oss.str ();
}
void test_roundtrip ( const char *fileName ) {
smp mp;
BOOST_REQUIRE_NO_THROW( mp = to_mime ( fileName ));
BOOST_CHECK_EQUAL ( readfile ( fileName ), from_mime ( mp ));
}
void test_expected_parse_fail ( const char *fileName ) {
}
}
using namespace boost::unit_test;
test_suite*
init_unit_test_suite( int argc, char* argv[] )
{
framework::master_test_suite().add ( BOOST_TEST_CASE( std::bind ( test_roundtrip, "TestMessages/00000001" )));
framework::master_test_suite().add ( BOOST_TEST_CASE( std::bind ( test_roundtrip, "TestMessages/00000019" )));
framework::master_test_suite().add ( BOOST_TEST_CASE( std::bind ( test_roundtrip, "TestMessages/00000431" )));
framework::master_test_suite().add ( BOOST_TEST_CASE( std::bind ( test_roundtrip, "TestMessages/00000975" )));
// Following test is removed because the file it used often tripped false-positives when scanned by virus checkers.
// framework::master_test_suite().add ( BOOST_TEST_CASE( std::bind ( test_roundtrip, "TestMessages/00001136" )));
// test cases that fail
// framework::master_test_suite().add ( BOOST_TEST_CASE( std::bind ( test_roundtrip, "TestMessages/0019-NoBoundary" )));
return 0;
}