1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
|
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/webshare/win/fake_uri_runtime_class_factory.h"
#include <string>
#include <tuple>
#include "base/notreached.h"
#include "base/win/scoped_hstring.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
using ABI::Windows::Foundation::IUriRuntimeClass;
using ABI::Windows::Foundation::IWwwFormUrlDecoderRuntimeClass;
using Microsoft::WRL::Make;
using Microsoft::WRL::RuntimeClass;
using Microsoft::WRL::RuntimeClassFlags;
using Microsoft::WRL::WinRtClassicComMix;
namespace webshare {
namespace {
// Provides an implementation of IUriRuntimeClass for use in GTests.
//
// Note that implementations for all the functions except get_RawUri are
// intentionally omitted. Though they are safe APIs, they have many subtle
// differences from the behaviors of a GURL. To prevent inconsistencies and
// unexpected edge cases get_RawUri should be used to construct a GURL and its
// similar functionality leveraged, rather than relying on these functions.
class FakeUriRuntimeClass final
: public RuntimeClass<RuntimeClassFlags<WinRtClassicComMix>,
IUriRuntimeClass> {
public:
explicit FakeUriRuntimeClass(std::string raw_uri) : raw_uri_(raw_uri) {}
FakeUriRuntimeClass(const FakeUriRuntimeClass&) = delete;
FakeUriRuntimeClass& operator=(const FakeUriRuntimeClass&) = delete;
~FakeUriRuntimeClass() final = default;
// ABI::Windows::Foundation::IUriRuntimeClass:
IFACEMETHODIMP get_AbsoluteUri(HSTRING* value) final {
NOTREACHED() << "get_RawUri should be the only function called on an "
"IUriRuntimeClass - see FakeUriRuntimeClass.";
}
IFACEMETHODIMP get_DisplayUri(HSTRING* value) final {
NOTREACHED() << "get_RawUri should be the only function called on an "
"IUriRuntimeClass - see FakeUriRuntimeClass.";
}
IFACEMETHODIMP get_Domain(HSTRING* value) final {
NOTREACHED() << "get_RawUri should be the only function called on an "
"IUriRuntimeClass - see FakeUriRuntimeClass.";
}
IFACEMETHODIMP get_Extension(HSTRING* value) final {
NOTREACHED() << "get_RawUri should be the only function called on an "
"IUriRuntimeClass - see FakeUriRuntimeClass.";
}
IFACEMETHODIMP get_Fragment(HSTRING* value) final {
NOTREACHED() << "get_RawUri should be the only function called on an "
"IUriRuntimeClass - see FakeUriRuntimeClass.";
}
IFACEMETHODIMP get_Host(HSTRING* value) final {
NOTREACHED() << "get_RawUri should be the only function called on an "
"IUriRuntimeClass - see FakeUriRuntimeClass.";
}
IFACEMETHODIMP get_Password(HSTRING* value) final {
NOTREACHED() << "get_RawUri should be the only function called on an "
"IUriRuntimeClass - see FakeUriRuntimeClass.";
}
IFACEMETHODIMP get_Path(HSTRING* value) final {
NOTREACHED() << "get_RawUri should be the only function called on an "
"IUriRuntimeClass - see FakeUriRuntimeClass.";
}
IFACEMETHODIMP get_Query(HSTRING* value) final {
NOTREACHED() << "get_RawUri should be the only function called on an "
"IUriRuntimeClass - see FakeUriRuntimeClass.";
}
IFACEMETHODIMP get_QueryParsed(
IWwwFormUrlDecoderRuntimeClass** www_form_url_decoder) final {
NOTREACHED() << "get_RawUri should be the only function called on an "
"IUriRuntimeClass - see FakeUriRuntimeClass.";
}
IFACEMETHODIMP get_RawUri(HSTRING* value) final {
auto copy = base::win::ScopedHString::Create(raw_uri_);
*value = copy.release();
return S_OK;
}
IFACEMETHODIMP get_SchemeName(HSTRING* value) final {
NOTREACHED() << "get_RawUri should be the only function called on an "
"IUriRuntimeClass - see FakeUriRuntimeClass.";
}
IFACEMETHODIMP get_UserName(HSTRING* value) final {
NOTREACHED() << "get_RawUri should be the only function called on an "
"IUriRuntimeClass - see FakeUriRuntimeClass.";
}
IFACEMETHODIMP get_Port(INT32* value) final {
NOTREACHED() << "get_RawUri should be the only function called on an "
"IUriRuntimeClass - see FakeUriRuntimeClass.";
}
IFACEMETHODIMP get_Suspicious(boolean* value) final {
NOTREACHED() << "get_RawUri should be the only function called on an "
"IUriRuntimeClass - see FakeUriRuntimeClass.";
}
IFACEMETHODIMP
Equals(IUriRuntimeClass* uri, boolean* value) final {
NOTREACHED() << "get_RawUri should be the only function called on an "
"IUriRuntimeClass - see FakeUriRuntimeClass.";
}
IFACEMETHODIMP
CombineUri(HSTRING relative_uri, IUriRuntimeClass** instance) final {
NOTREACHED() << "get_RawUri should be the only function called on an "
"IUriRuntimeClass - see FakeUriRuntimeClass.";
}
private:
std::string raw_uri_;
};
} // namespace
FakeUriRuntimeClassFactory::FakeUriRuntimeClassFactory() = default;
FakeUriRuntimeClassFactory::~FakeUriRuntimeClassFactory() = default;
IFACEMETHODIMP
FakeUriRuntimeClassFactory::CreateUri(HSTRING uri,
IUriRuntimeClass** instance) {
if (!uri) {
ADD_FAILURE() << "CreateUri called with null uri.";
return E_POINTER;
}
// ScopedHString takes ownership of the HSTRING provided to it, but taking
// ownership is not an expected behavior when passing an HSTRING to a system
// API, so we use a temporary ScopedHString to make a copy we can safely own
// and release ownership of the original 'back' to the caller.
base::win::ScopedHString holder(uri);
auto uri_string = holder.GetAsUTF8();
std::ignore = holder.release();
if (uri_string.empty()) {
ADD_FAILURE() << "CreateUri called with empty uri.";
return E_POINTER;
}
auto url = GURL(uri_string);
if (!url.is_valid()) {
ADD_FAILURE() << "CreateUri called with invalid uri.";
return E_INVALIDARG;
}
auto fake_uri_runtime_class = Make<FakeUriRuntimeClass>(uri_string);
HRESULT hr = fake_uri_runtime_class->QueryInterface(IID_PPV_ARGS(instance));
if (FAILED(hr)) {
EXPECT_HRESULT_SUCCEEDED(hr);
return hr;
}
return S_OK;
}
IFACEMETHODIMP FakeUriRuntimeClassFactory::CreateWithRelativeUri(
HSTRING base_uri,
HSTRING relative_uri,
IUriRuntimeClass** instance) {
NOTREACHED();
}
} // namespace webshare
|