4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 func setmetaf(user_data
int, name
string) int {
28 panic("expected user_data == 42")
31 if (name
== context_base_allocation
) {
37 func Test250OptSetMeta(t
*testing
.T
) {
38 /* Get into negotiating state without structured replies. */
41 t
.Fatalf("could not create handle: %s", err
)
45 err
= h
.SetOptMode(true)
47 t
.Fatalf("could not set opt mode: %s", err
)
49 err
= h
.SetRequestStructuredReplies(false)
51 t
.Fatalf("could not set opt mode: %s", err
)
54 err
= h
.ConnectCommand([]string{
55 "nbdkit", "-s", "--exit-with-parent", "-v",
59 t
.Fatalf("could not connect: %s", err
)
62 /* No contexts negotiated yet; CanMeta should be error if any requested */
63 sr
, err
:= h
.GetStructuredRepliesNegotiated()
65 t
.Fatalf("could not check structured replies negotiated: %s", err
)
68 t
.Fatalf("unexpected structured replies state")
70 meta
, err
:= h
.CanMetaContext(context_base_allocation
)
72 t
.Fatalf("could not check can meta context: %s", err
)
75 t
.Fatalf("unexpected can meta context state")
77 err
= h
.AddMetaContext(context_base_allocation
)
79 t
.Fatalf("could not request add_meta_context: %s", err
)
81 _
, err
= h
.CanMetaContext(context_base_allocation
)
83 t
.Fatalf("expected error")
86 /* SET cannot succeed until SR is negotiated. */
89 _
, err
= h
.OptSetMetaContext(func(name
string) int {
90 return setmetaf(42, name
)
93 t
.Fatalf("expected error")
95 if set_count
!= 0 || set_seen
{
96 t
.Fatalf("unexpected set_count after opt_set_meta_context")
98 sr
, err
= h
.OptStructuredReply()
100 t
.Fatalf("could not trigger opt_structured_reply: %s", err
)
103 t
.Fatalf("unexpected structured replies state")
105 sr
, err
= h
.GetStructuredRepliesNegotiated()
107 t
.Fatalf("could not check structured replies negotiated: %s", err
)
110 t
.Fatalf("unexpected structured replies state")
112 _
, err
= h
.CanMetaContext(context_base_allocation
)
114 t
.Fatalf("expected error")
117 /* nbdkit does not match wildcard for SET, even though it does for LIST */
120 err
= h
.ClearMetaContexts()
122 t
.Fatalf("could not request clear_meta_contexts: %s", err
)
124 err
= h
.AddMetaContext("base:")
126 t
.Fatalf("could not request add_meta_context: %s", err
)
128 r
, err
:= h
.OptSetMetaContext(func(name
string) int {
129 return setmetaf(42, name
)
132 t
.Fatalf("could not request opt_set_meta_context: %s", err
)
134 if r
!= set_count || r
!= 0 || set_seen
{
135 t
.Fatalf("unexpected set_count after opt_set_meta_context")
138 /* Negotiating with no contexts is not an error, but selects nothing */
141 err
= h
.ClearMetaContexts()
143 t
.Fatalf("could not request clear_meta_contexts: %s", err
)
145 r
, err
= h
.OptSetMetaContext(func(name
string) int {
146 return setmetaf(42, name
)
149 t
.Fatalf("could not request opt_set_meta_context: %s", err
)
151 if r
!= set_count || r
!= 0 || set_seen
{
152 t
.Fatalf("unexpected set_count after opt_set_meta_context")
154 meta
, err
= h
.CanMetaContext(context_base_allocation
)
156 t
.Fatalf("could not check can meta context: %s", err
)
159 t
.Fatalf("unexpected can meta context state")
162 /* Request 2 with expectation of 1; with SetRequestMetaContext off */
165 err
= h
.AddMetaContext("x-nosuch:context")
167 t
.Fatalf("could not request add_meta_context: %s", err
)
169 err
= h
.AddMetaContext(context_base_allocation
)
171 t
.Fatalf("could not request add_meta_context: %s", err
)
173 err
= h
.SetRequestMetaContext(false)
175 t
.Fatalf("could not set_request_meta_context: %s", err
)
177 r
, err
= h
.OptSetMetaContext(func(name
string) int {
178 return setmetaf(42, name
)
181 t
.Fatalf("could not request opt_set_meta_context: %s", err
)
183 if r
!= 1 || r
!= set_count ||
!set_seen
{
184 t
.Fatalf("unexpected set_count after opt_set_meta_context")
186 meta
, err
= h
.CanMetaContext(context_base_allocation
)
188 t
.Fatalf("could not check can meta context: %s", err
)
191 t
.Fatalf("unexpected can meta context state")
194 /* Transition to transmission phase; our last set should remain active */
195 err
= h
.ClearMetaContexts()
197 t
.Fatalf("could not request clear_meta_contexts: %s", err
)
199 err
= h
.AddMetaContext("x-nosuch:context")
201 t
.Fatalf("could not request add_meta_context: %s", err
)
205 t
.Fatalf("could not request opt_go: %s", err
)
207 meta
, err
= h
.CanMetaContext(context_base_allocation
)
209 t
.Fatalf("could not check can meta context: %s", err
)
212 t
.Fatalf("unexpected can meta context state")
215 /* Now too late to set; but should not lose earlier state */
218 _
, err
= h
.OptSetMetaContext(func(name
string) int {
219 return setmetaf(42, name
)
222 t
.Fatalf("expected error")
224 if set_count
!= 0 || set_seen
{
225 t
.Fatalf("unexpected set_count after opt_set_meta_context")
227 meta
, err
= h
.CanMetaContext(context_base_allocation
)
229 t
.Fatalf("could not check can meta context: %s", err
)
232 t
.Fatalf("unexpected can meta context state")
235 err
= h
.Shutdown(nil)
237 t
.Fatalf("could not request shutdown: %s", err
)