Add support for empty files to files_compare()
[platform/core/test/security-tests.git] / tests / libsmack-tests / test_cases.cpp
index c8e7606..7f71e8a 100644 (file)
@@ -67,8 +67,19 @@ int files_compare(int fd1, int fd2)
     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) //if files are identical size will be the same
+    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,