1 // Copyright 2022 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <google/protobuf/util/json_util.h>
16
17 #include <cstdint>
18 #include <string>
19
20 #include "gtest/gtest.h"
21 #include "startup.pb.h"
22
23 namespace netsim {
24 namespace testing {
25 namespace {
26
27 // Test json format of the proto
TEST(StartupTest,MessageToJsonStringTest)28 TEST(StartupTest, MessageToJsonStringTest) {
29 netsim::startup::StartupInfo info;
30
31 auto device = info.add_devices();
32 device->set_name("emulator-5554");
33
34 auto chip = device->add_chips();
35 chip->set_kind(common::ChipKind::BLUETOOTH);
36 chip->set_fd_in(1);
37 chip->set_fd_out(2);
38
39 std::string json_string;
40 google::protobuf::util::JsonPrintOptions options;
41 MessageToJsonString(info, &json_string, options);
42
43 EXPECT_EQ(
44 json_string,
45 std::string(
46 R"({"devices":[{"name":"emulator-5554","chips":[{"kind":"BLUETOOTH","fdIn":1,"fdOut":2}]}]})"));
47 }
48
49 // Test reading json format of the proto
TEST(StartupTest,JsonStringToMessageTest)50 TEST(StartupTest, JsonStringToMessageTest) {
51 auto r =
52 R"({devices:[
53 {name:"0.0.0.0:6520",chips:[{kind:"BLUETOOTH", fd_in:1,fd_out:2}]},
54 {name: "0.0.0.0:6521", chips:[{kind: "BLUETOOTH", fd_in:2, fd_out:3}]}]})";
55
56 google::protobuf::util::JsonParseOptions options;
57 netsim::startup::StartupInfo info;
58 JsonStringToMessage(r, &info, options);
59
60 ASSERT_EQ(info.devices().size(), 2);
61 auto &device = info.devices().Get(0);
62 ASSERT_EQ(device.name(), "0.0.0.0:6520");
63 ASSERT_EQ(device.chips().size(), 1);
64 auto &chip = device.chips().Get(0);
65 ASSERT_EQ(chip.kind(), common::ChipKind::BLUETOOTH);
66 ASSERT_EQ(chip.fd_in(), 1);
67 }
68
69 } // namespace
70 } // namespace testing
71 } // namespace netsim
72