Branch data Line data Source code
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 "openvswitch/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 : 1 : test_ofpbuf_main(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
30 : : {
31 : 1 : struct ofpbuf *buf = ofpbuf_new(BUF_SIZE);
32 : 1 : int exit_code = 0;
33 : :
34 : : /* Init checks. */
35 [ - + ]: 1 : ovs_assert(!buf->size);
36 [ - + ]: 1 : ovs_assert(buf->allocated == BUF_SIZE);
37 [ - + ]: 1 : ovs_assert(buf->base == buf->data);
38 : :
39 : : /* Sets 'buf->header' and 'buf->msg'. */
40 : 1 : buf->header = (char *) buf->base + HDR_OFS;
41 : 1 : buf->msg = (char *) buf->base + MSG_OFS;
42 : :
43 : : /* Gets another 'BUF_SIZE' bytes headroom. */
44 : 1 : ofpbuf_prealloc_headroom(buf, BUF_SIZE);
45 [ - + ]: 1 : ovs_assert(!buf->size);
46 [ - + ]: 1 : ovs_assert(buf->allocated == 2 * BUF_SIZE);
47 [ - + ]: 1 : 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 [ - + ]: 1 : ovs_assert(buf->header == (char *) buf->base + BUF_SIZE + HDR_OFS);
51 [ - + ]: 1 : ovs_assert(buf->msg == (char *) buf->base + BUF_SIZE + MSG_OFS);
52 : :
53 : : /* Gets another 'BUF_SIZE' bytes tailroom. */
54 : 1 : ofpbuf_prealloc_tailroom(buf, BUF_SIZE);
55 : : /* Must remain unchanged. */
56 [ - + ]: 1 : ovs_assert(!buf->size);
57 [ - + ]: 1 : ovs_assert(buf->allocated == 2 * BUF_SIZE);
58 [ - + ]: 1 : ovs_assert((char *) buf->base + BUF_SIZE == buf->data);
59 [ - + ]: 1 : ovs_assert(buf->header == (char *) buf->base + BUF_SIZE + HDR_OFS);
60 [ - + ]: 1 : ovs_assert(buf->msg == (char *) buf->base + BUF_SIZE + MSG_OFS);
61 : :
62 : 1 : ofpbuf_delete(buf);
63 : 1 : exit(exit_code);
64 : : }
65 : :
66 : 1175 : OVSTEST_REGISTER("test-ofpbuf", test_ofpbuf_main);
|