//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");
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));
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;
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);