1/* 2 * Copyright (C) 2016 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 "event_log_list_builder.h" 18 19#include <inttypes.h> 20#include <gtest/gtest.h> 21#include <gmock/gmock.h> 22#include <log/log.h> 23 24using testing::ElementsAreArray; 25 26TEST(EventLogListBuilder, Empty) { 27 EventLogListBuilder builder; 28 29 const uint8_t EXPECTED_LOG[] = { 30 EVENT_TYPE_LIST, 31 0, // Number of items in the list. 32 }; 33 34 std::unique_ptr<uint8_t[]> log; 35 size_t size; 36 builder.Release(&log, &size); 37 EXPECT_EQ(2U, size); 38 39 uint8_t* log_data = log.get(); 40 EXPECT_THAT(std::vector<uint8_t>(log_data, log_data + size), 41 ElementsAreArray(EXPECTED_LOG)); 42} 43 44TEST(EventLogListBuilder, SingleInt) { 45 EventLogListBuilder builder; 46 47 const uint8_t EXPECTED_LOG[] = { 48 EVENT_TYPE_LIST, 49 1, // Number of items in the list. 50 EVENT_TYPE_INT, 51 42, 0, 0, 0, // 4 byte integer value. 52 }; 53 54 builder.Append(42); 55 56 std::unique_ptr<uint8_t[]> log; 57 size_t size; 58 builder.Release(&log, &size); 59 EXPECT_EQ(7U, size); 60 61 uint8_t* log_data = log.get(); 62 EXPECT_THAT(std::vector<uint8_t>(log_data, log_data + size), 63 ElementsAreArray(EXPECTED_LOG)); 64} 65 66TEST(EventLogListBuilder, SingleString) { 67 EventLogListBuilder builder; 68 69 const uint8_t EXPECTED_LOG[] = { 70 EVENT_TYPE_LIST, 71 1, // Number of items in the list. 72 EVENT_TYPE_STRING, 73 5, 0, 0, 0, // 4 byte length of the string. 74 'D', 'r', 'o', 'i', 'd', 75 }; 76 77 builder.Append("Droid"); 78 79 std::unique_ptr<uint8_t[]> log; 80 size_t size; 81 builder.Release(&log, &size); 82 EXPECT_EQ(12U, size); 83 84 uint8_t* log_data = log.get(); 85 EXPECT_THAT(std::vector<uint8_t>(log_data, log_data + size), 86 ElementsAreArray(EXPECTED_LOG)); 87} 88 89TEST(EventLogListBuilder, IntThenString) { 90 EventLogListBuilder builder; 91 92 const uint8_t EXPECTED_LOG[] = { 93 EVENT_TYPE_LIST, 94 2, // Number of items in the list. 95 EVENT_TYPE_INT, 96 42, 0, 0, 0, // 4 byte integer value. 97 EVENT_TYPE_STRING, 98 5, 0, 0, 0, // 4 byte length of the string. 99 'D', 'r', 'o', 'i', 'd', 100 }; 101 102 builder.Append(42); 103 builder.Append("Droid"); 104 105 std::unique_ptr<uint8_t[]> log; 106 size_t size; 107 builder.Release(&log, &size); 108 EXPECT_EQ(17U, size); 109 110 uint8_t* log_data = log.get(); 111 EXPECT_THAT(std::vector<uint8_t>(log_data, log_data + size), 112 ElementsAreArray(EXPECTED_LOG)); 113} 114