Received: by 2002:ac0:a582:0:0:0:0:0 with SMTP id m2-v6csp14061imm; Tue, 16 Oct 2018 16:54:51 -0700 (PDT) X-Google-Smtp-Source: ACcGV624PNaxYrTp4au+5iaWs3N9DtpvhRMN7iDKgJLJKfQUPtABYASIx8J1cQcwl4Hawpqaiyb/ X-Received: by 2002:a63:dd0a:: with SMTP id t10-v6mr22186954pgg.379.1539734091154; Tue, 16 Oct 2018 16:54:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1539734091; cv=none; d=google.com; s=arc-20160816; b=0ywSOOJdhrp9i0wrWbJZz0fqRqW+L2AOOdEFUUOg8dwZwDJuy/4j835Ea3snCsJrB+ V4Lozw2koaBqoUMu7WJM4vlP5X/ThAa3hcjpSRzOmMS1lWFvqcYDzedqMrlwk3ZfHw4g gzwkZ8CPhYEjTzwqRDZ1fHebNech/McvZHCBTRR3QObjHUkKKXqLR+YUMq01aXn1v7Y8 wxgp/WBfTM7c+0lhdxCeP8Z6E9jFPulVYi3bNhRl9Scu8GS7GVYIK0hGGk40Cjg4K86i T5Q7RAC1zVAZioRWg2t8shOro2wgzfmbUmwp68a5ARRNkKUPSCDCjuUh6B74OyCVlBKD wRyw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:from:subject:references :mime-version:message-id:in-reply-to:date:dkim-signature; bh=dJsydBHJBHH2EKQc0vg9/d0tw0KtYquVOLVEuVx4f9A=; b=ZbuLYWlJOLGXM9BMjGwi+uuA/IOx93KInFNRy1ms1N6kUMbGLrao5uq4SYyMeRP9t5 E3Wfzrc93smqTuH9YAFn3cOj44N2UHOy3uU0tc4bhRyWWRKtZsheW3/4YU+jloZfI6iF 9AyvyVFGBghOMWxU7ERjyc6RufJuONlyIydie+o/JLdbTJa8ee0z+A8HxeM6PxxmakvC PCvEFjGEuk9tMRU+5zLVEYLN9IRqokH8H8yxru7GvDlum2TLcikjtEJxR2FbfqjFBBS8 fryRJ+p8tagdBY+s9YAneRChKHhPwBpRDZ6G3rfYR//fdJVoXnJ1+IEQMqexE5VWZCtm 3a+g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=ahWh4KkX; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id z5-v6si15136790pgf.561.2018.10.16.16.54.31; Tue, 16 Oct 2018 16:54:51 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=ahWh4KkX; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727359AbeJQHqi (ORCPT + 99 others); Wed, 17 Oct 2018 03:46:38 -0400 Received: from mail-ua1-f73.google.com ([209.85.222.73]:50310 "EHLO mail-ua1-f73.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727097AbeJQHqi (ORCPT ); Wed, 17 Oct 2018 03:46:38 -0400 Received: by mail-ua1-f73.google.com with SMTP id q3so2183140uao.17 for ; Tue, 16 Oct 2018 16:53:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=dJsydBHJBHH2EKQc0vg9/d0tw0KtYquVOLVEuVx4f9A=; b=ahWh4KkXQtuYcu7CO92XtCDzry1BTlRTtfQGrykZ/KsFGJmgJP3gWTjYVgnB29L8UT zH2nf/kBBSG8sHFtkQ0p0w9BAv0hrT22lG8JE+8GmvdZpyht/muIo6uPGBcn65PgzXjL kCxfT1XMDyBk40JbCU398KycI5ege89JsGYgonlK6Se6Ft+sAWAfr5os86cHPj0eaOZ0 ArIw8I9+9FHExxXdTRgQ1quIFT8XYxcAIvegKY/SSYBI05kJroegTnvc9bMsj/oticYS tZjaj1PpuSmIK8ZR6um2MRsMO6HeeUCAI+WIwHT2+yfjYELcLW3FGRTB8qKweqIx2f11 gCSA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=dJsydBHJBHH2EKQc0vg9/d0tw0KtYquVOLVEuVx4f9A=; b=TrlWF4lc+tT4wI3u7ZJmoQoM2JMVQBZNIESC2q8iftFASpUi2/ASfjnIi06060RLIs s5wwPE4GJeHlJnSEGv8UJbcPe3LbZAoPa9QxW5M64cxSeBkFz2aJWk3CeBF2u4UiHtem 99CXWyRbqCACt1Qy1t+WiuhA98jwjFCBRS1U0TVFKYj7oZnLVbR5x+LsQ6EDrXfQI2wH QvnqRrBtaRInrk3oVGXo8tZZP3xgGXkF6trJLlsRhe1ar/4pjNPqnq95z5VK8g77XPiI IkRHymLn8bRh/oGRSaBaEyFpSjee9qNRGE/ngdjFQBkShRQY6NgAsvYlht5Ga3I43tww sXWA== X-Gm-Message-State: ABuFfojV2mxqYkULXIHkrmyI5pWvHkG+jH3zVOIobnzd4rmiqI/lia33 l+68JSSO52o/78QNdYbXSsJQkwoAi6Zh1AuVJjbJSw== X-Received: by 2002:a1f:3250:: with SMTP id y77mr10272981vky.4.1539734028287; Tue, 16 Oct 2018 16:53:48 -0700 (PDT) Date: Tue, 16 Oct 2018 16:50:50 -0700 In-Reply-To: <20181016235120.138227-1-brendanhiggins@google.com> Message-Id: <20181016235120.138227-2-brendanhiggins@google.com> Mime-Version: 1.0 References: <20181016235120.138227-1-brendanhiggins@google.com> X-Mailer: git-send-email 2.19.1.331.ge82ca0e54c-goog Subject: [RFC v1 01/31] kunit: test: added string_stream a std::stream like string builder From: Brendan Higgins To: gregkh@linuxfoundation.org, keescook@google.com, mcgrof@kernel.org, shuah@kernel.org Cc: joel@jms.id.au, mpe@ellerman.id.au, joe@perches.com, brakmo@fb.com, rostedt@goodmis.org, Tim.Bird@sony.com, khilman@baylibre.com, julia.lawall@lip6.fr, linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com, linux-kernel@vger.kernel.org, jdike@addtoit.com, richard@nod.at, linux-um@lists.infradead.org, Brendan Higgins Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. This provides a library for constructing the string as you go similar to C++'s std::string. Signed-off-by: Brendan Higgins --- include/kunit/string-stream.h | 44 ++++++++++ kunit/Kconfig | 16 ++++ kunit/Makefile | 1 + kunit/string-stream.c | 149 ++++++++++++++++++++++++++++++++++ 4 files changed, 210 insertions(+) create mode 100644 include/kunit/string-stream.h create mode 100644 kunit/Kconfig create mode 100644 kunit/Makefile create mode 100644 kunit/string-stream.c diff --git a/include/kunit/string-stream.h b/include/kunit/string-stream.h new file mode 100644 index 0000000000000..933ed5740cf07 --- /dev/null +++ b/include/kunit/string-stream.h @@ -0,0 +1,44 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * C++ stream style string builder used in KUnit for building messages. + * + * Copyright (C) 2018, Google LLC. + * Author: Brendan Higgins + */ + +#ifndef _KUNIT_STRING_STREAM_H +#define _KUNIT_STRING_STREAM_H + +#include +#include +#include +#include + +struct string_stream_fragment { + struct list_head node; + char *fragment; +}; + +struct string_stream { + size_t length; + struct list_head fragments; + + /* length and fragments are protected by this lock */ + spinlock_t lock; + struct kref refcount; + int (*add)(struct string_stream *this, const char *fmt, ...); + int (*vadd)(struct string_stream *this, const char *fmt, va_list args); + char *(*get_string)(struct string_stream *this); + void (*clear)(struct string_stream *this); + bool (*is_empty)(struct string_stream *this); +}; + +struct string_stream *new_string_stream(void); + +void destroy_string_stream(struct string_stream *stream); + +void string_stream_get(struct string_stream *stream); + +int string_stream_put(struct string_stream *stream); + +#endif /* _KUNIT_STRING_STREAM_H */ diff --git a/kunit/Kconfig b/kunit/Kconfig new file mode 100644 index 0000000000000..64480092b2c24 --- /dev/null +++ b/kunit/Kconfig @@ -0,0 +1,16 @@ +# +# KUnit base configuration +# + +menu "KUnit support" + +config KUNIT + bool "Enable support for unit tests (KUnit)" + help + Enables support for kernel unit tests (KUnit), a lightweight unit + testing and mocking framework for the Linux kernel. These tests are + able to be run locally on a developer's workstation without a VM or + special hardware. For more information, please see + Documentation/kunit/ + +endmenu diff --git a/kunit/Makefile b/kunit/Makefile new file mode 100644 index 0000000000000..de16cce9d6a27 --- /dev/null +++ b/kunit/Makefile @@ -0,0 +1 @@ +obj-$(CONFIG_KUNIT) += string-stream.o diff --git a/kunit/string-stream.c b/kunit/string-stream.c new file mode 100644 index 0000000000000..1e7efa630cc35 --- /dev/null +++ b/kunit/string-stream.c @@ -0,0 +1,149 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * C++ stream style string builder used in KUnit for building messages. + * + * Copyright (C) 2018, Google LLC. + * Author: Brendan Higgins + */ + +#include +#include +#include + +static int string_stream_vadd(struct string_stream *this, + const char *fmt, + va_list args) +{ + struct string_stream_fragment *fragment; + int len; + va_list args_for_counting; + unsigned long flags; + + /* Make a copy because `vsnprintf` could change it */ + va_copy(args_for_counting, args); + + /* Need space for null byte. */ + len = vsnprintf(NULL, 0, fmt, args_for_counting) + 1; + + va_end(args_for_counting); + + fragment = kmalloc(sizeof(*fragment), GFP_KERNEL); + if (!fragment) + return -ENOMEM; + + fragment->fragment = kmalloc(len, GFP_KERNEL); + if (!fragment->fragment) { + kfree(fragment); + return -ENOMEM; + } + + len = vsnprintf(fragment->fragment, len, fmt, args); + spin_lock_irqsave(&this->lock, flags); + this->length += len; + list_add_tail(&fragment->node, &this->fragments); + spin_unlock_irqrestore(&this->lock, flags); + return 0; +} + +static int string_stream_add(struct string_stream *this, const char *fmt, ...) +{ + va_list args; + int result; + + va_start(args, fmt); + result = string_stream_vadd(this, fmt, args); + va_end(args); + return result; +} + +static void string_stream_clear(struct string_stream *this) +{ + struct string_stream_fragment *fragment, *fragment_safe; + unsigned long flags; + + spin_lock_irqsave(&this->lock, flags); + list_for_each_entry_safe(fragment, + fragment_safe, + &this->fragments, + node) { + list_del(&fragment->node); + kfree(fragment->fragment); + kfree(fragment); + } + this->length = 0; + spin_unlock_irqrestore(&this->lock, flags); +} + +static char *string_stream_get_string(struct string_stream *this) +{ + struct string_stream_fragment *fragment; + size_t buf_len = this->length + 1; /* +1 for null byte. */ + char *buf; + unsigned long flags; + + buf = kzalloc(buf_len, GFP_KERNEL); + if (!buf) + return NULL; + + spin_lock_irqsave(&this->lock, flags); + list_for_each_entry(fragment, &this->fragments, node) + strlcat(buf, fragment->fragment, buf_len); + spin_unlock_irqrestore(&this->lock, flags); + + return buf; +} + +static bool string_stream_is_empty(struct string_stream *this) +{ + bool is_empty; + unsigned long flags; + + spin_lock_irqsave(&this->lock, flags); + is_empty = list_empty(&this->fragments); + spin_unlock_irqrestore(&this->lock, flags); + + return is_empty; +} + +void destroy_string_stream(struct string_stream *stream) +{ + stream->clear(stream); + kfree(stream); +} + +static void string_stream_destroy(struct kref *kref) +{ + struct string_stream *stream = container_of(kref, + struct string_stream, + refcount); + destroy_string_stream(stream); +} + +struct string_stream *new_string_stream(void) +{ + struct string_stream *stream = kzalloc(sizeof(*stream), GFP_KERNEL); + + if (!stream) + return NULL; + + INIT_LIST_HEAD(&stream->fragments); + spin_lock_init(&stream->lock); + kref_init(&stream->refcount); + stream->add = string_stream_add; + stream->vadd = string_stream_vadd; + stream->get_string = string_stream_get_string; + stream->clear = string_stream_clear; + stream->is_empty = string_stream_is_empty; + return stream; +} + +void string_stream_get(struct string_stream *stream) +{ + kref_get(&stream->refcount); +} + +int string_stream_put(struct string_stream *stream) +{ + return kref_put(&stream->refcount, &string_stream_destroy); +} + -- 2.19.1.331.ge82ca0e54c-goog