Skip to content

Commit

Permalink
Addition of unit test cases for Switching Backend functionality
Browse files Browse the repository at this point in the history
Adding Unit test cases for newly added function pdbg_release_dt_root()

Signed-off-by: swarnendu.roy.chowdhury@ibm.com
  • Loading branch information
Swarnendu-R-C committed Jun 28, 2023
1 parent db2c1be commit 35a7c10
Show file tree
Hide file tree
Showing 3 changed files with 366 additions and 9 deletions.
8 changes: 7 additions & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ GIT_SHA1 ?= `git --work-tree=$(top_srcdir) --git-dir=$(top_srcdir)/.git describe
libpdbg_tests = libpdbg_target_test \
libpdbg_probe_test1 \
libpdbg_probe_test2 \
libpdbg_probe_test3
libpdbg_probe_test3 \
libpdbg_release_dt_root_test

bin_PROGRAMS = pdbg
check_PROGRAMS = $(libpdbg_tests) libpdbg_dtree_test \
Expand Down Expand Up @@ -249,6 +250,11 @@ libpdbg_target_test_CFLAGS = $(libpdbg_test_cflags)
libpdbg_target_test_LDFLAGS = $(libpdbg_test_ldflags)
libpdbg_target_test_LDADD = $(libpdbg_test_ldadd)

libpdbg_release_dt_root_test_SOURCES = src/tests/libpdbg_release_dt_root_test.c
libpdbg_release_dt_root_test_CFLAGS = $(libpdbg_test_cflags)
libpdbg_release_dt_root_test_LDFLAGS = $(libpdbg_test_ldflags)
libpdbg_release_dt_root_test_LDADD = $(libpdbg_test_ldadd)

libpdbg_probe_test1_SOURCES = src/tests/libpdbg_probe_test.c
libpdbg_probe_test1_CFLAGS = $(libpdbg_test_cflags) -DTEST_ID=1
libpdbg_probe_test1_LDFLAGS = $(libpdbg_test_ldflags)
Expand Down
9 changes: 1 addition & 8 deletions libpdbg/dtb.c
Original file line number Diff line number Diff line change
Expand Up @@ -414,14 +414,7 @@ bool pdbg_set_backend(enum pdbg_backend backend, const char *backend_option)
{
if (pdbg_target_root())
{
pdbg_log(PDBG_ERROR, "pdbg_set_backend() must be called before pdbg_targets_init() or
after calling pdbg_release_dt_root() if a dev tree is already set\n");
return false;
}

if (pdbg_backend == backend)
{
pdbg_log(PDBG_ERROR, "New backend is same as the current backend. Not proceeding further\n");
pdbg_log(PDBG_ERROR, "pdbg_set_backend() must be called before pdbg_targets_init() or after calling pdbg_release_dt_root() if a dev tree is already set\n");
return false;
}

Expand Down
358 changes: 358 additions & 0 deletions src/tests/libpdbg_release_dt_root_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,358 @@
/* Copyright 2023 IBM Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include <assert.h>

#include <libpdbg.h>

static int count_target(struct pdbg_target *parent, const char *classname)
{
struct pdbg_target *target = NULL;
int cnt = 0;

pdbg_for_each_target(classname, parent, target)
++cnt;

return cnt;
}

static int count_class_target(const char *classname)
{
struct pdbg_target *target = NULL;
int cnt = 0;

pdbg_for_each_class_target(classname, target)
++cnt;

return cnt;
}

static int count_child_target(struct pdbg_target *parent)
{
struct pdbg_target *child = NULL;
int cnt = 0;

pdbg_for_each_child_target(parent, child)
++cnt;

return cnt;
}

static void testClassCountTarget()
{
int count = count_class_target("fsi");
assert(count == 8);

count = count_class_target("pib");
assert(count == 8);

count = count_class_target("core");
assert(count == 32);

count = count_class_target("thread");
assert(count == 64);
}

static void testSetTwo(struct pdbg_target *root)
{
struct pdbg_target *parent, *target = NULL;
pdbg_for_each_child_target(root, parent)
{
const char *name = pdbg_target_dn_name(parent);
assert(strncmp(name, "proc", 4) == 0);

pdbg_for_each_target("pib", parent, target)
{
name = pdbg_target_class_name(target);
assert(!strcmp(name, "pib"));
}

pdbg_for_each_target("fsi", parent, target)
{
name = pdbg_target_class_name(target);
assert(!strcmp(name, "fsi"));
}
}
}

static void testSetOne(struct pdbg_target *root, const bool dtreeReleased)
{
if (dtreeReleased)
{
assert(!root);
return;
}
else
{
assert(root);
testClassCountTarget();
}
testSetTwo(root);
}

static void testFsiTarget()
{
struct pdbg_target *parent, *target = NULL;
pdbg_for_each_class_target("fsi", target)
{
parent = pdbg_target_parent(NULL, target);
assert(parent);

const char *name = pdbg_target_dn_name(parent);
assert(strncmp(name, "proc", 4) == 0);

parent = pdbg_target_parent("fsi", target);
assert(parent == NULL);

parent = pdbg_target_parent("pib", target);
assert(parent == NULL);

parent = pdbg_target_parent("core", target);
assert(parent == NULL);

parent = pdbg_target_parent("thread", target);
assert(parent == NULL);

int count = count_child_target(target);
assert(count == 0);

count = count_target(target, "fsi");
assert(count == 1);

count = count_target(target, "pib");
assert(count == 0);

count = count_target(target, "core");
assert(count == 0);

count = count_target(target, "thread");
assert(count == 0);

name = pdbg_target_name(target);
assert(!strcmp(name, "Fake FSI"));

name = pdbg_target_class_name(target);
assert(!strcmp(name, "fsi"));

name = pdbg_target_dn_name(target);
assert(!strncmp(name, "fsi", 3));
}
}

static void testPibTarget()
{
struct pdbg_target *parent, *target = NULL;
pdbg_for_each_class_target("pib", target)
{
parent = pdbg_target_parent(NULL, target);
assert(parent);

const char *name = pdbg_target_dn_name(parent);
assert(strncmp(name, "proc", 4) == 0);

parent = pdbg_target_parent("fsi", target);
assert(parent == NULL);

parent = pdbg_target_parent("pib", target);
assert(parent == NULL);

parent = pdbg_target_parent("core", target);
assert(parent == NULL);

parent = pdbg_target_parent("thread", target);
assert(parent == NULL);

int count = count_child_target(target);
assert(count == 4);

count = count_target(target, "fsi");
assert(count == 0);

count = count_target(target, "pib");
assert(count == 1);

count = count_target(target, "core");
assert(count == 4);

count = count_target(target, "thread");
assert(count == 8);

name = pdbg_target_name(target);
assert(!strcmp(name, "Fake PIB"));

name = pdbg_target_class_name(target);
assert(!strcmp(name, "pib"));

name = pdbg_target_dn_name(target);
assert(!strncmp(name, "pib", 3));
}
}

static void testCoreTarget()
{
struct pdbg_target *parent, *target, *parent2 = NULL;
pdbg_for_each_class_target("core", target)
{
uint64_t addr, size;
uint32_t index;

parent = pdbg_target_parent("fsi", target);
assert(parent == NULL);

parent = pdbg_target_parent("pib", target);
assert(parent);

parent2 = pdbg_target_require_parent("pib", target);
assert(parent == parent2);

parent = pdbg_target_parent("core", target);
assert(parent == NULL);

parent = pdbg_target_parent("thread", target);
assert(parent == NULL);

int count = count_child_target(target);
assert(count == 2);

count = count_target(target, "fsi");
assert(count == 0);

count = count_target(target, "pib");
assert(count == 0);

count = count_target(target, "core");
assert(count == 1);

count = count_target(target, "thread");
assert(count == 2);

const char *name = pdbg_target_name(target);
assert(!strcmp(name, "Fake Core"));

name = pdbg_target_class_name(target);
assert(!strcmp(name, "core"));

name = pdbg_target_dn_name(target);
assert(!strncmp(name, "core", 4));

index = pdbg_target_index(target);
addr = pdbg_target_address(target, &size);
assert(size == 0);
assert(addr == 0x10000 + (index + 1)*0x10);
}
}

static void testThreadTarget()
{
struct pdbg_target *parent, *target, *parent2 = NULL;
pdbg_for_each_class_target("thread", target)
{
parent = pdbg_target_parent("fsi", target);
assert(parent == NULL);

parent = pdbg_target_parent("pib", target);
assert(parent);

parent2 = pdbg_target_require_parent("pib", target);
assert(parent == parent2);

parent = pdbg_target_parent("core", target);
assert(parent);

parent2 = pdbg_target_require_parent("core", target);
assert(parent == parent2);

parent = pdbg_target_parent("thread", target);
assert(parent == NULL);

int count = count_child_target(target);
assert(count == 0);

count = count_target(target, "fsi");
assert(count == 0);

count = count_target(target, "pib");
assert(count == 0);

count = count_target(target, "core");
assert(count == 0);

count = count_target(target, "thread");
assert(count == 1);

const char *name = pdbg_target_name(target);
assert(!strcmp(name, "Fake Thread"));

name = pdbg_target_class_name(target);
assert(!strcmp(name, "thread"));

name = pdbg_target_dn_name(target);
assert(!strncmp(name, "thread", 6));
}
}

static void testVariousTargets()
{
testFsiTarget();
testPibTarget();
testCoreTarget();
testThreadTarget();
}

int main(void)
{
/**
* First set the backend to FAKE and inits the targets
* then test for various target counts for each class
* and other stuffs
*/
assert(pdbg_set_backend(PDBG_BACKEND_FAKE, NULL));
assert(pdbg_targets_init(NULL));
{
struct pdbg_target* root = pdbg_target_root();
testSetOne(root, false);
testVariousTargets();

/**
* Now releases the device tree and check for root
* invalidation and subsequent tests
*/
{
pdbg_release_dt_root();
root = pdbg_target_root();
testSetOne(root, true);
}
}
/**
* Now again set the backend to FAKE one and inits the targets.
* Testing against the same set of conditions now should work as
* it did for earlier. Logic is if there in any leftover targets
* or target classes after releasing of device tree then the counts
* will be mismatched for the conditions set for a fresh device tree
*/
assert(pdbg_set_backend(PDBG_BACKEND_FAKE, NULL));
assert(pdbg_targets_init(NULL));
{
struct pdbg_target* root = pdbg_target_root();
testSetOne(root, false);
testVariousTargets();
}

return 0;
}

0 comments on commit 35a7c10

Please sign in to comment.