1 //
2 // Copyright (C) 2020 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/modem_simulator/command_parser.h"
17 
18 #include <gtest/gtest.h>
19 
TEST(CommandParserUnitTest,SkipPrefix)20 TEST(CommandParserUnitTest, SkipPrefix) {
21   std::string command = "AT+SPUSATENVECMD=\"D3078202018190014E\"";
22 
23   cuttlefish::CommandParser cmd(command);
24   cmd.SkipPrefix();
25   std::string result(*cmd);
26   ASSERT_STREQ("\"D3078202018190014E\"", result.c_str());
27 }
28 
TEST(CommandParserUnitTest,SkipPrefixAT)29 TEST(CommandParserUnitTest, SkipPrefixAT) {
30   std::string command = "AT+SPUSATENVECMD=\"D3078202018190014E\"";
31 
32   cuttlefish::CommandParser cmd(command);
33   cmd.SkipPrefixAT();
34   std::string result(*cmd);
35   ASSERT_STREQ("+SPUSATENVECMD=\"D3078202018190014E\"", result.c_str());
36 }
37 
TEST(CommandParserUnitTest,SkipComma)38 TEST(CommandParserUnitTest, SkipComma) {
39   std::string command = "+COPS: 0,1,\"CMCC\",7";
40 
41   cuttlefish::CommandParser cmd(command);
42   cmd.SkipComma();
43   std::string result(*cmd);
44   ASSERT_STREQ("1,\"CMCC\",7", result.c_str());
45 }
46 
TEST(CommandParserUnitTest,SkipWhiteSpace)47 TEST(CommandParserUnitTest, SkipWhiteSpace) {
48   std::string command = "+COPS: 0,1,\"CMCC\",7";
49   cuttlefish::CommandParser cmd(command);
50 
51   cmd.GetNextStr(':');
52   cmd.SkipWhiteSpace();
53   std::string result(*cmd);
54   ASSERT_STREQ("0,1,\"CMCC\",7", result.c_str());
55 }
56 
TEST(CommandParserUnitTest,GetNextStr_default)57 TEST(CommandParserUnitTest, GetNextStr_default) {
58   std::string command = "+COPS: 0,1,\"CMCC\",7";
59 
60   cuttlefish::CommandParser cmd(command);
61   std::string result(cmd.GetNextStr());
62   ASSERT_STREQ("CMCC", result.c_str());
63 }
64 
TEST(CommandParserUnitTest,GetNextStr_withparam)65 TEST(CommandParserUnitTest, GetNextStr_withparam) {
66   std::string command = "+COPS: 0,1,\"CMCC\",7";
67 
68   cuttlefish::CommandParser cmd(command);
69   std::string result(cmd.GetNextStr(','));
70   ASSERT_STREQ("+COPS: 0", result.c_str());
71 
72   std::string result2(cmd.GetNextStr(';'));
73   ASSERT_STREQ("1,\"CMCC\",7", result2.c_str());
74 }
75 
TEST(CommandParserUnitTest,GetNextInt)76 TEST(CommandParserUnitTest, GetNextInt) {
77   std::string command = "AT+CRSM=192,28421,0,0,15,0,\"3F007FFF\"";
78 
79   cuttlefish::CommandParser cmd(command);
80   cmd.SkipPrefix();  // skip "AT+CRSM="
81   ASSERT_EQ(192, cmd.GetNextInt());
82   ASSERT_EQ(28421, cmd.GetNextInt());
83 }
84 
TEST(CommandParserUnitTest,GetNextHexInt)85 TEST(CommandParserUnitTest, GetNextHexInt) {  // Hexadecimal string to decimal value
86   std::string command = "C0,6F05";
87 
88   cuttlefish::CommandParser cmd(command);
89   ASSERT_EQ(192, cmd.GetNextHexInt());
90   ASSERT_EQ(28421, cmd.GetNextHexInt());
91 }
92 
TEST(CommandParserUnitTest,GetNextStrDeciToHex)93 TEST(CommandParserUnitTest, GetNextStrDeciToHex) {
94   std::string command = "AT+CRSM=192,28421,0,0,15,0,\"3F007FFF\"";
95 
96   cuttlefish::CommandParser cmd(command);
97   cmd.SkipPrefix();
98   std::string result(cmd.GetNextStrDeciToHex());
99   ASSERT_STREQ("C0", result.c_str());  // 192
100 
101   std::string result2(cmd.GetNextStrDeciToHex());
102   ASSERT_STREQ("6F05", result2.c_str());  // 28421
103 }
104