Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp971055rwb; Thu, 10 Nov 2022 09:31:14 -0800 (PST) X-Google-Smtp-Source: AMsMyM5HCwxb1XyauV0a/W71uSmgWvhI2Gw7ehulID91HjHu10NQgJn545/Ley9EWD/nVzxb5RxQ X-Received: by 2002:a17:90a:7e10:b0:213:9e81:87e2 with SMTP id i16-20020a17090a7e1000b002139e8187e2mr69663315pjl.1.1668101474642; Thu, 10 Nov 2022 09:31:14 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668101474; cv=none; d=google.com; s=arc-20160816; b=PleHYbuZcd6T9RPmd0G3YJpqZL2BIZv/gttsM74rLzP9EsBv2rW2g51I8cXet1X2Pv MkY59KgCkDLe/ZSqnFXMT3UE434n+4KDtNQRk0EO/zW/ZSYXYWUFOj63idNHSpaR+03U 5heAgKiwnfnC911IRCMLEkwVB5UGB1uee6try6ybRmlORfbIsblWRz+dH6ZEXDiHSZvM 8pmPZ5D7mg0qsOL2tL+QpF6hafJR+jKTC+9L7BkIo3OXXcfPKXM/qjYvA8I5fmo4oROH z4PDdvUSkIzfidmLEK2xOk1TjNJJNTKtF6ztAcZ7Occuq2RiZpml25e3QTzNEBuk5kFA PVxQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=rqIoOiosdNVnt6bctFzhXBJCZtOw+pblXMRa9R1VfP4=; b=mkQD1CMyBB73zTsvHjdpf3B0NeYSC1ZF5/jQiGZEUX1HqJhPlf5zFMUpAXPP/DGqBf PSA9oAYIi09sh2bSqwKSrGoq6JqYF2WxBhHxSq+2eUrI5lgPeGWixFwfJ5VZSKhCNlLM 8E0XkzOGArVHqCVdgyob/vSZPC0dZX5oulWJdL257Ldvi/zm+O2NoKF8rRdl/VKVlbwW TGtm1k7Bf2bp5T87+d2+6tHarU5qYuhD6QhqQIc6hMep+gW/h6khEA/qBpV+CpGfBuKw OpIZxXQE5n6QggJsWSESEjojewC+hhzQArqEXbaVze8mD/A2Mys0OjBaigjmo27T8Mqv 6cwA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=q9PPYY1R; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id a11-20020a056a000c8b00b005632b7b5e41si22926644pfv.136.2022.11.10.09.30.59; Thu, 10 Nov 2022 09:31:14 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=q9PPYY1R; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232527AbiKJQoW (ORCPT + 92 others); Thu, 10 Nov 2022 11:44:22 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40950 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232384AbiKJQni (ORCPT ); Thu, 10 Nov 2022 11:43:38 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5C91C45090; Thu, 10 Nov 2022 08:43:13 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id ECA5261BD9; Thu, 10 Nov 2022 16:43:12 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2831AC433B5; Thu, 10 Nov 2022 16:43:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1668098592; bh=McON9F/ZloZh668lpPUE38B16LOD0pkq/6vZiAe96vI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=q9PPYY1RDgJiOgY/j1c3gLqlW1NCi+Y11qnDMQHXS+tqtbOuG/Ny56LlRF7+NwNnB 9Y/lhcdYdOiv+uiVlkW9HWFhyfxJIlmxwlJmDK+2V8AGbt5TdWRtZXumE1qkOGtsdb tawjy5DCiUrEGcCnVjRUSgyL3zFL7l+yTA8i/wjzG4CAM7H5tJMMi6Mkph182Nyf9w C5zxzTSteoJtEOtbPiPSDC7NCl1TJ/O/ucRBFtii1TWmL0YtDpsXuNeTg5h9InIMyg y//I7SkZSGS9P304qTlJroPkvhPixeHCVtQ5A1JUpZCOX9v5FxNVUsIR0YqHFCtsxI +VfABN/suQgVw== From: Miguel Ojeda To: Miguel Ojeda , Wedson Almeida Filho , Alex Gaynor , Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev, Milan Landaverde Subject: [PATCH v1 18/28] rust: str: add `CStr` unit tests Date: Thu, 10 Nov 2022 17:41:30 +0100 Message-Id: <20221110164152.26136-19-ojeda@kernel.org> In-Reply-To: <20221110164152.26136-1-ojeda@kernel.org> References: <20221110164152.26136-1-ojeda@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Milan Landaverde Add unit tests for `CStr::from_bytes_with_nul()` and `CStr::from_bytes_with_nul_unchecked()`. These serve as an example of the first unit tests for Rust code (i.e. different from documentation tests). Signed-off-by: Milan Landaverde [Reworded, adapted for upstream and applied latest changes] Signed-off-by: Miguel Ojeda --- rust/kernel/str.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs index 11d297c1a61c..3ed685cb5a3c 100644 --- a/rust/kernel/str.rs +++ b/rust/kernel/str.rs @@ -321,6 +321,35 @@ where } } +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_cstr_to_str() { + let good_bytes = b"\xf0\x9f\xa6\x80\0"; + let checked_cstr = CStr::from_bytes_with_nul(good_bytes).unwrap(); + let checked_str = checked_cstr.to_str().unwrap(); + assert_eq!(checked_str, "????"); + } + + #[test] + #[should_panic] + fn test_cstr_to_str_panic() { + let bad_bytes = b"\xc3\x28\0"; + let checked_cstr = CStr::from_bytes_with_nul(bad_bytes).unwrap(); + checked_cstr.to_str().unwrap(); + } + + #[test] + fn test_cstr_as_str_unchecked() { + let good_bytes = b"\xf0\x9f\x90\xA7\0"; + let checked_cstr = CStr::from_bytes_with_nul(good_bytes).unwrap(); + let unchecked_str = unsafe { checked_cstr.as_str_unchecked() }; + assert_eq!(unchecked_str, "????"); + } +} + /// Allows formatting of [`fmt::Arguments`] into a raw buffer. /// /// It does not fail if callers write past the end of the buffer so that they can calculate the -- 2.38.1