Rewrite files_compare function 81/19781/3
authorZofia Abramowska <z.abramowska@samsung.com>
Thu, 17 Apr 2014 12:03:14 +0000 (14:03 +0200)
committerMarcin Niesluchowski <m.niesluchow@samsung.com>
Thu, 17 Apr 2014 14:37:21 +0000 (07:37 -0700)
Refactoring files_compare function now, when backtrack functionality
is available.

Change-Id: I5bde968289a3dea7b4c6aff2d9b075b5c22953de

tests/libsmack-tests/test_cases.cpp

index c9d8f96..e2fac42 100644 (file)
@@ -18,8 +18,9 @@
  * @file        test_cases.cpp
  * @author      Pawel Polawski (p.polawski@samsung.com)
  * @author      Jan Olszak (j.olszak@samsung.com)
+ * @author      Zofia Abramowska (z.abramowska@samsung.com)
  * @version     1.0
- * @brief       libprivilege test runer
+ * @brief       libsmack test runner
  */
 
 #include <string>
@@ -51,12 +52,8 @@ 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)
 {
-    int result = 0;
-
     //for getting files sizes
     struct stat fs1, fs2;
 
@@ -65,36 +62,25 @@ int files_compare(int fd1, int fd2)
     void *h2 = MAP_FAILED;
 
     //getting files information
-    if (fstat(fd1, &fs1) == -1) {
-        perror("fstat");
-        return -1;
-    }
-    if (fstat(fd2, &fs2) == -1) {
-        perror("fstat");
-        return -1;
-    }
+    RUNNER_ASSERT_MSG_BT(fstat(fd1, &fs1) == 0, "fstat failed: " << strerror(errno));
+    RUNNER_ASSERT_MSG_BT(fstat(fd2, &fs2) == 0, "fstat failed: " << strerror(errno));
 
     if (fs1.st_size != fs2.st_size) //if files are identical size will be the same
         return -1;
 
     //mapping files to process memory
-    if ((h1 = mmap(0, fs1.st_size, PROT_READ, MAP_SHARED, fd1, 0 )) == MAP_FAILED) {
-        result = -1;
-        goto end;
-    }
+    RUNNER_ASSERT_MSG_BT((h1 = mmap(0, fs1.st_size, PROT_READ, MAP_SHARED, fd1, 0 )) != MAP_FAILED,
+            "mmap failed for fd=" << fd1 << " : " << strerror(errno));
+
     if ((h2 = mmap(0, fs2.st_size, PROT_READ, MAP_SHARED, fd2, 0 )) == MAP_FAILED) {
-        result = -1;
-        goto end;
+        munmap(h1, fs1.st_size);
+        RUNNER_ASSERT_MSG_BT(h2 != MAP_FAILED, "mmap failed for fd=" << fd2
+                                                << " : " << strerror(errno));
     }
 
-    result = memcmp(h1, h2, fs1.st_size);
-
-    //cleaning after mmap()
-end:
-    if (h2 != MAP_FAILED)
-        munmap(h2, fs2.st_size);
-    if (h1 != MAP_FAILED)
-        munmap(h1, fs1.st_size);
+    int result = memcmp(h1, h2, fs1.st_size);
+    munmap(h1, fs1.st_size);
+    munmap(h2, fs2.st_size);
 
     return result;
 }