1 /*
2 * Copyright (c) Facebook, Inc. and its affiliates.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <fbjni/ByteBuffer.h>
18 #include <fbjni/ReadableByteChannel.h>
19 #include <fbjni/fbjni.h>
20 #include <vector>
21
22 #include "expect.h"
23
24 using namespace facebook::jni;
25
testSmallRead(alias_ref<JClass> cls,alias_ref<JReadableByteChannel> channel,alias_ref<JArrayByte> data)26 jboolean testSmallRead(
27 alias_ref<JClass> cls,
28 alias_ref<JReadableByteChannel> channel,
29 alias_ref<JArrayByte> data) {
30 std::vector<uint8_t> vec(data->size() * 2);
31 auto npp = JByteBuffer::wrapBytes(vec.data(), vec.size());
32
33 unsigned n = channel->read(npp);
34 EXPECT(n == data->size());
35
36 auto pinned = data->pin();
37 for (size_t i = 0; i < data->size(); i++) {
38 EXPECT(vec[i] == pinned[i]);
39 }
40
41 return JNI_TRUE;
42 }
43
testReadToBufferCapacity(alias_ref<JClass> cls,alias_ref<JReadableByteChannel> channel,alias_ref<JArrayByte> data)44 jboolean testReadToBufferCapacity(
45 alias_ref<JClass> cls,
46 alias_ref<JReadableByteChannel> channel,
47 alias_ref<JArrayByte> data) {
48 std::vector<uint8_t> vec(data->size() / 2);
49 auto npp = JByteBuffer::wrapBytes(vec.data(), vec.size());
50
51 unsigned n = channel->read(npp);
52 EXPECT(n == vec.size());
53
54 n = channel->read(npp);
55 EXPECT(n == 0);
56
57 auto pinned = data->pin();
58 for (size_t i = 0; i < vec.size(); i++) {
59 EXPECT(vec[i] == pinned[i]);
60 }
61
62 return JNI_TRUE;
63 }
64
testConsumeChannel(alias_ref<JClass> cls,alias_ref<JReadableByteChannel> channel,alias_ref<JArrayByte> data)65 jboolean testConsumeChannel(
66 alias_ref<JClass> cls,
67 alias_ref<JReadableByteChannel> channel,
68 alias_ref<JArrayByte> data) {
69 std::vector<uint8_t> vec(data->size() + 16);
70 auto npp = JByteBuffer::wrapBytes(vec.data(), vec.size());
71
72 int n = channel->read(npp);
73 EXPECT((unsigned)n == data->size());
74
75 n = channel->read(npp);
76 EXPECT(n == -1);
77
78 auto pinned = data->pin();
79 for (size_t i = 0; i < data->size(); i++) {
80 EXPECT(vec[i] == pinned[i]);
81 }
82
83 return JNI_TRUE;
84 }
85
testConsumeChannelIteratively(alias_ref<JClass> cls,alias_ref<JReadableByteChannel> channel,alias_ref<JArrayByte> data)86 jboolean testConsumeChannelIteratively(
87 alias_ref<JClass> cls,
88 alias_ref<JReadableByteChannel> channel,
89 alias_ref<JArrayByte> data) {
90 std::vector<uint8_t> vec(data->size() / 4);
91 auto npp = JByteBuffer::wrapBytes(vec.data(), vec.size());
92 auto pinned = data->pin();
93
94 for (size_t i = 0; i < 4; i++) {
95 unsigned n = channel->read(npp);
96 EXPECT(n == data->size() / 4);
97 npp->rewind();
98 }
99
100 int n = channel->read(npp);
101 EXPECT(n == -1);
102
103 return JNI_TRUE;
104 }
105
RegisterReadableByteChannelTests()106 void RegisterReadableByteChannelTests() {
107 registerNatives(
108 "com/facebook/jni/ReadableByteChannelTests",
109 {
110 makeNativeMethod("nativeTestSmallRead", testSmallRead),
111 makeNativeMethod(
112 "nativeTestReadToBufferCapacity", testReadToBufferCapacity),
113 makeNativeMethod("nativeTestConsumeChannel", testConsumeChannel),
114 makeNativeMethod(
115 "nativeTestConsumeChannelIteratively",
116 testConsumeChannelIteratively),
117 });
118 }
119