summaryrefslogtreecommitdiffstats
path: root/src/test/mime.cc
blob: 29c73e5a07338b5c2c5d76f444e63dd70762db95 (plain)
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
/*
 * Ceph - scalable distributed file system
 *
 * Copyright (C) 2011 New Dream Network
 *
 * This is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1, as published by the Free Software
 * Foundation.  See file COPYING.
 *
 */
#include "common/mime.h"
#include "gtest/gtest.h"

#include <stdint.h>
#include <string>

using std::string;

TEST(MimeTests, SimpleEncode) {
  char output[256];
  memset(output, 0, sizeof(output));
  int len;

  len = mime_encode_as_qp("abc", NULL, 0);
  ASSERT_EQ(len, 4);
  len = mime_encode_as_qp("abc", output, 4);
  ASSERT_EQ(len, 4);
  ASSERT_EQ(string("abc"), string(output));

  len = mime_encode_as_qp("a=b", NULL, 0);
  ASSERT_EQ(len, 6);
  len = mime_encode_as_qp("a=b", output, 6);
  ASSERT_EQ(len, 6);
  ASSERT_EQ(string("a=3Db"), string(output));

  len = mime_encode_as_qp("Libert\xc3\xa9", NULL, 0);
  ASSERT_EQ(len, 13);
  len = mime_encode_as_qp("Libert\xc3\xa9", output, 13);
  ASSERT_EQ(len, 13);
  ASSERT_EQ(string("Libert=C3=A9"), string(output));
}

TEST(MimeTests, EncodeOutOfSpace) {
  char output[256];
  memset(output, 0, sizeof(output));
  int len;

  len = mime_encode_as_qp("abcdefg", NULL, 0);
  ASSERT_EQ(len, 8);
  len = mime_encode_as_qp("abcdefg", output, 4);
  ASSERT_EQ(len, 8);
  ASSERT_EQ(string("abc"), string(output));
  len = mime_encode_as_qp("abcdefg", output, 1);
  ASSERT_EQ(len, 8);
  ASSERT_EQ(string(""), string(output));

  len = mime_encode_as_qp("a=b", output, 2);
  ASSERT_EQ(len, 6);
  ASSERT_EQ(string("a"), string(output));
  len = mime_encode_as_qp("a=b", output, 3);
  ASSERT_EQ(len, 6);
  ASSERT_EQ(string("a"), string(output));
}

TEST(MimeTests, SimpleDecode) {
  char output[256];
  memset(output, 0, sizeof(output));
  int len;

  len = mime_decode_from_qp("abc", NULL, 0);
  ASSERT_EQ(len, 4);
  len = mime_decode_from_qp("abc", output, 4);
  ASSERT_EQ(len, 4);
  ASSERT_EQ(string("abc"), string(output));

  len = mime_decode_from_qp("a=3Db", NULL, 0);
  ASSERT_EQ(len, 4);
  len = mime_decode_from_qp("a=3Db", output, 4);
  ASSERT_EQ(len, 4);
  ASSERT_EQ(string("a=b"), string(output));

  len = mime_decode_from_qp("Libert=C3=A9", NULL, 0);
  ASSERT_EQ(len, 9);
  len = mime_decode_from_qp("Libert=C3=A9", output, 9);
  ASSERT_EQ(len, 9);
  ASSERT_EQ(string("Libert\xc3\xa9"), string(output));
}

TEST(MimeTests, LowercaseDecode) {
  char output[256];
  memset(output, 0, sizeof(output));
  int len;

  len = mime_decode_from_qp("Libert=c3=a9", NULL, 0);
  ASSERT_EQ(len, 9);
  len = mime_decode_from_qp("Libert=c3=a9", output, 9);
  ASSERT_EQ(len, 9);
  ASSERT_EQ(string("Libert\xc3\xa9"), string(output));
}

TEST(MimeTests, DecodeOutOfSpace) {
  char output[256];
  memset(output, 0, sizeof(output));
  int len;

  len = mime_decode_from_qp("abcdefg", NULL, 0);
  ASSERT_EQ(len, 8);
  len = mime_decode_from_qp("abcdefg", output, 4);
  ASSERT_EQ(len, 8);
  ASSERT_EQ(string("abc"), string(output));
  len = mime_decode_from_qp("abcdefg", output, 1);
  ASSERT_EQ(len, 8);
  ASSERT_EQ(string(""), string(output));

  len = mime_decode_from_qp("a=3Db", output, 2);
  ASSERT_EQ(len, 4);
  ASSERT_EQ(string("a"), string(output));
  len = mime_decode_from_qp("a=3Db", output, 3);
  ASSERT_EQ(len, 4);
  ASSERT_EQ(string("a="), string(output));
}

TEST(MimeTests, DecodeErrors) {
  char output[128];
  memset(output, 0, sizeof(output));
  int len;

  // incomplete escape sequence
  len = mime_decode_from_qp("boo=", output, sizeof(output));
  ASSERT_LT(len, 0);

  // invalid escape sequences
  len = mime_decode_from_qp("boo=gg", output, sizeof(output));
  ASSERT_LT(len, 0);
  len = mime_decode_from_qp("boo=g", output, sizeof(output));
  ASSERT_LT(len, 0);
  len = mime_decode_from_qp("boo==", output, sizeof(output));
  ASSERT_LT(len, 0);
  len = mime_decode_from_qp("boo=44bar=z", output, sizeof(output));
  ASSERT_LT(len, 0);

  // high bit should not be set in quoted-printable mime output
  unsigned char bad_input2[] = { 0x81, 0x6a, 0x0 };
  len = mime_decode_from_qp(reinterpret_cast<const char*>(bad_input2),
			    output, sizeof(output));
  ASSERT_LT(len, 0);
}