1 /*
2  * This library is free software: you can redistribute it and/or modify it
3  * under the terms of the GNU Lesser General Public License as published by
4  * the Free Software Foundation.
5  *
6  * This library is distributed in the hope that it will be useful, but
7  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
8  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
9  * for more details.
10  *
11  * You should have received a copy of the GNU Lesser General Public License
12  * along with this library. If not, see <http://www.gnu.org/licenses/>.
13  */
14 
15 /*
16   test1.c
17  *
18   Create a message, save it.
19  *
20   Retrieve message, compare content.
21  *
22   Operations:
23 	writing / loading from different types of streams
24 	reading / writing different content
25 	reading / writing different encodings
26 	reading / writing different charsets
27  *
28   Just testing streams:
29 	different stream types
30 	different file ops
31 	seek, eof, etc.
32 */
33 
34 #include "camel-test.h"
35 #include "messages.h"
36 
37 /* for stat */
38 #include <sys/stat.h>
39 #include <unistd.h>
40 #include <string.h>
41 
42 struct _text {
43 	gchar *text;
44 	gint len;
45 };
46 
47 #define MAX_TEXTS (14)
48 struct _text texts[MAX_TEXTS];
49 
50 static void
setup(void)51 setup (void)
52 {
53 	gint i, j;
54 	gchar *p;
55 
56 	/* setup various edge and other general cases */
57 	texts[0].text = g_strdup ("");
58 	texts[0].len = 0;
59 	texts[1].text = g_strdup ("");
60 	texts[1].len = 1;
61 	texts[2].text = g_strdup ("\n");
62 	texts[2].len = 1;
63 	texts[3].text = g_strdup ("A");
64 	texts[3].len = 1;
65 	texts[4].text = g_strdup ("This is a test.\n.");
66 	texts[4].len = strlen (texts[4].text);
67 	texts[5].text = g_strdup ("This is a test.\n\n.\n");
68 	texts[5].len = strlen (texts[5].text);
69 	texts[6].text = g_malloc0 (1024);
70 	texts[6].len = 1024;
71 	texts[7].text = g_malloc0 (102400);
72 	texts[7].len = 102400;
73 	texts[8].text = g_malloc (1024);
74 	memset (texts[8].text, '\n', 1024);
75 	texts[8].len = 1024;
76 	texts[9].text = g_malloc (102400);
77 	memset (texts[9].text, '\n', 102400);
78 	texts[9].len = 102400;
79 	texts[10].text = g_malloc (1024);
80 	memset (texts[10].text, ' ', 1024);
81 	texts[10].len = 1024;
82 	texts[11].text = g_malloc (102400);
83 	memset (texts[11].text, ' ', 102400);
84 	texts[11].len = 102400;
85 
86 	srand (42);
87 	p = texts[12].text = g_malloc (1024);
88 	for (i = 0; i < 1024; i++) {
89 		j = g_random_int ();
90 		if (j < G_MAXUINT32 / 120)
91 			*p++ = '\n';
92 		else
93 			*p++ = (j % 95) + 32;
94 	}
95 	texts[12].len = 1024;
96 	p = texts[13].text = g_malloc (102400);
97 	for (i = 0; i < 102400; i++) {
98 		j = g_random_int ();
99 		if (j < G_MAXUINT32 / 120)
100 			*p++ = '\n';
101 		else
102 			*p++ = (j % 95) + 32;
103 	}
104 	texts[13].len = 102400;
105 }
106 
107 static void
cleanup(void)108 cleanup (void)
109 {
110 	gint i;
111 
112 	for (i = 0; i < MAX_TEXTS; i++)
113 		g_free (texts[i].text);
114 }
115 
116 gint
main(gint argc,gchar ** argv)117 main (gint argc,
118       gchar **argv)
119 {
120 	CamelMimeMessage *msg, *msg2;
121 	gint i, j;
122 	gchar *text;
123 	gint len;
124 
125 	camel_test_init (argc, argv);
126 
127 	setup ();
128 
129 	camel_test_start ("Simple memory-based content creation");
130 
131 	/* test all ways of setting simple content for a message (i.e. memory based) */
132 	for (j = 0; j < MAX_TEXTS; j++) {
133 		push ("testing text number %d", j);
134 		text = texts[j].text;
135 		len = texts[j].len;
136 		for (i = 0; i < SET_CONTENT_WAYS; i++) {
137 			push ("create simple message %d", i);
138 			msg = test_message_create_simple ();
139 
140 			push ("set simple content");
141 			test_message_set_content_simple ((CamelMimePart *) msg, i, "text/plain", text, len);
142 			pull ();
143 
144 			push ("compare original content");
145 			test_message_compare_content (camel_medium_get_content ((CamelMedium *) msg), text, len);
146 			pull ();
147 
148 			push ("save message to test1.msg");
149 			unlink ("test1.msg");
150 			test_message_write_file (msg, "test1.msg");
151 			check_unref (msg, 1);
152 			pull ();
153 
154 			push ("read from test1.msg");
155 			msg2 = test_message_read_file ("test1.msg");
156 			pull ();
157 
158 			push ("compare read with original content");
159 			test_message_compare_content (camel_medium_get_content ((CamelMedium *) msg2), text, len);
160 			check_unref (msg2, 1);
161 			pull ();
162 
163 			unlink ("test1.msg");
164 			pull ();
165 		}
166 		pull ();
167 	}
168 
169 	camel_test_end ();
170 
171 	camel_test_start ("Different encodings");
172 	for (j = 0; j < MAX_TEXTS; j++) {
173 		push ("testing text number %d", j);
174 		text = texts[j].text;
175 		len = texts[j].len;
176 		for (i = 0; i < CAMEL_TRANSFER_NUM_ENCODINGS; i++) {
177 
178 			push ("test simple message, encoding %s", camel_transfer_encoding_to_string (i));
179 			msg = test_message_create_simple ();
180 
181 			push ("set simple content");
182 			test_message_set_content_simple ((CamelMimePart *) msg, 0, "text/plain", text, len);
183 			pull ();
184 
185 			camel_mime_part_set_encoding ((CamelMimePart *) msg, i);
186 
187 			push ("save message to test1.msg");
188 			unlink ("test1.msg");
189 			test_message_write_file (msg, "test1.msg");
190 			check_unref (msg, 1);
191 			pull ();
192 
193 			push ("read from test1.msg");
194 			msg2 = test_message_read_file ("test1.msg");
195 			pull ();
196 
197 			push ("compare read with original content");
198 			test_message_compare_content (camel_medium_get_content ((CamelMedium *) msg2), text, len);
199 			check_unref (msg2, 1);
200 			pull ();
201 
202 			unlink ("test1.msg");
203 			pull ();
204 		}
205 		pull ();
206 	}
207 	camel_test_end ();
208 
209 	cleanup ();
210 
211 	return 0;
212 }
213