File: test_cowbuilder_util.c

package info (click to toggle)
cowdancer 0.73
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 752 kB
  • ctags: 269
  • sloc: ansic: 3,822; sh: 481; makefile: 191; cpp: 6
file content (65 lines) | stat: -rwxr-xr-x 2,289 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
/*BINFMTC: cowbuilder_util.c
 */
#include "cowbuilder_util.h"

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


void test_check_mountpoint() {
  // something is usually mounted under /.
  if(0 == check_mountpoint("/")) {
    fprintf(stderr, 
	    "*******************************************************************************\n"
	    "* '/' is not mounted, something is wrong with the system or the code\n"
	    "*******************************************************************************\n");
  }
  // something is usually mounted under /run, but can't be sure.
  // assert(1 == check_mountpoint("/run")); // commented out so that I don't break automatic builds.
  // usually nothing is mounted under /tmp/nothing-is-mounted
  fprintf(stderr, "The following message is expected:\n");
  assert(0 == check_mountpoint("/tmp/nothing-is-mounted"));
}

/** 
 * make sure strings are equal, and return true when they are. Just to
 * save myself from strcmp being a macro.
 */
int verify_string_equal(const char* a, const char* b) {
  return !strcmp(a, b);
}

void test_canonicalize_doubleslash() {
  char dest[256];
  canonicalize_doubleslash("/no/double/slash", dest);
  assert(verify_string_equal(dest, "/no/double/slash"));
  canonicalize_doubleslash("/trailing/slash/", dest);
  assert(verify_string_equal(dest, "/trailing/slash/"));
  canonicalize_doubleslash("//starting/double/slash/", dest);
  assert(verify_string_equal(dest, "/starting/double/slash/"));
  canonicalize_doubleslash("/double//slash", dest);
  assert(verify_string_equal(dest, "/double/slash"));
  canonicalize_doubleslash("/more//double//slash", dest);
  assert(verify_string_equal(dest, "/more/double/slash"));
  canonicalize_doubleslash("no/starting/slash//", dest);
  assert(verify_string_equal(dest, "no/starting/slash/"));
  canonicalize_doubleslash("///", dest);
  assert(verify_string_equal(dest, "/"));

  const char* test_buffer_overrun = "/some/string";
  strcpy(dest,                      "-some-string+g");
  canonicalize_doubleslash(test_buffer_overrun, dest);
  assert(verify_string_equal(dest, "/some/string"));
  assert(strlen(dest) == 12);
  assert(dest[12] == 0);
  assert(dest[13] == 'g');
}

int main()
{
  test_check_mountpoint();
  test_canonicalize_doubleslash();

  return 0;
}