diff options
author | Kevin Chavez <kechavez@google.com> | 2016-08-04 19:00:38 -0400 |
---|---|---|
committer | Kevin Chavez <kechavez@google.com> | 2016-08-04 19:18:23 -0400 |
commit | aeaad8e8c7e612d20a8b4c9cccf44b7589a28a99 (patch) | |
tree | 2496133a52e1b1c3905762df1419e19892fc23eb | |
parent | ddd8ee3bd6e0b44d00c672e847985cc360d5cb36 (diff) | |
download | brillo-aeaad8e8c7e612d20a8b4c9cccf44b7589a28a99.tar.gz |
brillo: Add util and ab flow posix test harness.
Setup a test harness for ab flow in a posix environment. Utility functions and
ab flow logic, which will be used in a uefi environment, can be tested
by adding unit tests to bub_ab_flow_unittest.cc.
BUG=29072323
TEST=Passing unit tests for utf8 to ucs2 conversion and ongoing ab flow
logic testing.
Change-Id: Ifd3591e4cd54d92ef4979c9f4cc5df69359b96bd
-rw-r--r-- | brillo_uefi_x86_64/boot_loader/Android.mk | 76 | ||||
-rw-r--r-- | brillo_uefi_x86_64/boot_loader/bub_ab_flow.c | 21 | ||||
-rw-r--r-- | brillo_uefi_x86_64/boot_loader/bub_ab_flow.h | 29 | ||||
-rw-r--r-- | brillo_uefi_x86_64/boot_loader/bub_ab_flow_unittest.cc | 54 | ||||
-rw-r--r-- | brillo_uefi_x86_64/boot_loader/bub_sysdeps_posix.c | 65 | ||||
-rw-r--r-- | brillo_uefi_x86_64/boot_loader/bub_util.c | 3 |
6 files changed, 246 insertions, 2 deletions
diff --git a/brillo_uefi_x86_64/boot_loader/Android.mk b/brillo_uefi_x86_64/boot_loader/Android.mk new file mode 100644 index 0000000..ef5275d --- /dev/null +++ b/brillo_uefi_x86_64/boot_loader/Android.mk @@ -0,0 +1,76 @@ +# +# Copyright (C) 2016 The Android Open Source Project +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +LOCAL_PATH := $(my-dir) + +bub_common_cflags := \ + -D_FILE_OFFSET_BITS=64 \ + -D_POSIX_C_SOURCE=199309L \ + -Wa,--noexecstack \ + -Werror \ + -Wall \ + -Wextra \ + -Wformat=2 \ + -Wno-psabi \ + -Wno-unused-parameter \ + -ffunction-sections \ + -fstack-protector-strong \ + -fvisibility=hidden +bub_common_cppflags := \ + -Wnon-virtual-dtor \ + -fno-strict-aliasing +bub_common_ldflags := \ + -Wl,--gc-sections + + +# Build for the host (for unit tests). +include $(CLEAR_VARS) +LOCAL_MODULE := libbub_host +LOCAL_MODULE_HOST_OS := linux +LOCAL_MODULE_CLASS := STATIC_LIBRARIES +LOCAL_CLANG := true +LOCAL_CFLAGS := $(bub_common_cflags) -fno-stack-protector -DBUB_ENABLE_DEBUG -DBUB_COMPILATION +LOCAL_LDFLAGS := $(bub_common_ldflags) +LOCAL_C_INCLUDES := +LOCAL_SRC_FILES := \ + bub_sysdeps_posix.c \ + bub_ab_flow.c \ + bub_util.c \ +include $(BUILD_HOST_STATIC_LIBRARY) + +include $(CLEAR_VARS) +LOCAL_MODULE := libbub_host_unittest +LOCAL_MODULE_HOST_OS := linux +LOCAL_CPP_EXTENSION := .cc +LOCAL_CLANG := true +LOCAL_CFLAGS := $(bub_common_cflags) -DBUB_ENABLE_DEBUG -DBUB_COMPILATION +LOCAL_CPPFLAGS := $(bub_common_cppflags) +LOCAL_LDFLAGS := $(bub_common_ldflags) +LOCAL_C_INCLUDES := \ + $(LOCAL_PATH)/bub_sysdeps.h \ + $(LOCAL_PATH)/bub_ab_flow.h \ + $(LOCAL_PATH)/bub_util.h \ + external/gtest/include +LOCAL_STATIC_LIBRARIES := \ + libbub_host \ + libgmock_host \ + libgtest_host +LOCAL_SHARED_LIBRARIES := \ + libchrome +LOCAL_SRC_FILES := \ + bub_ab_flow_unittest.cc +LOCAL_LDLIBS_linux := -lrt +include $(BUILD_HOST_NATIVE_TEST) diff --git a/brillo_uefi_x86_64/boot_loader/bub_ab_flow.c b/brillo_uefi_x86_64/boot_loader/bub_ab_flow.c new file mode 100644 index 0000000..a3fbaf8 --- /dev/null +++ b/brillo_uefi_x86_64/boot_loader/bub_ab_flow.c @@ -0,0 +1,21 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "bub_ab_flow.h" + + BubAbFlowResult bub_ab_flow(BubOps* ops, char** out_selected_suffix) { + return BUB_AB_FLOW_RESULT_OK; + }
\ No newline at end of file diff --git a/brillo_uefi_x86_64/boot_loader/bub_ab_flow.h b/brillo_uefi_x86_64/boot_loader/bub_ab_flow.h new file mode 100644 index 0000000..ab297d0 --- /dev/null +++ b/brillo_uefi_x86_64/boot_loader/bub_ab_flow.h @@ -0,0 +1,29 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef BUB_AB_FLOW_H_ +#define BUB_AB_FLOW_H_ + +#include "bub_ops.h" + +typedef enum { + BUB_AB_FLOW_RESULT_OK, + BUB_AB_FLOW_RESULT_ERROR +} BubAbFlowResult; + +BubAbFlowResult bub_ab_flow(BubOps* ops, char** out_selected_suffix); + +#endif /* BUB_AB_FLOW_H_ */
\ No newline at end of file diff --git a/brillo_uefi_x86_64/boot_loader/bub_ab_flow_unittest.cc b/brillo_uefi_x86_64/boot_loader/bub_ab_flow_unittest.cc new file mode 100644 index 0000000..a7885a1 --- /dev/null +++ b/brillo_uefi_x86_64/boot_loader/bub_ab_flow_unittest.cc @@ -0,0 +1,54 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <gtest/gtest.h> +#include "bub_sysdeps.h" +#include "bub_ab_flow.h" +#include "bub_util.h" + +static int converted_utf8_ucs2(const char* data, + const char* raw_bytes, + size_t utf8_num_bytes) { + int ret; + size_t ucs2_num_bytes = sizeof(uint16_t) * utf8_num_bytes; + uint16_t* test_str = (uint16_t*)bub_calloc(ucs2_num_bytes); + if (test_str == NULL) { + fprintf(stderr, "Bad bub_calloc.\n"); + return 1; + } + if (utf8_to_ucs2(reinterpret_cast<const uint8_t*>(data), + utf8_num_bytes, test_str, ucs2_num_bytes)) { + bub_free(test_str); + return 1; + } + ret = bub_memcmp(reinterpret_cast<const uint16_t*>(raw_bytes), + test_str, bub_strlen(raw_bytes) + 1); + bub_free(test_str); + return ret; +} + +TEST(UtilTest, Utf8toUcs2) { + // UTF-8 3 bytes encoding case. + EXPECT_EQ(0, converted_utf8_ucs2("æ", "\xE6\x00", 3)); + // UTF-8 4 bytes encoding case. + EXPECT_EQ(0, converted_utf8_ucs2("€", "\xAC\x20", 4)); + // UTF-8 multiple character case. + EXPECT_EQ(0, converted_utf8_ucs2("AB", "\x41\x00\x42\x00", 6)); + + // These should fail. + // UTF-8 5 bytes encoding case. + EXPECT_NE(0, converted_utf8_ucs2("👦", "\x66\xF4", 5)); +} diff --git a/brillo_uefi_x86_64/boot_loader/bub_sysdeps_posix.c b/brillo_uefi_x86_64/boot_loader/bub_sysdeps_posix.c new file mode 100644 index 0000000..8782a50 --- /dev/null +++ b/brillo_uefi_x86_64/boot_loader/bub_sysdeps_posix.c @@ -0,0 +1,65 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// NOTE: See avb_sysdeps_posix.c + +#include <endian.h> +#include <stdarg.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "bub_sysdeps.h" + +int bub_memcmp(const void* src1, const void* src2, size_t n) { + return memcmp(src1, src2, n); +} + +void* bub_memcpy(void* dest, const void* src, size_t n) { + return memcpy(dest, src, n); +} + +void* bub_memset(void* dest, const int c, size_t n) { + return memset(dest, c, n); +} + +int bub_strcmp(const char* s1, const char* s2) { return strcmp(s1, s2); } + +size_t bub_strlen(const char* str) { return strlen(str); } + +int bub_safe_memcmp(const void* s1, const void* s2, size_t n) { + const unsigned char* us1 = s1; + const unsigned char* us2 = s2; + int result = 0; + + if (0 == n) return 0; + + /* + * Code snippet without data-dependent branch due to Nate Lawson + * (nate@root.org) of Root Labs. + */ + while (n--) result |= *us1++ ^ *us2++; + + return result != 0; +} + +void bub_abort(void) { abort(); } + +void bub_print(const char* format) { fprintf(stderr, format); } + +void* bub_malloc_(size_t size) { return malloc(size); } + +void bub_free(void* ptr) { free(ptr); }
\ No newline at end of file diff --git a/brillo_uefi_x86_64/boot_loader/bub_util.c b/brillo_uefi_x86_64/boot_loader/bub_util.c index 7a8e63b..fa7294b 100644 --- a/brillo_uefi_x86_64/boot_loader/bub_util.c +++ b/brillo_uefi_x86_64/boot_loader/bub_util.c @@ -15,6 +15,7 @@ */ #include "bub_sysdeps.h" +#include <stdio.h> int utf8_to_ucs2(const uint8_t* utf8_data, size_t utf8_num_bytes, @@ -39,13 +40,11 @@ int utf8_to_ucs2(const uint8_t* utf8_data, } else if (!(utf8_data[i8] >> 7)) { ucs2_data[i2] = (uint16_t)((uint16_t)utf8_data[i8] & 0x00FF); - //bub_print("(1) ucs2 = %lc, utf8 = %c ",ucs2_data[i2], utf8_data[i8]); i8++; } // invalid utf-8 else return 1; - //bub_print("\nloop\n"); i2++; } while (i8 < utf8_num_bytes - 1); return 0; |