File: fltcnt.c

package info (click to toggle)
mailutils 1%3A3.19-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 38,832 kB
  • sloc: ansic: 186,739; sh: 110,867; yacc: 7,463; cpp: 3,834; makefile: 3,161; lex: 1,972; python: 1,617; exp: 1,563; awk: 152; lisp: 132; sed: 31
file content (227 lines) | stat: -rw-r--r-- 6,026 bytes parent folder | download | duplicates (2)
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
NAME
  fltcnt - checks transport reference counter changes during filter creation.

SYNOPSIS
  fltcnt [NAME...]
  
DESCRIPTION
  Any filter creation call should behave as follows:

  On success, it shall increment the reference counter in its "transport"
  argument by 1.
  On error, the reference counter shall remain unchanged.

  The following functions failed to meet these requirements in mailutils
  versions prior to 2021-07-15:

  mu_filter_chain_create
    On error, this function decremented the transport reference counter.
    This happened, in particular, if an invalid filter has been requested.
    However, if improper arguments were given to a valid filter, the
    mu_filter_stream_create bug described below would compensate for this.

    To check for this bug, the program does two mu_filter_chain_create
    calls: one with a valid filter name, and another one with a non-existing
    filter name.  Reference counter values are verified after each call.
    
  mu_filter_stream_create
    If the mu_filter_xcode_t function failed in mu_filter_init request,
    mu_filter_stream_create would leave the transport reference counter
    incremented by one.

  By default, the program runs both tests in this order.  If any of them
  fails, it prints two diagnostic messages: the first one describes where
  exactly the program failed to meet its expectations and the second one
  gives the name of the test that failed.

  To run a single test, give its name as a command line argument.
 
LICENSE
  GNU Mailutils -- a suite of utilities for electronic mail
  Copyright (C) 2021-2025 Free Software Foundation, Inc.

  GNU Mailutils is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 3, or (at your option)
  any later version.

  GNU Mailutils is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
*/  
  
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdlib.h>
#include <mailutils/mailutils.h>
#include <mailutils/sys/stream.h>

int
test_mu_filter_chain_create (mu_stream_t in)
{
  mu_stream_t flt;
  int rc;
  char *fargv[] = { "7bit", "+", "7bit", NULL };
  int init_ref_count;

  /* Save the initial reference counter. */
  init_ref_count = in->ref_count;

  /*
   * First pass.
   *
   * Check if input reference counter increases by 1 after successful
   * call to mu_filter_chain_create.
   */
  
  /* Create valid filter chain */
  rc = mu_filter_chain_create (&flt, in,
			       MU_FILTER_ENCODE,
			       MU_STREAM_READ,
			       MU_ARRAY_SIZE(fargv) - 1,
			       (char**) fargv);
  if (rc != 0)
    {
      mu_diag_funcall (MU_DIAG_ERROR, "first mu_filter_chain_create", NULL, rc);
      return 1;
    }

  /* Check if ref_count value is as expected */
  if (in->ref_count != init_ref_count + 1)
    {
      mu_error ("success filter: unexpected ref_count: %lu",
		(unsigned long) in->ref_count);
      return 1;
    }

  /* Destroy the filter and check if ref_count dropped to its initial value. */
  mu_stream_destroy (&flt);
      
  if (in->ref_count != init_ref_count)
    {
      mu_error ("after destroying success filter: unexpected ref_count: %lu",
		(unsigned long) in->ref_count);
      return 1;
    }

  /*
   * Second pass.
   *
   * Check if input reference counter remains unchanged after a failed
   * call to mu_filter_chain_create.
   */

  /* Request a non-existing filter. */
  fargv[2] = "there_is_no_such_filter";
  rc = mu_filter_chain_create (&flt, in,
			       MU_FILTER_ENCODE,
			       MU_STREAM_READ,
			       MU_ARRAY_SIZE(fargv) - 1,
			       (char**) fargv);
  if (rc == 0)
    {
      mu_error ("second mu_filter_chain_create succeeded where it should not");
      return 1;
    }

  if (in->ref_count != init_ref_count)
    {
      mu_error ("after failed filter attempt: unexpected ref_count: %lu",
		(unsigned long) in->ref_count);
      return 1;
    }
  
  return 0;
}

static enum mu_filter_result
xfail_transcoder (void *xdata,
		  enum mu_filter_command cmd,
		  struct mu_filter_io *iobuf)
{
  if (cmd == mu_filter_init)
    {
      iobuf->errcode = MU_ERR_USER0;
      return mu_filter_failure;
    }
  return mu_filter_ok;
}

int
test_mu_filter_stream_create (mu_stream_t in)
{
  int rc;
  int init_ref_count;
  mu_stream_t flt;
  void *data = malloc(1);
  
  /* Save the initial reference counter. */
  init_ref_count = in->ref_count;

  rc = mu_filter_stream_create (&flt, in, 
				MU_FILTER_ENCODE, 
				xfail_transcoder,
				data, MU_STREAM_READ);

  if (rc != MU_ERR_USER0)
    {
      mu_diag_funcall (MU_DIAG_ERROR, "mu_filter_stream_create", NULL, rc);
      return 1;
    }

  if (in->ref_count != init_ref_count)
    {
      mu_error ("after failed mu_filter_stream_create: unexpected ref_count: %lu",
		(unsigned long) in->ref_count);
      return 1;
    }
  return 0;
}

int
main (int argc, char **argv)
{
  mu_stream_t in;
  static struct testtab
  {
    char *name;
    int (*test) (mu_stream_t);
  } testtab[] = {
    { "mu_filter_chain_create", test_mu_filter_chain_create },
    { "mu_filter_stream_create", test_mu_filter_stream_create },
    { NULL }
  };
  int i;
  
  /* Create input stream and increase its reference counter. */ 
  MU_ASSERT (mu_stdio_stream_create (&in, MU_STDIN_FD, 0));
  mu_stream_ref (in);

  for (i = 0; testtab[i].name; i++)
    {
      int rc;
      
      if (argc > 1)
	{
	  int j;
	  for (j = 1; j < argc; j++)
	    if (strcmp (argv[j], testtab[i].name) == 0)
	      break;
	  if (j == argc)
	    continue;
	}
      rc = testtab[i].test (in);
      if (rc)
	{
	  mu_error ("%s: FAIL", testtab[i].name);
	  return rc;
	}
    }
  return 0;
}