1 //
2 // Copyright (C) 2022 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 #include "host/commands/cvd_send_sms/sms_sender.h"
17
18 #include <sstream>
19
20 #include <android-base/logging.h>
21 #include <gmock/gmock.h>
22 #include <gtest/gtest.h>
23
24 #include "common/libs/fs/shared_fd.h"
25
26 namespace cuttlefish {
27 namespace {
28
29 class SmsSenderTest : public ::testing::Test {
30 protected:
SetUp()31 void SetUp() override {
32 CHECK(SharedFD::SocketPair(AF_LOCAL, SOCK_STREAM, 0, &client_fd_,
33 &fake_server_fd_))
34 << strerror(errno);
35 CHECK(client_fd_->IsOpen());
36 CHECK(fake_server_fd_->IsOpen());
37 }
38
AssertCommandIsSent(std::string expected_command)39 void AssertCommandIsSent(std::string expected_command) {
40 std::stringstream ss;
41 std::vector<char> buffer(4096);
42 ssize_t bytes_read;
43 do {
44 bytes_read = fake_server_fd_->Read(buffer.data(), buffer.size());
45 CHECK(bytes_read >= 0) << strerror(errno);
46 ss << std::string(buffer.data(), bytes_read);
47 } while (buffer[bytes_read - 1] != '\r');
48 EXPECT_THAT(ss.str(), testing::Eq(expected_command));
49 }
50
51 SharedFD client_fd_;
52 SharedFD fake_server_fd_;
53 };
54
TEST_F(SmsSenderTest,InvalidContentFails)55 TEST_F(SmsSenderTest, InvalidContentFails) {
56 SmsSender sender(client_fd_);
57
58 bool result = sender.Send("", "+16501234567");
59
60 EXPECT_FALSE(result);
61 }
62
TEST_F(SmsSenderTest,ValidContentSucceeds)63 TEST_F(SmsSenderTest, ValidContentSucceeds) {
64 SmsSender sender(client_fd_);
65
66 bool result = sender.Send("hellohello", "+16501234567");
67
68 EXPECT_TRUE(result);
69 AssertCommandIsSent(
70 "REM0AT+REMOTESMS=0001000b916105214365f700000ae8329bfd4697d9ec37\r");
71 }
72
TEST_F(SmsSenderTest,NonDefaultModemIdValueSucceeds)73 TEST_F(SmsSenderTest, NonDefaultModemIdValueSucceeds) {
74 SmsSender sender(client_fd_);
75
76 bool result = sender.Send("hellohello", "+16501234567", 1);
77
78 EXPECT_TRUE(result);
79 AssertCommandIsSent(
80 "REM1AT+REMOTESMS=0001000b916105214365f700000ae8329bfd4697d9ec37\r");
81 }
82
83 } // namespace
84 } // namespace cuttlefish
85