]> ruderich.org/simon Gitweb - nsscash/nsscash.git/blob - group_test.go
Use github.com/google/renameio for atomic file updates
[nsscash/nsscash.git] / group_test.go
1 // Copyright (C) 2019-2020  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         "fmt"
20         "reflect"
21         "strings"
22         "testing"
23 )
24
25 func TestParseGroups(t *testing.T) {
26         tests := []struct {
27                 data string
28                 exp  []Group
29                 err  error
30         }{
31                 {
32                         "",
33                         nil,
34                         nil,
35                 },
36                 {
37                         "root:x:0:",
38                         nil,
39                         fmt.Errorf("no newline in last line: \"root:x:0:\""),
40                 },
41                 {
42                         "root:x:0:\n",
43                         []Group{
44                                 Group{
45                                         Name:    "root",
46                                         Passwd:  "x",
47                                         Gid:     0,
48                                         Members: nil,
49                                 },
50                         },
51                         nil,
52                 },
53                 {
54                         "root:x:0:foo\n",
55                         []Group{
56                                 Group{
57                                         Name:   "root",
58                                         Passwd: "x",
59                                         Gid:    0,
60                                         Members: []string{
61                                                 "foo",
62                                         },
63                                 },
64                         },
65                         nil,
66                 },
67                 {
68                         "root:x:0:foo,bar\n",
69                         []Group{
70                                 Group{
71                                         Name:   "root",
72                                         Passwd: "x",
73                                         Gid:    0,
74                                         Members: []string{
75                                                 "foo",
76                                                 "bar",
77                                         },
78                                 },
79                         },
80                         nil,
81                 },
82         }
83
84         for n, tc := range tests {
85                 res, err := ParseGroups(strings.NewReader(tc.data))
86                 if !reflect.DeepEqual(err, tc.err) {
87                         t.Errorf("%d: err = %v, want %v",
88                                 n, err, tc.err)
89                 }
90                 if !reflect.DeepEqual(res, tc.exp) {
91                         t.Errorf("%d: res = %v, want %v",
92                                 n, res, tc.exp)
93                 }
94         }
95 }