File: Util.c

package info (click to toggle)
cunit 2.1-3-dfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 4,196 kB
  • sloc: sh: 9,860; ansic: 9,503; cpp: 1,270; makefile: 380; perl: 45
file content (609 lines) | stat: -rw-r--r-- 17,227 bytes parent folder | download | duplicates (4)
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
/*
 *  CUnit - A Unit testing framework library for C.
 *  Copyright (C) 2001       Anil Kumar
 *  Copyright (C) 2004-2006  Anil Kumar, Jerry St.Clair
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library 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
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/*
 *  Generic (internal) utility functions used across CUnit.
 *  These were originally distributed across the other CUnit
 *  source files, but were consolidated here for consistency.
 *
 *  13/Oct/2001   Initial implementation (AK)
 *
 *  26/Jul/2003   Added a function to convert a string containing special
 *                characters into escaped character for XML/HTML usage. (AK)
 *
 *  16-Jul-2004   New interface, doxygen comments. (JDS)
 *
 *  17-Apr-2006   Added CU_translated_strlen() and CU_number_width().
 *                Fixed off-by-1 error in CU_translate_special_characters(),
 *                modifying implementation & results in some cases.  User can
 *                now tell if conversion failed. (JDS)
 */

/** @file
 *  Utility functions (implementation).
 */
/** @addtogroup Framework
 @{
*/

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <assert.h>
#include <string.h>

#include "CUnit.h"
#include "TestDB.h"
#include "Util.h"


/*------------------------------------------------------------------------*/
/**
 *  Structure containing mappings of special characters to xml entity codes.
 *  special_char's in the CU_bindings array will be translated during calls
 *  to CU_translate_special_characters().  Add additional chars/replacements
 *  or modify existing ones to change the behavior upon translation.
 */
static const struct bindings {
	const char special_char;    /**< Special character. */
	const char *replacement;    /**< Entity code for special character. */
} CU_bindings [] = {
    {'&', "&amp;"},
    {'>', "&gt;"},
    {'<', "&lt;"}
};

/*------------------------------------------------------------------------*/
/**
 *  Checks whether a character is a special xml character.
 *  This function performs a lookup of the specified character in
 *  the CU_bindings structure.  If it is a special character, its
 *  index into the CU_bindings array is returned.  If not, -1 is returned.
 *
 *  @param ch The character to check
 *  @return Index into CU_bindings if a special character, -1 otherwise.
 */
static int get_index(const char ch)
{
	int length = sizeof(CU_bindings)/sizeof(CU_bindings[0]);
	int counter;

	for (counter = 0; counter < length && CU_bindings[counter].special_char != ch; ++counter) {
		;
	}

	return (counter < length ? counter : -1);
}

size_t CU_translate_special_characters(const char *szSrc, char *szDest, size_t maxlen)
{
/* old implementation
  size_t count = 0;
	size_t src = 0;
	size_t dest = 0;
	size_t length = 0;
	int conv_index;

  assert(NULL != szSrc);
  assert(NULL != szDest);

	length = strlen(szSrc);
	memset(szDest, 0, maxlen);
	while ((dest < maxlen) && (src < length)) {

		if ((-1 != (conv_index = get_index(szSrc[src]))) &&
        ((dest + strlen(CU_bindings[conv_index].replacement)) < maxlen)) {
			strcat(szDest, CU_bindings[conv_index].replacement);
			dest += strlen(CU_bindings[conv_index].replacement);
			++count;
		} else {
			szDest[dest++] = szSrc[src];
		}

		++src;
	}

	return count;
*/
  size_t count = 0;
  size_t repl_len;
  int conv_index;
  char *dest_start = szDest;

  assert(NULL != szSrc);
  assert(NULL != szDest);

  /* only process if destination buffer not 0-length */
  if (maxlen > 0) {

    while ((maxlen > 0) && (*szSrc != '\0')) {
      conv_index = get_index(*szSrc);
      if (-1 != conv_index) {
        if (maxlen > (repl_len = strlen(CU_bindings[conv_index].replacement))) {
			    memcpy(szDest, CU_bindings[conv_index].replacement, repl_len);
			    szDest += repl_len;
          maxlen -= repl_len;
			    ++count;
        } else {
          maxlen = 0;   /* ran out of room - abort conversion */
          break;
        }
		  } else {
			  *szDest++ = *szSrc;
        --maxlen;
		  }
		  ++szSrc;
	  }

    if (0 == maxlen) {
      *dest_start = '\0';   /* ran out of room - return empty string in szDest */
      count = 0;
    } else {
      *szDest = '\0';       /* had room - make sure szDest has a terminating \0 */
    }
  }
	return count;
}

/*------------------------------------------------------------------------*/
size_t CU_translated_strlen(const char* szSrc)
{
	size_t count = 0;
  int conv_index;

  assert(NULL != szSrc);

	while (*szSrc != '\0') {
    if (-1 != (conv_index = get_index(*szSrc))) {
      count += strlen(CU_bindings[conv_index].replacement);
    } else {
      ++count;
    }
    ++szSrc;
  }
	return count;
}

/*------------------------------------------------------------------------*/
int CU_compare_strings(const char* szSrc, const char* szDest)
{
  assert(NULL != szSrc);
  assert(NULL != szDest);

	while (('\0' != *szSrc) && ('\0' != *szDest) && (toupper(*szSrc) == toupper(*szDest))) {
		szSrc++;
		szDest++;
	}

	return (int)(*szSrc - *szDest);
}

/*------------------------------------------------------------------------*/
void CU_trim(char* szString)
{
	CU_trim_left(szString);
	CU_trim_right(szString);
}

/*------------------------------------------------------------------------*/
void CU_trim_left(char* szString)
{
	int nOffset = 0;
	char* szSrc = szString;
	char* szDest = szString;

	assert(NULL != szString);

	/* Scan for the spaces in the starting of string. */
	for (; '\0' != *szSrc; szSrc++, nOffset++) {
		if (!isspace(*szSrc)) {
			break;
		}
	}

	for(; (0 != nOffset) && ('\0' != (*szDest = *szSrc)); szSrc++, szDest++) {
		;
	}
}

/*------------------------------------------------------------------------*/
void CU_trim_right(char* szString)
{
	size_t nLength;
	char* szSrc = szString;

	assert(NULL != szString);
	nLength = strlen(szString);
	/*
	 * Scan for specs in the end of string.
	 */
	for (; (0 != nLength) && isspace(*(szSrc + nLength - 1)); nLength--) {
		;
	}

	*(szSrc + nLength) = '\0';
}

/*------------------------------------------------------------------------*/
size_t CU_number_width(int number)
{
	char buf[33];

	snprintf(buf, 33, "%d", number);
	buf[32] = '\0';
	return (strlen(buf));
}

/** @} */

#ifdef CUNIT_BUILD_TESTS
#include "test_cunit.h"

/* Keep BUF_LEN even or trouble ensues below... */
#define BUF_LEN 1000
#define MAX_LEN BUF_LEN/2

static void test_CU_translate_special_characters(void)
{
  char dest_buf[BUF_LEN];
  char *dest = dest_buf + MAX_LEN;
  char ref_buf[BUF_LEN];
  const int mask_char = 0x01;   /* char written to buffer  */

  /* set up reference buffer for testing of translated strings */
  memset(ref_buf, mask_char, BUF_LEN);

  /* empty src */
  memset(dest_buf, mask_char, BUF_LEN);
  TEST(0 == CU_translate_special_characters("", dest, MAX_LEN));
  TEST(!strncmp(dest_buf, ref_buf, MAX_LEN));
  TEST(!strncmp(dest, "\0", 1));
  TEST(!strncmp((dest+1), ref_buf, MAX_LEN-1));

  /* 1 char src */
  memset(dest_buf, mask_char, BUF_LEN);
  TEST(0 == CU_translate_special_characters("#", dest, 0));
  TEST(!strncmp(dest_buf, ref_buf, BUF_LEN));

  memset(dest_buf, mask_char, BUF_LEN);
  TEST(0 == CU_translate_special_characters("#", dest, 1));
  TEST(!strncmp(dest_buf, ref_buf, MAX_LEN));
  TEST(!strncmp(dest, "\0", 1));
  TEST(!strncmp((dest+1), ref_buf, MAX_LEN-1));

  memset(dest_buf, mask_char, BUF_LEN);
  TEST(0 == CU_translate_special_characters("&", dest, 2));
  TEST(!strncmp(dest_buf, ref_buf, MAX_LEN));
  TEST(!strncmp(dest, "\0", 1));
  TEST(!strncmp((dest+2), ref_buf, MAX_LEN-2));

  memset(dest_buf, mask_char, BUF_LEN);
  TEST(0 == CU_translate_special_characters("&", dest, 4));
  TEST(!strncmp(dest_buf, ref_buf, MAX_LEN));
  TEST(!strncmp(dest_buf, ref_buf, MAX_LEN));
  TEST(!strncmp(dest, "\0", 1));
  TEST(!strncmp((dest+4), ref_buf, MAX_LEN-4));

  memset(dest_buf, mask_char, BUF_LEN);
  TEST(0 == CU_translate_special_characters("&", dest, 5));
  TEST(!strncmp(dest_buf, ref_buf, MAX_LEN));
  TEST(!strncmp(dest, "\0", 1));
  TEST(!strncmp((dest+5), ref_buf, MAX_LEN-5));

  memset(dest_buf, mask_char, BUF_LEN);
  TEST(1 == CU_translate_special_characters("&", dest, 6));
  TEST(!strncmp(dest_buf, ref_buf, MAX_LEN));
  TEST(!strncmp(dest, "&amp;\0", 6));
  TEST(!strncmp((dest+6), ref_buf, MAX_LEN-6));

  /* maxlen=0 */
  memset(dest_buf, mask_char, BUF_LEN);
  strcpy(dest, "random initialized string");
  TEST(0 == CU_translate_special_characters("some <<string & another>", dest, 0));
  TEST(!strncmp(dest_buf, ref_buf, MAX_LEN));
  TEST(!strcmp(dest, "random initialized string"));
  TEST(!strncmp(dest+strlen(dest)+1, ref_buf, MAX_LEN-strlen(dest)-1));

  /* maxlen < len(converted szSrc) */
  memset(dest_buf, mask_char, BUF_LEN);
  TEST(0 == CU_translate_special_characters("some <<string & another>", dest, 1));
  TEST(!strncmp(dest_buf, ref_buf, MAX_LEN));
  TEST(!strncmp(dest, "\0", 1));
  TEST(!strncmp((dest+1), ref_buf, MAX_LEN-1));

  memset(dest_buf, mask_char, BUF_LEN);
  TEST(0 == CU_translate_special_characters("some <<string & another>", dest, 2));
  TEST(!strncmp(dest_buf, ref_buf, MAX_LEN));
  TEST(!strncmp(dest, "\0", 1));
  TEST(!strncmp((dest+2), ref_buf, MAX_LEN-2));

  memset(dest_buf, mask_char, BUF_LEN);
  TEST(0 == CU_translate_special_characters("some <<string & another>", dest, 5));
  TEST(!strncmp(dest_buf, ref_buf, MAX_LEN));
  TEST(!strncmp(dest, "\0", 1));
  TEST(!strncmp((dest+5), ref_buf, MAX_LEN-5));

  memset(dest_buf, mask_char, BUF_LEN);
  TEST(0 == CU_translate_special_characters("some <<string & another>", dest, 10));
  TEST(!strncmp(dest_buf, ref_buf, MAX_LEN));
  TEST(!strncmp(dest, "\0", 1));
  TEST(!strncmp((dest+10), ref_buf, MAX_LEN-10));

  memset(dest_buf, mask_char, BUF_LEN);
  TEST(0 == CU_translate_special_characters("some <<string & another>", dest, 20));
  TEST(!strncmp(dest_buf, ref_buf, MAX_LEN));
  TEST(!strncmp(dest, "\0", 1));
  TEST(!strncmp((dest+20), ref_buf, MAX_LEN-20));

  memset(dest_buf, mask_char, BUF_LEN);
  TEST(0 == CU_translate_special_characters("some <<string & another>", dest, 24));
  TEST(!strncmp(dest_buf, ref_buf, MAX_LEN));
  TEST(!strncmp(dest, "\0", 1));
  TEST(!strncmp((dest+24), ref_buf, MAX_LEN-24));

  memset(dest_buf, mask_char, BUF_LEN);
  TEST(0 == CU_translate_special_characters("some <<string & another>", dest, 25));
  TEST(!strncmp(dest_buf, ref_buf, MAX_LEN));
  TEST(!strncmp(dest, "\0", 1));
  TEST(!strncmp((dest+25), ref_buf, MAX_LEN-25));

  memset(dest_buf, mask_char, BUF_LEN);
  TEST(0 == CU_translate_special_characters("some <<string & another>", dest, 37));
  TEST(!strncmp(dest_buf, ref_buf, MAX_LEN));
  TEST(!strncmp(dest, "\0", 1));
  TEST(!strncmp((dest+37), ref_buf, MAX_LEN-37));

  /* maxlen > len(converted szSrc) */
  memset(dest_buf, mask_char, BUF_LEN);
  TEST(4 == CU_translate_special_characters("some <<string & another>", dest, 38));
  TEST(!strncmp(dest_buf, ref_buf, MAX_LEN));
  TEST(!strncmp(dest, "some &lt;&lt;string &amp; another&gt;\0", 38));
  TEST(!strncmp((dest+38), ref_buf, MAX_LEN-38));

  /* maxlen > len(converted szSrc) */
  memset(dest_buf, mask_char, BUF_LEN);
  TEST(4 == CU_translate_special_characters("some <<string & another>", dest, MAX_LEN));
  TEST(!strncmp(dest_buf, ref_buf, MAX_LEN));
  TEST(!strncmp(dest, "some &lt;&lt;string &amp; another&gt;\0", 38));

  /* no special characters */
  memset(dest_buf, mask_char, BUF_LEN);
  TEST(0 == CU_translate_special_characters("some string or another", dest, MAX_LEN));
  TEST(!strncmp(dest_buf, ref_buf, MAX_LEN));
  TEST(!strncmp(dest, "some string or another\0", 23));

  /* only special characters */
  memset(dest_buf, mask_char, BUF_LEN);
  TEST(11 == CU_translate_special_characters("<><><<>>&&&", dest, MAX_LEN));
  TEST(!strncmp(dest_buf, ref_buf, MAX_LEN));
  TEST(!strcmp(dest, "&lt;&gt;&lt;&gt;&lt;&lt;&gt;&gt;&amp;&amp;&amp;"));
}

static void test_CU_translated_strlen(void)
{
  /* empty src */
  TEST(0 == CU_translated_strlen(""));

  /* 1 char src */
  TEST(1 == CU_translated_strlen("#"));
  TEST(5 == CU_translated_strlen("&"));
  TEST(4 == CU_translated_strlen("<"));
  TEST(4 == CU_translated_strlen(">"));
  TEST(1 == CU_translated_strlen("?"));

  /* 2 char src */
  TEST(2 == CU_translated_strlen("#@"));
  TEST(10 == CU_translated_strlen("&&"));
  TEST(9 == CU_translated_strlen(">&"));

  /* longer src */
  TEST(37 == CU_translated_strlen("some <<string & another>"));
  TEST(22 == CU_translated_strlen("some string or another"));
  TEST(47 == CU_translated_strlen("<><><<>>&&&"));
}

static void test_CU_compare_strings(void)
{
  TEST(0 == CU_compare_strings("",""));
  TEST(0 == CU_compare_strings("@","@"));
  TEST(0 == CU_compare_strings("D","d"));
  TEST(0 == CU_compare_strings("s1","s1"));
  TEST(0 == CU_compare_strings("s1","S1"));
  TEST(0 != CU_compare_strings("s1","s12"));
  TEST(0 == CU_compare_strings("this is string 1","tHIS iS sTRING 1"));
  TEST(0 == CU_compare_strings("i have \t a tab!","I have \t a tab!"));
  TEST(0 != CU_compare_strings("not the same"," not the same"));
}

static void test_CU_trim(void)
{
  char string[MAX_LEN];

  strcpy(string, "");
  CU_trim(string);
  TEST(!strcmp("", string));

  strcpy(string, " ");
  CU_trim(string);
  TEST(!strcmp("", string));

  strcpy(string, "    ");
  CU_trim(string);
  TEST(!strcmp("", string));

  strcpy(string, " b");
  CU_trim(string);
  TEST(!strcmp("b", string));

  strcpy(string, "  B");
  CU_trim(string);
  TEST(!strcmp("B", string));

  strcpy(string, "s ");
  CU_trim(string);
  TEST(!strcmp("s", string));

  strcpy(string, "S  ");
  CU_trim(string);
  TEST(!strcmp("S", string));

  strcpy(string, "  5   ");
  CU_trim(string);
  TEST(!strcmp("5", string));

  strcpy(string, "~ & ^ ( ^  ");
  CU_trim(string);
  TEST(!strcmp("~ & ^ ( ^", string));

  strcpy(string, "  ~ & ^ ( ^");
  CU_trim(string);
  TEST(!strcmp("~ & ^ ( ^", string));

  strcpy(string, "  ~ & ^ ( ^  ");
  CU_trim(string);
  TEST(!strcmp("~ & ^ ( ^", string));
}

static void test_CU_trim_left(void)
{
  char string[MAX_LEN];

  strcpy(string, "");
  CU_trim_left(string);
  TEST(!strcmp("", string));

  strcpy(string, " ");
  CU_trim_left(string);
  TEST(!strcmp("", string));

  strcpy(string, "    ");
  CU_trim_left(string);
  TEST(!strcmp("", string));

  strcpy(string, " b");
  CU_trim_left(string);
  TEST(!strcmp("b", string));

  strcpy(string, "  B");
  CU_trim_left(string);
  TEST(!strcmp("B", string));

  strcpy(string, "s ");
  CU_trim_left(string);
  TEST(!strcmp("s ", string));

  strcpy(string, "S  ");
  CU_trim_left(string);
  TEST(!strcmp("S  ", string));

  strcpy(string, "  5   ");
  CU_trim_left(string);
  TEST(!strcmp("5   ", string));

  strcpy(string, "~ & ^ ( ^  ");
  CU_trim_left(string);
  TEST(!strcmp("~ & ^ ( ^  ", string));

  strcpy(string, "  ~ & ^ ( ^");
  CU_trim_left(string);
  TEST(!strcmp("~ & ^ ( ^", string));

  strcpy(string, "  ~ & ^ ( ^  ");
  CU_trim_left(string);
  TEST(!strcmp("~ & ^ ( ^  ", string));
}

static void test_CU_trim_right(void)
{
  char string[MAX_LEN];

  strcpy(string, "");
  CU_trim_right(string);
  TEST(!strcmp("", string));

  strcpy(string, " ");
  CU_trim_right(string);
  TEST(!strcmp("", string));

  strcpy(string, "    ");
  CU_trim_right(string);
  TEST(!strcmp("", string));

  strcpy(string, " b");
  CU_trim_right(string);
  TEST(!strcmp(" b", string));

  strcpy(string, "  B");
  CU_trim_right(string);
  TEST(!strcmp("  B", string));

  strcpy(string, "s ");
  CU_trim_right(string);
  TEST(!strcmp("s", string));

  strcpy(string, "S  ");
  CU_trim_right(string);
  TEST(!strcmp("S", string));

  strcpy(string, "  5   ");
  CU_trim_right(string);
  TEST(!strcmp("  5", string));

  strcpy(string, "~ & ^ ( ^  ");
  CU_trim_right(string);
  TEST(!strcmp("~ & ^ ( ^", string));

  strcpy(string, "  ~ & ^ ( ^");
  CU_trim_right(string);
  TEST(!strcmp("  ~ & ^ ( ^", string));

  strcpy(string, "  ~ & ^ ( ^  ");
  CU_trim_right(string);
  TEST(!strcmp("  ~ & ^ ( ^", string));
}

static void test_CU_number_width(void)
{
  TEST(1 == CU_number_width(0));
  TEST(1 == CU_number_width(1));
  TEST(2 == CU_number_width(-1));
  TEST(4 == CU_number_width(2346));
  TEST(7 == CU_number_width(-257265));
  TEST(9 == CU_number_width(245723572));
  TEST(9 == CU_number_width(-45622572));
}

void test_cunit_Util(void)
{

  test_cunit_start_tests("Util.c");

  test_CU_translate_special_characters();
  test_CU_translated_strlen();
  test_CU_compare_strings();
  test_CU_trim();
  test_CU_trim_left();
  test_CU_trim_right();
  test_CU_number_width();

  test_cunit_end_tests();
}

#endif    /* CUNIT_BUILD_TESTS */