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
|
#include "gtest/gtest.h"
#include "nsCOMPtr.h"
#include "mozilla/Maybe.h"
#include "mozilla/PreloadHashKey.h"
#include "mozilla/SpinEventLoopUntil.h"
#include "nsNetUtil.h"
#include "nsIChannel.h"
#include "nsIStreamListener.h"
#include "nsThreadUtils.h"
#include "nsStringStream.h"
#include "nsIPrivateBrowsingChannel.h"
#include "nsIInterfaceRequestor.h"
#include "nsContentUtils.h"
using namespace mozilla;
class FakeListener : public nsIStreamListener, public nsIInterfaceRequestor {
public:
NS_DECL_ISUPPORTS
NS_DECL_NSIREQUESTOBSERVER
NS_DECL_NSISTREAMLISTENER
NS_DECL_NSIINTERFACEREQUESTOR
enum { Never, OnStart, OnData, OnStop } mCancelIn = Never;
nsresult mOnStartResult = NS_OK;
nsresult mOnDataResult = NS_OK;
nsresult mOnStopResult = NS_OK;
bool mOnStart = false;
nsCString mOnData;
Maybe<nsresult> mOnStop;
private:
virtual ~FakeListener() = default;
};
NS_IMPL_ISUPPORTS(FakeListener, nsIStreamListener, nsIRequestObserver,
nsIInterfaceRequestor)
NS_IMETHODIMP
FakeListener::GetInterface(const nsIID& aIID, void** aResult) {
NS_ENSURE_ARG_POINTER(aResult);
*aResult = nullptr;
return NS_NOINTERFACE;
}
NS_IMETHODIMP FakeListener::OnStartRequest(nsIRequest* request) {
EXPECT_FALSE(mOnStart);
mOnStart = true;
if (mCancelIn == OnStart) {
request->Cancel(NS_ERROR_ABORT);
}
return mOnStartResult;
}
NS_IMETHODIMP FakeListener::OnDataAvailable(nsIRequest* request,
nsIInputStream* input,
uint64_t offset, uint32_t count) {
nsAutoCString data;
data.SetLength(count);
uint32_t read;
input->Read(data.BeginWriting(), count, &read);
mOnData += data;
if (mCancelIn == OnData) {
request->Cancel(NS_ERROR_ABORT);
}
return mOnDataResult;
}
NS_IMETHODIMP FakeListener::OnStopRequest(nsIRequest* request,
nsresult status) {
EXPECT_FALSE(mOnStop);
mOnStop.emplace(status);
if (mCancelIn == OnStop) {
request->Cancel(NS_ERROR_ABORT);
}
return mOnStopResult;
}
// Test that nsHttpChannel::AsyncOpen properly picks up changes to
// loadInfo.mPrivateBrowsingId that occur after the channel was created.
TEST(TestHttpChannel, PBAsyncOpen)
{
nsCOMPtr<nsIURI> uri;
NS_NewURI(getter_AddRefs(uri), "http://localhost/"_ns);
nsCOMPtr<nsIChannel> channel;
nsresult rv = NS_NewChannel(
getter_AddRefs(channel), uri, nsContentUtils::GetSystemPrincipal(),
nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_SEC_CONTEXT_IS_NULL,
nsIContentPolicy::TYPE_OTHER);
ASSERT_EQ(rv, NS_OK);
RefPtr<FakeListener> listener = new FakeListener();
rv = channel->SetNotificationCallbacks(listener);
ASSERT_EQ(rv, NS_OK);
nsCOMPtr<nsIPrivateBrowsingChannel> pbchannel = do_QueryInterface(channel);
ASSERT_TRUE(pbchannel);
bool isPrivate = false;
rv = pbchannel->GetIsChannelPrivate(&isPrivate);
ASSERT_EQ(rv, NS_OK);
ASSERT_EQ(isPrivate, false);
nsCOMPtr<nsILoadInfo> loadInfo = channel->LoadInfo();
OriginAttributes attrs;
attrs.mPrivateBrowsingId = 1;
rv = loadInfo->SetOriginAttributes(attrs);
ASSERT_EQ(rv, NS_OK);
rv = pbchannel->GetIsChannelPrivate(&isPrivate);
ASSERT_EQ(rv, NS_OK);
ASSERT_EQ(isPrivate, false);
rv = channel->AsyncOpen(listener);
ASSERT_EQ(rv, NS_OK);
rv = pbchannel->GetIsChannelPrivate(&isPrivate);
ASSERT_EQ(rv, NS_OK);
ASSERT_EQ(isPrivate, true);
MOZ_ALWAYS_TRUE(mozilla::SpinEventLoopUntil(
"TEST(TestHttpChannel, PBAsyncOpen)"_ns,
[&]() -> bool { return listener->mOnStop.isSome(); }));
}
|