• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
18 #include "socket_mock_fuzz.h"
19 
SocketMockFuzz()20 SocketMockFuzz::SocketMockFuzz() : Socket(INVALID_SOCKET) {}
21 
~SocketMockFuzz()22 SocketMockFuzz::~SocketMockFuzz() {}
23 
Send(const void * data,size_t length)24 bool SocketMockFuzz::Send(const void* data, size_t length) {
25     if (events_.empty()) {
26         return false;
27     }
28 
29     if (events_.front().type != EventType::kSend) {
30         return false;
31     }
32 
33     std::string message(reinterpret_cast<const char*>(data), length);
34     if (events_.front().message != message) {
35         return false;
36     }
37 
38     bool return_value = events_.front().status;
39     events_.pop();
40     return return_value;
41 }
42 
43 // Mock out multi-buffer send to be one large send, since that's what it should looks like from
44 // the user's perspective.
Send(std::vector<cutils_socket_buffer_t> buffers)45 bool SocketMockFuzz::Send(std::vector<cutils_socket_buffer_t> buffers) {
46     std::string data;
47     for (const auto& buffer : buffers) {
48         data.append(reinterpret_cast<const char*>(buffer.data), buffer.length);
49     }
50     return Send(data.data(), data.size());
51 }
52 
Receive(void * data,size_t length,int)53 ssize_t SocketMockFuzz::Receive(void* data, size_t length, int /*timeout_ms*/) {
54     if (events_.empty()) {
55         return -1;
56     }
57 
58     const Event& event = events_.front();
59     if (event.type != EventType::kReceive) {
60         return -1;
61     }
62 
63     const std::string& message = event.message;
64     if (message.length() > length) {
65         return -1;
66     }
67 
68     receive_timed_out_ = event.status;
69     ssize_t return_value = message.length();
70 
71     // Empty message indicates failure.
72     if (message.empty()) {
73         return_value = -1;
74     } else {
75         memcpy(data, message.data(), message.length());
76     }
77 
78     events_.pop();
79     return return_value;
80 }
81 
Close()82 int SocketMockFuzz::Close() {
83     return 0;
84 }
85 
Accept()86 std::unique_ptr<Socket> SocketMockFuzz::Accept() {
87     if (events_.empty()) {
88         return nullptr;
89     }
90 
91     if (events_.front().type != EventType::kAccept) {
92         return nullptr;
93     }
94 
95     std::unique_ptr<Socket> sock = std::move(events_.front().sock);
96     events_.pop();
97     return sock;
98 }
99 
ExpectSend(std::string message)100 void SocketMockFuzz::ExpectSend(std::string message) {
101     events_.push(Event(EventType::kSend, std::move(message), true, nullptr));
102 }
103 
ExpectSendFailure(std::string message)104 void SocketMockFuzz::ExpectSendFailure(std::string message) {
105     events_.push(Event(EventType::kSend, std::move(message), false, nullptr));
106 }
107 
AddReceive(std::string message)108 void SocketMockFuzz::AddReceive(std::string message) {
109     events_.push(Event(EventType::kReceive, std::move(message), false, nullptr));
110 }
111 
AddReceiveTimeout()112 void SocketMockFuzz::AddReceiveTimeout() {
113     events_.push(Event(EventType::kReceive, "", true, nullptr));
114 }
115 
AddReceiveFailure()116 void SocketMockFuzz::AddReceiveFailure() {
117     events_.push(Event(EventType::kReceive, "", false, nullptr));
118 }
119 
AddAccept(std::unique_ptr<Socket> sock)120 void SocketMockFuzz::AddAccept(std::unique_ptr<Socket> sock) {
121     events_.push(Event(EventType::kAccept, "", false, std::move(sock)));
122 }
123 
Event(EventType _type,std::string _message,ssize_t _status,std::unique_ptr<Socket> _sock)124 SocketMockFuzz::Event::Event(EventType _type, std::string _message, ssize_t _status,
125                              std::unique_ptr<Socket> _sock)
126     : type(_type), message(_message), status(_status), sock(std::move(_sock)) {}
127