1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <deque>
18 #include <fcntl.h>
19 #include <random>
20 #include <string.h>
21 #include <stdio.h>
22 #include <sys/stat.h>
23 #include <unistd.h>
24 
25 #include <gtest/gtest.h>
26 
27 #include <storaged.h>               // data structures
28 #include <storaged_utils.h>         // functions to test
29 
30 #define MMC_DISK_STATS_PATH "/sys/block/mmcblk0/stat"
31 #define SDA_DISK_STATS_PATH "/sys/block/sda/stat"
32 
pause(uint32_t sec)33 static void pause(uint32_t sec) {
34     const char* path = "/cache/test";
35     int fd = open(path, O_WRONLY | O_CREAT, 0600);
36     ASSERT_LT(-1, fd);
37     char buffer[2048];
38     memset(buffer, 1, sizeof(buffer));
39     int loop_size = 100;
40     for (int i = 0; i < loop_size; ++i) {
41         ASSERT_EQ(2048, write(fd, buffer, sizeof(buffer)));
42     }
43     fsync(fd);
44     close(fd);
45 
46     fd = open(path, O_RDONLY);
47     ASSERT_LT(-1, fd);
48     for (int i = 0; i < loop_size; ++i) {
49         ASSERT_EQ(2048, read(fd, buffer, sizeof(buffer)));
50     }
51     close(fd);
52 
53     sleep(sec);
54 }
55 
56 // the return values of the tested functions should be the expected ones
57 const char* DISK_STATS_PATH;
TEST(storaged_test,retvals)58 TEST(storaged_test, retvals) {
59     struct disk_stats stats;
60     memset(&stats, 0, sizeof(struct disk_stats));
61 
62     if (access(MMC_DISK_STATS_PATH, R_OK) >= 0) {
63         DISK_STATS_PATH = MMC_DISK_STATS_PATH;
64     } else if (access(SDA_DISK_STATS_PATH, R_OK) >= 0) {
65         DISK_STATS_PATH = SDA_DISK_STATS_PATH;
66     } else {
67         return;
68     }
69 
70     EXPECT_TRUE(parse_disk_stats(DISK_STATS_PATH, &stats));
71 
72     struct disk_stats old_stats;
73     memset(&old_stats, 0, sizeof(struct disk_stats));
74     old_stats = stats