Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:34

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /*
0004  * Copyright (c) 2014 embedded brains GmbH & Co. KG
0005  *
0006  * Redistribution and use in source and binary forms, with or without
0007  * modification, are permitted provided that the following conditions
0008  * are met:
0009  * 1. Redistributions of source code must retain the above copyright
0010  *    notice, this list of conditions and the following disclaimer.
0011  * 2. Redistributions in binary form must reproduce the above copyright
0012  *    notice, this list of conditions and the following disclaimer in the
0013  *    documentation and/or other materials provided with the distribution.
0014  *
0015  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0016  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0017  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0018  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0019  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0020  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0021  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0022  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0023  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0024  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0025  * POSSIBILITY OF SUCH DAMAGE.
0026  */
0027 
0028 #ifdef HAVE_CONFIG_H
0029 #include "config.h"
0030 #endif
0031 
0032 #include <sys/stat.h>
0033 #include <sys/types.h>
0034 #include <errno.h>
0035 #include <grp.h>
0036 #include <pwd.h>
0037 #include <stdio.h>
0038 #include <unistd.h>
0039 
0040 #include "tmacros.h"
0041 
0042 const char rtems_test_name[] = "PWDGRP 1";
0043 
0044 static void create_file(const char *name, const char *content)
0045 {
0046   FILE *fp;
0047   int rv;
0048 
0049   fp = fopen(name, "wx");
0050   rtems_test_assert(fp != NULL);
0051 
0052   rv = fputs(content, fp);
0053   rtems_test_assert(rv == 0);
0054 
0055   rv = fclose(fp);
0056   rtems_test_assert(rv == 0);
0057 }
0058 
0059 static void assert_pwd(struct passwd *pwd)
0060 {
0061   rtems_test_assert(strcmp(pwd->pw_name, "moop") == 0);
0062   rtems_test_assert(strcmp(pwd->pw_passwd, "foo") == 0);
0063   rtems_test_assert(pwd->pw_uid == 1);
0064   rtems_test_assert(pwd->pw_gid == 3);
0065   rtems_test_assert(strcmp(pwd->pw_comment, "blob") == 0);
0066   rtems_test_assert(strcmp(pwd->pw_gecos, "a") == 0);
0067   rtems_test_assert(strcmp(pwd->pw_dir, "b") == 0);
0068   rtems_test_assert(strcmp(pwd->pw_shell, "c") == 0);
0069 }
0070 
0071 static void assert_grp(struct group *grp)
0072 {
0073   rtems_test_assert(strcmp(grp->gr_name, "blub") == 0);
0074   rtems_test_assert(strcmp(grp->gr_passwd, "bar") == 0);
0075   rtems_test_assert(grp->gr_gid == 3);
0076   rtems_test_assert(strcmp(grp->gr_mem[0], "moop") == 0);
0077   rtems_test_assert(grp->gr_mem[1] == NULL);
0078 }
0079 
0080 static void test(void)
0081 {
0082   int rv;
0083   struct passwd pwd;
0084   struct group grp;
0085   struct passwd *pwd_res;
0086   struct group *grp_res;
0087   char buf[256];
0088   gid_t grps[5];
0089 
0090   rv = mkdir("/etc", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
0091   rtems_test_assert(rv == 0);
0092 
0093   create_file(
0094     "/etc/passwd",
0095     "moop:foo:1:3:blob:a:b:c\n"
0096   );
0097 
0098   create_file(
0099     "/etc/group",
0100     "A::1:moop,u,v,w\n"
0101     "B::2:moop\n"
0102     "blub:bar:3:moop\n"
0103     "C::4:l,m,n,moop\n"
0104     "D::5:moop,moop\n"
0105     "E::6:x\n"
0106     "E::7:y,z\n"
0107     "F::8:s,moop,t\n"
0108   );
0109 
0110   memset(&pwd, 0xff, sizeof(pwd));
0111   rv = getpwnam_r("moop", &pwd, &buf[0], sizeof(buf), &pwd_res);
0112   rtems_test_assert(rv == 0);
0113   rtems_test_assert(&pwd == pwd_res);
0114   assert_pwd(pwd_res);
0115 
0116   memset(&pwd, 0xff, sizeof(pwd));
0117   rv = getpwuid_r(1, &pwd, &buf[0], sizeof(buf), &pwd_res);
0118   rtems_test_assert(rv == 0);
0119   rtems_test_assert(&pwd == pwd_res);
0120   assert_pwd(pwd_res);
0121 
0122   memset(&grp, 0xff, sizeof(grp));
0123   rv = getgrnam_r("blub", &grp, &buf[0], sizeof(buf), &grp_res);
0124   rtems_test_assert(rv == 0);
0125   rtems_test_assert(&grp == grp_res);
0126   assert_grp(grp_res);
0127 
0128   memset(&grp, 0xff, sizeof(grp));
0129   rv = getgrgid_r(3, &grp, &buf[0], sizeof(buf), &grp_res);
0130   rtems_test_assert(rv == 0);
0131   rtems_test_assert(&grp == grp_res);
0132   assert_grp(grp_res);
0133 
0134   rv = setuid(0);
0135   rtems_test_assert(rv == 0);
0136 
0137   errno = 0;
0138   rv = getgroups(0, NULL);
0139   rtems_test_assert(rv == -1);
0140   rtems_test_assert(errno == EINVAL);
0141 
0142   rv = setuid(1);
0143   rtems_test_assert(rv == 0);
0144 
0145   rv = getgroups(0, NULL);
0146   rtems_test_assert(rv == 5);
0147 
0148   errno = 0;
0149   rv = getgroups(1, &grps[0]);
0150   rtems_test_assert(rv == -1);
0151   rtems_test_assert(errno == EINVAL);
0152 
0153   memset(&grps[0], 0xff, sizeof(grps));
0154   rv = getgroups(5, &grps[0]);
0155   rtems_test_assert(rv == 5);
0156   rtems_test_assert(grps[0] == 1);
0157   rtems_test_assert(grps[1] == 2);
0158   rtems_test_assert(grps[2] == 4);
0159   rtems_test_assert(grps[3] == 5);
0160   rtems_test_assert(grps[4] == 8);
0161 }
0162 
0163 static void Init(rtems_task_argument arg)
0164 {
0165   TEST_BEGIN();
0166 
0167   test();
0168 
0169   TEST_END();
0170   rtems_test_exit(0);
0171 }
0172 
0173 #define CONFIGURE_APPLICATION_DOES_NOT_NEED_CLOCK_DRIVER
0174 #define CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER
0175 
0176 #define CONFIGURE_MAXIMUM_FILE_DESCRIPTORS 4
0177 
0178 #define CONFIGURE_MAXIMUM_TASKS 1
0179 #define CONFIGURE_MAXIMUM_POSIX_KEYS 1
0180 #define CONFIGURE_MAXIMUM_POSIX_KEY_VALUE_PAIRS 1
0181 
0182 #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
0183 
0184 #define CONFIGURE_RTEMS_INIT_TASKS_TABLE
0185 
0186 #define CONFIGURE_INIT
0187 
0188 #include <rtems/confdefs.h>