• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "flutter/shell/platform/common/cpp/client_wrapper/include/flutter/standard_message_codec.h"
6 
7 #include <map>
8 #include <vector>
9 
10 #include "flutter/shell/platform/common/cpp/client_wrapper/testing/encodable_value_utils.h"
11 #include "gtest/gtest.h"
12 
13 namespace flutter {
14 
15 // Validates round-trip encoding and decoding of |value|, and checks that the
16 // encoded value matches |expected_encoding|.
CheckEncodeDecode(const EncodableValue & value,const std::vector<uint8_t> & expected_encoding)17 static void CheckEncodeDecode(const EncodableValue& value,
18                               const std::vector<uint8_t>& expected_encoding) {
19   const StandardMessageCodec& codec = StandardMessageCodec::GetInstance();
20   auto encoded = codec.EncodeMessage(value);
21   ASSERT_TRUE(encoded);
22   EXPECT_EQ(*encoded, expected_encoding);
23 
24   auto decoded = codec.DecodeMessage(*encoded);
25   EXPECT_TRUE(testing::EncodableValuesAreEqual(value, *decoded));
26 }
27 
28 // Validates round-trip encoding and decoding of |value|, and checks that the
29 // encoded value has the given prefix and length.
30 //
31 // This should be used only for Map, where asserting the order of the elements
32 // in a test is undesirable.
CheckEncodeDecodeWithEncodePrefix(const EncodableValue & value,const std::vector<uint8_t> & expected_encoding_prefix,size_t expected_encoding_length)33 static void CheckEncodeDecodeWithEncodePrefix(
34     const EncodableValue& value,
35     const std::vector<uint8_t>& expected_encoding_prefix,
36     size_t expected_encoding_length) {
37   EXPECT_TRUE(value.IsMap());
38   const StandardMessageCodec& codec = StandardMessageCodec::GetInstance();
39   auto encoded = codec.EncodeMessage(value);
40   ASSERT_TRUE(encoded);
41 
42   EXPECT_EQ(encoded->size(), expected_encoding_length);
43   ASSERT_GT(encoded->size(), expected_encoding_prefix.size());
44   EXPECT_TRUE(std::equal(
45       encoded->begin(), encoded->begin() + expected_encoding_prefix.size(),
46       expected_encoding_prefix.begin(), expected_encoding_prefix.end()));
47 
48   auto decoded = codec.DecodeMessage(*encoded);
49   EXPECT_TRUE(testing::EncodableValuesAreEqual(value, *decoded));
50 }
51 
TEST(StandardMessageCodec,CanEncodeAndDecodeNull)52 TEST(StandardMessageCodec, CanEncodeAndDecodeNull) {
53   std::vector<uint8_t> bytes = {0x00};
54   CheckEncodeDecode(EncodableValue(), bytes);
55 }
56 
TEST(StandardMessageCodec,CanEncodeAndDecodeTrue)57 TEST(StandardMessageCodec, CanEncodeAndDecodeTrue) {
58   std::vector<uint8_t> bytes = {0x01};
59   CheckEncodeDecode(EncodableValue(true), bytes);
60 }
61 
TEST(StandardMessageCodec,CanEncodeAndDecodeFalse)62 TEST(StandardMessageCodec, CanEncodeAndDecodeFalse) {
63   std::vector<uint8_t> bytes = {0x02};
64   CheckEncodeDecode(EncodableValue(false), bytes);
65 }
66 
TEST(StandardMessageCodec,CanEncodeAndDecodeInt32)67 TEST(StandardMessageCodec, CanEncodeAndDecodeInt32) {
68   std::vector<uint8_t> bytes = {0x03, 0x78, 0x56, 0x34, 0x12};
69   CheckEncodeDecode(EncodableValue(0x12345678), bytes);
70 }
71 
TEST(StandardMessageCodec,CanEncodeAndDecodeInt64)72 TEST(StandardMessageCodec, CanEncodeAndDecodeInt64) {
73   std::vector<uint8_t> bytes = {0x04, 0xef, 0xcd, 0xab, 0x90,
74                                 0x78, 0x56, 0x34, 0x12};
75   CheckEncodeDecode(EncodableValue(INT64_C(0x1234567890abcdef)), bytes);
76 }
77 
TEST(StandardMessageCodec,CanEncodeAndDecodeDouble)78 TEST(StandardMessageCodec, CanEncodeAndDecodeDouble) {
79   std::vector<uint8_t> bytes = {0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
80                                 0x18, 0x2d, 0x44, 0x54, 0xfb, 0x21, 0x09, 0x40};
81   CheckEncodeDecode(EncodableValue(3.14159265358979311599796346854), bytes);
82 }
83 
TEST(StandardMessageCodec,CanEncodeAndDecodeString)84 TEST(StandardMessageCodec, CanEncodeAndDecodeString) {
85   std::vector<uint8_t> bytes = {0x07, 0x0b, 0x68, 0x65, 0x6c, 0x6c, 0x6f,
86                                 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64};
87   CheckEncodeDecode(EncodableValue(u8"hello world"), bytes);
88 }
89 
TEST(StandardMessageCodec,CanEncodeAndDecodeStringWithNonAsciiCodePoint)90 TEST(StandardMessageCodec, CanEncodeAndDecodeStringWithNonAsciiCodePoint) {
91   std::vector<uint8_t> bytes = {0x07, 0x05, 0x68, 0xe2, 0x98, 0xba, 0x77};
92   CheckEncodeDecode(EncodableValue(u8"h\u263Aw"), bytes);
93 }
94 
TEST(StandardMessageCodec,CanEncodeAndDecodeStringWithNonBMPCodePoint)95 TEST(StandardMessageCodec, CanEncodeAndDecodeStringWithNonBMPCodePoint) {
96   std::vector<uint8_t> bytes = {0x07, 0x06, 0x68, 0xf0, 0x9f, 0x98, 0x82, 0x77};
97   CheckEncodeDecode(EncodableValue(u8"h\U0001F602w"), bytes);
98 }
99 
TEST(StandardMessageCodec,CanEncodeAndDecodeList)100 TEST(StandardMessageCodec, CanEncodeAndDecodeList) {
101   std::vector<uint8_t> bytes = {
102       0x0c, 0x05, 0x00, 0x07, 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x06,
103       0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0x85, 0xeb, 0x51, 0xb8, 0x1e,
104       0x09, 0x40, 0x03, 0x2f, 0x00, 0x00, 0x00, 0x0c, 0x02, 0x03, 0x2a,
105       0x00, 0x00, 0x00, 0x07, 0x06, 0x6e, 0x65, 0x73, 0x74, 0x65, 0x64,
106   };
107   EncodableValue value(EncodableList{
108       EncodableValue(),
109       EncodableValue("hello"),
110       EncodableValue(3.14),
111       EncodableValue(47),
112       EncodableValue(EncodableList{
113           EncodableValue(42),
114           EncodableValue("nested"),
115       }),
116   });
117   CheckEncodeDecode(value, bytes);
118 }
119 
TEST(StandardMessageCodec,CanEncodeAndDecodeMap)120 TEST(StandardMessageCodec, CanEncodeAndDecodeMap) {
121   std::vector<uint8_t> bytes_prefix = {0x0d, 0x04};
122   EncodableValue value(EncodableMap{
123       {EncodableValue("a"), EncodableValue(3.14)},
124       {EncodableValue("b"), EncodableValue(47)},
125       {EncodableValue(), EncodableValue()},
126       {EncodableValue(3.14), EncodableValue(EncodableList{
127                                  EncodableValue("nested"),
128                              })},
129   });
130   CheckEncodeDecodeWithEncodePrefix(value, bytes_prefix, 48);
131 }
132 
TEST(StandardMessageCodec,CanEncodeAndDecodeByteArray)133 TEST(StandardMessageCodec, CanEncodeAndDecodeByteArray) {
134   std::vector<uint8_t> bytes = {0x08, 0x04, 0xba, 0x5e, 0xba, 0x11};
135   EncodableValue value(std::vector<uint8_t>{0xba, 0x5e, 0xba, 0x11});
136   CheckEncodeDecode(value, bytes);
137 }
138 
TEST(StandardMessageCodec,CanEncodeAndDecodeInt32Array)139 TEST(StandardMessageCodec, CanEncodeAndDecodeInt32Array) {
140   std::vector<uint8_t> bytes = {0x09, 0x03, 0x00, 0x00, 0x78, 0x56, 0x34, 0x12,
141                                 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00};
142   EncodableValue value(std::vector<int32_t>{0x12345678, -1, 0});
143   CheckEncodeDecode(value, bytes);
144 }
145 
TEST(StandardMessageCodec,CanEncodeAndDecodeInt64Array)146 TEST(StandardMessageCodec, CanEncodeAndDecodeInt64Array) {
147   std::vector<uint8_t> bytes = {0x0a, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
148                                 0xef, 0xcd, 0xab, 0x90, 0x78, 0x56, 0x34, 0x12,
149                                 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
150   EncodableValue value(std::vector<int64_t>{0x1234567890abcdef, -1});
151   CheckEncodeDecode(value, bytes);
152 }
153 
TEST(StandardMessageCodec,CanEncodeAndDecodeFloat64Array)154 TEST(StandardMessageCodec, CanEncodeAndDecodeFloat64Array) {
155   std::vector<uint8_t> bytes = {0x0b, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
156                                 0x18, 0x2d, 0x44, 0x54, 0xfb, 0x21, 0x09, 0x40,
157                                 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x8f, 0x40};
158   EncodableValue value(
159       std::vector<double>{3.14159265358979311599796346854, 1000.0});
160   CheckEncodeDecode(value, bytes);
161 }
162 
163 }  // namespace flutter
164