Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
win_pclone/win_pfree refactor and unit tests
Change-Id: I120f06f4e030f33a1578edd41d5c414fe5c22b32 Partial-Bug: #1737177
- Loading branch information
1 parent
cd6f3a2
commit b3d243e
Showing
15 changed files
with
416 additions
and
66 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/* | ||
* Copyright (c) 2018 Juniper Networks, Inc. All rights reserved. | ||
*/ | ||
void | ||
win_update_drop_stats(struct vr_packet *pkt, unsigned short reason) | ||
{ | ||
} |
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,26 @@ | ||
/* | ||
* fake_win_memory.c | ||
* | ||
* Copyright (c) 2018 Juniper Networks, Inc. All rights reserved. | ||
*/ | ||
|
||
#include "win_memory.h" | ||
|
||
#include <stdarg.h> | ||
#include <stdint.h> | ||
#include <setjmp.h> | ||
#include <cmocka.h> | ||
|
||
void | ||
WinRawFree(void *buffer) | ||
{ | ||
test_free(buffer); | ||
} | ||
|
||
void *(*WinRawAllocate_Callback)(size_t size); | ||
|
||
void * | ||
WinRawAllocate(size_t size) | ||
{ | ||
return WinRawAllocate_Callback(size); | ||
} |
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,185 @@ | ||
/* | ||
* test_win_pclone.c | ||
* | ||
* Copyright (c) 2018 Juniper Networks, Inc. All rights reserved. | ||
*/ | ||
|
||
#include <stdarg.h> | ||
#include <stdbool.h> | ||
#include <stdint.h> | ||
#include <setjmp.h> | ||
#include <cmocka.h> | ||
|
||
#include "vr_packet.h" | ||
#include "win_packet.h" | ||
#include "fake_win_packet.h" | ||
|
||
extern struct vr_packet *win_pclone(struct vr_packet *pkt); | ||
|
||
static PWIN_PACKET (*Saved_WinPacketRawAllocateClone)(PWIN_PACKET Packet); | ||
static PVOID (*Saved_WinRawAllocate)(size_t size); | ||
|
||
static uint32_t RawAllocateClone_CalledCount; | ||
static uint32_t RawAllocateClone_FirstFailure; | ||
|
||
static void | ||
Fake_WinPacketRawAllocateClone_SetFailureOnNthCall(uint32_t N) | ||
{ | ||
RawAllocateClone_CalledCount = 0; | ||
RawAllocateClone_FirstFailure = N; | ||
} | ||
|
||
static PWIN_PACKET | ||
Failing_WinPacketRawAllocateClone(PWIN_PACKET Packet) | ||
{ | ||
RawAllocateClone_CalledCount++; | ||
if (RawAllocateClone_CalledCount >= RawAllocateClone_FirstFailure) { | ||
return NULL; | ||
} else { | ||
return Fake_WinPacketAllocateOwned(); | ||
} | ||
} | ||
|
||
static PWIN_PACKET | ||
Succeeding_WinPacketRawAllocateClone(PWIN_PACKET Packet) | ||
{ | ||
return Fake_WinPacketAllocateOwned(); | ||
} | ||
|
||
static PVOID | ||
Failing_WinRawAllocate(size_t size) | ||
{ | ||
return NULL; | ||
} | ||
|
||
static PVOID | ||
Succeeding_WinRawAllocate(size_t size) | ||
{ | ||
return test_calloc(1, size); | ||
} | ||
|
||
extern PVOID (*WinRawAllocate_Callback)(size_t size); | ||
|
||
static struct vr_packet * | ||
AllocateVrPacketNonOwned(VOID) | ||
{ | ||
PVR_PACKET_WRAPPER pkt = test_calloc(1, sizeof(*pkt)); | ||
pkt->WinPacket = Fake_WinPacketAllocateNonOwned(); | ||
return &pkt->VrPacket; | ||
} | ||
|
||
static VOID | ||
FreeVrPacket(struct vr_packet * vrPkt) | ||
{ | ||
win_pfree(vrPkt, 0); | ||
} | ||
|
||
//Use _ suffix because there already exists "GetParent" function in WDK | ||
static PWIN_PACKET | ||
GetParent_(struct vr_packet * vrPkt) | ||
{ | ||
PWIN_PACKET winPacket = GetWinPacketFromVrPacket(vrPkt); | ||
return WinPacketRawGetParentOf(winPacket); | ||
} | ||
|
||
int | ||
Test_win_pclone_SetUp(void **state) | ||
{ | ||
Saved_WinPacketRawAllocateClone = WinPacketRawAllocateClone_Callback; | ||
Saved_WinRawAllocate = WinRawAllocate_Callback; | ||
return 0; | ||
} | ||
|
||
int | ||
Test_win_pclone_TearDown(void** state) | ||
{ | ||
WinPacketRawAllocateClone_Callback = Saved_WinPacketRawAllocateClone; | ||
WinRawAllocate_Callback = Saved_WinRawAllocate; | ||
return 0; | ||
} | ||
|
||
void | ||
Test_win_pclone_ReturnsNullWhenNullIsPassed(void **state) | ||
{ | ||
expect_assert_failure(win_pclone(NULL)); | ||
} | ||
|
||
void | ||
Test_win_pclone_ReturnsNullWhenFirstCloneFails(void **state) | ||
{ | ||
WinPacketRawAllocateClone_Callback = Failing_WinPacketRawAllocateClone; | ||
Fake_WinPacketRawAllocateClone_SetFailureOnNthCall(1); | ||
|
||
struct vr_packet *vrPkt = AllocateVrPacketNonOwned(); | ||
|
||
struct vr_packet *clonedVrPkt = win_pclone(vrPkt); | ||
assert_null(clonedVrPkt); | ||
|
||
FreeVrPacket(vrPkt); | ||
} | ||
|
||
void | ||
Test_win_pclone_ReturnsNullWhenSecondCloneFails(void **state) | ||
{ | ||
WinPacketRawAllocateClone_Callback = Failing_WinPacketRawAllocateClone; | ||
Fake_WinPacketRawAllocateClone_SetFailureOnNthCall(2); | ||
|
||
struct vr_packet *vrPkt = AllocateVrPacketNonOwned(); | ||
|
||
struct vr_packet *clonedVrPkt = win_pclone(vrPkt); | ||
assert_null(clonedVrPkt); | ||
|
||
FreeVrPacket(vrPkt); | ||
} | ||
|
||
void | ||
Test_win_pclone_ReturnsNullWhenWrapperAllocationFails(void **state) | ||
{ | ||
WinPacketRawAllocateClone_Callback = Succeeding_WinPacketRawAllocateClone; | ||
WinRawAllocate_Callback = Failing_WinRawAllocate; | ||
|
||
struct vr_packet *vrPkt = AllocateVrPacketNonOwned(); | ||
|
||
struct vr_packet *clonedVrPkt = win_pclone(vrPkt); | ||
assert_null(clonedVrPkt); | ||
|
||
FreeVrPacket(vrPkt); | ||
|
||
WinRawAllocate_Callback = Saved_WinRawAllocate; | ||
} | ||
|
||
void | ||
Test_win_pclone_ReturnsPacketWhenCloneSucceeds(void **state) | ||
{ | ||
WinPacketRawAllocateClone_Callback = Succeeding_WinPacketRawAllocateClone; | ||
WinRawAllocate_Callback = Succeeding_WinRawAllocate; | ||
|
||
struct vr_packet *vrPkt = AllocateVrPacketNonOwned(); | ||
vrPkt->vp_type = 1; | ||
PWIN_PACKET originalWinPkt = GetWinPacketFromVrPacket(vrPkt); | ||
|
||
struct vr_packet *clonedVrPkt = win_pclone(vrPkt); | ||
assert_non_null(clonedVrPkt); | ||
|
||
assert_int_equal(WinPacketRawGetChildCountOf(originalWinPkt), 2); | ||
assert_ptr_equal(GetParent_(vrPkt), originalWinPkt); | ||
assert_ptr_equal(GetParent_(clonedVrPkt), originalWinPkt); | ||
assert_int_equal(clonedVrPkt->vp_type, 1); | ||
|
||
FreeVrPacket(vrPkt); | ||
FreeVrPacket(clonedVrPkt); | ||
} | ||
|
||
#define win_pclone_UnitTest_(p, f) cmocka_unit_test_setup_teardown(p##f, p##SetUp, p##TearDown) | ||
#define win_pclone_UnitTest(f) win_pclone_UnitTest_(Test_win_pclone_, f) | ||
|
||
int main(void) { | ||
const struct CMUnitTest tests[] = { | ||
win_pclone_UnitTest(ReturnsNullWhenNullIsPassed), | ||
win_pclone_UnitTest(ReturnsNullWhenFirstCloneFails), | ||
win_pclone_UnitTest(ReturnsNullWhenSecondCloneFails), | ||
win_pclone_UnitTest(ReturnsNullWhenWrapperAllocationFails), | ||
win_pclone_UnitTest(ReturnsPacketWhenCloneSucceeds), | ||
}; | ||
return cmocka_run_group_tests(tests, NULL, NULL); | ||
} |
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,38 @@ | ||
/* | ||
* test_win_pclone.c | ||
* | ||
* Copyright (c) 2018 Juniper Networks, Inc. All rights reserved. | ||
*/ | ||
|
||
#include <stdarg.h> | ||
#include <stdbool.h> | ||
#include <stdint.h> | ||
#include <setjmp.h> | ||
#include <cmocka.h> | ||
|
||
#include "vr_packet.h" | ||
#include "win_packet.h" | ||
#include "fake_win_packet.h" | ||
|
||
extern void win_pfree(struct vr_packet *pkt, unsigned short reason); | ||
|
||
void Test_win_pfree_AssertsWhenVrPacketIsNull(void** state) { | ||
expect_assert_failure(win_pfree(NULL, 0)); | ||
} | ||
|
||
void Test_win_pfree_FreeSuccessful(void** state) { | ||
PVR_PACKET_WRAPPER pkt = test_calloc(1, sizeof(*pkt)); | ||
pkt->WinPacket = Fake_WinPacketAllocateNonOwned(); | ||
struct vr_packet *vrPkt = &pkt->VrPacket; | ||
|
||
win_pfree(vrPkt, 0); | ||
} | ||
|
||
|
||
int main(void) { | ||
const struct CMUnitTest tests[] = { | ||
cmocka_unit_test(Test_win_pfree_AssertsWhenVrPacketIsNull), | ||
cmocka_unit_test(Test_win_pfree_FreeSuccessful), | ||
}; | ||
return cmocka_run_group_tests(tests, NULL, NULL); | ||
} |
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
Oops, something went wrong.