blob: bc5a512a0b822708a34180a3fb5b6c8513c428b6 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
3 * Copyright 2014, Google Inc.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
34#include <grpc/byte_buffer_reader.h>
35#include <grpc/byte_buffer.h>
36#include <grpc/support/slice.h>
37#include <grpc/grpc.h>
38
39#include <grpc/support/alloc.h>
40#include <grpc/support/log.h>
41#include <grpc/support/thd.h>
42#include <grpc/support/time.h>
43#include "test/core/util/test_config.h"
44
45#include <string.h>
46
47#define LOG_TEST() gpr_log(GPR_INFO, "%s", __FUNCTION__)
48
49static void test_create() {
50 grpc_byte_buffer *buffer;
51 grpc_byte_buffer_reader *reader;
52 gpr_slice empty = gpr_empty_slice();
53 LOG_TEST();
54 buffer = grpc_byte_buffer_create(&empty, 1);
55 reader = grpc_byte_buffer_reader_create(buffer);
56 grpc_byte_buffer_reader_destroy(reader);
57 grpc_byte_buffer_destroy(buffer);
58}
59
60static void test_read_one_slice() {
61 gpr_slice slice;
62 grpc_byte_buffer *buffer;
63 grpc_byte_buffer_reader *reader;
64 gpr_slice first_slice, second_slice;
65 int first_code, second_code;
66
67 LOG_TEST();
68 slice = gpr_slice_from_copied_string("test");
69 buffer = grpc_byte_buffer_create(&slice, 1);
70 gpr_slice_unref(slice);
71 reader = grpc_byte_buffer_reader_create(buffer);
72 first_code = grpc_byte_buffer_reader_next(reader, &first_slice);
73 GPR_ASSERT(first_code != 0);
74 GPR_ASSERT(memcmp(GPR_SLICE_START_PTR(first_slice), "test", 4) == 0);
75 gpr_slice_unref(first_slice);
76 second_code = grpc_byte_buffer_reader_next(reader, &second_slice);
77 GPR_ASSERT(second_code == 0);
78 grpc_byte_buffer_reader_destroy(reader);
79 grpc_byte_buffer_destroy(buffer);
80}
81
82static void test_read_one_slice_malloc() {
83 gpr_slice slice;
84 grpc_byte_buffer *buffer;
85 grpc_byte_buffer_reader *reader;
86 gpr_slice first_slice, second_slice;
87 int first_code, second_code;
88
89 LOG_TEST();
90 slice = gpr_slice_malloc(4);
91 memcpy(GPR_SLICE_START_PTR(slice), "test", 4);
92 buffer = grpc_byte_buffer_create(&slice, 1);
93 gpr_slice_unref(slice);
94 reader = grpc_byte_buffer_reader_create(buffer);
95 first_code = grpc_byte_buffer_reader_next(reader, &first_slice);
96 GPR_ASSERT(first_code != 0);
97 GPR_ASSERT(memcmp(GPR_SLICE_START_PTR(first_slice), "test", 4) == 0);
98 gpr_slice_unref(first_slice);
99 second_code = grpc_byte_buffer_reader_next(reader, &second_slice);
100 GPR_ASSERT(second_code == 0);
101 grpc_byte_buffer_reader_destroy(reader);
102 grpc_byte_buffer_destroy(buffer);
103}
104
105int main(int argc, char **argv) {
106 grpc_test_init(argc, argv);
107 test_create();
108 test_read_one_slice();
109 test_read_one_slice_malloc();
110 return 0;
111}