File: test_util_safestrncpy.c

package info (click to toggle)
portsentry 2.0.5-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,396 kB
  • sloc: ansic: 6,473; sh: 916; perl: 18; makefile: 5
file content (81 lines) | stat: -rw-r--r-- 2,125 bytes parent folder | download
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "../src/util.h"

void TestSafeStrncpyNormalCase(void);
void TestSafeStrncpyNullDest(void);
void TestSafeStrncpyZeroSize(void);
void TestSafeStrncpyExactSize(void);
void TestSafeStrncpyTruncated(void);
void TestSafeStrncpySecurity(void);

void TestSafeStrncpyNormalCase(void) {
  char dest[10];
  const char *src = "test";
  char *result = SafeStrncpy(dest, src, sizeof(dest));

  assert(result == dest);
  assert(strcmp(dest, "test") == 0);
  printf("Normal case test passed\n");
}

void TestSafeStrncpyNullDest(void) {
  char *result = SafeStrncpy(NULL, "test", 10);
  assert(result == NULL);
  printf("Null destination test passed\n");
}

void TestSafeStrncpyZeroSize(void) {
  char dest[10];
  char *result = SafeStrncpy(dest, "test", 0);
  assert(result == NULL);
  printf("Zero size test passed\n");
}

void TestSafeStrncpyExactSize(void) {
  char dest[5];
  char *result = SafeStrncpy(dest, "test", 5);
  assert(result == dest);
  assert(strcmp(dest, "test") == 0);
  printf("Exact size test passed\n");
}

void TestSafeStrncpyTruncated(void) {
  char dest[4];
  char *result = SafeStrncpy(dest, "test", 4);
  assert(result == dest);
  assert(strcmp(dest, "tes") == 0);
  printf("Truncated test passed\n");
}

void TestSafeStrncpySecurity(void) {
  char dest[32];

  assert(SafeStrncpy(dest, "test", MAX_SAFESTRNCMP_SIZE + 1) == NULL);

  strcpy(dest, "test");
  assert(SafeStrncpy(dest + 1, dest, sizeof(dest) - 1) != NULL);
  assert(strcmp(dest + 1, "test") == 0);

  char *long_string = malloc(MAX_SAFESTRNCMP_SIZE);
  if (long_string) {
    memset(long_string, 'A', MAX_SAFESTRNCMP_SIZE - 1);
    long_string[MAX_SAFESTRNCMP_SIZE - 1] = '\0';
    assert(SafeStrncpy(dest, long_string, sizeof(dest)) != NULL);
    assert(strlen(dest) == sizeof(dest) - 1);
    free(long_string);
  }
}

int main(void) {
  TestSafeStrncpyNormalCase();
  TestSafeStrncpyNullDest();
  TestSafeStrncpyZeroSize();
  TestSafeStrncpyExactSize();
  TestSafeStrncpyTruncated();
  TestSafeStrncpySecurity();
  printf("All tests passed!\n");
  return 0;
}