source: rtems/testsuites/libtests/pwdgrp01/init.c @ 6935428

4.115
Last change on this file since 6935428 was 6935428, checked in by Sebastian Huber <sebastian.huber@…>, on 11/14/14 at 10:30:50

libcsupport: Avoid TOCTOU and format errors

  • Property mode set to 100644
File size: 3.1 KB
Line 
1/*
2 * Copyright (c) 2014 embedded brains GmbH.  All rights reserved.
3 *
4 *  embedded brains GmbH
5 *  Dornierstr. 4
6 *  82178 Puchheim
7 *  Germany
8 *  <rtems@embedded-brains.de>
9 *
10 * The license and distribution terms for this file may be
11 * found in the file LICENSE in this distribution or at
12 * http://www.rtems.org/license/LICENSE.
13 */
14
15#ifdef HAVE_CONFIG_H
16  #include "config.h"
17#endif
18
19#include <sys/stat.h>
20#include <grp.h>
21#include <pwd.h>
22#include <stdio.h>
23
24#include "tmacros.h"
25
26const char rtems_test_name[] = "PWDGRP 1";
27
28static void create_file(const char *name, const char *content)
29{
30  FILE *fp;
31  int rv;
32
33  fp = fopen(name, "wx");
34  rtems_test_assert(fp != NULL);
35
36  rv = fputs(content, fp);
37  rtems_test_assert(rv == 0);
38
39  rv = fclose(fp);
40  rtems_test_assert(rv == 0);
41}
42
43static void assert_pwd(struct passwd *pwd)
44{
45  rtems_test_assert(strcmp(pwd->pw_name, "moop") == 0);
46  rtems_test_assert(strcmp(pwd->pw_passwd, "foo") == 0);
47  rtems_test_assert(pwd->pw_uid == 1);
48  rtems_test_assert(pwd->pw_gid == 3);
49  rtems_test_assert(strcmp(pwd->pw_comment, "blob") == 0);
50  rtems_test_assert(strcmp(pwd->pw_gecos, "a") == 0);
51  rtems_test_assert(strcmp(pwd->pw_dir, "b") == 0);
52  rtems_test_assert(strcmp(pwd->pw_shell, "c") == 0);
53}
54
55static void assert_grp(struct group *grp)
56{
57  rtems_test_assert(strcmp(grp->gr_name, "blub") == 0);
58  rtems_test_assert(strcmp(grp->gr_passwd, "bar") == 0);
59  rtems_test_assert(grp->gr_gid == 3);
60  rtems_test_assert(strcmp(grp->gr_mem[0], "moop") == 0);
61  rtems_test_assert(grp->gr_mem[1] == NULL);
62}
63
64static void test(void)
65{
66  int rv;
67  struct passwd pwd;
68  struct group grp;
69  struct passwd *pwd_res;
70  struct group *grp_res;
71  char buf[256];
72
73  rv = mkdir("/etc", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
74  rtems_test_assert(rv == 0);
75
76  create_file(
77    "/etc/passwd",
78    "moop:foo:1:3:blob:a:b:c\n"
79  );
80
81  create_file(
82    "/etc/group",
83    "blub:bar:3:moop\n"
84  );
85
86  memset(&pwd, 0xff, sizeof(pwd));
87  rv = getpwnam_r("moop", &pwd, &buf[0], sizeof(buf), &pwd_res);
88  rtems_test_assert(rv == 0);
89  rtems_test_assert(&pwd == pwd_res);
90  assert_pwd(pwd_res);
91
92  memset(&pwd, 0xff, sizeof(pwd));
93  rv = getpwuid_r(1, &pwd, &buf[0], sizeof(buf), &pwd_res);
94  rtems_test_assert(rv == 0);
95  rtems_test_assert(&pwd == pwd_res);
96  assert_pwd(pwd_res);
97
98  memset(&grp, 0xff, sizeof(grp));
99  rv = getgrnam_r("blub", &grp, &buf[0], sizeof(buf), &grp_res);
100  rtems_test_assert(rv == 0);
101  rtems_test_assert(&grp == grp_res);
102  assert_grp(grp_res);
103
104  memset(&grp, 0xff, sizeof(grp));
105  rv = getgrgid_r(3, &grp, &buf[0], sizeof(buf), &grp_res);
106  rtems_test_assert(rv == 0);
107  rtems_test_assert(&grp == grp_res);
108  assert_grp(grp_res);
109}
110
111static void Init(rtems_task_argument arg)
112{
113  TEST_BEGIN();
114
115  test();
116
117  TEST_END();
118  rtems_test_exit(0);
119}
120
121#define CONFIGURE_APPLICATION_DOES_NOT_NEED_CLOCK_DRIVER
122#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
123
124#define CONFIGURE_USE_IMFS_AS_BASE_FILESYSTEM
125
126#define CONFIGURE_LIBIO_MAXIMUM_FILE_DESCRIPTORS 4
127
128#define CONFIGURE_MAXIMUM_TASKS 1
129
130#define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
131
132#define CONFIGURE_RTEMS_INIT_TASKS_TABLE
133
134#define CONFIGURE_INIT
135
136#include <rtems/confdefs.h>
Note: See TracBrowser for help on using the repository browser.