1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
/******************************************************************************
*
* Copyright (C) 2016 Google, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
#include <gtest/gtest.h>
#include <base/logging.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "osi/include/wakelock.h"
#include "AllocationTestHarness.h"
static bool is_wake_lock_acquired = false;
static int acquire_wake_lock_cb(const char* lock_name) {
is_wake_lock_acquired = true;
return BT_STATUS_SUCCESS;
}
static int release_wake_lock_cb(const char* lock_name) {
is_wake_lock_acquired = false;
return BT_STATUS_SUCCESS;
}
static bt_os_callouts_t bt_wakelock_callouts = {
sizeof(bt_os_callouts_t), NULL, acquire_wake_lock_cb, release_wake_lock_cb};
class WakelockTest : public AllocationTestHarness {
protected:
virtual void SetUp() {
AllocationTestHarness::SetUp();
// TODO (jamuraa): maybe use base::CreateNewTempDirectory instead?
#if defined(OS_GENERIC)
tmp_dir_ = "/tmp/btwlXXXXXX";
#else
tmp_dir_ = "/data/local/tmp/btwlXXXXXX";
#endif // !defined(OS_GENERIC)
char* buffer = const_cast<char*>(tmp_dir_.c_str());
char* dtemp = mkdtemp(buffer);
if (!dtemp) {
perror("Can't make wake lock test directory: ");
CHECK(false);
}
lock_path_ = tmp_dir_ + "/wake_lock";
unlock_path_ = tmp_dir_ + "/wake_unlock";
creat(lock_path_.c_str(), S_IRWXU);
creat(unlock_path_.c_str(), S_IRWXU);
}
virtual void TearDown() {
is_wake_lock_acquired = false;
wakelock_cleanup();
wakelock_set_os_callouts(NULL);
// Clean up the temp wake lock directory
unlink(lock_path_.c_str());
unlink(unlock_path_.c_str());
rmdir(tmp_dir_.c_str());
AllocationTestHarness::TearDown();
}
//
// Test whether the file-based wakelock is acquired.
//
bool IsFileWakeLockAcquired() {
bool acquired = false;
int lock_fd = open(lock_path_.c_str(), O_RDONLY);
CHECK(lock_fd >= 0);
int unlock_fd = open(unlock_path_.c_str(), O_RDONLY);
CHECK(unlock_fd >= 0);
struct stat lock_stat, unlock_stat;
fstat(lock_fd, &lock_stat);
fstat(unlock_fd, &unlock_stat);
CHECK(lock_stat.st_size >= unlock_stat.st_size);
void* lock_file =
mmap(nullptr, lock_stat.st_size, PROT_READ, MAP_PRIVATE, lock_fd, 0);
void* unlock_file = mmap(nullptr, unlock_stat.st_size, PROT_READ,
MAP_PRIVATE, unlock_fd, 0);
if (memcmp(lock_file, unlock_file, unlock_stat.st_size) == 0) {
acquired = lock_stat.st_size > unlock_stat.st_size;
} else {
// these files should always either be with a lock that has more,
// or equal.
CHECK(false);
}
munmap(lock_file, lock_stat.st_size);
munmap(unlock_file, unlock_stat.st_size);
close(lock_fd);
close(unlock_fd);
return acquired;
}
std::string tmp_dir_;
std::string lock_path_;
std::string unlock_path_;
};
TEST_F(WakelockTest, test_set_os_callouts) {
wakelock_set_os_callouts(&bt_wakelock_callouts);
// Initially, the wakelock is not acquired
ASSERT_FALSE(is_wake_lock_acquired);
for (size_t i = 0; i < 1000; i++) {
wakelock_acquire();
ASSERT_TRUE(is_wake_lock_acquired);
wakelock_release();
ASSERT_FALSE(is_wake_lock_acquired);
}
}
TEST_F(WakelockTest, test_set_paths) {
wakelock_set_os_callouts(NULL); // Make sure we use native wakelocks
wakelock_set_paths(lock_path_.c_str(), unlock_path_.c_str());
// Initially, the wakelock is not acquired
ASSERT_FALSE(IsFileWakeLockAcquired());
for (size_t i = 0; i < 1000; i++) {
wakelock_acquire();
ASSERT_TRUE(IsFileWakeLockAcquired());
wakelock_release();
ASSERT_FALSE(IsFileWakeLockAcquired());
}
}
|