]> git.ipfire.org Git - thirdparty/tor.git/commitdiff
Add a simple test for checkSpace.pl
authorNick Mathewson <nickm@torproject.org>
Tue, 26 Nov 2019 18:10:39 +0000 (13:10 -0500)
committerteor <teor@torproject.org>
Tue, 3 Dec 2019 02:56:46 +0000 (12:56 +1000)
This script is not expected to work on windows due to line-ending
issues, so I'm not making it get run on an automated basis.  We
should use it when editing checkSpace.pl.

Closes ticket 32613.

scripts/maint/checkSpaceTest.sh [new file with mode: 0755]
scripts/maint/checkspace_tests/dubious.c [new file with mode: 0644]
scripts/maint/checkspace_tests/dubious.h [new file with mode: 0644]
scripts/maint/checkspace_tests/expected.txt [new file with mode: 0644]
scripts/maint/checkspace_tests/good_guard.h [new file with mode: 0644]
scripts/maint/checkspace_tests/same_guard.h [new file with mode: 0644]
scripts/maint/checkspace_tests/subdir/dubious.c [new file with mode: 0644]

diff --git a/scripts/maint/checkSpaceTest.sh b/scripts/maint/checkSpaceTest.sh
new file mode 100755 (executable)
index 0000000..a1eea58
--- /dev/null
@@ -0,0 +1,23 @@
+#!/bin/sh
+# Copyright 2019, The Tor Project, Inc.
+# See LICENSE for licensing information
+
+# Integration test for checkSpace.pl, which we want to rewrite.
+
+umask 077
+set -e
+
+# make a safe space for temporary files
+DATA_DIR=$(mktemp -d -t tor_checkspace_tests.XXXXXX)
+trap 'rm -rf "$DATA_DIR"' 0
+
+RECEIVED_FNAME="${DATA_DIR}/got.txt"
+
+cd "$(dirname "$0")/checkspace_tests"
+
+# we expect this to give an error code.
+../checkSpace.pl -C ./*.[ch] ./*/*.[ch] > "${RECEIVED_FNAME}" && exit 1
+
+diff -u expected.txt "${RECEIVED_FNAME}" || exit 1
+
+echo "OK"
diff --git a/scripts/maint/checkspace_tests/dubious.c b/scripts/maint/checkspace_tests/dubious.c
new file mode 100644 (file)
index 0000000..59c5f8e
--- /dev/null
@@ -0,0 +1,83 @@
+
+// The { coming up should be on its own line.
+int
+foo(void) {
+  // There should be a space before (1)
+  if(1) x += 1;
+
+  // The following empty line is unnecessary.
+
+}
+
+
+// There should be a newline between void and bar.
+void bar(void)
+{
+  // too wide:
+  testing("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
+}
+
+long
+bad_spacing()
+{
+  // here comes a tab
+       return 2;
+  // here comes a label without space:
+foo:
+  ;
+}
+
+// Here comes a CR:\r
+
+// Trailing space:         
+
+int
+non_k_and_r(void)
+{
+  // non-k&r
+  if (foo)
+    {
+      // double-semi
+      return 1;;
+    }
+  else
+    {
+      return 2;
+    }
+}
+
+// #else #if causes a warning.
+#if 1
+#else
+#if 2
+#else
+#endif
+#endif
+
+// always space before a brace.
+foo{
+}
+
+void
+unexpected_space(void)
+{
+  // This space gives a warning.
+  foobar (77);
+}
+
+void
+bad_function_calls(long)
+{
+  // These are forbidden:
+  assert(1);
+  memcmp("a","b",1);
+  strcat(foo,x);
+  strcpy(foo,y);
+  sprintf(foo,"x");
+  malloc(7);
+  free(p);
+  realloc(p);
+  strdup(s);
+  strndup(s,10);
+  calloc(a,b);
+}
diff --git a/scripts/maint/checkspace_tests/dubious.h b/scripts/maint/checkspace_tests/dubious.h
new file mode 100644 (file)
index 0000000..744ec33
--- /dev/null
@@ -0,0 +1,4 @@
+
+// no guards.
+
+int foo(int);
diff --git a/scripts/maint/checkspace_tests/expected.txt b/scripts/maint/checkspace_tests/expected.txt
new file mode 100644 (file)
index 0000000..935b750
--- /dev/null
@@ -0,0 +1,31 @@
+    fn() {:./dubious.c:4
+       KW(:./dubious.c:6
+   UnnecNL:./dubious.c:10
+  DoubleNL:./dubious.c:12
+   tp fn():./dubious.c:15
+      Wide:./dubious.c:17
+       TAB:./dubious.c:24
+ nosplabel:./dubious.c:26
+        CR:./dubious.c:30
+ Space@EOL:./dubious.c:32
+ non-K&R {:./dubious.c:39
+        ;;:./dubious.c:41
+   }\nelse:./dubious.c:43
+  #else#if:./dubious.c:52
+        o{:./dubious.c:58
+    fn() {:./dubious.c:58
+     fn ():./dubious.c:65
+    assert:./dubious.c:72   (use tor_assert)
+    memcmp:./dubious.c:73   (use {tor,fast}_mem{eq,neq,cmp}
+   strcat(:./dubious.c:74
+   strcpy(:./dubious.c:75
+  sprintf(:./dubious.c:76
+   malloc(:./dubious.c:77    (use tor_malloc, tor_free, etc)
+     free(:./dubious.c:78    (use tor_malloc, tor_free, etc)
+  realloc(:./dubious.c:79    (use tor_malloc, tor_free, etc)
+   strdup(:./dubious.c:80    (use tor_malloc, tor_free, etc)
+  strndup(:./dubious.c:81    (use tor_malloc, tor_free, etc)
+   calloc(:./dubious.c:82    (use tor_malloc, tor_free, etc)
+   noguard:./dubious.h (No #ifndef/#define header guard pair found)
+  dupguard:./same_guard.h (Guard macro GUARD_MACRO_H also used in ./good_guard.h)
+ dup fname:./subdir/dubious.c (same as ./dubious.c).
diff --git a/scripts/maint/checkspace_tests/good_guard.h b/scripts/maint/checkspace_tests/good_guard.h
new file mode 100644 (file)
index 0000000..b792912
--- /dev/null
@@ -0,0 +1,6 @@
+#ifndef GUARD_MACRO_H
+#define GUARD_MACRO_H
+
+int bar(void);
+
+#endif
diff --git a/scripts/maint/checkspace_tests/same_guard.h b/scripts/maint/checkspace_tests/same_guard.h
new file mode 100644 (file)
index 0000000..b792912
--- /dev/null
@@ -0,0 +1,6 @@
+#ifndef GUARD_MACRO_H
+#define GUARD_MACRO_H
+
+int bar(void);
+
+#endif
diff --git a/scripts/maint/checkspace_tests/subdir/dubious.c b/scripts/maint/checkspace_tests/subdir/dubious.c
new file mode 100644 (file)
index 0000000..7f22bf7
--- /dev/null
@@ -0,0 +1 @@
+// Nothing wrong with this file, but the name is a duplicate.