Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/netwerk/test/gtest/TestBufferedInputStream.cpp
Line
Count
Source (jump to first uncovered line)
1
#include "gtest/gtest.h"
2
3
#include "nsBufferedStreams.h"
4
#include "nsStreamUtils.h"
5
#include "nsThreadUtils.h"
6
#include "Helpers.h"
7
8
// Helper function for creating a testing::AsyncStringStream
9
already_AddRefed<nsBufferedInputStream>
10
CreateStream(uint32_t aSize, nsCString& aBuffer)
11
0
{
12
0
  aBuffer.SetLength(aSize);
13
0
  for (uint32_t i = 0; i < aSize; ++i) {
14
0
    aBuffer.BeginWriting()[i] = i % 10;
15
0
  }
16
0
17
0
  nsCOMPtr<nsIInputStream> stream = new testing::AsyncStringStream(aBuffer);
18
0
19
0
  RefPtr<nsBufferedInputStream> bis = new nsBufferedInputStream();
20
0
  bis->Init(stream, aSize);
21
0
  return bis.forget();
22
0
}
23
24
// Simple reading.
25
0
TEST(TestBufferedInputStream, SimpleRead) {
26
0
  const size_t kBufSize = 10;
27
0
28
0
  nsCString buf;
29
0
  RefPtr<nsBufferedInputStream> bis = CreateStream(kBufSize, buf);
30
0
31
0
  uint64_t length;
32
0
  ASSERT_EQ(NS_OK, bis->Available(&length));
33
0
  ASSERT_EQ((uint64_t)kBufSize, length);
34
0
35
0
  char buf2[kBufSize];
36
0
  uint32_t count;
37
0
  ASSERT_EQ(NS_OK, bis->Read(buf2, sizeof(buf2), &count));
38
0
  ASSERT_EQ(count, buf.Length());
39
0
  ASSERT_TRUE(nsCString(buf.get(), kBufSize).Equals(nsCString(buf2, count)));
40
0
}
41
42
// Simple segment reading.
43
0
TEST(TestBufferedInputStream, SimpleReadSegments) {
44
0
  const size_t kBufSize = 10;
45
0
46
0
  nsCString buf;
47
0
  RefPtr<nsBufferedInputStream> bis = CreateStream(kBufSize, buf);
48
0
49
0
  char buf2[kBufSize];
50
0
  uint32_t count;
51
0
  ASSERT_EQ(NS_OK, bis->ReadSegments(NS_CopySegmentToBuffer, buf2, sizeof(buf2), &count));
52
0
  ASSERT_EQ(count, buf.Length());
53
0
  ASSERT_TRUE(nsCString(buf.get(), kBufSize).Equals(nsCString(buf2, count)));
54
0
}
55
56
// AsyncWait - sync
57
0
TEST(TestBufferedInputStream, AsyncWait_sync) {
58
0
  const size_t kBufSize = 10;
59
0
60
0
  nsCString buf;
61
0
  RefPtr<nsBufferedInputStream> bis = CreateStream(kBufSize, buf);
62
0
63
0
  RefPtr<testing::InputStreamCallback> cb =
64
0
    new testing::InputStreamCallback();
65
0
66
0
  ASSERT_EQ(NS_OK, bis->AsyncWait(cb, 0, 0, nullptr));
67
0
68
0
  // Immediatelly called
69
0
  ASSERT_TRUE(cb->Called());
70
0
}
71
72
// AsyncWait - async
73
0
TEST(TestBufferedInputStream, AsyncWait_async) {
74
0
  const size_t kBufSize = 10;
75
0
76
0
  nsCString buf;
77
0
  RefPtr<nsBufferedInputStream> bis = CreateStream(kBufSize, buf);
78
0
79
0
  RefPtr<testing::InputStreamCallback> cb =
80
0
    new testing::InputStreamCallback();
81
0
  nsCOMPtr<nsIThread> thread = do_GetCurrentThread();
82
0
83
0
  ASSERT_EQ(NS_OK, bis->AsyncWait(cb, 0, 0, thread));
84
0
85
0
  ASSERT_FALSE(cb->Called());
86
0
87
0
  // Eventually it is called.
88
0
  MOZ_ALWAYS_TRUE(mozilla::SpinEventLoopUntil([&]() { return cb->Called(); }));
89
0
  ASSERT_TRUE(cb->Called());
90
0
}
91
92
// AsyncWait - sync - closureOnly
93
0
TEST(TestBufferedInputStream, AsyncWait_sync_closureOnly) {
94
0
  const size_t kBufSize = 10;
95
0
96
0
  nsCString buf;
97
0
  RefPtr<nsBufferedInputStream> bis = CreateStream(kBufSize, buf);
98
0
99
0
  RefPtr<testing::InputStreamCallback> cb =
100
0
    new testing::InputStreamCallback();
101
0
102
0
  ASSERT_EQ(NS_OK, bis->AsyncWait(cb, nsIAsyncInputStream::WAIT_CLOSURE_ONLY,
103
0
                                  0, nullptr));
104
0
  ASSERT_FALSE(cb->Called());
105
0
106
0
  bis->CloseWithStatus(NS_ERROR_FAILURE);
107
0
108
0
  // Immediatelly called
109
0
  ASSERT_TRUE(cb->Called());
110
0
}
111
112
// AsyncWait - async
113
0
TEST(TestBufferedInputStream, AsyncWait_async_closureOnly) {
114
0
  const size_t kBufSize = 10;
115
0
116
0
  nsCString buf;
117
0
  RefPtr<nsBufferedInputStream> bis = CreateStream(kBufSize, buf);
118
0
119
0
  RefPtr<testing::InputStreamCallback> cb =
120
0
    new testing::InputStreamCallback();
121
0
  nsCOMPtr<nsIThread> thread = do_GetCurrentThread();
122
0
123
0
  ASSERT_EQ(NS_OK, bis->AsyncWait(cb, nsIAsyncInputStream::WAIT_CLOSURE_ONLY,
124
0
                                  0, thread));
125
0
126
0
  ASSERT_FALSE(cb->Called());
127
0
  bis->CloseWithStatus(NS_ERROR_FAILURE);
128
0
  ASSERT_FALSE(cb->Called());
129
0
130
0
  // Eventually it is called.
131
0
  MOZ_ALWAYS_TRUE(mozilla::SpinEventLoopUntil([&]() { return cb->Called(); }));
132
0
  ASSERT_TRUE(cb->Called());
133
0
}