1 /*
2 * Copyright (C) 2017 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 #include <cstring>
18 #include "gtest/gtest.h"
19
20 #include "chre/apps/wifi_offload/flatbuffers_serialization.h"
21
22 #include "include/utility.h"
23
24 using wifi_offload::fbs::Deserialize;
25 using wifi_offload::fbs::Serialize;
26
27 template <typename TestType>
28 class FlatbuffersSerializationTest : public testing::Test {
29 public:
30 // RandomGenerator used to initialize data-types with random values
31 wifi_offload_test::RandomGenerator random_gen_;
32
33 static const size_t kBufferLen = CHRE_MESSAGE_TO_HOST_MAX_SIZE;
34 uint8_t buffer[kBufferLen];
35 };
36
37 typedef testing::Types<wifi_offload::ScanStats, wifi_offload::ScanConfig,
38 wifi_offload::Vector<wifi_offload::ScanResult>>
39 Implementations;
40
41 TYPED_TEST_CASE(FlatbuffersSerializationTest, Implementations);
42
TYPED_TEST(FlatbuffersSerializationTest,SerializationWithNullBufferReturnsRequiredBufferSize)43 TYPED_TEST(FlatbuffersSerializationTest,
44 SerializationWithNullBufferReturnsRequiredBufferSize) {
45 TypeParam test_obj;
46 init(test_obj, this->random_gen_);
47
48 size_t required_buff_size = Serialize(test_obj, nullptr, 0);
49 EXPECT_NE(0, required_buff_size);
50
51 size_t serialized_size = Serialize(test_obj, this->buffer, this->kBufferLen);
52 ASSERT_NE(0, serialized_size);
53
54 EXPECT_EQ(serialized_size, required_buff_size);
55 }
56
TYPED_TEST(FlatbuffersSerializationTest,SerializationThenDeserializationCreatesEqualValue)57 TYPED_TEST(FlatbuffersSerializationTest,
58 SerializationThenDeserializationCreatesEqualValue) {
59 TypeParam test_obj;
60 init(test_obj, this->random_gen_);
61
62 size_t serialized_size = Serialize(test_obj, this->buffer, this->kBufferLen);
63 ASSERT_NE(0, serialized_size);
64
65 TypeParam deserialized_obj;
66 ASSERT_TRUE(Deserialize(this->buffer, serialized_size, &deserialized_obj));
67 EXPECT_EQ(test_obj, deserialized_obj);
68 }
69
TYPED_TEST(FlatbuffersSerializationTest,NegativeTestsForSerialization)70 TYPED_TEST(FlatbuffersSerializationTest, NegativeTestsForSerialization) {
71 TypeParam test_obj;
72 init(test_obj, this->random_gen_);
73
74 EXPECT_EQ(0, Serialize(test_obj, this->buffer, 0)); // zero buffer size
75 EXPECT_EQ(0, Serialize(test_obj, this->buffer, 10)); // buffer too small
76 }
77
TYPED_TEST(FlatbuffersSerializationTest,NegativeTestsForDeserialization)78 TYPED_TEST(FlatbuffersSerializationTest, NegativeTestsForDeserialization) {
79 TypeParam test_obj;
80 init(test_obj, this->random_gen_);
81
82 // The first 4 bytes in the buffer represent the position of the root
83 // table, so corrupting it should force the deserialization to fail.
84 constexpr size_t kRootTableOffsetSize = 4;
85 size_t serialized_size = Serialize(test_obj, this->buffer, this->kBufferLen);
86 ASSERT_GE(serialized_size, kRootTableOffsetSize);
87
88 TypeParam new_obj;
89 EXPECT_FALSE(Deserialize(nullptr, serialized_size, &new_obj));
90 EXPECT_FALSE(Deserialize(this->buffer, 0, &new_obj));
91
92 // Corrupt the root table's offset
93 std::memset(this->buffer, 0xff, kRootTableOffsetSize);
94 EXPECT_FALSE(Deserialize(this->buffer, serialized_size, &new_obj));
95 }
96