Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix a double free error in vr_message_process_response
This patch fixes a double free error and introduces user land unit test for the vrouter code. The cmocka unit test framework is used for that purpose. A first unit test is provided that check this fixes. Change-Id: Ic8c84d076d078a87adaf6d4398373c368db0361c Closes-bug: #1394147
- Loading branch information
Sylvain Afchain
committed
Nov 19, 2014
1 parent
82330a6
commit 9f7b8ca
Showing
7 changed files
with
180 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# | ||
# Copyright (c) 2014 Juniper Networks, Inc. All rights reserved. | ||
# | ||
|
||
Import('VRouterEnv') | ||
env = VRouterEnv.Clone() | ||
|
||
vrouter_suite = [] | ||
|
||
# Include paths | ||
|
||
# CFLAGS | ||
env.Append(CCFLAGS = '-g') | ||
|
||
env.Replace(LIBPATH = env['TOP_LIB']) | ||
env.Append(LIBPATH = ['../host', '../sandesh', '../dp-core']) | ||
env.Replace(LIBS = ['dp_core', 'vrouter', 'dp_sandesh_c', 'vrouter', 'dp_core', 'dp_sandesh_c', 'dp_core', 'sandesh-c', 'cmocka']) | ||
|
||
test_dep_srcs = ['common_test.c'] | ||
|
||
dp_core_test = VRouterEnv.MakeTestCmd(env, 'dp_core_test', vrouter_suite, test_dep_srcs) | ||
|
||
test = env.TestSuite('vrouter-test', vrouter_suite) | ||
env.Alias('vrouter:test', test) | ||
Return('vrouter_suite') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#include "vr_types.h" | ||
|
||
void | ||
get_random_bytes(void *buf, int nbytes) | ||
{ | ||
} | ||
|
||
uint32_t | ||
jhash(void *key, uint32_t length, uint32_t interval) | ||
{ | ||
uint32_t ret; | ||
int i; | ||
unsigned char *data = (unsigned char *)key; | ||
|
||
for (i = 0; i < length; i ++) | ||
ret += data[i]; | ||
|
||
return ret; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#ifndef __COMMON_TEST_H__ | ||
#define __COMMON_TEST_H__ | ||
|
||
#include "vr_types.h" | ||
|
||
void get_random_bytes(void *buf, int nbytes); | ||
uint32_t jhash(void *key, uint32_t length, uint32_t interval); | ||
|
||
#endif /* __COMMON_TEST_H__ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
#include <stdio.h> | ||
#include <unistd.h> | ||
#include <stdarg.h> | ||
#include <stddef.h> | ||
#include <setjmp.h> | ||
#include <cmocka.h> | ||
|
||
#include "vr_types.h" | ||
#include "vr_os.h" | ||
#include "vr_packet.h" | ||
#include "vr_message.h" | ||
#include "vr_interface.h" | ||
|
||
#include "host/vr_host.h" | ||
#include "host/vr_host_packet.h" | ||
#include "host/vr_host_interface.h" | ||
|
||
extern int vrouter_host_init(unsigned int); | ||
extern unsigned int vr_num_cpus; | ||
|
||
unsigned int allocated = 0; | ||
|
||
void *alloc_for_test(unsigned int size) { | ||
void *ptr; | ||
|
||
ptr = malloc(size); | ||
allocated++; | ||
|
||
return ptr; | ||
} | ||
|
||
void free_for_test(void *ptr) { | ||
free(ptr); | ||
allocated--; | ||
} | ||
|
||
int fake_response_cb(void *ptr1, unsigned int i, void *ptr2) { | ||
return 1; | ||
} | ||
|
||
void drop_stats_memory_test(void **state) { | ||
vr_drop_stats_req req = { | ||
.h_op = SANDESH_OP_GET, | ||
.vds_rid = 0 | ||
}; | ||
|
||
/* process request */ | ||
vr_drop_stats_req_process(&req); | ||
|
||
/* currently one response queued */ | ||
assert_int_equal(allocated, 2); | ||
|
||
/* dequeue it, and check the alloctions */ | ||
vr_message_process_response(fake_response_cb, NULL); | ||
assert_int_equal(allocated, 0); | ||
} | ||
|
||
static void setup(void **state) { | ||
vrouter_host->hos_malloc = alloc_for_test; | ||
vrouter_host->hos_zalloc = alloc_for_test; | ||
vrouter_host->hos_free = free_for_test; | ||
} | ||
|
||
static void teardown(void **state) { | ||
free(*state); | ||
} | ||
|
||
int main(void) { | ||
int ret; | ||
|
||
/* test suite */ | ||
const UnitTest tests[] = { | ||
unit_test_setup_teardown(drop_stats_memory_test, setup, teardown), | ||
}; | ||
|
||
vr_diet_message_proto_init(); | ||
|
||
/* init the vrouter */ | ||
ret = vrouter_host_init(VR_MPROTO_SANDESH); | ||
if (ret) | ||
return ret; | ||
|
||
|
||
/* let's run the test suite */ | ||
ret = run_tests(tests); | ||
|
||
return ret; | ||
} |