]> ruderich.org/simon Gitweb - nsscash/nsscash.git/blob - group_test.go
nss: Makefile: don't link against asan
[nsscash/nsscash.git] / group_test.go
1 // Copyright (C) 2019  Simon Ruderich
2 //
3 // This program is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU Affero General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 // GNU Affero General Public License for more details.
12 //
13 // You should have received a copy of the GNU Affero General Public License
14 // along with this program.  If not, see <https://www.gnu.org/licenses/>.
15
16 package main
17
18 import (
19         "reflect"
20         "strings"
21         "testing"
22 )
23
24 func TestParseGroups(t *testing.T) {
25         tests := []struct {
26                 data string
27                 exp  []Group
28         }{
29                 {
30                         "",
31                         nil,
32                 },
33                 {
34                         "root:x:0:\n",
35                         []Group{
36                                 Group{
37                                         Name:    "root",
38                                         Passwd:  "x",
39                                         Gid:     0,
40                                         Members: nil,
41                                 },
42                         },
43                 },
44                 {
45                         "root:x:0:foo\n",
46                         []Group{
47                                 Group{
48                                         Name:   "root",
49                                         Passwd: "x",
50                                         Gid:    0,
51                                         Members: []string{
52                                                 "foo",
53                                         },
54                                 },
55                         },
56                 },
57                 {
58                         "root:x:0:foo,bar\n",
59                         []Group{
60                                 Group{
61                                         Name:   "root",
62                                         Passwd: "x",
63                                         Gid:    0,
64                                         Members: []string{
65                                                 "foo",
66                                                 "bar",
67                                         },
68                                 },
69                         },
70                 },
71         }
72
73         for n, tc := range tests {
74                 res, err := ParseGroups(strings.NewReader(tc.data))
75                 if err != nil {
76                         t.Errorf("%d: err = %v, want %v",
77                                 n, res, nil)
78                 }
79                 if !reflect.DeepEqual(res, tc.exp) {
80                         t.Errorf("%d: res = %v, want %v",
81                                 n, res, tc.exp)
82                 }
83         }
84 }