/src/mozilla-central/xpcom/tests/gtest/TestInputStreamLengthHelper.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | #include "gtest/gtest.h" |
2 | | |
3 | | #include "mozilla/InputStreamLengthHelper.h" |
4 | | #include "nsCOMPtr.h" |
5 | | #include "nsIInputStream.h" |
6 | | #include "nsIRunnable.h" |
7 | | #include "nsIThreadManager.h" |
8 | | #include "nsStreamUtils.h" |
9 | | #include "nsString.h" |
10 | | #include "nsStringStream.h" |
11 | | #include "nsThreadUtils.h" |
12 | | #include "nsXPCOM.h" |
13 | | #include "Helpers.h" |
14 | | |
15 | | using namespace mozilla; |
16 | | |
17 | 0 | TEST(TestInputStreamLengthHelper, NonLengthStream) { |
18 | 0 | nsCString buf; |
19 | 0 | buf.AssignLiteral("Hello world"); |
20 | 0 |
|
21 | 0 | nsCOMPtr<nsIInputStream> stream; |
22 | 0 | NS_NewCStringInputStream(getter_AddRefs(stream), buf); |
23 | 0 |
|
24 | 0 | bool called = false; |
25 | 0 | InputStreamLengthHelper::GetAsyncLength(stream, [&](int64_t aLength) { |
26 | 0 | ASSERT_EQ(buf.Length(), aLength); |
27 | 0 | called = true; |
28 | 0 | }); |
29 | 0 |
|
30 | 0 | MOZ_ALWAYS_TRUE(SpinEventLoopUntil([&]() { return called; })); |
31 | 0 | } |
32 | | |
33 | | class LengthStream final : public nsIInputStreamLength |
34 | | , public nsIAsyncInputStreamLength |
35 | | , public nsIInputStream |
36 | | { |
37 | | public: |
38 | | NS_DECL_ISUPPORTS |
39 | | |
40 | | LengthStream(int64_t aLength, nsresult aLengthRv, |
41 | | uint64_t aAvailable, bool aIsAsyncLength) |
42 | | : mLength(aLength) |
43 | | , mLengthRv(aLengthRv) |
44 | | , mAvailable(aAvailable) |
45 | | , mIsAsyncLength(aIsAsyncLength) |
46 | 0 | {} |
47 | | |
48 | 0 | NS_IMETHOD Close(void) override { MOZ_CRASH("Invalid call!"); } |
49 | 0 | NS_IMETHOD Read(char* aBuf, uint32_t aCount, uint32_t* _retval) override { MOZ_CRASH("Invalid call!"); } |
50 | 0 | NS_IMETHOD ReadSegments(nsWriteSegmentFun aWriter, void* aClosure, uint32_t aCount, uint32_t* _retval) override { MOZ_CRASH("Invalid call!"); } |
51 | 0 | NS_IMETHOD IsNonBlocking(bool* _retval) override { MOZ_CRASH("Invalid call!"); } |
52 | | |
53 | | NS_IMETHOD Length(int64_t* aLength) override |
54 | 0 | { |
55 | 0 | *aLength = mLength; |
56 | 0 | return mLengthRv; |
57 | 0 | } |
58 | | |
59 | | NS_IMETHOD AsyncLengthWait(nsIInputStreamLengthCallback* aCallback, |
60 | | nsIEventTarget* aEventTarget) override |
61 | 0 | { |
62 | 0 | aCallback->OnInputStreamLengthReady(this, mLength); |
63 | 0 | return NS_OK; |
64 | 0 | } |
65 | | |
66 | | NS_IMETHOD Available(uint64_t* aAvailable) override |
67 | 0 | { |
68 | 0 | *aAvailable = mAvailable; |
69 | 0 | return NS_OK; |
70 | 0 | } |
71 | | |
72 | | private: |
73 | | ~LengthStream() = default; |
74 | | |
75 | | int64_t mLength; |
76 | | nsresult mLengthRv; |
77 | | uint64_t mAvailable; |
78 | | |
79 | | bool mIsAsyncLength; |
80 | | }; |
81 | | |
82 | | NS_IMPL_ADDREF(LengthStream); |
83 | | NS_IMPL_RELEASE(LengthStream); |
84 | | |
85 | 0 | NS_INTERFACE_MAP_BEGIN(LengthStream) |
86 | 0 | NS_INTERFACE_MAP_ENTRY(nsIInputStream) |
87 | 0 | NS_INTERFACE_MAP_ENTRY(nsIInputStreamLength) |
88 | 0 | NS_INTERFACE_MAP_ENTRY_CONDITIONAL(nsIAsyncInputStreamLength, mIsAsyncLength) |
89 | 0 | NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIInputStream) |
90 | 0 | NS_INTERFACE_MAP_END |
91 | | |
92 | 0 | TEST(TestInputStreamLengthHelper, LengthStream) { |
93 | 0 | nsCOMPtr<nsIInputStream> stream = new LengthStream(42, NS_OK, 0, false); |
94 | 0 |
|
95 | 0 | bool called = false; |
96 | 0 | InputStreamLengthHelper::GetAsyncLength(stream, [&](int64_t aLength) { |
97 | 0 | ASSERT_EQ(42, aLength); |
98 | 0 | called = true; |
99 | 0 | }); |
100 | 0 |
|
101 | 0 | MOZ_ALWAYS_TRUE(SpinEventLoopUntil([&]() { return called; })); |
102 | 0 | } |
103 | | |
104 | 0 | TEST(TestInputStreamLengthHelper, InvalidLengthStream) { |
105 | 0 | nsCOMPtr<nsIInputStream> stream = |
106 | 0 | new LengthStream(42, NS_ERROR_NOT_AVAILABLE, 0, false); |
107 | 0 |
|
108 | 0 | bool called = false; |
109 | 0 | InputStreamLengthHelper::GetAsyncLength(stream, [&](int64_t aLength) { |
110 | 0 | ASSERT_EQ(-1, aLength); |
111 | 0 | called = true; |
112 | 0 | }); |
113 | 0 |
|
114 | 0 | MOZ_ALWAYS_TRUE(SpinEventLoopUntil([&]() { return called; })); |
115 | 0 | } |
116 | | |
117 | 0 | TEST(TestInputStreamLengthHelper, AsyncLengthStream) { |
118 | 0 | nsCOMPtr<nsIInputStream> stream = |
119 | 0 | new LengthStream(22, NS_BASE_STREAM_WOULD_BLOCK, 123, true); |
120 | 0 |
|
121 | 0 | bool called = false; |
122 | 0 | InputStreamLengthHelper::GetAsyncLength(stream, [&](int64_t aLength) { |
123 | 0 | ASSERT_EQ(22, aLength); |
124 | 0 | called = true; |
125 | 0 | }); |
126 | 0 |
|
127 | 0 | MOZ_ALWAYS_TRUE(SpinEventLoopUntil([&]() { return called; })); |
128 | 0 | } |
129 | | |
130 | 0 | TEST(TestInputStreamLengthHelper, FallbackLengthStream) { |
131 | 0 | nsCOMPtr<nsIInputStream> stream = |
132 | 0 | new LengthStream(-1, NS_BASE_STREAM_WOULD_BLOCK, 123, false); |
133 | 0 |
|
134 | 0 | bool called = false; |
135 | 0 | InputStreamLengthHelper::GetAsyncLength(stream, [&](int64_t aLength) { |
136 | 0 | ASSERT_EQ(123, aLength); |
137 | 0 | called = true; |
138 | 0 | }); |
139 | 0 |
|
140 | 0 | MOZ_ALWAYS_TRUE(SpinEventLoopUntil([&]() { return called; })); |
141 | 0 | } |