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