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