1 /*
2  * Copyright (C) 2021 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 <stdlib.h>
18 #include <unistd.h>
19 
20 #include <string>
21 
22 #include "common/libs/fs/shared_fd.h"
23 #include "common/libs/security/keymaster_channel_sharedfd.h"
24 #include "gtest/gtest.h"
25 
26 namespace cuttlefish {
27 
TEST(KeymasterChannel,SendAndReceiveRequest)28 TEST(KeymasterChannel, SendAndReceiveRequest) {
29   SharedFD read_fd;
30   SharedFD write_fd;
31   ASSERT_TRUE(SharedFD::Pipe(&read_fd, &write_fd)) << "Failed to create pipe";
32 
33   SharedFdKeymasterChannel channel{read_fd, write_fd};
34 
35   char buffer[] = {1, 2, 3, 4, 5, 6};
36   keymaster::Buffer request(buffer, sizeof(buffer));
37 
38   ASSERT_TRUE(channel.SendRequest(keymaster::GET_VERSION, request))
39       << "Failed to send request";
40   auto response = channel.ReceiveMessage();
41   EXPECT_EQ(response->cmd, keymaster::GET_VERSION) << "Command mismatch";
42   EXPECT_FALSE(response->is_response) << "Request/response mismatch";
43 
44   keymaster::Buffer read;
45   const uint8_t* read_data = response->payload;
46   EXPECT_TRUE(read.Deserialize(&read_data, read_data + response->payload_size))
47       << "Failed to deserialize request";
48   ASSERT_EQ(request.end() - request.begin(), read.end() - read.begin());
49   ASSERT_EQ(request.buffer_size(), read.buffer_size());
50   ASSERT_TRUE(std::equal(request.begin(), request.end(), read.begin()));
51 }
52 
TEST(KeymasterChannel,SendAndReceiveResponse)53 TEST(KeymasterChannel, SendAndReceiveResponse) {
54   SharedFD read_fd;
55   SharedFD write_fd;
56   ASSERT_TRUE(SharedFD::Pipe(&read_fd, &write_fd)) << "Failed to create pipe";
57 
58   SharedFdKeymasterChannel channel{read_fd, write_fd};
59 
60   char buffer[] = {1, 2, 3, 4, 5, 6};
61   keymaster::Buffer request(buffer, sizeof(buffer));
62 
63   ASSERT_TRUE(channel.SendResponse(keymaster::GET_VERSION, request))
64       << "Failed to send request";
65   auto response = channel.ReceiveMessage();
66   EXPECT_EQ(response->cmd, keymaster::GET_VERSION) << "Command mismatch";
67   EXPECT_TRUE(response->is_response) << "Request/response mismatch";
68 
69   keymaster::Buffer read;
70   const uint8_t* read_data = response->payload;
71   EXPECT_TRUE(read.Deserialize(&read_data, read_data + response->payload_size))
72       << "Failed to deserialize request";
73   ASSERT_EQ(request.end() - request.begin(), read.end() - read.begin());
74   ASSERT_EQ(request.buffer_size(), read.buffer_size());
75   ASSERT_TRUE(std::equal(request.begin(), request.end(), read.begin()));
76 }
77 
78 }  // namespace cuttlefish
79