]> ruderich.org/simon Gitweb - nsscash/nsscash.git/blob - passwd_test.go
.github: update upstream actions to latest version
[nsscash/nsscash.git] / passwd_test.go
1 // Copyright (C) 2019-2021  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 TestParsePasswd(t *testing.T) {
26         tests := []struct {
27                 data string
28                 exp  []Passwd
29                 err  error
30         }{
31                 {
32                         "",
33                         nil,
34                         nil,
35                 },
36                 {
37                         "root:x:0:0:root:/root:/bin/zsh",
38                         nil,
39                         fmt.Errorf("no newline in last line: \"root:x:0:0:root:/root:/bin/zsh\""),
40                 },
41                 {
42                         "root:x:0:0:root:/root:/bin/zsh\n",
43                         []Passwd{
44                                 Passwd{
45                                         Name:   "root",
46                                         Passwd: "x",
47                                         Uid:    0,
48                                         Gid:    0,
49                                         Gecos:  "root",
50                                         Dir:    "/root",
51                                         Shell:  "/bin/zsh",
52                                 },
53                         },
54                         nil,
55                 },
56                 {
57                         "root:x:0:0:root:/root:/bin/zsh\n" +
58                                 "daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin\n" +
59                                 "bin:x:2:3:bin:/bin:/usr/sbin/nologin\n",
60                         []Passwd{
61                                 Passwd{
62                                         Name:   "root",
63                                         Passwd: "x",
64                                         Uid:    0,
65                                         Gid:    0,
66                                         Gecos:  "root",
67                                         Dir:    "/root",
68                                         Shell:  "/bin/zsh",
69                                 },
70                                 Passwd{
71                                         Name:   "daemon",
72                                         Passwd: "x",
73                                         Uid:    1,
74                                         Gid:    1,
75                                         Gecos:  "daemon",
76                                         Dir:    "/usr/sbin",
77                                         Shell:  "/usr/sbin/nologin",
78                                 },
79                                 Passwd{
80                                         Name:   "bin",
81                                         Passwd: "x",
82                                         Uid:    2,
83                                         Gid:    3,
84                                         Gecos:  "bin",
85                                         Dir:    "/bin",
86                                         Shell:  "/usr/sbin/nologin",
87                                 },
88                         },
89                         nil,
90                 },
91         }
92
93         for n, tc := range tests {
94                 res, err := ParsePasswds(strings.NewReader(tc.data))
95                 if !reflect.DeepEqual(err, tc.err) {
96                         t.Errorf("%d: err = %v, want %v",
97                                 n, err, tc.err)
98                 }
99                 if !reflect.DeepEqual(res, tc.exp) {
100                         t.Errorf("%d: res = %v, want %v",
101                                 n, res, tc.exp)
102                 }
103         }
104 }