1 /*
2 * Copyright 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 <gtest/gtest.h>
17 #include <list>
18 #include <vector>
19
20 #include "bundler.h"
21 #include "bundler_generated.h"
22 #include "flatbuffers/flatbuffers.h"
23
24 bool LoadBinarySchema(const char* filename, std::string* binary_schema);
25 bool VerifyBinarySchema(const std::vector<uint8_t>& raw_schema);
26 bool CreateBinarySchemaBundle(
27 flatbuffers::FlatBufferBuilder* builder,
28 const std::vector<std::string>& filenames,
29 std::vector<flatbuffers::Offset<bluetooth::dumpsys::BundledSchemaMap>>* vector_map,
30 std::list<std::string>* bundled_names);
31 int WriteHeaderFile(FILE* fp, const uint8_t* data, size_t data_len);
32
33 class BundlerTest : public ::testing::Test {
34 public:
SetUp()35 void SetUp() override {}
36
TearDown()37 void TearDown() override {}
38 };
39
TEST_F(BundlerTest,LoadBinarySchema)40 TEST_F(BundlerTest, LoadBinarySchema) {
41 std::string string_schema;
42 ASSERT_FALSE(LoadBinarySchema(nullptr, &string_schema));
43 ASSERT_DEATH(LoadBinarySchema("test.bfbs", nullptr), "");
44 ASSERT_TRUE(LoadBinarySchema("test.bfbs", &string_schema));
45 ASSERT_FALSE(LoadBinarySchema("does_not_exist.bfbs", &string_schema));
46 }
47
TEST_F(BundlerTest,VerifyBinarySchema)48 TEST_F(BundlerTest, VerifyBinarySchema) {
49 std::string string_schema;
50 ASSERT_TRUE(LoadBinarySchema("test.bfbs", &string_schema));
51 std::vector<uint8_t> raw_schema(string_schema.begin(), string_schema.end());
52 ASSERT_TRUE(VerifyBinarySchema(raw_schema));
53
54 std::vector<uint8_t> bogus_raw_schema(string_schema.begin() + 1, string_schema.end());
55 ASSERT_FALSE(VerifyBinarySchema(bogus_raw_schema));
56 }
57
TEST_F(BundlerTest,CreateBinarySchemaBundle)58 TEST_F(BundlerTest, CreateBinarySchemaBundle) {
59 flatbuffers::FlatBufferBuilder builder;
60 std::vector<std::string> filenames;
61 std::vector<flatbuffers::Offset<bluetooth::dumpsys::BundledSchemaMap>> vector_map;
62 std::list<std::string> bundled_names;
63 ASSERT_TRUE(CreateBinarySchemaBundle(&builder, filenames, &vector_map, &bundled_names));
64 ASSERT_EQ(0, vector_map.size());
65 }
66
TEST_F(BundlerTest,WriteHeaderFile)67 TEST_F(BundlerTest, WriteHeaderFile) {
68 std::vector<uint8_t> data;
69 data.push_back(0x10);
70 data.push_back(0x11);
71 data.push_back(0x12);
72 data.push_back(0x13);
73 ASSERT_DEATH(WriteHeaderFile(nullptr, data.data(), data.size()), "");
74 FILE* fp = fopen("/tmp/test.h", "w+");
75 ASSERT_NE(fp, nullptr);
76 WriteHeaderFile(fp, data.data(), data.size());
77 fseek(fp, 0L, SEEK_SET);
78 char buf[16];
79 fread(buf, 1, 15, fp);
80 buf[12] = '\0';
81 std::string s(buf);
82 ASSERT_EQ("// Generated", s);
83 fclose(fp);
84 unlink("/tmp/test.h");
85 }
86