1 //
2 // Copyright 2018 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 "hidl_memory_driver/VtsHidlMemoryDriver.h"
18 
19 #include <errno.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <unistd.h>
23 
24 #include <gtest/gtest.h>
25 
26 using namespace std;
27 
28 namespace android {
29 namespace vts {
30 
31 // Unit test to test operations on hidl_memory_driver.
32 class HidlMemoryDriverUnitTest : public ::testing::Test {
33  protected:
SetUp()34   virtual void SetUp() {
35     mem_id_ = mem_driver_.Allocate(100);
36     ASSERT_NE(mem_id_, -1);
37   }
38 
TearDown()39   virtual void TearDown() {}
40 
41   VtsHidlMemoryDriver mem_driver_;
42   int mem_id_;
43 };
44 
45 // Helper method to initialize an array of random integers.
InitData(int * data,size_t len)46 void InitData(int* data, size_t len) {
47   for (size_t i = 0; i < len; i++) {
48     data[i] = rand() % 100 + 1;
49   }
50 }
51 
52 // Tests trying to read from an invalid memory object.
TEST_F(HidlMemoryDriverUnitTest,InvalidMemId)53 TEST_F(HidlMemoryDriverUnitTest, InvalidMemId) {
54   ASSERT_FALSE(mem_driver_.Read(42));
55 }
56 
57 // Tests GetSize() method.
TEST_F(HidlMemoryDriverUnitTest,GetSizeTest)58 TEST_F(HidlMemoryDriverUnitTest, GetSizeTest) {
59   size_t mem_size;
60   ASSERT_TRUE(mem_driver_.GetSize(mem_id_, &mem_size));
61   ASSERT_EQ(100, mem_size);
62 }
63 
64 // Tests writing to the memory and reading the same data back.
TEST_F(HidlMemoryDriverUnitTest,SimpleWriteRead)65 TEST_F(HidlMemoryDriverUnitTest, SimpleWriteRead) {
66   string write_data = "abcdef";
67   // Write into the memory.
68   ASSERT_TRUE(mem_driver_.Update(mem_id_));
69   ASSERT_TRUE(mem_driver_.UpdateBytes(mem_id_, write_data.c_str(),
70                                       write_data.length()));
71   ASSERT_TRUE(mem_driver_.Commit(mem_id_));
72 
73   // Read from the memory.
74   char read_data[write_data.length()];
75   ASSERT_TRUE(mem_driver_.Read(mem_id_));
76   ASSERT_TRUE(mem_driver_.ReadBytes(mem_id_, read_data, write_data.length()));
77   ASSERT_TRUE(mem_driver_.Commit(mem_id_));
78 
79   // Check read data.
80   ASSERT_EQ(0, strncmp(write_data.c_str(), read_data, write_data.length()));
81 }
82 
83 // Tests consecutive writes and reads using integers.
84 // For each of the 5 iterations, write 5 integers into
85 // different chunks of memory, and read them back.
TEST_F(HidlMemoryDriverUnitTest,LargeWriteRead)86 TEST_F(HidlMemoryDriverUnitTest, LargeWriteRead) {
87   int write_data[5];
88   int read_data[5];
89 
90   // Every write shifts 5 * sizeof(int) bytes of region.
91   // Every iteration writes 5 integers.
92   for (int i = 0; i < 100; i += 5 * sizeof(int)) {
93     InitData(write_data, 5);
94     // Write 5 integers, which is equivalent to writing 5 * sizeof(int) bytes.
95     ASSERT_TRUE(mem_driver_.UpdateRange(mem_id_, i, 5 * sizeof(int)));
96     ASSERT_TRUE(mem_driver_.UpdateBytes(
97         mem_id_, reinterpret_cast<char*>(write_data), 5 * sizeof(int), i));
98     ASSERT_TRUE(mem_driver_.Commit(mem_id_));
99 
100     // Read the integers back.
101     ASSERT_TRUE(mem_driver_.ReadRange(mem_id_, i, 5 * sizeof(int)));
102     ASSERT_TRUE(mem_driver_.ReadBytes(
103         mem_id_, reinterpret_cast<char*>(read_data), 5 * sizeof(int), i));
104     ASSERT_TRUE(mem_driver_.Commit(mem_id_));
105 
106     ASSERT_EQ(0, memcmp(write_data, read_data, 5 * sizeof(int)));
107   }
108 }
109 
110 // Tests writing into different regions in the memory buffer.
111 // Writer requests the beginning of the first half and
112 // the beginning of the second half of the buffer.
113 // It writes to the second half, commits, and reads the data back.
114 // Then it writes to the first half, commits, and reads the data back.
TEST_F(HidlMemoryDriverUnitTest,WriteTwoRegionsInOneBuffer)115 TEST_F(HidlMemoryDriverUnitTest, WriteTwoRegionsInOneBuffer) {
116   string write_data1 = "abcdef";
117   string write_data2 = "ghijklmno";
118   char read_data1[write_data1.length()];
119   char read_data2[write_data2.length()];
120 
121   // Request writes to two separate regions in the same buffer.
122   // One from the start, the second from offset 50.
123   ASSERT_TRUE(mem_driver_.UpdateRange(mem_id_, 0, write_data1.length()));
124   ASSERT_TRUE(mem_driver_.UpdateRange(mem_id_, 50, write_data2.length()));
125   // Update the second region.
126   ASSERT_TRUE(mem_driver_.UpdateBytes(mem_id_, write_data2.c_str(),
127                                       write_data2.length(), 50));
128   ASSERT_TRUE(mem_driver_.Commit(mem_id_));
129 
130   // Read from the second region.
131   ASSERT_TRUE(mem_driver_.Read(mem_id_));
132   ASSERT_TRUE(
133       mem_driver_.ReadBytes(mem_id_, read_data2, write_data2.length(), 50));
134   ASSERT_TRUE(mem_driver_.Commit(mem_id_));
135   ASSERT_EQ(0, strncmp(read_data2, write_data2.c_str(), write_data2.length()));
136 
137   // Update the first region.
138   ASSERT_TRUE(mem_driver_.UpdateBytes(mem_id_, write_data1.c_str(),
139                                       write_data1.length()));
140   ASSERT_TRUE(mem_driver_.Commit(mem_id_));
141 
142   // Read from the first region.
143   ASSERT_TRUE(mem_driver_.Read(mem_id_));
144   ASSERT_TRUE(mem_driver_.ReadBytes(mem_id_, read_data1, write_data1.length()));
145   ASSERT_TRUE(mem_driver_.Commit(mem_id_));
146   ASSERT_EQ(0, strncmp(read_data1, write_data1.c_str(), write_data1.length()));
147 }
148 
149 }  // namespace vts
150 }  // namespace android
151