From patchwork Tue Aug 5 02:12:03 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laszlo Ersek X-Patchwork-Id: 34887 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-oi0-f71.google.com (mail-oi0-f71.google.com [209.85.218.71]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 9E9CE20F2E for ; Tue, 5 Aug 2014 02:12:35 +0000 (UTC) Received: by mail-oi0-f71.google.com with SMTP id e131sf1161982oig.10 for ; Mon, 04 Aug 2014 19:12:35 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:delivered-to:from:to:date:message-id:in-reply-to :references:subject:precedence:reply-to:list-id:list-unsubscribe :list-archive:list-post:list-help:list-subscribe:mime-version :errors-to:x-original-sender:x-original-authentication-results :mailing-list:content-type:content-transfer-encoding; bh=BXl+5aFyaXvmorlb/8durV7/DpkDnXM8pBNDOdRoV9U=; b=axvf3bVnhZkgwab8/t0D0xJRGeDQxWj7r4wsfy/2X2O+nhfEIIIaZoYbMXGyTUFL6K xX0lS03lsUS/QQCTyzXS3IGQrawlIG61+sh4fURcavMZfjFylTLdrj4xb4738LruijTt y73nluWY3Xzmi/LhYW0ZrcMHksen2d8NG9XVrKDRe2pWQsw76QwB9Ovaf1VDLdaIQ5Mt 4GL1sPE0+E2S0/lOXJ3p4N8NvfGv16zUTQmFP2qtVLjJm+Q2P2AmLTYJCnxTR9pqBRMm aaGjbarbrEOGdSbOyP6OnSR/ykC8nESZq/RxMGirL3UQCJKYPaNqBw/6v9YYLVk9eSHb ptpg== X-Gm-Message-State: ALoCoQloiyNIX9jXZ/9TK+fWU1N4OozXdai0L0UdKrg7SUgBentGzDuKTN/0DUcZx6r/Ty08lLPs X-Received: by 10.42.16.69 with SMTP id o5mr957211ica.17.1407204755196; Mon, 04 Aug 2014 19:12:35 -0700 (PDT) X-BeenThere: patchwork-forward@linaro.org Received: by 10.140.81.169 with SMTP id f38ls104946qgd.24.gmail; Mon, 04 Aug 2014 19:12:35 -0700 (PDT) X-Received: by 10.220.114.5 with SMTP id c5mr641925vcq.28.1407204755037; Mon, 04 Aug 2014 19:12:35 -0700 (PDT) Received: from mail-vc0-f172.google.com (mail-vc0-f172.google.com [209.85.220.172]) by mx.google.com with ESMTPS id c10si196891vel.91.2014.08.04.19.12.35 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Mon, 04 Aug 2014 19:12:35 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.220.172 as permitted sender) client-ip=209.85.220.172; Received: by mail-vc0-f172.google.com with SMTP id im17so460297vcb.17 for ; Mon, 04 Aug 2014 19:12:34 -0700 (PDT) X-Received: by 10.220.122.132 with SMTP id l4mr502984vcr.41.1407204754933; Mon, 04 Aug 2014 19:12:34 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.221.37.5 with SMTP id tc5csp348425vcb; Mon, 4 Aug 2014 19:12:33 -0700 (PDT) X-Received: by 10.50.73.133 with SMTP id l5mr2145579igv.18.1407204753575; Mon, 04 Aug 2014 19:12:33 -0700 (PDT) Received: from lists.sourceforge.net (lists.sourceforge.net. [216.34.181.88]) by mx.google.com with ESMTPS id ow3si690767icc.61.2014.08.04.19.12.33 for (version=TLSv1 cipher=RC4-SHA bits=128/128); Mon, 04 Aug 2014 19:12:33 -0700 (PDT) Received-SPF: pass (google.com: domain of edk2-devel-bounces@lists.sourceforge.net designates 216.34.181.88 as permitted sender) client-ip=216.34.181.88; Received: from localhost ([127.0.0.1] helo=sfs-ml-4.v29.ch3.sourceforge.com) by sfs-ml-4.v29.ch3.sourceforge.com with esmtp (Exim 4.76) (envelope-from ) id 1XEUEf-0007JA-JY; Tue, 05 Aug 2014 02:12:21 +0000 Received: from sog-mx-3.v43.ch3.sourceforge.com ([172.29.43.193] helo=mx.sourceforge.net) by sfs-ml-4.v29.ch3.sourceforge.com with esmtp (Exim 4.76) (envelope-from ) id 1XEUEd-0007J5-QP for edk2-devel@lists.sourceforge.net; Tue, 05 Aug 2014 02:12:19 +0000 Received-SPF: pass (sog-mx-3.v43.ch3.sourceforge.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=lersek@redhat.com; helo=mx1.redhat.com; Received: from mx1.redhat.com ([209.132.183.28]) by sog-mx-3.v43.ch3.sourceforge.com with esmtps (TLSv1:AES256-SHA:256) (Exim 4.76) id 1XEUEb-0003Pz-7Q for edk2-devel@lists.sourceforge.net; Tue, 05 Aug 2014 02:12:19 +0000 Received: from int-mx13.intmail.prod.int.phx2.redhat.com (int-mx13.intmail.prod.int.phx2.redhat.com [10.5.11.26]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id s752CBch032277 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Mon, 4 Aug 2014 22:12:11 -0400 Received: from lacos-laptop-7.usersys.redhat.com (ovpn-116-31.ams2.redhat.com [10.36.116.31]) by int-mx13.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id s752C5In005360 for ; Mon, 4 Aug 2014 22:12:10 -0400 From: Laszlo Ersek To: edk2-devel@lists.sourceforge.net Date: Tue, 5 Aug 2014 04:12:03 +0200 Message-Id: <1407204723-16642-4-git-send-email-lersek@redhat.com> In-Reply-To: <1407204723-16642-1-git-send-email-lersek@redhat.com> References: <1407204723-16642-1-git-send-email-lersek@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.26 X-Spam-Score: -2.2 (--) X-Spam-Report: Spam Filtering performed by mx.sourceforge.net. See http://spamassassin.org/tag/ for more details. -1.5 SPF_CHECK_PASS SPF reports sender host as permitted sender for sender-domain -0.0 SPF_HELO_PASS SPF: HELO matches SPF record -0.0 SPF_PASS SPF: sender matches SPF record -0.7 RP_MATCHES_RCVD Envelope sender domain matches handover relay domain X-Headers-End: 1XEUEb-0003Pz-7Q Subject: [edk2] [PATCH v2 3/3] AppPkg: introduce OrderedCollectionTest X-BeenThere: edk2-devel@lists.sourceforge.net X-Mailman-Version: 2.1.9 Precedence: list Reply-To: edk2-devel@lists.sourceforge.net List-Id: List-Unsubscribe: , List-Archive: List-Post: , List-Help: , List-Subscribe: , MIME-Version: 1.0 Errors-To: edk2-devel-bounces@lists.sourceforge.net X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: lersek@redhat.com X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.220.172 as permitted sender) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 In this patch a small application is added to AppPkg, with the following two goals: - demonstrate how to use OrderedCollectionLib, - allow users to test and "fuzz" BaseOrderedCollectionRedBlackTreeLib in particular, entering API "commands" interactively, or providing them from a script file. A shell script is included that generates such an API command script. Speaking about BaseOrderedCollectionRedBlackTreeLib specifically, OrderedCollectionTest validates the internal red-black properties of the tree by calling the OrderedCollectionValidate() function after each read-write operation. The OrderedCollectionTest application's debugging environment is strictly specified in the DSC file, because OrderedCollectionTest is entirely useless for unit testing without full ASSERT() enablement. The OrderedCollectionTest application deliberately doesn't follow the edk2 coding style in the following: - const vs. CONST, - void vs. VOID, - assert() vs. ASSERT(), - calloc() and free() vs. AllocateZeroPool() and FreePool(), - integer types. This is because OrderedCollectionTest is a standard C application, not a UEFI application per se. In particular it relies on stdio. INTN, EFIAPI and CONST VOID are used only in two places, where we provide the comparator callbacks to OrderedCollectionLib. Proper range checking is ensured for integers. The application takes command input from stdin or a file (if the user requests it), sends command output to stdout or a file (if the user requests it), prints debug output to the console (as other AppPkg applications do when debugging is enabled for them), and prints diagnostics to stderr (like well behaved standard C programs should). Input/output selection is implemented manually because the old shell doesn't support input redirection at all, and because the new shell's input redirection does not co-operate with fgets() for the time being. Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Laszlo Ersek --- Notes: v2: - adapt to renamed interfaces [Mike] - adapt to EFI_* --> RETURN_* [Mike] - adapt to renamed Init() / Uninit() allocation [Mike+Laszlo] - root node access disappeared from the interface (not justifiable for collections in general and not important for rbtrees), drop dependent TearDown() function and replace it with a CmdForwardEmpty() call [Mike+Laszlo] .../OrderedCollectionTest.inf | 42 ++ .../OrderedCollectionTest/OrderedCollectionTest.c | 660 +++++++++++++++++++++ AppPkg/AppPkg.dsc | 12 + .../Applications/OrderedCollectionTest/gentest.sh | 86 +++ AppPkg/ReadMe.txt | 6 + 5 files changed, 806 insertions(+) create mode 100644 AppPkg/Applications/OrderedCollectionTest/OrderedCollectionTest.inf create mode 100644 AppPkg/Applications/OrderedCollectionTest/OrderedCollectionTest.c create mode 100644 AppPkg/Applications/OrderedCollectionTest/gentest.sh diff --git a/AppPkg/Applications/OrderedCollectionTest/OrderedCollectionTest.inf b/AppPkg/Applications/OrderedCollectionTest/OrderedCollectionTest.inf new file mode 100644 index 0000000..5eb71fe --- /dev/null +++ b/AppPkg/Applications/OrderedCollectionTest/OrderedCollectionTest.inf @@ -0,0 +1,42 @@ +## @file +# A simple "fuzzer" application for OrderedCollectionLib, reading commands +# from the standard input, and writing results to the standard output. +# +# Copyright (C) 2014, Red Hat, Inc. +# Copyright (c) 2010 - 2011, Intel Corporation. All rights reserved.
+# +# This program and the accompanying materials are licensed and made available +# under the terms and conditions of the BSD License which accompanies this +# distribution. The full text of the license may be found at +# http://opensource.org/licenses/bsd-license. +# +# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, +# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR +# IMPLIED. +## + +[Defines] + INF_VERSION = 0x00010006 + BASE_NAME = OrderedCollectionTest + FILE_GUID = E6C7EBB7-1604-4FCB-8F87-B3A6F48730AE + MODULE_TYPE = UEFI_APPLICATION + VERSION_STRING = 0.1 + ENTRY_POINT = ShellCEntryLib + +# +# VALID_ARCHITECTURES = IA32 X64 IPF +# + +[Sources] + OrderedCollectionTest.c + +[Packages] + StdLib/StdLib.dec + MdePkg/MdePkg.dec + ShellPkg/ShellPkg.dec + +[LibraryClasses] + LibC + LibStdio + DevShell + OrderedCollectionLib diff --git a/AppPkg/Applications/OrderedCollectionTest/OrderedCollectionTest.c b/AppPkg/Applications/OrderedCollectionTest/OrderedCollectionTest.c new file mode 100644 index 0000000..6bc18c2 --- /dev/null +++ b/AppPkg/Applications/OrderedCollectionTest/OrderedCollectionTest.c @@ -0,0 +1,660 @@ +/** @file + A simple "fuzzer" application for OrderedCollectionLib, reading commands from + the standard input, and writing results to the standard output. + + Make sure you configure your platform so that the console stderr device is + visible to the user (or else run the program from wherever stderr is visible + per default, eg. serial line). + + Copyright (C) 2014, Red Hat, Inc. + Copyright (c) 2010 - 2011, Intel Corporation. All rights reserved.
+ + This program and the accompanying materials are licensed and made available + under the terms and conditions of the BSD License which accompanies this + distribution. The full text of the license may be found at + http://opensource.org/licenses/bsd-license. + + THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT + WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. +**/ + +#include // assert() +#include // errno +#include // INT_MIN +#include // fgets() +#include // EXIT_FAILURE +#include // strerror() +#include // getopt() + +#include + +// +// We allow the user to select between stdin+stdout and regular input+output +// files via command line options. We don't rely on shell redirection for two +// reasons: +// +// - The "old shell" doesn't support input redirection (0 If StandaloneKey compares greater than UserStruct's key. +**/ +static +INTN +EFIAPI +KeyCompare ( + IN CONST VOID *StandaloneKey, + IN CONST VOID *UserStruct + ) +{ + const USER_KEY *CmpKey; + const USER_STRUCT *CmpStruct; + + CmpKey = StandaloneKey; + CmpStruct = UserStruct; + + return CmpKey->Value < CmpStruct->Key.Value ? -1 : + CmpKey->Value > CmpStruct->Key.Value ? 1 : + 0; +} + + +/** + Comparator function type for two user structures. + + @param[in] UserStruct1 Pointer to the first user structure. + + @param[in] UserStruct2 Pointer to the second user structure. + + @retval <0 If UserStruct1 compares less than UserStruct2. + + @retval 0 If UserStruct1 compares equal to UserStruct2. + + @retval >0 If UserStruct1 compares greater than UserStruct2. +**/ +static +INTN +EFIAPI +UserStructCompare ( + IN CONST VOID *UserStruct1, + IN CONST VOID *UserStruct2 + ) +{ + const USER_STRUCT *CmpStruct1; + + CmpStruct1 = UserStruct1; + return KeyCompare (&CmpStruct1->Key, UserStruct2); +} + + +/** + Empty the collection by iterating forward through its entries. + + This function demonstrates that iterators different from the one being + removed remain valid. + + @param[in,out] Collection The collection to empty. +**/ +static void +CmdForwardEmpty ( + IN OUT ORDERED_COLLECTION *Collection + ) +{ + ORDERED_COLLECTION_ENTRY *Entry; + + Entry = OrderedCollectionMin (Collection); + while (Entry != NULL) { + ORDERED_COLLECTION_ENTRY *Next; + void *Ptr; + USER_STRUCT *UserStruct; + + Next = OrderedCollectionNext (Entry); + OrderedCollectionDelete (Collection, Entry, &Ptr); + OrderedCollectionValidate (Collection); + + UserStruct = Ptr; + fprintf (Output, "%s: %d: removed\n", __FUNCTION__, UserStruct->Key.Value); + free (UserStruct); + + Entry = Next; + } +} + + +/** + Empty the collection by iterating backward through its entries. + + This function demonstrates that iterators different from the one being + removed remain valid. + + @param[in,out] Collection The collection to empty. +**/ +static void +CmdBackwardEmpty ( + IN OUT ORDERED_COLLECTION *Collection + ) +{ + ORDERED_COLLECTION_ENTRY *Entry; + + Entry = OrderedCollectionMax (Collection); + while (Entry != NULL) { + ORDERED_COLLECTION_ENTRY *Prev; + void *Ptr; + USER_STRUCT *UserStruct; + + Prev = OrderedCollectionPrev (Entry); + OrderedCollectionDelete (Collection, Entry, &Ptr); + OrderedCollectionValidate (Collection); + + UserStruct = Ptr; + fprintf (Output, "%s: %d: removed\n", __FUNCTION__, UserStruct->Key.Value); + free (UserStruct); + + Entry = Prev; + } +} + + +/** + List the user structures linked into the collection, in increasing order. + + @param[in] Collection The collection to list. +**/ +static void +CmdForwardList ( + IN ORDERED_COLLECTION *Collection + ) +{ + ORDERED_COLLECTION_ENTRY *Entry; + + for (Entry = OrderedCollectionMin (Collection); Entry != NULL; + Entry = OrderedCollectionNext (Entry)) { + USER_STRUCT *UserStruct; + + UserStruct = OrderedCollectionUserStruct (Entry); + fprintf (Output, "%s: %d\n", __FUNCTION__, UserStruct->Key.Value); + } +} + + +/** + List the user structures linked into the collection, in decreasing order. + + @param[in] Collection The collection to list. +**/ +static void +CmdBackwardList ( + IN ORDERED_COLLECTION *Collection + ) +{ + ORDERED_COLLECTION_ENTRY *Entry; + + for (Entry = OrderedCollectionMax (Collection); Entry != NULL; + Entry = OrderedCollectionPrev (Entry)) { + USER_STRUCT *UserStruct; + + UserStruct = OrderedCollectionUserStruct (Entry); + fprintf (Output, "%s: %d\n", __FUNCTION__, UserStruct->Key.Value); + } +} + + +/** + Create a new user structure and attempt to insert it into the collection. + + @param[in] Value The key value of the user structure to create. + + @param[in,out] Collection The collection to insert the new user structure + into. +**/ +static void +CmdInsert ( + IN int Value, + IN OUT ORDERED_COLLECTION *Collection + ) +{ + USER_STRUCT *UserStruct, *UserStruct2; + RETURN_STATUS Status; + ORDERED_COLLECTION_ENTRY *Entry; + + UserStruct = calloc (1, sizeof *UserStruct); + if (UserStruct == NULL) { + fprintf (Output, "%s: %d: calloc(): out of memory\n", __FUNCTION__, Value); + return; + } + + UserStruct->Key.Value = Value; + Status = OrderedCollectionInsert (Collection, &Entry, UserStruct); + OrderedCollectionValidate (Collection); + + switch (Status) { + case RETURN_OUT_OF_RESOURCES: + fprintf (Output, "%s: %d: OrderedCollectionInsert(): out of memory\n", + __FUNCTION__, Value); + goto ReleaseUserStruct; + + case RETURN_ALREADY_STARTED: + UserStruct2 = OrderedCollectionUserStruct (Entry); + assert (UserStruct != UserStruct2); + assert (UserStruct2->Key.Value == Value); + fprintf (Output, "%s: %d: already exists\n", __FUNCTION__, + UserStruct2->Key.Value); + goto ReleaseUserStruct; + + default: + assert (Status == RETURN_SUCCESS); + break; + } + + assert (OrderedCollectionUserStruct (Entry) == UserStruct); + fprintf (Output, "%s: %d: inserted\n", __FUNCTION__, Value); + return; + +ReleaseUserStruct: + free (UserStruct); +} + + +/** + Look up a user structure by key in the collection and print it. + + @param[in] Value The key of the user structure to find. + + @param[in] Collection The collection to search for the user structure with + the key. +**/ +static void +CmdFind ( + IN int Value, + IN ORDERED_COLLECTION *Collection + ) +{ + USER_KEY StandaloneKey; + ORDERED_COLLECTION_ENTRY *Entry; + USER_STRUCT *UserStruct; + + StandaloneKey.Value = Value; + Entry = OrderedCollectionFind (Collection, &StandaloneKey); + + if (Entry == NULL) { + fprintf (Output, "%s: %d: not found\n", __FUNCTION__, Value); + return; + } + + UserStruct = OrderedCollectionUserStruct (Entry); + assert (UserStruct->Key.Value == StandaloneKey.Value); + fprintf (Output, "%s: %d: found\n", __FUNCTION__, UserStruct->Key.Value); +} + + +/** + Look up a user structure by key in the collection and delete it. + + @param[in] Value The key of the user structure to find. + + @param[in] Collection The collection to search for the user structure with + the key. +**/ +static void +CmdDelete ( + IN int Value, + IN ORDERED_COLLECTION *Collection + ) +{ + USER_KEY StandaloneKey; + ORDERED_COLLECTION_ENTRY *Entry; + void *Ptr; + USER_STRUCT *UserStruct; + + StandaloneKey.Value = Value; + Entry = OrderedCollectionFind (Collection, &StandaloneKey); + + if (Entry == NULL) { + fprintf (Output, "%s: %d: not found\n", __FUNCTION__, Value); + return; + } + + OrderedCollectionDelete (Collection, Entry, &Ptr); + OrderedCollectionValidate (Collection); + + UserStruct = Ptr; + assert (UserStruct->Key.Value == StandaloneKey.Value); + fprintf (Output, "%s: %d: removed\n", __FUNCTION__, UserStruct->Key.Value); + free (UserStruct); +} + + +typedef struct { + const char *Command; + void (*Function) (ORDERED_COLLECTION *Collection); + const char *Description; +} KEYLESS_COMMAND; + +typedef struct { + const char *Command; + void (*Function) (int Value, ORDERED_COLLECTION *Collection); + const char *Description; +} KEYED_COMMAND; + +static const KEYLESS_COMMAND KeylessCommands[] = { + { "forward-empty", CmdForwardEmpty, + "empty the collection iterating forward" }, + { "fe", CmdForwardEmpty, + "shorthand for forward-empty" }, + { "backward-empty", CmdBackwardEmpty, + "empty the collection iterating backward" }, + { "be", CmdBackwardEmpty, + "shorthand for backward-empty" }, + { "forward-list", CmdForwardList, + "list contents, iterating forward" }, + { "fl", CmdForwardList, + "shorthand for forward-list" }, + { "backward-list", CmdBackwardList, + "list contents, iterating backward" }, + { "bl", CmdBackwardList, + "shorthand for backward-list" }, + { NULL } +}; + +static const KEYED_COMMAND KeyedCommands[] = { + { "insert ", CmdInsert, "insert value into collection" }, + { "i ", CmdInsert, "shorthand for insert" }, + { "find ", CmdFind, "find value in collection" }, + { "f ", CmdFind, "shorthand for find" }, + { "delete ", CmdDelete, "delete value from collection" }, + { "d ", CmdDelete, "shorthand for delete" }, + { NULL } +}; + + +/** + List the supported commands on stderr. +**/ +static void +ListCommands ( + void + ) +{ + const KEYLESS_COMMAND *KeylessCmd; + const KEYED_COMMAND *KeyedCmd; + + fprintf (stderr, "Supported commands:\n\n"); + for (KeylessCmd = KeylessCommands; KeylessCmd->Command != NULL; + ++KeylessCmd) { + fprintf (stderr, "%-14s: %s\n", KeylessCmd->Command, + KeylessCmd->Description); + } + for (KeyedCmd = KeyedCommands; KeyedCmd->Command != NULL; ++KeyedCmd) { + fprintf (stderr, "%-9s: %s\n", KeyedCmd->Command, + KeyedCmd->Description); + } +} + + +/** + Configure stdio FILEs that we'll use for input and output. + + @param[in] ArgC The number of elements in ArgV, from main(). The environment + is required to ensure ArgC >= 1 (ie. that the program name, + ArgV[0], is available). + + @param[in] ArgV Command line argument list, from main(). +**/ +static void +SetupInputOutput ( + IN int ArgC, + IN char **ArgV + ) +{ + char *InputName, *OutputName; + int Loop; + + assert (ArgC >= 1); + + InputName = NULL; + OutputName = NULL; + Loop = 1; + + while (Loop) { + switch (getopt (ArgC, ArgV, ":i:o:h")) { + case 'i': + InputName = optarg; + break; + + case 'o': + OutputName = optarg; + break; + + case 'h': + fprintf (stderr, + "%1$s: simple OrderedCollectionLib tester\n" + "\n" + "Usage: 1. %1$s [-i InputFile] [-o OutputFile]\n" + " 2. %1$s -h\n" + "\n" + "Options:\n" + " -i InputFile : read commands from InputFile\n" + " (will read from stdin if absent)\n" + " -o OutputFile: write command responses to OutputFile\n" + " (will write to stdout if absent)\n" + " -h : print this help and exit\n" + "\n", ArgV[0]); + ListCommands (); + exit (EXIT_SUCCESS); + +// +// The current "compatibility" getopt() implementation doesn't support optopt, +// but it gracefully degrades these branches to the others (one of the optarg +// ones or the excess operands one). +// +#if 0 + case ':': + fprintf (stderr, "%s: option -%c requires an argument; pass -h for " + "help\n", ArgV[0], optopt); + exit (EXIT_FAILURE); + + case '?': + fprintf (stderr, "%s: unknown option -%c; pass -h for help\n", ArgV[0], + optopt); + exit (EXIT_FAILURE); +#endif + + case -1: + if (optind != ArgC) { + fprintf (stderr, "%s: excess operands on command line; pass -h for " + "help\n", ArgV[0]); + exit (EXIT_FAILURE); + } + Loop = 0; + break; + + default: + assert (0); + } + } + + if (InputName == NULL) { + Input = stdin; + } else { + Input = fopen (InputName, "r"); + if (Input == NULL) { + fprintf (stderr, "%s: fopen(\"%s\", \"r\"): %s\n", ArgV[0], InputName, + strerror (errno)); + exit (EXIT_FAILURE); + } + } + + if (OutputName == NULL) { + Output = stdout; + } else { + Output = fopen (OutputName, "w"); + if (Output == NULL) { + fprintf (stderr, "%s: fopen(\"%s\", \"w\"): %s\n", ArgV[0], OutputName, + strerror (errno)); + exit (EXIT_FAILURE); + } + } + + // + // When reading commands from the standard input, assume interactive mode, + // and list the supported commands. However, delay this until both streams + // are set up. + // + if (InputName == NULL) { + ListCommands (); + } +} + + +int +main ( + IN int ArgC, + IN char **ArgV + ) +{ + int RetVal; + ORDERED_COLLECTION *Collection; + char Line[256]; + + SetupInputOutput (ArgC, ArgV); + + Collection = OrderedCollectionInit (UserStructCompare, KeyCompare); + if (Collection == NULL) { + fprintf (stderr, "%s: OrderedCollectionInit(): out of memory\n", + __FUNCTION__); + return EXIT_FAILURE; + } + OrderedCollectionValidate (Collection); + + RetVal = EXIT_SUCCESS; + while (fgets (Line, sizeof Line, Input) != NULL) { + size_t Length; + const KEYLESS_COMMAND *KeylessCmd; + const KEYED_COMMAND *KeyedCmd; + + Length = strlen (Line); + assert (Length > 0); + if (Line[Length - 1] != '\n') { + fprintf (stderr, "%s: overlong line\n", __FUNCTION__); + RetVal = EXIT_FAILURE; + break; + } + + // + // Strip [\r]\n. + // + Line[Length - 1] = '\0'; + if (Length >= 2 && Line[Length - 2] == '\r') { + Line[Length - 2] = '\0'; + } + // + // Ignore empty lines and comments. + // + if (Line[0] == '\0' || Line[0] == '#') { + if (Input != stdin) { + // + // ... but echo them back in non-interactive mode. + // + fprintf (Output, "%s\n", Line); + } + continue; + } + + // + // Ironically, this is the kind of loop that should be replaced with an + // ORDERED_COLLECTION. + // + for (KeylessCmd = KeylessCommands; KeylessCmd->Command != NULL; + ++KeylessCmd) { + if (strcmp (KeylessCmd->Command, Line) == 0) { + KeylessCmd->Function (Collection); + break; + } + } + if (KeylessCmd->Command != NULL) { + continue; + } + + for (KeyedCmd = KeyedCommands; KeyedCmd->Command != NULL; ++KeyedCmd) { + size_t CmdLength; + + CmdLength = strlen (KeyedCmd->Command); + assert (CmdLength >= 2); + if (strncmp (KeyedCmd->Command, Line, CmdLength) == 0) { + char *CommandArg, *EndPtr; + long Value; + + CommandArg = Line + CmdLength; + errno = 0; + Value = strtol (CommandArg, &EndPtr, 10); + if (EndPtr == CommandArg || // no conversion performed + errno != 0 || // not in long's range, etc + *EndPtr != '\0' || // final string not empty + Value < INT_MIN || Value > INT_MAX // parsed long not in int range + ) { + fprintf (stderr, "%s: %.*s: \"%s\": not an int\n", __FUNCTION__, + (int)(CmdLength - 1), Line, CommandArg); + } else { + KeyedCmd->Function (Value, Collection); + } + + break; + } + } + if (KeyedCmd->Command != NULL) { + continue; + } + + fprintf (stderr, "%s: \"%s\": unknown command\n", __FUNCTION__, Line); + } + + if (RetVal == EXIT_SUCCESS && ferror (Input)) { + fprintf (stderr, "%s: fgets(): %s\n", __FUNCTION__, strerror (errno)); + RetVal = EXIT_FAILURE; + } + + CmdForwardEmpty (Collection); + OrderedCollectionUninit (Collection); + return RetVal; +} diff --git a/AppPkg/AppPkg.dsc b/AppPkg/AppPkg.dsc index d0aac2c..381484d 100644 --- a/AppPkg/AppPkg.dsc +++ b/AppPkg/AppPkg.dsc @@ -112,6 +112,18 @@ AppPkg/Applications/Main/Main.inf # Simple invocation. No other LibC functions. AppPkg/Applications/Enquire/Enquire.inf # +#### A simple fuzzer for OrderedCollectionLib, in particular for +#### BaseOrderedCollectionRedBlackTreeLib. + AppPkg/Applications/OrderedCollectionTest/OrderedCollectionTest.inf { + + OrderedCollectionLib|MdePkg/Library/BaseOrderedCollectionRedBlackTreeLib/BaseOrderedCollectionRedBlackTreeLib.inf + DebugLib|MdePkg/Library/UefiDebugLibConOut/UefiDebugLibConOut.inf + DebugPrintErrorLevelLib|MdePkg/Library/BaseDebugPrintErrorLevelLib/BaseDebugPrintErrorLevelLib.inf + + gEfiMdePkgTokenSpaceGuid.PcdDebugPropertyMask|0x2F + gEfiMdePkgTokenSpaceGuid.PcdDebugPrintErrorLevel|0x80400040 + } + #### After extracting the Python distribution, un-comment the following line to build Python. # AppPkg/Applications/Python/PythonCore.inf diff --git a/AppPkg/Applications/OrderedCollectionTest/gentest.sh b/AppPkg/Applications/OrderedCollectionTest/gentest.sh new file mode 100644 index 0000000..535faa6 --- /dev/null +++ b/AppPkg/Applications/OrderedCollectionTest/gentest.sh @@ -0,0 +1,86 @@ +## @file +# Small test script generator for OrderedCollectionTest. +# +# Usage: +# - generate script: sh gentest.sh >input.txt +# - run script with tester: OrderedCollectionTest -i input.txt -o output.txt +# +# Copyright (C) 2014, Red Hat, Inc. +# +# This program and the accompanying materials are licensed and made available +# under the terms and conditions of the BSD License which accompanies this +# distribution. The full text of the license may be found at +# http://opensource.org/licenses/bsd-license. +# +# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, +# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR +# IMPLIED. +## + +set -e -u -C + +RANGE_START=0 +RANGE_STOP=9999 + +gen_rnd_insert() +{ + shuf --input-range=$RANGE_START-$RANGE_STOP | sed 's/^/insert /' +} + +gen_rnd_delete() +{ + shuf --input-range=$RANGE_START-$RANGE_STOP | sed 's/^/delete /' +} + +gen_mon_inc_insert() +{ + seq $RANGE_START $RANGE_STOP | sed 's/^/insert /' +} + +gen_mon_inc_delete() +{ + seq $RANGE_START $RANGE_STOP | sed 's/^/delete /' +} + +gen_mon_dec_delete() +{ + seq $RANGE_START $RANGE_STOP | tac | sed 's/^/delete /' +} + +{ + echo '# populate the tree in random order and empty it iterating forward' + gen_rnd_insert + echo forward-empty + + echo + echo '# populate the tree in random order and empty it iterating backward' + gen_rnd_insert + echo backward-empty + + echo + echo '# populate the tree in random order, list it in increasing and' + echo '# decreasing order, then empty it in random order' + gen_rnd_insert + echo forward-list + echo backward-list + gen_rnd_delete + + echo + echo '# populate the tree in monotonically increasing order, then undo it' + echo '# piecewise in the same order' + gen_mon_inc_insert + gen_mon_inc_delete + + echo + echo '# populate the tree in monotonically increasing order, then undo it' + echo '# piecewise in reverse order' + gen_mon_inc_insert + gen_mon_dec_delete + + echo + echo '# populate the tree randomly, trigger a run of collisions, then exit' + echo '# and let CmdForwardEmpty() empty the tree' + gen_rnd_insert + gen_mon_inc_insert +} \ +| unix2dos diff --git a/AppPkg/ReadMe.txt b/AppPkg/ReadMe.txt index 35e3b6a..3d41588 100644 --- a/AppPkg/ReadMe.txt +++ b/AppPkg/ReadMe.txt @@ -48,6 +48,12 @@ The EADK is comprised of three packages: See the PythonReadMe.txt file, in the Python directory, for information on configuring and building Python. + OrderedCollectionTest A small Standard C Library application that + demonstrates the use of the OrderedCollectionLib library class + (provided by the BaseOrderedCollectionRedBlackTreeLib library + instance in this application), and allows the user to "fuzz" the + library with interactive or scripted API calls. + Sockets A collection of applications demonstrating use of the EDK II Socket Libraries. These applications include: