Path: blob/main/lib/libc/tests/nss/getusershell_test.c
39530 views
/*-1* Copyright (c) 2006 Michael Bushkov <[email protected]>2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12*13* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND14* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE15* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE16* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE17* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS19* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)20* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT21* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY22* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF23* SUCH DAMAGE.24*25*/2627#include <assert.h>28#include <errno.h>29#include <stdio.h>30#include <stdlib.h>31#include <string.h>32#include <unistd.h>3334#include <atf-c.h>3536#include "testutil.h"3738enum test_methods {39TEST_GETUSERSHELL,40TEST_BUILD_SNAPSHOT41};4243struct usershell {44char *path;45};4647DECLARE_TEST_DATA(usershell)48DECLARE_TEST_FILE_SNAPSHOT(usershell)49DECLARE_2PASS_TEST(usershell)5051static void clone_usershell(struct usershell *, struct usershell const *);52static int compare_usershell(struct usershell *, struct usershell *, void *);53static void free_usershell(struct usershell *);5455static void sdump_usershell(struct usershell *, char *, size_t);56static void dump_usershell(struct usershell *);5758IMPLEMENT_TEST_DATA(usershell)59IMPLEMENT_TEST_FILE_SNAPSHOT(usershell)60IMPLEMENT_2PASS_TEST(usershell)6162static void63clone_usershell(struct usershell *dest, struct usershell const *src)64{65assert(dest != NULL);66assert(src != NULL);6768if (src->path != NULL) {69dest->path = strdup(src->path);70assert(dest->path != NULL);71}72}7374static int75compare_usershell(struct usershell *us1, struct usershell *us2,76void *mdata __unused)77{78int rv;7980assert(us1 != NULL);81assert(us2 != NULL);8283dump_usershell(us1);84dump_usershell(us2);8586if (us1 == us2)87return (0);8889rv = strcmp(us1->path, us2->path);90if (rv != 0) {91printf("following structures are not equal:\n");92dump_usershell(us1);93dump_usershell(us2);94}9596return (rv);97}9899static void100free_usershell(struct usershell *us)101{102free(us->path);103}104105static void106sdump_usershell(struct usershell *us, char *buffer, size_t buflen)107{108snprintf(buffer, buflen, "%s", us->path);109}110111static void112dump_usershell(struct usershell *us)113{114if (us != NULL) {115char buffer[2048];116sdump_usershell(us, buffer, sizeof(buffer));117printf("%s\n", buffer);118} else119printf("(null)\n");120}121122static int123usershell_read_snapshot_func(struct usershell *us, char *line)124{125126us->path = strdup(line);127ATF_REQUIRE(us->path != NULL);128129return (0);130}131132static int133run_tests(const char *snapshot_file, enum test_methods method)134{135struct usershell_test_data td, td_snap;136struct usershell ushell;137int rv;138139rv = 0;140141TEST_DATA_INIT(usershell, &td, clone_usershell, free_usershell);142TEST_DATA_INIT(usershell, &td_snap, clone_usershell, free_usershell);143144setusershell();145while ((ushell.path = getusershell()) != NULL) {146printf("usershell found:\n");147dump_usershell(&ushell);148TEST_DATA_APPEND(usershell, &td, &ushell);149}150endusershell();151152if (snapshot_file != NULL) {153if (access(snapshot_file, W_OK | R_OK) != 0) {154if (errno == ENOENT)155method = TEST_BUILD_SNAPSHOT;156else {157printf("can't access the snapshot file %s\n",158snapshot_file);159160rv = -1;161goto fin;162}163} else {164rv = TEST_SNAPSHOT_FILE_READ(usershell, snapshot_file,165&td_snap, usershell_read_snapshot_func);166if (rv != 0) {167printf("error reading snapshot file\n");168goto fin;169}170}171}172173switch (method) {174case TEST_GETUSERSHELL:175rv = DO_2PASS_TEST(usershell, &td, &td_snap,176compare_usershell, NULL);177break;178case TEST_BUILD_SNAPSHOT:179if (snapshot_file != NULL) {180rv = TEST_SNAPSHOT_FILE_WRITE(usershell, snapshot_file,181&td, sdump_usershell);182}183break;184default:185rv = 0;186break;187}188189fin:190TEST_DATA_DESTROY(usershell, &td_snap);191TEST_DATA_DESTROY(usershell, &td);192193return (rv);194}195196#define SNAPSHOT_FILE "snapshot_usershell"197198ATF_TC_WITHOUT_HEAD(getusershell_with_snapshot);199ATF_TC_BODY(getusershell_with_snapshot, tc)200{201202ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_BUILD_SNAPSHOT) == 0);203}204205ATF_TC_WITHOUT_HEAD(getusershell_with_two_pass);206ATF_TC_BODY(getusershell_with_two_pass, tc)207{208209ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_BUILD_SNAPSHOT) == 0);210ATF_REQUIRE(run_tests(SNAPSHOT_FILE, TEST_GETUSERSHELL) == 0);211}212213ATF_TP_ADD_TCS(tp)214{215216ATF_TP_ADD_TC(tp, getusershell_with_snapshot);217ATF_TP_ADD_TC(tp, getusershell_with_two_pass);218219return (atf_no_error());220}221222223