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 "gtest/gtest.h"
18
19 #include "include/utility.h"
20
21 template <typename TestType>
22 class OffloadTypesTest : public testing::Test {
23 public:
24 // RandomGenerator used to initialize data-types with random values
25 wifi_offload_test::RandomGenerator random_gen_;
26
27 flatbuffers::FlatBufferBuilder builder;
28
EqualOperatorReturnsTrueForEqualValues()29 void EqualOperatorReturnsTrueForEqualValues() {
30 TestType lhs;
31 init(lhs, random_gen_);
32
33 random_gen_.Reset();
34 TestType rhs;
35 init(rhs, random_gen_);
36
37 EXPECT_EQ(lhs, rhs);
38 }
39
EqualOperatorReturnsFalseForDifferentValues()40 void EqualOperatorReturnsFalseForDifferentValues() {
41 TestType lhs, rhs;
42 init(lhs, random_gen_);
43 init(rhs, random_gen_);
44
45 ASSERT_FALSE(lhs == rhs);
46 }
47
SerializationThenDeserializationCreatesEqualValue()48 void SerializationThenDeserializationCreatesEqualValue() {
49 TestType test_obj;
50 init(test_obj, random_gen_);
51 builder.Finish(test_obj.Serialize(&builder));
52
53 const uint8_t *serialized_buff = builder.GetBufferPointer();
54 const size_t serialized_size = builder.GetSize();
55 ASSERT_NE(nullptr, serialized_buff);
56 ASSERT_NE(0, serialized_size);
57
58 flatbuffers::Verifier verifier(serialized_buff, serialized_size);
59 ASSERT_TRUE(verifier.VerifyBuffer<typename TestType::FbsType>(nullptr));
60
61 const auto fbs_obj =
62 flatbuffers::GetRoot<typename TestType::FbsType>(serialized_buff);
63 ASSERT_NE(nullptr, fbs_obj);
64
65 TestType deserialized_obj;
66 ASSERT_TRUE(deserialized_obj.Deserialize(*fbs_obj));
67 EXPECT_EQ(test_obj, deserialized_obj);
68 }
69 };
70
71 typedef testing::Types<wifi_offload::PreferredNetwork, wifi_offload::ScanResult,
72 wifi_offload::ScanParams, wifi_offload::ScanFilter,
73 wifi_offload::ScanConfig, wifi_offload::ScanRecord,
74 wifi_offload::RpcLogRecord, wifi_offload::ScanStats>
75 Implementations;
76
77 TYPED_TEST_CASE(OffloadTypesTest, Implementations);
78
TYPED_TEST(OffloadTypesTest,EqualOperatorReturnsTrueForEqualValues)79 TYPED_TEST(OffloadTypesTest, EqualOperatorReturnsTrueForEqualValues) {
80 this->EqualOperatorReturnsTrueForEqualValues();
81 }
82
TYPED_TEST(OffloadTypesTest,EqualOperatorReturnsFalseForDifferentValues)83 TYPED_TEST(OffloadTypesTest, EqualOperatorReturnsFalseForDifferentValues) {
84 this->EqualOperatorReturnsFalseForDifferentValues();
85 }
86
TYPED_TEST(OffloadTypesTest,SerializationThenDeserializationCreatesEqualValue)87 TYPED_TEST(OffloadTypesTest,
88 SerializationThenDeserializationCreatesEqualValue) {
89 this->SerializationThenDeserializationCreatesEqualValue();
90 }
91