1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to you under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  * https://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14  * implied.  See the License for the specific language governing
15  * permissions and limitations under the License.
16  */
17 
18 #include <avro.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 
22 #define check_exit(call) \
23 	do { \
24 		int  __rc = call; \
25 		if (__rc != 0) { \
26 			fprintf(stderr, "Unexpected error:\n  %s\n  %s\n", \
27 				avro_strerror(), #call); \
28 			exit(EXIT_FAILURE); \
29 		} \
30 	} while (0)
31 
32 #define expect_eof(call) \
33 	do { \
34 		int  __rc = call; \
35 		if (__rc != EOF) { \
36 			fprintf(stderr, "Expected EOF:\n  %s\n", #call); \
37 			exit(EXIT_FAILURE); \
38 		} \
39 	} while (0)
40 
41 #define expect_error(call) \
42 	do { \
43 		int  __rc = call; \
44 		if (__rc == 0) { \
45 			fprintf(stderr, "Expected an error:\n  %s\n", #call); \
46 			exit(EXIT_FAILURE); \
47 		} \
48 	} while (0)
49 
50 #define check_expected_value(actual, expected) \
51 	do { \
52 		if (!avro_value_equal_fast((actual), (expected))) { \
53 			char  *actual_json; \
54 			char  *expected_json; \
55 			avro_value_to_json((actual), 1, &actual_json); \
56 			avro_value_to_json((expected), 1, &expected_json); \
57 			fprintf(stderr, "Expected %s\nGot      %s\n", \
58 				expected_json, actual_json); \
59 			free(actual_json); \
60 			free(expected_json); \
61 			exit(EXIT_FAILURE); \
62 		} \
63 	} while (0)
64 
main(void)65 int main(void)
66 {
67 	avro_schema_t  schema;
68 	avro_file_reader_t  reader;
69 	avro_value_iface_t  *iface;
70 	avro_value_t  actual;
71 	avro_value_t  expected;
72 	avro_value_t  branch;
73 
74 	schema = avro_schema_union();
75 	avro_schema_union_append(schema, avro_schema_null());
76 	avro_schema_union_append(schema, avro_schema_int());
77 
78 	iface = avro_generic_class_from_schema(schema);
79 	avro_generic_value_new(iface, &actual);
80 	avro_generic_value_new(iface, &expected);
81 
82 
83 	/* First read the contents of the good file. */
84 
85 	check_exit(avro_file_reader("avro-1238-good.avro", &reader));
86 
87 	check_exit(avro_file_reader_read_value(reader, &actual));
88 	check_exit(avro_value_set_branch(&expected, 0, &branch));
89 	check_exit(avro_value_set_null(&branch));
90 	check_expected_value(&actual, &expected);
91 
92 	check_exit(avro_file_reader_read_value(reader, &actual));
93 	check_exit(avro_value_set_branch(&expected, 1, &branch));
94 	check_exit(avro_value_set_int(&branch, 100));
95 	check_expected_value(&actual, &expected);
96 
97 	expect_eof(avro_file_reader_read_value(reader, &actual));
98 	check_exit(avro_file_reader_close(reader));
99 
100 
101 	/* Then read from the truncated file. */
102 
103 	check_exit(avro_file_reader("avro-1238-truncated.avro", &reader));
104 
105 	check_exit(avro_file_reader_read_value(reader, &actual));
106 	check_exit(avro_value_set_branch(&expected, 0, &branch));
107 	check_exit(avro_value_set_null(&branch));
108 	check_expected_value(&actual, &expected);
109 
110 	check_exit(avro_file_reader_read_value(reader, &actual));
111 	check_exit(avro_value_set_branch(&expected, 1, &branch));
112 	check_exit(avro_value_set_int(&branch, 100));
113 	check_expected_value(&actual, &expected);
114 
115 	expect_error(avro_file_reader_read_value(reader, &actual));
116 	check_exit(avro_file_reader_close(reader));
117 
118 
119 	/* Clean up and exit */
120 	avro_value_decref(&actual);
121 	avro_value_decref(&expected);
122 	avro_value_iface_decref(iface);
123 	avro_schema_decref(schema);
124 	exit(EXIT_SUCCESS);
125 }
126