File: gshadow_test.go

package info (click to toggle)
golang-github-tredoe-osutil 1.5.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 476 kB
  • sloc: makefile: 4
file content (129 lines) | stat: -rw-r--r-- 2,682 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright 2010 Jonas mg
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

package user

import (
	"bufio"
	"io"
	"os"
	"testing"
)

func TestGShadowParser(t *testing.T) {
	f, err := os.Open(fileGShadow)
	if err != nil {
		t.Fatal(err)
	}
	defer f.Close()

	buf := bufio.NewReader(f)

	for {
		line, _, err := buf.ReadLine()
		if err != nil {
			if err == io.EOF {
				break
			}
			t.Error(err)
			continue
		}

		if _, err = parseGShadow(string(line)); err != nil {
			t.Error(err)
		}
	}
}

func TestGShadowFull(t *testing.T) {
	entry, err := LookupGShadow("root")
	if err != nil || entry == nil {
		t.Error(err)
	}

	entries, err := LookupInGShadow(GS_PASSWD, "!", -1)
	if err != nil || entries == nil {
		t.Error(err)
	}

	entries, err = LookupInGShadow(GS_ALL, "", -1)
	if err != nil || len(entries) == 0 {
		t.Error(err)
	}
}

func TestGShadowCount(t *testing.T) {
	count := 5
	entries, err := LookupInGShadow(GS_ALL, "", count)
	if err != nil || len(entries) != count {
		t.Error(err)
	}
}

func TestGShadowError(t *testing.T) {
	_, err := LookupGShadow("!!!???")
	if _, ok := err.(NoFoundError); !ok {
		t.Error("expected to report NoFoundError")
	}

	if _, err = LookupInGShadow(GS_MEMBER, "", 0); err != errSearch {
		t.Error("expected to report errSearch")
	}

	gs := &GShadow{}
	if err = gs.Add(nil); err != RequiredError("Name") {
		t.Error("expected to report RequiredError")
	}
}

func TestGShadow_Add(t *testing.T) {
	shadow := NewGShadow(GROUP, MEMBERS...)
	err := shadow.Add(nil)
	if err != nil {
		t.Fatal(err)
	}

	if err = shadow.Add(nil); err == nil {
		t.Fatal("a shadowed group existent can not be added again")
	} else {
		if !IsExist(err) {
			t.Error("shadowed group: expected to report ErrExist")
		}
	}

	s, err := LookupGShadow(GROUP)
	if err != nil {
		t.Fatal(err)
	}

	if s.Name != GROUP {
		t.Errorf("shadowed group: expected to get name %q", GROUP)
	}
}

var (
	groupKey1 = []byte("abc")
	groupKey2 = []byte("def")
)

func TestGShadowCrypt(t *testing.T) {
	gs, err := LookupGShadow(GROUP)
	if err != nil {
		t.Fatal(err)
	}
	gs.Passwd(groupKey1)
	if err = config.crypter.Verify(gs.password, groupKey1); err != nil {
		t.Fatalf("expected to get the same hashed password for %q", groupKey1)
	}

	if err = ChGPasswd(GROUP, groupKey2); err != nil {
		t.Fatalf("expected to change password: %s", err)
	}
	gs, _ = LookupGShadow(GROUP)
	if err = config.crypter.Verify(gs.password, groupKey2); err != nil {
		t.Fatalf("ChGPasswd: expected to get the same hashed password for %q", groupKey2)
	}
}