Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/toolkit/profile/gtest/TestProfileLockRetry.cpp
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* This Source Code Form is subject to the terms of the Mozilla Public
3
 * License, v. 2.0. If a copy of the MPL was not distributed with this
4
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6
#include "gtest/gtest.h"
7
8
#include <sys/eventfd.h>
9
#include <sched.h>
10
11
#include "nsCOMPtr.h"
12
#include "nsIFile.h"
13
#include "nsProfileLock.h"
14
#include "nsString.h"
15
16
TEST(ProfileLock, RetryLock)
17
0
{
18
0
  char templ[] = "/tmp/profilelocktest.XXXXXX";
19
0
  char * tmpdir = mkdtemp(templ);
20
0
  ASSERT_NE(tmpdir, nullptr);
21
0
22
0
  nsProfileLock myLock;
23
0
  nsProfileLock myLock2;
24
0
  nsresult rv;
25
0
  nsCOMPtr<nsIFile> dir(do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv));
26
0
  ASSERT_EQ(NS_SUCCEEDED(rv), true);
27
0
28
0
  rv = dir->InitWithNativePath(nsCString(tmpdir));
29
0
  ASSERT_EQ(NS_SUCCEEDED(rv), true);
30
0
31
0
  int eventfd_fd = eventfd(0, 0);
32
0
  ASSERT_GE(eventfd_fd, 0);
33
0
34
0
  // fcntl advisory locks are per process, so it's hard
35
0
  // to avoid using fork here.
36
0
  pid_t childpid = fork();
37
0
38
0
  if (childpid) {
39
0
    // parent
40
0
41
0
    // blocking read causing us to lose the race
42
0
    eventfd_t value;
43
0
    EXPECT_EQ(0, eventfd_read(eventfd_fd, &value));
44
0
45
0
    rv = myLock2.Lock(dir, nullptr);
46
0
    EXPECT_EQ(NS_ERROR_FILE_ACCESS_DENIED, rv);
47
0
48
0
    // kill the child
49
0
    EXPECT_EQ(0, kill(childpid, SIGTERM));
50
0
51
0
    // reap zombie (required to collect the lock)
52
0
    int status;
53
0
    EXPECT_EQ(childpid, waitpid(childpid, &status, 0));
54
0
55
0
    rv = myLock2.Lock(dir, nullptr);
56
0
    EXPECT_EQ(NS_SUCCEEDED(rv), true);
57
0
  } else {
58
0
    // child
59
0
    rv = myLock.Lock(dir, nullptr);
60
0
    ASSERT_EQ(NS_SUCCEEDED(rv), true);
61
0
62
0
    // unblock parent
63
0
    EXPECT_EQ(0, eventfd_write(eventfd_fd, 1));
64
0
65
0
    // parent will kill us
66
0
    for (;;)
67
0
      sleep(1);
68
0
  }
69
0
70
0
  close(eventfd_fd);
71
0
72
0
  myLock.Cleanup();
73
0
  myLock2.Cleanup();
74
0
}