Browse Source

fix rgpgfs_strncpy_without_suffix when max_len << len(src) / len(suffix);
added unit tests

Fabian Peter Hammerle 5 years ago
parent
commit
d6fe4ab5b6
7 changed files with 55 additions and 4 deletions
  1. 1 0
      .dockerignore
  2. 1 0
      .gitignore
  3. 6 0
      Dockerfile
  4. 8 2
      Makefile
  5. 3 2
      src/str.c
  6. 2 0
      src/str.h
  7. 34 0
      tests/str.c

+ 1 - 0
.dockerignore

@@ -2,5 +2,6 @@
 !Makefile
 !docker/
 !src/
+!tests/
 
 # https://docs.docker.com/engine/reference/builder/#dockerignore-file

+ 1 - 0
.gitignore

@@ -1,2 +1,3 @@
 *.o
 /rgpgfs
+/tests/str

+ 6 - 0
Dockerfile

@@ -17,6 +17,12 @@ WORKDIR /rgpgfs
 RUN make
 
 
+FROM build as test
+
+COPY --chown=build:nogroup tests /rgpgfs/tests
+RUN make tests/str && tests/str
+
+
 FROM alpine:3.9 as runtime
 
 RUN apk add --no-cache \

+ 8 - 2
Makefile

@@ -1,4 +1,4 @@
-CC ?= gcc
+CC = gcc
 LD = gcc
 
 CFLAGS := ${CFLAGS} -Wall -Werror -I"$(CURDIR)"
@@ -27,5 +27,11 @@ src/str.o : src/str.c src/str.h
 rgpgfs : src/fs.o src/gpgme.o src/main.o src/str.o
 	$(LD) $^ -o $@ $(LIBS)
 
-format : src/*.h src/*.c
+tests/str.o : tests/str.c src/str.h
+	$(CC) $(CFLAGS) -c $< -o $@
+
+tests/str : tests/str.o src/str.o
+	$(LD) $^ -o $@
+
+format : src/*.h src/*.c tests/*.c
 	clang-format -i -verbose $^

+ 3 - 2
src/str.c

@@ -1,9 +1,10 @@
+#include <stdio.h>
 #include <string.h>
 
 int rgpgfs_strncpy_without_suffix(char *dest, const char *src,
                                   const char *suffix, size_t max_len) {
-  size_t src_len = strnlen(src, max_len * 2);
-  size_t suffix_len = strnlen(suffix, max_len * 2);
+  size_t src_len = strnlen(src, FILENAME_MAX);
+  size_t suffix_len = strnlen(suffix, FILENAME_MAX);
   if (suffix_len > src_len) {
     return 1;
   }

+ 2 - 0
src/str.h

@@ -1,2 +1,4 @@
+#include <stddef.h>
+
 int rgpgfs_strncpy_without_suffix(char *dest, const char *src,
                                   const char *suffix, size_t max_len);

+ 34 - 0
tests/str.c

@@ -0,0 +1,34 @@
+#include "src/str.h"
+
+#include <assert.h>
+#include <string.h>
+
+void test_strncpy_without_suffix(const char *expected_dest, const char *src,
+                                 const char *suffix, size_t max_len) {
+  char dest[32] = "default";
+  int result = rgpgfs_strncpy_without_suffix(dest, src, suffix, max_len);
+  if (expected_dest == NULL) {
+    assert(result);
+    assert(!strcmp(dest, "default"));
+  } else {
+    assert(!result);
+    assert(!strcmp(dest, expected_dest));
+  }
+}
+
+int main() {
+  test_strncpy_without_suffix("abc", "abc", "", 8);
+  test_strncpy_without_suffix("ab", "abc", "c", 8);
+  test_strncpy_without_suffix("a", "abc", "bc", 8);
+  test_strncpy_without_suffix("", "abc", "abc", 8);
+  test_strncpy_without_suffix("abcda", "abcdabc", "bc", 8);
+  test_strncpy_without_suffix(NULL, "abc", "d", 8);
+  test_strncpy_without_suffix("a", "abc", "bc", 1);
+  test_strncpy_without_suffix("ab", "abc", "c", 2);
+  test_strncpy_without_suffix(NULL, "abc", "c", 1);
+  test_strncpy_without_suffix(NULL, "abc", "abcd", 8);
+  test_strncpy_without_suffix(NULL, "bcd", "abcd", 8);
+  test_strncpy_without_suffix("/folder/sub/file.txt",
+                              "/folder/sub/file.txt.gpg", ".gpg", 24);
+  return 0;
+}