Imported Upstream version 3.4
[platform/upstream/ccache.git] / unittest / test_lockfile.c
1 // Copyright (C) 2010-2018 Joel Rosdahl
2 //
3 // This program is free software; you can redistribute it and/or modify it
4 // under the terms of the GNU General Public License as published by the Free
5 // Software Foundation; either version 3 of the License, or (at your option)
6 // any later version.
7 //
8 // This program is distributed in the hope that it will be useful, but WITHOUT
9 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 // more details.
12 //
13 // You should have received a copy of the GNU General Public License along with
14 // this program; if not, write to the Free Software Foundation, Inc., 51
15 // Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
17 // This file contains tests for functions in lockfile.c.
18
19 #include "../src/ccache.h"
20 #include "framework.h"
21 #include "util.h"
22
23 TEST_SUITE(lockfile)
24
25 TEST(acquire_should_create_symlink)
26 {
27         lockfile_acquire("test", 1000);
28
29 #ifdef _WIN32
30         CHECK(path_exists("test.lock"));
31 #else
32         CHECK(is_symlink("test.lock"));
33 #endif
34 }
35
36 TEST(release_should_delete_file)
37 {
38         create_file("test.lock", "");
39         lockfile_release("test");
40
41         CHECK(!path_exists("test.lock"));
42 }
43
44 TEST(lock_breaking)
45 {
46         char *p;
47
48 #ifdef _WIN32
49         create_file("test.lock", "foo");
50         create_file("test.lock.lock", "foo");
51 #else
52         CHECK_INT_EQ(0, symlink("foo", "test.lock"));
53         CHECK_INT_EQ(0, symlink("foo", "test.lock.lock"));
54 #endif
55         CHECK(lockfile_acquire("test", 1000));
56
57 #ifdef _WIN32
58         p = read_text_file("test.lock", 0);
59 #else
60         p = x_readlink("test.lock");
61 #endif
62         CHECK(p);
63         CHECK(!str_eq(p, "foo"));
64         CHECK(!path_exists("test.lock.lock"));
65
66         free(p);
67 }
68
69 #ifndef _WIN32
70 TEST(failed_lock_breaking)
71 {
72         create_file("test.lock", "");
73         CHECK(!lockfile_acquire("test", 1000));
74 }
75 #endif
76
77 TEST_SUITE_END