ofpbuf: Update msg when resizing ofpbuf.
[cascardo/ovs.git] / tests / test-ofpbuf.c
1 /*
2  * Copyright (c) 2015 Nicira, Inc.
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 <config.h>
18 #undef NDEBUG
19 #include <stdio.h>
20 #include "ofpbuf.h"
21 #include "ovstest.h"
22 #include "util.h"
23
24 #define BUF_SIZE 100
25 #define HDR_OFS 10
26 #define MSG_OFS 50
27
28 static void
29 test_ofpbuf_main(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
30 {
31     struct ofpbuf *buf = ofpbuf_new(BUF_SIZE);
32     int exit_code = 0;
33
34     /* Init checks. */
35     ovs_assert(!buf->size);
36     ovs_assert(buf->allocated == BUF_SIZE);
37     ovs_assert(buf->base == buf->data);
38
39     /* Sets 'buf->header' and 'buf->msg'. */
40     buf->header = (char *) buf->base + HDR_OFS;
41     buf->msg = (char *) buf->base + MSG_OFS;
42
43     /* Gets another 'BUF_SIZE' bytes headroom. */
44     ofpbuf_prealloc_headroom(buf, BUF_SIZE);
45     ovs_assert(!buf->size);
46     ovs_assert(buf->allocated == 2 * BUF_SIZE);
47     ovs_assert((char *) buf->base + BUF_SIZE == buf->data);
48     /* Now 'buf->header' and 'buf->msg' must be BUF_SIZE away from
49      * their original offsets. */
50     ovs_assert(buf->header == (char *) buf->base + BUF_SIZE + HDR_OFS);
51     ovs_assert(buf->msg == (char *) buf->base + BUF_SIZE + MSG_OFS);
52
53     /* Gets another 'BUF_SIZE' bytes tailroom. */
54     ofpbuf_prealloc_tailroom(buf, BUF_SIZE);
55     /* Must remain unchanged. */
56     ovs_assert(!buf->size);
57     ovs_assert(buf->allocated == 2 * BUF_SIZE);
58     ovs_assert((char *) buf->base + BUF_SIZE == buf->data);
59     ovs_assert(buf->header == (char *) buf->base + BUF_SIZE + HDR_OFS);
60     ovs_assert(buf->msg == (char *) buf->base + BUF_SIZE + MSG_OFS);
61
62     ofpbuf_delete(buf);
63     exit(exit_code);
64 }
65
66 OVSTEST_REGISTER("test-ofpbuf", test_ofpbuf_main);