]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - usr.bin/cpio/test/test_owner_parse.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / usr.bin / cpio / test / test_owner_parse.c
1 /*-
2  * Copyright (c) 2003-2009 Tim Kientzle
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 #include "test.h"
26 __FBSDID("$FreeBSD$");
27
28 #include "../cpio.h"
29 #include "err.h"
30
31 #if defined(__CYGWIN__)
32 /* On cygwin, the Administrator user most likely exists (unless
33  * it has been renamed or is in a non-English localization), but
34  * its primary group membership depends on how the user set up
35  * their /etc/passwd. Likely values are 513 (None), 545 (Users),
36  * or 544 (Administrators). Just check for one of those...
37  * TODO: Handle non-English localizations...e.g. French 'Administrateur'
38  *       Use CreateWellKnownSID() and LookupAccountName()?
39  */
40 #define ROOT "Administrator"
41 static int root_uids[] = { 500 };
42 static int root_gids[] = { 513, 545, 544 };
43 #else
44 #define ROOT "root"
45 static int root_uids[] = { 0 };
46 static int root_gids[] = { 0 };
47 #endif
48
49 static int
50 int_in_list(int i, int *l, size_t n)
51 {
52         while (n-- > 0)
53                 if (*l++ == i)
54                         return (1);
55         failure("%d", i);
56         return (0);
57 }
58
59 DEFINE_TEST(test_owner_parse)
60 {
61 #if defined(_WIN32) && !defined(__CYGWIN__)
62         /* TODO: Does this need cygwin style handling of uid/gid ? */
63         skipping("Windows cannot handle uid/gid as UNIX like system");
64 #else
65         int uid, gid;
66
67         assertEqualInt(0, owner_parse(ROOT, &uid, &gid));
68         assert(int_in_list(uid, root_uids,
69                 sizeof(root_uids)/sizeof(root_uids[0])));
70         assertEqualInt(-1, gid);
71
72
73         assertEqualInt(0, owner_parse(ROOT ":", &uid, &gid));
74         assert(int_in_list(uid, root_uids,
75                 sizeof(root_uids)/sizeof(root_uids[0])));
76         assert(int_in_list(gid, root_gids,
77                 sizeof(root_gids)/sizeof(root_gids[0])));
78
79         assertEqualInt(0, owner_parse(ROOT ".", &uid, &gid));
80         assert(int_in_list(uid, root_uids,
81                 sizeof(root_uids)/sizeof(root_uids[0])));
82         assert(int_in_list(gid, root_gids,
83                 sizeof(root_gids)/sizeof(root_gids[0])));
84
85         assertEqualInt(0, owner_parse("111", &uid, &gid));
86         assertEqualInt(111, uid);
87         assertEqualInt(-1, gid);
88
89         assertEqualInt(0, owner_parse("112:", &uid, &gid));
90         assertEqualInt(112, uid);
91         /* Can't assert gid, since we don't know gid for user #112. */
92
93         assertEqualInt(0, owner_parse("113.", &uid, &gid));
94         assertEqualInt(113, uid);
95         /* Can't assert gid, since we don't know gid for user #113. */
96
97         assertEqualInt(0, owner_parse(":114", &uid, &gid));
98         assertEqualInt(-1, uid);
99         assertEqualInt(114, gid);
100
101         assertEqualInt(0, owner_parse(".115", &uid, &gid));
102         assertEqualInt(-1, uid);
103         assertEqualInt(115, gid);
104
105         assertEqualInt(0, owner_parse("116:117", &uid, &gid));
106         assertEqualInt(116, uid);
107         assertEqualInt(117, gid);
108
109         /*
110          * TODO: Lookup current user/group name, build strings and
111          * use those to verify username/groupname lookups for ordinary
112          * users.
113          */
114
115         /*
116          * TODO: Rework owner_parse to either return a char * pointing
117          * to an error message or accept a function pointer to an
118          * error-reporting routine so that the following tests don't
119          * generate any output.
120          *
121          * Alternatively, redirect stderr temporarily to suppress the output.
122          */
123
124         cpio_progname = "Ignore this message";
125         assertEqualInt(1, owner_parse(":nonexistentgroup", &uid, &gid));
126         assertEqualInt(1, owner_parse(ROOT ":nonexistentgroup", &uid, &gid));
127         assertEqualInt(1,
128             owner_parse("nonexistentuser:nonexistentgroup", &uid, &gid));
129 #endif
130 }