Move files_compare() function to test commons 13/30713/3
authorPawel Wieczorek <p.wieczorek2@samsung.com>
Mon, 8 Sep 2014 14:23:58 +0000 (16:23 +0200)
committerPawel Wieczorek <p.wieczorek2@samsung.com>
Mon, 1 Dec 2014 11:40:36 +0000 (12:40 +0100)
Function files_compare() from tests for libsmack can be used in other
tests as well. It is now accessible outside libsmack tests.

Change-Id: Ic56b4aff4c4170e24b5cfb2754e2ef4aed4cf541

tests/common/tests_common.cpp
tests/common/tests_common.h
tests/libsmack-tests/test_cases.cpp

index 1bf0236..6808b57 100644 (file)
@@ -22,6 +22,9 @@
  */
 
 #include "tests_common.h"
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/types.h>
 #include <unistd.h>
 #include <grp.h>
 #include <errno.h>
@@ -136,3 +139,47 @@ std::string formatCstr(const char *cstr)
         return std::string("nullptr");
     return std::string("\"") + cstr + "\"";
 }
+
+int files_compare(int fd1, int fd2)
+{
+    //for getting files sizes
+    struct stat fs1, fs2;
+
+    //handlers for mmap()
+    void *h1 = MAP_FAILED;
+    void *h2 = MAP_FAILED;
+
+    //getting files information
+    RUNNER_ASSERT_ERRNO_MSG(fstat(fd1, &fs1) == 0, "fstat failed");
+    RUNNER_ASSERT_ERRNO_MSG(fstat(fd2, &fs2) == 0, "fstat failed");
+
+    if (fs1.st_size < fs2.st_size) {
+        return -1;
+    }
+
+    if (fs1.st_size > fs2.st_size) {
+        return 1;
+    }
+
+    //since Linux 2.6.12, mmap returns EINVAL if length is 0
+    //if both lengths are 0, files are actually the same
+    if (0 == fs1.st_size && 0 == fs2.st_size) {
+        return 0;
+    }
+
+    //mapping files to process memory
+    RUNNER_ASSERT_ERRNO_MSG((h1 = mmap(0, fs1.st_size, PROT_READ, MAP_SHARED, fd1, 0 )) != MAP_FAILED,
+                               "mmap failed for fd=" << fd1);
+
+    if ((h2 = mmap(0, fs2.st_size, PROT_READ, MAP_SHARED, fd2, 0 )) == MAP_FAILED) {
+        munmap(h1, fs1.st_size);
+        RUNNER_ASSERT_MSG(h2 != MAP_FAILED, "mmap failed for fd=" << fd2
+                                            << ". " << strerror(errno));
+    }
+
+    int result = memcmp(h1, h2, fs1.st_size);
+    munmap(h1, fs1.st_size);
+    munmap(h2, fs2.st_size);
+
+    return result;
+}
index 47dac47..34427da 100644 (file)
@@ -46,6 +46,7 @@ void setLabelForSelf(const int line, const char *label);
 void add_process_group(const char* group_name);
 void remove_process_group(const char* group_name);
 std::string formatCstr(const char *cstr);
+int files_compare(int fd1, int fd2);
 
 #define RUNNER_TEST_SMACK(Proc)                                                     \
     void Proc();                                                                    \
index 7f71e8a..e1a5265 100644 (file)
@@ -32,7 +32,6 @@
 #include <dpl/log/log.h>
 #include <sys/types.h>
 #include <sys/stat.h>
-#include <sys/mman.h>
 #include <sys/smack.h>
 #include <sys/xattr.h>
 #include <sys/socket.h>
@@ -54,50 +53,6 @@ const std::vector<std::string> accessesBasic = { "r", "w", "x", "wx", "rx", "rw"
 //This one define is required for sockaddr_un initialization
 #define SOCK_PATH "/tmp/test-smack-socket"
 
-int files_compare(int fd1, int fd2)
-{
-    //for getting files sizes
-    struct stat fs1, fs2;
-
-    //handlers for mmap()
-    void *h1 = MAP_FAILED;
-    void *h2 = MAP_FAILED;
-
-    //getting files information
-    RUNNER_ASSERT_ERRNO_MSG(fstat(fd1, &fs1) == 0, "fstat failed");
-    RUNNER_ASSERT_ERRNO_MSG(fstat(fd2, &fs2) == 0, "fstat failed");
-
-    if (fs1.st_size < fs2.st_size) {
-        return -1;
-    }
-
-    if (fs1.st_size > fs2.st_size) {
-        return 1;
-    }
-
-    //since Linux 2.6.12, mmap returns EINVAL if length is 0
-    //if both lengths are 0, files are actually the same
-    if (0 == fs1.st_size && 0 == fs2.st_size) {
-        return 0;
-    }
-
-    //mapping files to process memory
-    RUNNER_ASSERT_ERRNO_MSG((h1 = mmap(0, fs1.st_size, PROT_READ, MAP_SHARED, fd1, 0 )) != MAP_FAILED,
-                               "mmap failed for fd=" << fd1);
-
-    if ((h2 = mmap(0, fs2.st_size, PROT_READ, MAP_SHARED, fd2, 0 )) == MAP_FAILED) {
-        munmap(h1, fs1.st_size);
-        RUNNER_ASSERT_MSG(h2 != MAP_FAILED, "mmap failed for fd=" << fd2
-                                            << ". " << strerror(errno));
-    }
-
-    int result = memcmp(h1, h2, fs1.st_size);
-    munmap(h1, fs1.st_size);
-    munmap(h2, fs2.st_size);
-
-    return result;
-}
-
 RUNNER_TEST_GROUP_INIT(libsmack)
 /**
  * Helper method to reset privileges at the begginning of tests.