GCC Code Coverage Report


Directory: src/gate/
File: src/gate/net/emails.c
Date: 2026-10-19 17:06:30
Exec Total Coverage
Lines: 68 89 76.4%
Functions: 7 7 100.0%
Branches: 30 56 53.6%

Line Branch Exec Source
1 /* GATE PROJECT LICENSE:
2 +----------------------------------------------------------------------------+
3 | Copyright (c) 2018-2026, Stefan Meislinger <sm@opengate.at> |
4 | All rights reserved. |
5 | |
6 | Redistribution and use in source and binary forms, with or without |
7 | modification, are permitted provided that the following conditions are met:|
8 | |
9 | 1. Redistributions of source code must retain the above copyright notice, |
10 | this list of conditions and the following disclaimer. |
11 | 2. Redistributions in binary form must reproduce the above copyright |
12 | notice, this list of conditions and the following disclaimer in the |
13 | documentation and/or other materials provided with the distribution. |
14 | |
15 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"|
16 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
17 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
18 | ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
19 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
20 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
21 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
22 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
23 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
24 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
25 | THE POSSIBILITY OF SUCH DAMAGE. |
26 +----------------------------------------------------------------------------+
27 */
28
29 #include "gate/net/emails.h"
30 #include "gate/utilities.h"
31 #include "gate/results.h"
32 #include "gate/debugging.h"
33
34 3 gate_result_t gate_mail_message_create(gate_mail_message_t* message)
35 {
36 3 gate_result_t ret = GATE_RESULT_OUTOFMEMORY;
37
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 GATE_DEBUG_ASSERT(message != NULL);
38 do
39 {
40 3 gate_mem_clear(message, sizeof(gate_mail_message_t));
41
42 3 message->recipients = gate_util_stringarray_create();
43
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if (gate_arraylist_is_null(message->recipients)) break;
44
45 3 message->recipients_cc = gate_util_stringarray_create();
46
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if (gate_arraylist_is_null(message->recipients_cc)) break;
47
48 3 message->recipients_bcc = gate_util_stringarray_create();
49
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if (gate_arraylist_is_null(message->recipients_cc)) break;
50
51
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if (NULL == gate_map_create(&message->attachments, &gate_compare_string,
52 sizeof(gate_string_t), &gate_string_copy_constructor, &gate_string_destructor,
53 sizeof(gate_stream_t*), gate_object_ptr_copyctor, gate_object_ptr_dtor))
54 {
55 ✗ break;
56 }
57
58 /* initialization succeeded */
59 3 ret = GATE_RESULT_OK;
60 } while (0);
61
62
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (GATE_FAILED(ret))
63 {
64 ✗ gate_mail_message_destroy(message);
65 }
66 3 return ret;
67 }
68
69 2 gate_result_t gate_mail_message_copy(gate_mail_message_t* dest, gate_mail_message_t const* source)
70 {
71 gate_result_t ret;
72
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 GATE_DEBUG_ASSERT(dest != NULL);
73
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 GATE_DEBUG_ASSERT(source != NULL);
74 do
75 {
76 2 gate_mem_clear(dest, sizeof(gate_mail_message_t));
77
78
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (NULL == gate_string_clone(&dest->sender, &source->sender))
79 {
80 ✗ ret = GATE_RESULT_OUTOFMEMORY;
81 ✗ break;
82 }
83
84 2 dest->recipients = gate_arraylist_copy(source->recipients);
85
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (gate_arraylist_is_null(dest->recipients))
86 {
87 ✗ ret = GATE_RESULT_OUTOFMEMORY;
88 ✗ break;
89 }
90
91 2 dest->recipients_cc = gate_arraylist_copy(source->recipients_cc);
92
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (gate_arraylist_is_null(dest->recipients_cc))
93 {
94 ✗ ret = GATE_RESULT_OUTOFMEMORY;
95 ✗ break;
96 }
97
98 2 dest->recipients_bcc = gate_arraylist_copy(source->recipients_bcc);
99
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (gate_arraylist_is_null(dest->recipients_bcc))
100 {
101 ✗ ret = GATE_RESULT_OUTOFMEMORY;
102 ✗ break;
103 }
104
105
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (NULL == gate_string_clone(&dest->subject, &source->subject))
106 {
107 ✗ ret = GATE_RESULT_OUTOFMEMORY;
108 ✗ break;
109 }
110
111
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (NULL == gate_string_clone(&dest->text_body, &source->text_body))
112 {
113 ✗ ret = GATE_RESULT_OUTOFMEMORY;
114 ✗ break;
115 }
116
117
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (NULL == gate_string_clone(&dest->html_body, &source->html_body))
118 {
119 ✗ ret = GATE_RESULT_OUTOFMEMORY;
120 ✗ break;
121 }
122
123
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (NULL == gate_map_copy(&dest->attachments, &source->attachments))
124 {
125 ✗ ret = GATE_RESULT_OUTOFMEMORY;
126 ✗ break;
127 }
128 /* success state reached */
129 2 ret = GATE_RESULT_OK;
130 } while (0);
131
132
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (GATE_FAILED(ret))
133 {
134 ✗ gate_mail_message_destroy(dest);
135 }
136
137 2 return ret;
138 }
139
140
141 5 gate_result_t gate_mail_message_destroy(gate_mail_message_t* message)
142 {
143
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 GATE_DEBUG_ASSERT(message != NULL);
144
145 5 gate_string_release(&message->sender);
146 5 gate_arraylist_release(&message->recipients);
147 5 gate_arraylist_release(&message->recipients_cc);
148 5 gate_arraylist_release(&message->recipients_bcc);
149 5 gate_string_release(&message->subject);
150 5 gate_string_release(&message->text_body);
151 5 gate_string_release(&message->html_body);
152 5 gate_map_destroy(&message->attachments);
153 5 gate_mem_clear(message, sizeof(gate_mail_message_t));
154 5 return GATE_RESULT_OK;
155 }
156
157 10 gate_result_t gate_mail_message_add_recipient(gate_mail_message_t* message, gate_string_t const* recipient, gate_enumint_t recipient_type)
158 {
159 10 gate_string_t const* item_added = NULL;
160
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 GATE_DEBUG_ASSERT(message != NULL);
161
162
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
10 switch(recipient_type)
163 {
164 4 case GATE_MAIL_MESSAGE_RECIPIENT_TO: item_added = gate_util_stringarray_add(message->recipients, recipient); break;
165 3 case GATE_MAIL_MESSAGE_RECIPIENT_CC: item_added = gate_util_stringarray_add(message->recipients_cc, recipient); break;
166 3 case GATE_MAIL_MESSAGE_RECIPIENT_BCC: item_added = gate_util_stringarray_add(message->recipients_bcc, recipient); break;
167 ✗ default: return GATE_RESULT_INVALIDARG;
168 }
169
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 return item_added == NULL ? GATE_RESULT_OUTOFMEMORY : GATE_RESULT_OK;
170 }
171
172 13 gate_result_t gate_mail_message_clear_recipients(gate_mail_message_t* message, gate_enumint_t recipient_type)
173 {
174 gate_arraylist_t* ptr_lst;
175
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 GATE_DEBUG_ASSERT(message != NULL);
176
177
3/4
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
13 switch(recipient_type)
178 {
179 5 case GATE_MAIL_MESSAGE_RECIPIENT_TO: ptr_lst = &message->recipients; break;
180 4 case GATE_MAIL_MESSAGE_RECIPIENT_CC: ptr_lst = &message->recipients_cc; break;
181 4 case GATE_MAIL_MESSAGE_RECIPIENT_BCC: ptr_lst = &message->recipients_bcc; break;
182 ✗ default: return GATE_RESULT_INVALIDARG;
183 }
184 13 gate_arraylist_clear(*ptr_lst);
185 13 return GATE_RESULT_OK;
186 }
187
188
189 2 gate_result_t gate_mail_message_add_attachment(gate_mail_message_t* message, gate_string_t const* filename, gate_stream_t* readable_stream)
190 {
191
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 GATE_DEBUG_ASSERT(message != NULL);
192 2 return gate_map_add(&message->attachments, filename, &readable_stream) == NULL
193 ? GATE_RESULT_FAILED
194
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 : GATE_RESULT_OK;
195 }
196
197 1 void gate_mail_message_clear_attachments(gate_mail_message_t* message)
198 {
199
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 GATE_DEBUG_ASSERT(message != NULL);
200 1 gate_map_clear(&message->attachments);
201 1 }
202