security-manager-tests: use libsmack functions for operations on socket labels 46/36446/2
authorRafal Krypa <r.krypa@samsung.com>
Fri, 6 Mar 2015 09:46:09 +0000 (10:46 +0100)
committerRafal Krypa <r.krypa@samsung.com>
Fri, 6 Mar 2015 10:33:04 +0000 (11:33 +0100)
Use smack_set_label_for_file() and smack_new_label_from_file() instead
of plain fsetxattr() and fgetxattr(). These libsmack functions have
been recently provided by libsmack 1.1.
This change also fixes a bug in the tests. We incorrectly expected
the value from fgetxattr() to be null-terminated. It was before kernel
3.20, but that was incorrect for the kernel to do. With most current
kernels the test failed. Libsmack functions do the null-termination
for us, so the test is working again.

Change-Id: Ifc1820cddad0c5acbe49f3c98e622b07acc31358
Signed-off-by: Rafal Krypa <r.krypa@samsung.com>
tests/security-manager-tests/security_manager_tests.cpp

index 6ab415e..27db73d 100644 (file)
@@ -523,30 +523,28 @@ RUNNER_CHILD_TEST_SMACK(security_manager_03_set_label_from_appid)
     result = bind(sock, (struct sockaddr*) &sockaddr, sizeof(struct sockaddr_un));
     RUNNER_ASSERT_ERRNO_MSG(result == 0, "bind failed");
     //Set socket label to something different than expecedLabel
-    result = fsetxattr(sock, XATTR_NAME_SMACKIPIN, socketLabel,
-        strlen(socketLabel), 0);
+    result = smack_set_label_for_file(sock, XATTR_NAME_SMACKIPIN, socketLabel);
     RUNNER_ASSERT_ERRNO_MSG(result == 0,
         "Can't set socket label. Result: " << result);
-    result = fsetxattr(sock, XATTR_NAME_SMACKIPOUT, socketLabel,
-        strlen(socketLabel), 0);
+    result = smack_set_label_for_file(sock, XATTR_NAME_SMACKIPOUT, socketLabel);
     RUNNER_ASSERT_ERRNO_MSG(result == 0,
         "Can't set socket label. Result: " << result);
 
     Api::setProcessLabel(app_id);
 
-    char value[SMACK_LABEL_LEN + 1];
-    ssize_t size;
-    size = fgetxattr(sock, XATTR_NAME_SMACKIPIN, value, sizeof(value));
-    RUNNER_ASSERT_ERRNO_MSG(size != -1, "fgetxattr failed: " << value);
-    result = expected_label.compare(value);
+    result = smack_new_label_from_file(sock, XATTR_NAME_SMACKIPIN, &label);
+    RUNNER_ASSERT_ERRNO_MSG(result != -1, "smack_new_label_from_file failed: " << label);
+    labelPtr.reset(label);
+    result = expected_label.compare(label);
     RUNNER_ASSERT_MSG(result == 0, "Socket label is incorrect. Expected: " <<
-        expected_label << " Actual: " << value);
+        expected_label << " Actual: " << label);
 
-    size = fgetxattr(sock, XATTR_NAME_SMACKIPOUT, value, sizeof(value));
-    RUNNER_ASSERT_ERRNO_MSG(size != -1, "fgetxattr failed: " << value);
-    result = expected_label.compare(value);
+    result = smack_new_label_from_file(sock, XATTR_NAME_SMACKIPOUT, &label);
+    RUNNER_ASSERT_ERRNO_MSG(result != -1, "smack_new_label_from_file failed: " << label);
+    labelPtr.reset(label);
+    result = expected_label.compare(label);
     RUNNER_ASSERT_MSG(result == 0, "Socket label is incorrect. Expected: " <<
-        expected_label << " Actual: " << value);
+        expected_label << " Actual: " << label);
 
     result = smack_new_label_from_self(&label);
     RUNNER_ASSERT_MSG(result >= 0,