Commit Graph

14 Commits

Author SHA1 Message Date
Nico Pache bbc5d2a151 kunit: string-stream: Allow ERR_PTR to be passed to string_stream_destroy()
commit 15bf0000147ae9fc8fa4969025f71848fc558cba
Author: Richard Fitzgerald <rf@opensource.cirrus.com>
Date:   Mon Oct 30 10:47:46 2023 +0000

    kunit: string-stream: Allow ERR_PTR to be passed to string_stream_destroy()

    Check the stream pointer passed to string_stream_destroy() for
    IS_ERR_OR_NULL() instead of only NULL.

    Whatever alloc_string_stream() returns should be safe to pass
    to string_stream_destroy(), and that will be an ERR_PTR.

    It's obviously good practise and generally helpful to also check
    for NULL pointers so that client cleanup code can call
    string_stream_destroy() unconditionally - which could include
    pointers that have never been set to anything and so are NULL.

    Signed-off-by: Richard Fitzgerald <rf@opensource.cirrus.com>
    Reviewed-by: Rae Moar <rmoar@google.com>
    Reviewed-by: David Gow <davidgow@google.com>
    Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>

JIRA: https://issues.redhat.com/browse/RHEL-39303
Signed-off-by: Nico Pache <npache@redhat.com>
2024-07-31 20:32:27 -06:00
Nico Pache 89c2324c15 kunit: string-stream: Add tests for freeing resource-managed string_stream
commit d1a0d699bfc00ae5b5e74bb640d791a93e825b68
Author: Richard Fitzgerald <rf@opensource.cirrus.com>
Date:   Mon Aug 28 11:41:09 2023 +0100

    kunit: string-stream: Add tests for freeing resource-managed string_stream

    string_stream_managed_free_test() allocates a resource-managed
    string_stream and tests that kunit_free_string_stream() calls
    string_stream_destroy().

    string_stream_resource_free_test() allocates a resource-managed
    string_stream and tests that string_stream_destroy() is called
    when the test resources are cleaned up.

    The old string_stream_init_test() has been split into two tests,
    one for kunit_alloc_string_stream() and the other for
    alloc_string_stream().

    Signed-off-by: Richard Fitzgerald <rf@opensource.cirrus.com>
    Reviewed-by: David Gow <davidgow@google.com>
    Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>

JIRA: https://issues.redhat.com/browse/RHEL-39303
Signed-off-by: Nico Pache <npache@redhat.com>
2024-07-31 20:27:47 -06:00
Nico Pache 2a517badae kunit: string-stream: Decouple string_stream from kunit
commit a3fdf784780ccb0008d630e8722d1389c49c7499
Author: Richard Fitzgerald <rf@opensource.cirrus.com>
Date:   Mon Aug 28 11:41:08 2023 +0100

    kunit: string-stream: Decouple string_stream from kunit

    Re-work string_stream so that it is not tied to a struct kunit. This is
    to allow using it for the log of struct kunit_suite.

    Instead of resource-managing individual allocations the whole string_stream
    can be resource-managed, if required.

        alloc_string_stream() now allocates a string stream that is
        not resource-managed.

        string_stream_destroy() now works on an unmanaged string_stream
        allocated by alloc_string_stream() and frees the entire
        string_stream (previously it only freed the fragments).

        string_stream_clear() has been made public for callers that
        want to free the fragments without destroying the string_stream.

    For resource-managed allocations use kunit_alloc_string_stream()
    and kunit_free_string_stream().

    In addition to this, string_stream_get_string() now returns an
    unmanaged buffer that the caller must kfree().

    Signed-off-by: Richard Fitzgerald <rf@opensource.cirrus.com>
    Reviewed-by: David Gow <davidgow@google.com>
    Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>

JIRA: https://issues.redhat.com/browse/RHEL-39303
Signed-off-by: Nico Pache <npache@redhat.com>
2024-07-31 20:27:47 -06:00
Nico Pache d0c53e56b4 kunit: string-stream: Add kunit_alloc_string_stream()
commit 20631e154c78f4140fffe111f5c79464fae3c38c
Author: Richard Fitzgerald <rf@opensource.cirrus.com>
Date:   Mon Aug 28 11:41:07 2023 +0100

    kunit: string-stream: Add kunit_alloc_string_stream()

    Add function kunit_alloc_string_stream() to do a resource-managed
    allocation of a string stream, and corresponding
    kunit_free_string_stream() to free the resource-managed stream.

    This is preparing for decoupling the string_stream
    implementation from struct kunit, to reduce the amount of code
    churn when that happens. Currently:
     - kunit_alloc_string_stream() only calls alloc_string_stream().
     - kunit_free_string_stream() takes a struct kunit* which
       isn't used yet.

    Callers of the old alloc_string_stream() and
    string_stream_destroy() are all requesting a managed allocation
    so have been changed to use the new functions.

    alloc_string_stream() has been temporarily made static because
    its current behavior has been replaced with
    kunit_alloc_string_stream().

    Signed-off-by: Richard Fitzgerald <rf@opensource.cirrus.com>
    Reviewed-by: David Gow <davidgow@google.com>
    Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>

JIRA: https://issues.redhat.com/browse/RHEL-39303
Signed-off-by: Nico Pache <npache@redhat.com>
2024-07-31 20:27:47 -06:00
Nico Pache 47a3d0786d kunit: string-stream: Add option to make all lines end with newline
commit a5abe7b201779b0000f1e8ab522e5c6fc0c413bf
Author: Richard Fitzgerald <rf@opensource.cirrus.com>
Date:   Mon Aug 28 11:41:04 2023 +0100

    kunit: string-stream: Add option to make all lines end with newline

    Add an optional feature to string_stream that will append a newline to
    any added string that does not already end with a newline. The purpose
    of this is so that string_stream can be used to collect log lines.

    This is enabled/disabled by calling string_stream_set_append_newlines().

    Signed-off-by: Richard Fitzgerald <rf@opensource.cirrus.com>
    Reviewed-by: Rae Moar <rmoar@google.com>
    Reviewed-by: David Gow <davidgow@google.com>
    Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>

JIRA: https://issues.redhat.com/browse/RHEL-39303
Signed-off-by: Nico Pache <npache@redhat.com>
2024-07-31 20:27:47 -06:00
Nico Pache 7e16976648 kunit: string-stream: Don't create a fragment for empty strings
commit 5c54c9ebb1f4758a4da7731a809148ff1a3a1844
Author: Richard Fitzgerald <rf@opensource.cirrus.com>
Date:   Mon Aug 28 11:41:02 2023 +0100

    kunit: string-stream: Don't create a fragment for empty strings

    If the result of the formatted string is an empty string just return
    instead of creating an empty fragment.

    Signed-off-by: Richard Fitzgerald <rf@opensource.cirrus.com>
    Reviewed-by: Rae Moar <rmoar@google.com>
    Reviewed-by: David Gow <davidgow@google.com>
    Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>

JIRA: https://issues.redhat.com/browse/RHEL-39303
Signed-off-by: Nico Pache <npache@redhat.com>
2024-07-31 20:27:47 -06:00
Nico Pache e81196b656 kunit: remove unused structure definition
commit 8f8b51f7d5c8bd3a89e7ea87aed2cdaa52ca5ba4
Author: YoungJun.park <her0gyugyu@gmail.com>
Date:   Mon Oct 24 18:59:46 2022 -0700

    kunit: remove unused structure definition

    remove unused string_stream_alloc_context structure definition.

    Signed-off-by: YoungJun.park <her0gyugyu@gmail.com>
    Reviewed-by: David Gow <davidgow@google.com>
    Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>

JIRA: https://issues.redhat.com/browse/RHEL-5618
Signed-off-by: Nico Pache <npache@redhat.com>
2023-11-02 15:26:54 -06:00
Nico Pache 7d8dd25512 kunit: drop test pointer in string_stream_fragment
commit 4db4598b5ed8fc26f5fd9312623a9ec5cebbe74a
Author: Daniel Latypov <dlatypov@google.com>
Date:   Fri Jul 22 17:15:31 2022 +0000

    kunit: drop test pointer in string_stream_fragment

    We already store the `struct kunit *test` in the string_stream object
    itself, so we need don't need to store a copy of this pointer in every
    fragment in the stream.

    Drop it, getting string_stream_fragment down the bare minimum: a
    list_head and the `char *` with the actual fragment.

    Signed-off-by: Daniel Latypov <dlatypov@google.com>
    Reviewed-by: David Gow <davidgow@google.com>
    Reviewed-by: Brendan Higgins <brendanhiggins@google.com>
    Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>

JIRA: https://issues.redhat.com/browse/RHEL-5618
Signed-off-by: Nico Pache <npache@redhat.com>
2023-11-02 15:26:54 -06:00
Nico Pache fa599e7acd kunit: alloc_string_stream_fragment error handling bug fix
commit 93ef83050e597634d2c7dc838a28caf5137b9404
Author: YoungJun.park <her0gyugyu@gmail.com>
Date:   Fri Oct 28 07:42:41 2022 -0700

    kunit: alloc_string_stream_fragment error handling bug fix

    When it fails to allocate fragment, it does not free and return error.
    And check the pointer inappropriately.

    Fixed merge conflicts with
    commit 618887768bb7 ("kunit: update NULL vs IS_ERR() tests")
    Shuah Khan <skhan@linuxfoundation.org>

    Signed-off-by: YoungJun.park <her0gyugyu@gmail.com>
    Reviewed-by: David Gow <davidgow@google.com>
    Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>

JIRA: https://issues.redhat.com/browse/RHEL-5618
Signed-off-by: Nico Pache <npache@redhat.com>
2023-11-02 15:26:54 -06:00
Nico Pache 5c9ca317d9 kunit: update NULL vs IS_ERR() tests
commit 618887768bb71f0a475334fa5a4fba7dc98d7ab5
Author: Dan Carpenter <dan.carpenter@oracle.com>
Date:   Fri Oct 14 12:37:25 2022 +0300

    kunit: update NULL vs IS_ERR() tests

    The alloc_string_stream() functions were changed from returning NULL on
    error to returning error pointers so these caller needs to be updated
    as well.

    Fixes: 78b1c6584fce ("kunit: string-stream: Simplify resource use")
    Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
    Reviewed-by: Daniel Latypov <dlatypov@google.com>
    Reviewed-by: David Gow <davidgow@google.com>
    Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>

JIRA: https://issues.redhat.com/browse/RHEL-5618
Signed-off-by: Nico Pache <npache@redhat.com>
2023-11-02 15:26:54 -06:00
Nico Pache dba9a6413b kunit: string-stream: Simplify resource use
commit 78b1c6584fcedcf2d9687a4455c461859094cf04
Author: David Gow <davidgow@google.com>
Date:   Fri Jul 22 17:15:30 2022 +0000

    kunit: string-stream: Simplify resource use

    Currently, KUnit's string streams are themselves "KUnit resources".
    This is redundant since the stream itself is already allocated with
    kunit_kzalloc() and will thus be freed automatically at the end of the
    test.

    string-stream is only used internally within KUnit, and isn't using the
    extra features that resources provide like reference counting, being
    able to locate them dynamically as "test-local variables", etc.

    Indeed, the resource's refcount is never incremented when the
    pointer is returned. The fact that it's always manually destroyed is
    more evidence that the reference counting is unused.

    Signed-off-by: David Gow <davidgow@google.com>
    Signed-off-by: Daniel Latypov <dlatypov@google.com>
    Reviewed-by: Brendan Higgins <brendanhiggins@google.com>
    Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>

JIRA: https://issues.redhat.com/browse/RHEL-5618
Signed-off-by: Nico Pache <npache@redhat.com>
2023-11-02 15:26:53 -06:00
Alan Maguire d4cdd146d0 kunit: generalize kunit_resource API beyond allocated resources
In its original form, the kunit resources API - consisting the
struct kunit_resource and associated functions - was focused on
adding allocated resources during test operation that would be
automatically cleaned up on test completion.

The recent RFC patch proposing converting KASAN tests to KUnit [1]
showed another potential model - where outside of test context,
but with a pointer to the test state, we wish to access/update
test-related data, but expressly want to avoid allocations.

It turns out we can generalize the kunit_resource to support
static resources where the struct kunit_resource * is passed
in and initialized for us. As part of this work, we also
change the "allocation" field to the more general "data" name,
as instead of associating an allocation, we can associate a
pointer to static data.  Static data is distinguished by a NULL
free functions.  A test is added to cover using kunit_add_resource()
with a static resource and data.

Finally we also make use of the kernel's krefcount interfaces
to manage reference counting of KUnit resources.  The motivation
for this is simple; if we have kernel threads accessing and
using resources (say via kunit_find_resource()) we need to
ensure we do not remove said resources (or indeed free them
if they were dynamically allocated) until the reference count
reaches zero.  A new function - kunit_put_resource() - is
added to handle this, and it should be called after a
thread using kunit_find_resource() is finished with the
retrieved resource.

We ensure that the functions needed to look up, use and
drop reference count are "static inline"-defined so that
they can be used by builtin code as well as modules in
the case that KUnit is built as a module.

A cosmetic change here also; I've tried moving to
kunit_[action]_resource() as the format of function names
for consistency and readability.

[1] https://lkml.org/lkml/2020/2/26/1286

Signed-off-by: Alan Maguire <alan.maguire@oracle.com>
Reviewed-by: Brendan Higgins <brendanhiggins@google.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
2020-06-26 14:12:00 -06:00
Alan Maguire 109fb06fdc kunit: move string-stream.h to lib/kunit
string-stream interfaces are not intended for external use;
move them from include/kunit to lib/kunit accordingly.

Co-developed-by: Knut Omang <knut.omang@oracle.com>
Signed-off-by: Knut Omang <knut.omang@oracle.com>
Signed-off-by: Alan Maguire <alan.maguire@oracle.com>
Reviewed-by: Brendan Higgins <brendanhiggins@google.com>
Tested-by: Brendan Higgins <brendanhiggins@google.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
2020-01-09 16:41:05 -07:00
Brendan Higgins d1fadef194 kunit: test: add string_stream a std::stream like string builder
A number of test features need to do pretty complicated string printing
where it may not be possible to rely on a single preallocated string
with parameters.

So provide a library for constructing the string as you go similar to
C++'s std::string. string_stream is really just a string builder,
nothing more.

Signed-off-by: Brendan Higgins <brendanhiggins@google.com>
Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Reviewed-by: Logan Gunthorpe <logang@deltatee.com>
Reviewed-by: Stephen Boyd <swboyd@chromium.org>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
2019-09-30 17:35:00 -06:00