• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_handle_driver/VtsHidlHandleDriver.h"
18 
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <gtest/gtest.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <unistd.h>
25 
26 using namespace std;
27 
28 static constexpr const char* kTestFilePath = "/data/local/tmp/test.txt";
29 
30 namespace android {
31 namespace vts {
32 
33 // Unit test to test operations on hidl_memory_driver.
34 class HidlHandleDriverUnitTest : public ::testing::Test {
35  protected:
SetUp()36   virtual void SetUp() {
37     // Register handle objects.
38     client1_id_ = handle_driver_.CreateFileHandle(string(kTestFilePath),
39                                                   O_RDWR | O_CREAT | O_TRUNC,
40                                                   S_IRWXG, vector<int>());
41     client2_id_ = handle_driver_.CreateFileHandle(string(kTestFilePath),
42                                                   O_RDONLY, 0, vector<int>());
43     ASSERT_NE(client1_id_, -1);
44     ASSERT_NE(client2_id_, -1);
45   }
46 
TearDown()47   virtual void TearDown() {
48     ASSERT_TRUE(handle_driver_.UnregisterHidlHandle(client1_id_));
49     ASSERT_TRUE(handle_driver_.UnregisterHidlHandle(client2_id_));
50     // Delete files for testing.
51     remove(kTestFilePath);
52   }
53 
54   VtsHidlHandleDriver handle_driver_;
55   int client1_id_;
56   int client2_id_;
57 };
58 
59 // Tests trying to write to an invalid handle object.
TEST_F(HidlHandleDriverUnitTest,InvalidHandleId)60 TEST_F(HidlHandleDriverUnitTest, InvalidHandleId) {
61   // Invalid ID: 42, tries to read 10 bytes.
62   ASSERT_EQ(handle_driver_.WriteFile(42, nullptr, 10), -1);
63 }
64 
65 // Tests reader doesn't have the permission to edit test.txt.
TEST_F(HidlHandleDriverUnitTest,ReaderInvalidWrite)66 TEST_F(HidlHandleDriverUnitTest, ReaderInvalidWrite) {
67   char write_data[10];
68   ASSERT_EQ(handle_driver_.WriteFile(client2_id_, write_data, 10), -1);
69 }
70 
71 // Tests unregistering a handle and using that handle after returns error.
TEST_F(HidlHandleDriverUnitTest,UnregisterHandle)72 TEST_F(HidlHandleDriverUnitTest, UnregisterHandle) {
73   int new_id = handle_driver_.CreateFileHandle(string(kTestFilePath), O_RDONLY,
74                                                0, vector<int>());
75   // Reading 0 bytes should work, because handle object is found.
76   ASSERT_EQ(handle_driver_.ReadFile(new_id, nullptr, 0), 0);
77 
78   // Now unregister the handle.
79   ASSERT_TRUE(handle_driver_.UnregisterHidlHandle(new_id));
80   // Read 0 bytes again, this time should fail because handle object is deleted.
81   ASSERT_EQ(handle_driver_.ReadFile(new_id, nullptr, 0), -1);
82 }
83 
84 // Tests simple read/write operations on the same file from two clients.
TEST_F(HidlHandleDriverUnitTest,SimpleReadWrite)85 TEST_F(HidlHandleDriverUnitTest, SimpleReadWrite) {
86   string write_data = "Hello World!";
87 
88   // Writer writes to test.txt.
89   ASSERT_EQ(handle_driver_.WriteFile(
90                 client1_id_, static_cast<const void*>(write_data.c_str()),
91                 write_data.length()),
92             write_data.length());
93 
94   // Reader reads from test.txt.
95   char read_data[write_data.length()];
96   ASSERT_EQ(handle_driver_.ReadFile(client2_id_, static_cast<void*>(read_data),
97                                     write_data.length()),
98             write_data.length());
99 
100   ASSERT_TRUE(write_data == string(read_data, write_data.length()));
101 }
102 
103 // Tests large read/write interaction between reader and writer.
104 // Writer keeps adding "abcd" at the end of the file.
105 // After every iteration, reader should read back one extra repeated "abcd".
TEST_F(HidlHandleDriverUnitTest,LargeReadWrite)106 TEST_F(HidlHandleDriverUnitTest, LargeReadWrite) {
107   static constexpr size_t NUM_ITERS = 10;
108   const string write_data = "abcd";
109   string curr_correct_data = "";
110   char read_data[write_data.length() * NUM_ITERS];
111   char* curr_read_data_ptr = read_data;
112 
113   for (int i = 0; i < NUM_ITERS; i++) {
114     // Writer writes to test1.txt.
115     ASSERT_EQ(handle_driver_.WriteFile(
116                   client1_id_, static_cast<const void*>(write_data.c_str()),
117                   write_data.length()),
118               write_data.length());
119 
120     // Reader reads from test1.txt.
121     ASSERT_EQ(handle_driver_.ReadFile(client2_id_,
122                                       static_cast<void*>(curr_read_data_ptr),
123                                       write_data.length()),
124               write_data.length());
125 
126     string curr_read_data = string(read_data, write_data.length() * (i + 1));
127     curr_correct_data += write_data;
128     curr_read_data_ptr += write_data.length();
129     ASSERT_TRUE(curr_read_data == curr_correct_data);
130   }
131 }
132 
133 }  // namespace vts
134 }  // namespace android
135