github.com/rkt/rkt@v1.30.1-0.20200224141603-171c416fac02/pkg/passwd/passwd_test.go (about)

     1  // Copyright 2016 The rkt Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package passwd
    16  
    17  import (
    18  	"reflect"
    19  	"testing"
    20  )
    21  
    22  func TestParsePasswdLine(t *testing.T) {
    23  	tests := []struct {
    24  		line          string
    25  		passwdLine    *User
    26  		shouldSucceed bool
    27  	}{
    28  		{
    29  			"nobody:x:1000:100::/home/nobody:",
    30  			&User{
    31  				"nobody",
    32  				"x",
    33  				1000,
    34  				100,
    35  				"",
    36  				"/home/nobody",
    37  				"",
    38  			},
    39  			true,
    40  		},
    41  		{
    42  			"nobody:x:1000:100::/home/nobody:/bin/nologin",
    43  			&User{
    44  				"nobody",
    45  				"x",
    46  				1000,
    47  				100,
    48  				"",
    49  				"/home/nobody",
    50  				"/bin/nologin",
    51  			},
    52  			true,
    53  		},
    54  		{
    55  			"nobody:x:",
    56  			nil,
    57  			false,
    58  		},
    59  		{
    60  			"",
    61  			nil,
    62  			false,
    63  		},
    64  		{
    65  			"nobody:x:1000:100::/home/nobody:/bin/nologin:more:stuff",
    66  			&User{
    67  				"nobody",
    68  				"x",
    69  				1000,
    70  				100,
    71  				"",
    72  				"/home/nobody",
    73  				"/bin/nologin",
    74  			},
    75  			true,
    76  		},
    77  	}
    78  
    79  	for i, tt := range tests {
    80  		p, err := parsePasswdLine(tt.line)
    81  		if err != nil {
    82  			if tt.shouldSucceed {
    83  				t.Errorf("#%d: parsing line %q failed unexpectedly", i, tt.line)
    84  			}
    85  			continue
    86  		}
    87  		if !reflect.DeepEqual(p, tt.passwdLine) {
    88  			t.Errorf("#%d: got user %v, want user %v", i, p, tt.passwdLine)
    89  		}
    90  	}
    91  }