summaryrefslogtreecommitdiffstats
path: root/xpcom/tests/gtest/TestInputStreamLengthHelper.cpp
blob: 5bb3f2bbe4e3c45cfbbb67fb99e10e74fb6930fa (plain)
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
#include "gtest/gtest.h"

#include "mozilla/InputStreamLengthHelper.h"
#include "mozilla/SpinEventLoopUntil.h"
#include "nsCOMPtr.h"
#include "nsIInputStream.h"
#include "nsStreamUtils.h"
#include "nsString.h"
#include "nsStringStream.h"
#include "nsThreadUtils.h"
#include "nsXPCOM.h"
#include "Helpers.h"

using namespace mozilla;

TEST(TestInputStreamLengthHelper, NonLengthStream)
{
  nsCString buf;
  buf.AssignLiteral("Hello world");

  nsCOMPtr<nsIInputStream> stream;
  NS_NewCStringInputStream(getter_AddRefs(stream), buf);

  bool called = false;
  InputStreamLengthHelper::GetAsyncLength(stream, [&](int64_t aLength) {
    ASSERT_EQ(int64_t(buf.Length()), aLength);
    called = true;
  });

  MOZ_ALWAYS_TRUE(SpinEventLoopUntil(
      "xpcom:TEST(TestInputStreamLengthHelper, NonLengthStream)"_ns,
      [&]() { return called; }));
}

class LengthStream final : public nsIInputStreamLength,
                           public nsIAsyncInputStreamLength,
                           public nsIInputStream {
 public:
  NS_DECL_ISUPPORTS

  LengthStream(int64_t aLength, nsresult aLengthRv, uint64_t aAvailable,
               bool aIsAsyncLength)
      : mLength(aLength),
        mLengthRv(aLengthRv),
        mAvailable(aAvailable),
        mIsAsyncLength(aIsAsyncLength) {}

  NS_IMETHOD Close(void) override { MOZ_CRASH("Invalid call!"); }
  NS_IMETHOD Read(char* aBuf, uint32_t aCount, uint32_t* _retval) override {
    MOZ_CRASH("Invalid call!");
  }
  NS_IMETHOD ReadSegments(nsWriteSegmentFun aWriter, void* aClosure,
                          uint32_t aCount, uint32_t* _retval) override {
    MOZ_CRASH("Invalid call!");
  }
  NS_IMETHOD IsNonBlocking(bool* _retval) override {
    MOZ_CRASH("Invalid call!");
  }

  NS_IMETHOD Length(int64_t* aLength) override {
    *aLength = mLength;
    return mLengthRv;
  }

  NS_IMETHOD AsyncLengthWait(nsIInputStreamLengthCallback* aCallback,
                             nsIEventTarget* aEventTarget) override {
    if (aCallback) {
      aCallback->OnInputStreamLengthReady(this, mLength);
    }
    return NS_OK;
  }

  NS_IMETHOD Available(uint64_t* aAvailable) override {
    *aAvailable = mAvailable;
    return NS_OK;
  }

  NS_IMETHOD StreamStatus() override { return NS_OK; }

 private:
  ~LengthStream() = default;

  int64_t mLength;
  nsresult mLengthRv;
  uint64_t mAvailable;

  bool mIsAsyncLength;
};

NS_IMPL_ADDREF(LengthStream);
NS_IMPL_RELEASE(LengthStream);

NS_INTERFACE_MAP_BEGIN(LengthStream)
  NS_INTERFACE_MAP_ENTRY(nsIInputStream)
  NS_INTERFACE_MAP_ENTRY(nsIInputStreamLength)
  NS_INTERFACE_MAP_ENTRY_CONDITIONAL(nsIAsyncInputStreamLength, mIsAsyncLength)
  NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIInputStream)
NS_INTERFACE_MAP_END

TEST(TestInputStreamLengthHelper, LengthStream)
{
  nsCOMPtr<nsIInputStream> stream = new LengthStream(42, NS_OK, 0, false);

  bool called = false;
  InputStreamLengthHelper::GetAsyncLength(stream, [&](int64_t aLength) {
    ASSERT_EQ(42, aLength);
    called = true;
  });

  MOZ_ALWAYS_TRUE(SpinEventLoopUntil(
      "xpcom:TEST(TestInputStreamLengthHelper, LengthStream)"_ns,
      [&]() { return called; }));
}

TEST(TestInputStreamLengthHelper, InvalidLengthStream)
{
  nsCOMPtr<nsIInputStream> stream =
      new LengthStream(42, NS_ERROR_NOT_AVAILABLE, 0, false);

  bool called = false;
  InputStreamLengthHelper::GetAsyncLength(stream, [&](int64_t aLength) {
    ASSERT_EQ(-1, aLength);
    called = true;
  });

  MOZ_ALWAYS_TRUE(SpinEventLoopUntil(
      "xpcom:TEST(TestInputStreamLengthHelper, InvalidLengthStream)"_ns,
      [&]() { return called; }));
}

TEST(TestInputStreamLengthHelper, AsyncLengthStream)
{
  nsCOMPtr<nsIInputStream> stream =
      new LengthStream(22, NS_BASE_STREAM_WOULD_BLOCK, 123, true);

  bool called = false;
  InputStreamLengthHelper::GetAsyncLength(stream, [&](int64_t aLength) {
    ASSERT_EQ(22, aLength);
    called = true;
  });

  MOZ_ALWAYS_TRUE(SpinEventLoopUntil(
      "xpcom:TEST(TestInputStreamLengthHelper, AsyncLengthStream)"_ns,
      [&]() { return called; }));
}

TEST(TestInputStreamLengthHelper, FallbackLengthStream)
{
  nsCOMPtr<nsIInputStream> stream =
      new LengthStream(-1, NS_BASE_STREAM_WOULD_BLOCK, 123, false);

  bool called = false;
  InputStreamLengthHelper::GetAsyncLength(stream, [&](int64_t aLength) {
    ASSERT_EQ(123, aLength);
    called = true;
  });

  MOZ_ALWAYS_TRUE(SpinEventLoopUntil(
      "xpcom:TEST(TestInputStreamLengthHelper, FallbackLengthStream)"_ns,
      [&]() { return called; }));
}