aboutsummaryrefslogtreecommitdiffstats
path: root/camel/tests/message/test1.c
blob: 8dcf84c92cb97a8f3ae75144bbda7340546fe1b9 (plain) (blame)
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
  test1.c

  Create a message, save it.

  Retrieve message, compare content.


  Operations:
    writing/loading from different types of streams
    reading/writing different content
    reading/writing different encodings
    reading/writing different charsets

  Just testing streams:
    different stream types
    different file ops
    seek, eof, etc.
*/

#include "camel-test.h"
#include "messages.h"

/* for stat */
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>

#include <camel/camel-mime-message.h>
#include <camel/camel-stream-fs.h>
#include <camel/camel-stream-mem.h>

struct _text {
    char *text;
    int len;
};

#define MAX_TEXTS (14)
struct _text texts[MAX_TEXTS];

static void
setup(void)
{
    int i, j;
    char *p;

    /* setup various edge and other general cases */
    texts[0].text = "";
    texts[0].len = 0;
    texts[1].text = "";
    texts[1].len = 1;
    texts[2].text = "\n";
    texts[2].len = 1;
    texts[3].text = "A";
    texts[3].len = 1;
    texts[4].text = "This is a test.\n.";
    texts[4].len = strlen(texts[4].text);
    texts[5].text = "This is a test.\n\n.\n";
    texts[5].len = strlen(texts[5].text);
    texts[6].text = g_malloc0(1024);
    texts[6].len = 1024;
    texts[7].text = g_malloc0(102400);
    texts[7].len = 102400;
    texts[8].text = g_malloc(1024);
    memset(texts[8].text, '\n', 1024);
    texts[8].len = 1024;
    texts[9].text = g_malloc(102400);
    memset(texts[9].text, '\n', 102400);
    texts[9].len = 102400;
    texts[10].text = g_malloc(1024);
    memset(texts[10].text, ' ', 1024);
    texts[10].len = 1024;
    texts[11].text = g_malloc(102400);
    memset(texts[11].text, ' ', 102400);
    texts[11].len = 102400;

    srand(42);
    p = texts[12].text = g_malloc(1024);
    for (i=0;i<1024;i++) {
        j = rand();
        if (j<RAND_MAX/120)
            *p++ = '\n';
        else
            *p++ = (j % 95) + 32;
    }
    texts[12].len = 1024;
    p = texts[13].text = g_malloc(102400);
    for (i=0;i<102400;i++) {
        j = rand();
        if (j<RAND_MAX/120)
            *p++ = '\n';
        else
            *p++ = (j % 95) + 32;
    }
    texts[13].len = 102400;
}

static void cleanup(void)
{
    int i;

    for (i=6;i<14;i++) {
        g_free(texts[i].text);
    }
}

int main(int argc, char **argv)
{
    CamelMimeMessage *msg, *msg2;
    int i, j;
    char *text;
    int len;

    camel_test_init(argc, argv);

    setup();

    camel_test_start("Simple memory-based content creation");

    /* test all ways of setting simple content for a message (i.e. memory based) */
    for (j=0;j<MAX_TEXTS;j++) {
        push("testing text number %d", j);
        text = texts[j].text;
        len = texts[j].len;
        for (i=0;i<SET_CONTENT_WAYS;i++) {
            push("create simple message %d", i);
            msg = test_message_create_simple();

            push("set simple content");
            test_message_set_content_simple((CamelMimePart *)msg, i, "text/plain", text, len);
            pull();

            push("compare original content");
            test_message_compare_content(camel_medium_get_content_object((CamelMedium *)msg), text, len);
            pull();

            push("save message to test1.msg");
            unlink("test1.msg");
            test_message_write_file(msg, "test1.msg");
            check_unref(msg, 1);
            pull();
    
            push("read from test1.msg");
            msg2 = test_message_read_file("test1.msg");
            pull();

            push("compare read with original content");
            test_message_compare_content(camel_medium_get_content_object((CamelMedium *)msg2), text, len);
            check_unref(msg2, 1);
            pull();

            unlink("test1.msg");
            pull();
        }
        pull();
    }

    camel_test_end();

    camel_test_start("Different encodings");
    for (j=0;j<MAX_TEXTS;j++) {
        push("testing text number %d", j);
        text = texts[j].text;
        len = texts[j].len;
        for (i=0;i<CAMEL_TRANSFER_NUM_ENCODINGS;i++) {
            
            push("test simple message, encoding %s", camel_transfer_encoding_to_string(i));
            msg = test_message_create_simple();

            push("set simple content");
            test_message_set_content_simple((CamelMimePart *)msg, 0, "text/plain", text, len);
            pull();

            camel_mime_part_set_encoding((CamelMimePart *)msg, i);

            push("save message to test1.msg");
            unlink("test1.msg");
            test_message_write_file(msg, "test1.msg");
            check_unref(msg, 1);
            pull();
    
            push("read from test1.msg");
            msg2 = test_message_read_file("test1.msg");
            pull();

            push("compare read with original content");
            test_message_compare_content(camel_medium_get_content_object((CamelMedium *)msg2), text, len);
            check_unref(msg2, 1);
            pull();

            unlink("test1.msg");
            pull();
        }
        pull();
    }
    camel_test_end();

    cleanup();

    return 0;
}