From a7003ab882cedf38542e79b48d137c0c49a79503 Mon Sep 17 00:00:00 2001 From: Jan Cybulski Date: Wed, 17 Apr 2013 15:51:46 +0200 Subject: [PATCH] Change test new_label_from_socket [Issue#] SDWSSP-189 [Feature] Changing testcase [Cause] Test failed. Label kept in the socket can be different than the label of the peer, when the peer changed the label after creating socket. [Solution] Changing label before creating socket. Socket and peer label will stay the same. [Verification] Successful build. Running the libsmack tests Change-Id: I79941bc57c883361a6707ce9f486cbdefb0e44fa --- tests/libsmack-tests/test_cases.cpp | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/tests/libsmack-tests/test_cases.cpp b/tests/libsmack-tests/test_cases.cpp index b197de6..d8eb58e 100644 --- a/tests/libsmack-tests/test_cases.cpp +++ b/tests/libsmack-tests/test_cases.cpp @@ -1359,12 +1359,15 @@ RUNNER_TEST(smack11_saving_loading_rules) //int smack_new_label_from_socket(int fd, char **label); -static void smack_set_random_label_for_self(void) + +static void smack_set_another_label_for_self(void) { + static int number = time(NULL); int result; char* smack_label; - result = asprintf(&smack_label, "s%ld", random()); + number++; + result = asprintf(&smack_label, "s%ld", number); RUNNER_ASSERT_MSG(result > 0, "asprintf failed"); result = smack_set_label_for_self(smack_label); RUNNER_ASSERT_MSG(result == 0, "smack_set_label_for_self(" << smack_label << ") failed"); @@ -1391,17 +1394,17 @@ static void smack_unix_sock_server(int sock) RUNNER_TEST(smack09_new_label_from_socket) { + int pid; struct sockaddr_un sockaddr = {AF_UNIX, SOCK_PATH}; - unlink(SOCK_PATH); + smack_set_another_label_for_self(); pid = fork(); RUNNER_ASSERT_MSG(pid >= 0, "Fork failed"); if (!pid) { /* child process, server */ int sock, result; - /* Set the process label before creating a socket */ - smack_set_random_label_for_self(); + sock = socket(AF_UNIX, SOCK_STREAM, 0); RUNNER_ASSERT_MSG(sock >= 0, "socket failed: " << strerror(errno)); result = bind(sock, (struct sockaddr *) &sockaddr, sizeof(struct sockaddr_un)); @@ -1410,24 +1413,23 @@ RUNNER_TEST(smack09_new_label_from_socket) RUNNER_ASSERT_MSG(result == 0, "listen failed: " << strerror(errno)); smack_unix_sock_server(sock); - /* Change the process label with listening socket */ - smack_set_random_label_for_self(); - smack_unix_sock_server(sock); - pid = fork(); RUNNER_ASSERT_MSG(pid >= 0, "Fork failed"); - /* Now running two concurrent servers. Test if socket label was unaffected by fork() */ - smack_unix_sock_server(sock); - /* Let's give the two servers different labels */ - smack_set_random_label_for_self(); + /* Test if socket label was unaffected by fork() */ smack_unix_sock_server(sock); + if(!pid) { + usleep (100); + smack_set_another_label_for_self(); + smack_unix_sock_server(sock); + } close(sock); if (pid) waitpid(pid, NULL, 0); exit(0); } else { /* parent process, client */ sleep(1); /* Give server some time to setup listening socket */ - for (int i = 0; i < 6; ++i) { + for (int i = 0; i < 4; ++i) { + int sock, result; char smack_label1[SMACK_LABEL_LEN + 1]; char* smack_label2; @@ -1444,7 +1446,10 @@ RUNNER_TEST(smack09_new_label_from_socket) result = smack_new_label_from_socket(sock, &smack_label2); RUNNER_ASSERT_MSG(result == 0, "smack_label_from_socket failed"); result = strcmp(smack_label1, smack_label2); - RUNNER_ASSERT_MSG(result == 0, "smack labels differ: '" << smack_label1 << "' != '" << smack_label2 << "'"); + if(i < 3) + RUNNER_ASSERT_MSG(result == 0, "smack labels differ: '" << smack_label1 << "' != '" << smack_label2 << "' i == " << i); + else + RUNNER_ASSERT_MSG(result != 0, "smack labels do not differ: '" << smack_label1 << "' != '" << smack_label2 << "' i == " << i); close(sock); } waitpid(pid, NULL, 0); -- 2.7.4