/Users/deen/code/yugabyte-db/src/yb/common/wire_protocol-test.cc
Line | Count | Source |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | // |
18 | | // The following only applies to changes made to this file as part of YugaByte development. |
19 | | // |
20 | | // Portions Copyright (c) YugaByte, Inc. |
21 | | // |
22 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
23 | | // in compliance with the License. You may obtain a copy of the License at |
24 | | // |
25 | | // http://www.apache.org/licenses/LICENSE-2.0 |
26 | | // |
27 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
28 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
29 | | // or implied. See the License for the specific language governing permissions and limitations |
30 | | // under the License. |
31 | | // |
32 | | |
33 | | #include <gtest/gtest.h> |
34 | | |
35 | | #include "yb/common/common.pb.h" |
36 | | #include "yb/common/ql_type.h" |
37 | | #include "yb/common/schema.h" |
38 | | #include "yb/common/wire_protocol.h" |
39 | | #include "yb/common/wire_protocol.pb.h" |
40 | | |
41 | | #include "yb/util/errno.h" |
42 | | #include "yb/util/status.h" |
43 | | #include "yb/util/test_macros.h" |
44 | | #include "yb/util/test_util.h" |
45 | | |
46 | | namespace yb { |
47 | | |
48 | | class WireProtocolTest : public YBTest { |
49 | | public: |
50 | | WireProtocolTest() |
51 | | : schema_({ ColumnSchema("col1", STRING), |
52 | | ColumnSchema("col2", STRING), |
53 | | ColumnSchema("col3", UINT32, true /* nullable */) }, |
54 | 6 | 1) { |
55 | 6 | } |
56 | | |
57 | | protected: |
58 | | Schema schema_; |
59 | | }; |
60 | | |
61 | 1 | TEST_F(WireProtocolTest, TestOKStatus) { |
62 | 1 | Status s = Status::OK(); |
63 | 1 | AppStatusPB pb; |
64 | 1 | StatusToPB(s, &pb); |
65 | 1 | EXPECT_EQ(AppStatusPB::OK, pb.code()); |
66 | 1 | EXPECT_FALSE(pb.has_message()); |
67 | 1 | EXPECT_FALSE(pb.has_posix_code()); |
68 | | |
69 | 1 | Status s2 = StatusFromPB(pb); |
70 | 1 | ASSERT_OK(s2); |
71 | 1 | } |
72 | | |
73 | 1 | TEST_F(WireProtocolTest, TestBadStatus) { |
74 | 1 | Status s = STATUS(NotFound, "foo", "bar"); |
75 | 1 | AppStatusPB pb; |
76 | 1 | StatusToPB(s, &pb); |
77 | 1 | EXPECT_EQ(AppStatusPB::NOT_FOUND, pb.code()); |
78 | 1 | EXPECT_TRUE(pb.has_message()); |
79 | 1 | EXPECT_EQ("foo: bar", pb.message()); |
80 | 1 | EXPECT_FALSE(pb.has_posix_code()); |
81 | | |
82 | 1 | Status s2 = StatusFromPB(pb); |
83 | 1 | EXPECT_TRUE(s2.IsNotFound()); |
84 | 1 | EXPECT_EQ(s.ToString(/* no file/line */ false), s2.ToString(/* no file/line */ false)); |
85 | 1 | } |
86 | | |
87 | 1 | TEST_F(WireProtocolTest, TestBadStatusWithPosixCode) { |
88 | 1 | Status s = STATUS(NotFound, "foo", "bar", Errno(1234)); |
89 | 1 | AppStatusPB pb; |
90 | 1 | StatusToPB(s, &pb); |
91 | 1 | EXPECT_EQ(AppStatusPB::NOT_FOUND, pb.code()); |
92 | 1 | EXPECT_TRUE(pb.has_message()); |
93 | 1 | EXPECT_EQ("foo: bar", pb.message()); |
94 | 1 | EXPECT_TRUE(pb.has_posix_code()); |
95 | 1 | EXPECT_EQ(1234, pb.posix_code()); |
96 | | |
97 | 1 | Status s2 = StatusFromPB(pb); |
98 | 1 | EXPECT_TRUE(s2.IsNotFound()); |
99 | 1 | EXPECT_EQ(1234, Errno(s2)); |
100 | 1 | EXPECT_EQ(s.ToString(/* no file/line */ false), s2.ToString(/* no file/line */ false)); |
101 | 1 | } |
102 | | |
103 | 1 | TEST_F(WireProtocolTest, TestSchemaRoundTrip) { |
104 | 1 | google::protobuf::RepeatedPtrField<ColumnSchemaPB> pbs; |
105 | | |
106 | 1 | SchemaToColumnPBs(schema_, &pbs); |
107 | 1 | ASSERT_EQ(3, pbs.size()); |
108 | | |
109 | | // Column 0. |
110 | 1 | EXPECT_TRUE(pbs.Get(0).is_key()); |
111 | 1 | EXPECT_EQ("col1", pbs.Get(0).name()); |
112 | 1 | EXPECT_EQ(DataType::STRING, pbs.Get(0).type().main()); |
113 | 1 | EXPECT_FALSE(pbs.Get(0).is_nullable()); |
114 | | |
115 | | // Column 1. |
116 | 1 | EXPECT_FALSE(pbs.Get(1).is_key()); |
117 | 1 | EXPECT_EQ("col2", pbs.Get(1).name()); |
118 | 1 | EXPECT_EQ(DataType::STRING, pbs.Get(1).type().main()); |
119 | 1 | EXPECT_FALSE(pbs.Get(1).is_nullable()); |
120 | | |
121 | | // Column 2. |
122 | 1 | EXPECT_FALSE(pbs.Get(2).is_key()); |
123 | 1 | EXPECT_EQ("col3", pbs.Get(2).name()); |
124 | 1 | EXPECT_EQ(DataType::UINT32, pbs.Get(2).type().main()); |
125 | 1 | EXPECT_TRUE(pbs.Get(2).is_nullable()); |
126 | | |
127 | | // Convert back to a Schema object and verify they're identical. |
128 | 1 | Schema schema2; |
129 | 1 | ASSERT_OK(ColumnPBsToSchema(pbs, &schema2)); |
130 | 1 | EXPECT_EQ(schema_.ToString(), schema2.ToString()); |
131 | 1 | EXPECT_EQ(schema_.num_key_columns(), schema2.num_key_columns()); |
132 | 1 | } |
133 | | |
134 | | // Test that, when non-contiguous key columns are passed, an error Status |
135 | | // is returned. |
136 | 1 | TEST_F(WireProtocolTest, TestBadSchema_NonContiguousKey) { |
137 | 1 | google::protobuf::RepeatedPtrField<ColumnSchemaPB> pbs; |
138 | | |
139 | | // Column 0: key |
140 | 1 | ColumnSchemaPB* col_pb = pbs.Add(); |
141 | 1 | col_pb->set_name("c0"); |
142 | 1 | QLType::Create(STRING)->ToQLTypePB(col_pb->mutable_type()); |
143 | 1 | col_pb->set_is_key(true); |
144 | | |
145 | | // Column 1: not a key |
146 | 1 | col_pb = pbs.Add(); |
147 | 1 | col_pb->set_name("c1"); |
148 | 1 | QLType::Create(STRING)->ToQLTypePB(col_pb->mutable_type()); |
149 | 1 | col_pb->set_is_key(false); |
150 | | |
151 | | // Column 2: marked as key. This is an error. |
152 | 1 | col_pb = pbs.Add(); |
153 | 1 | col_pb->set_name("c2"); |
154 | 1 | QLType::Create(STRING)->ToQLTypePB(col_pb->mutable_type()); |
155 | 1 | col_pb->set_is_key(true); |
156 | | |
157 | 1 | Schema schema; |
158 | 1 | Status s = ColumnPBsToSchema(pbs, &schema); |
159 | 1 | ASSERT_STR_CONTAINS(s.ToString(), "Got out-of-order key column"); |
160 | 1 | } |
161 | | |
162 | | // Test that, when multiple columns with the same name are passed, an |
163 | | // error Status is returned. |
164 | 1 | TEST_F(WireProtocolTest, TestBadSchema_DuplicateColumnName) { |
165 | 1 | google::protobuf::RepeatedPtrField<ColumnSchemaPB> pbs; |
166 | | |
167 | | // Column 0: |
168 | 1 | ColumnSchemaPB* col_pb = pbs.Add(); |
169 | 1 | col_pb->set_name("c0"); |
170 | 1 | QLType::Create(STRING)->ToQLTypePB(col_pb->mutable_type()); |
171 | 1 | col_pb->set_is_key(true); |
172 | | |
173 | | // Column 1: |
174 | 1 | col_pb = pbs.Add(); |
175 | 1 | col_pb->set_name("c1"); |
176 | 1 | QLType::Create(STRING)->ToQLTypePB(col_pb->mutable_type()); |
177 | 1 | col_pb->set_is_key(false); |
178 | | |
179 | | // Column 2: same name as column 0 |
180 | 1 | col_pb = pbs.Add(); |
181 | 1 | col_pb->set_name("c0"); |
182 | 1 | QLType::Create(STRING)->ToQLTypePB(col_pb->mutable_type()); |
183 | 1 | col_pb->set_is_key(false); |
184 | | |
185 | 1 | Schema schema; |
186 | 1 | Status s = ColumnPBsToSchema(pbs, &schema); |
187 | 1 | ASSERT_EQ("Invalid argument: Duplicate column name: c0", |
188 | 1 | s.ToString(/* no file/line */ false)); |
189 | 1 | } |
190 | | |
191 | | } // namespace yb |