blob: 2bdfb7ddb52fe7e6137a390282b2fce1605bbf4f [file] [log] [blame]
// Copyright 2022 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <lib/efi/testing/stub_boot_services.h>
#include <string>
#include <string_view>
#include <efi/protocol/loaded-image.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "xefi.h"
// Needed to use the ""s operator to embed nulls in string literals.
using namespace std::string_literals;
namespace {
const efi_handle kImageHandle = reinterpret_cast<efi_handle>(0x10);
// Injects mock load options, calls xefi_get_load_options(), and returns the
// resulting UTF-16 string (including the null terminator).
std::u16string GetLoadOptions(const void* contents, uint32_t size) {
efi::MockBootServices boot_services;
gImg = kImageHandle;
gBS = boot_services.services();
efi_loaded_image_protocol loaded_image_protocol = {.LoadOptionsSize = size,
.LoadOptions = const_cast<void*>(contents)};
boot_services.ExpectProtocol(gImg, LoadedImageProtocol, &loaded_image_protocol);
void* load_options = nullptr;
size_t load_options_size = 0;
EXPECT_EQ(EFI_SUCCESS, xefi_get_load_options(&load_options_size, &load_options));
// Extract the result before we free the pool.
EXPECT_EQ(0u, load_options_size % sizeof(char16_t));
std::u16string result(reinterpret_cast<char16_t*>(load_options),
load_options_size / sizeof(char16_t));
boot_services.FreePool(load_options);
return result;
}
// Overload to make it easier to call using just a string_view param.
std::u16string GetLoadOptions(std::u16string_view contents) {
return GetLoadOptions(contents.data(),
static_cast<uint32_t>(contents.length() * sizeof(contents[0])));
}
// TODO(https://fxbug.dev/100791): 0x5A is our default memory contents in tests
// to make sure we're initializing allocated memory properly. In this case we
// are not, and doing so causes other bugs to trigger. Fix the other bugs and
// correct initialize memory.
TEST(Cmdline, XefiGetLoadOptions) {
EXPECT_EQ(GetLoadOptions(u"foo bar 123"), u"foo bar 123\x5A5A");
}
TEST(Cmdline, XefiGetLoadOptionsNull) { EXPECT_EQ(GetLoadOptions(nullptr, 0), u"\x5A5A"); }
TEST(Cmdline, XefiGetLoadOptionsEmpty) { EXPECT_EQ(GetLoadOptions(u""), u"\x5A5A"); }
TEST(Cmdline, XefiGetLoadOptionsEmbeddedNull) {
EXPECT_EQ(GetLoadOptions(u"foo\0bar"s), u"foo\0bar\x5A5A"s);
}
} // namespace