github.com/gidoBOSSftw5731/go/src@v0.0.0-20210226122457-d24b0edbf019/os/user/lookup_unix_test.go (about)

     1  // Copyright 2016 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build (aix || darwin || dragonfly || freebsd || (!android && linux) || netbsd || openbsd || solaris) && !cgo
     6  // +build aix darwin dragonfly freebsd !android,linux netbsd openbsd solaris
     7  // +build !cgo
     8  
     9  package user
    10  
    11  import (
    12  	"reflect"
    13  	"strings"
    14  	"testing"
    15  )
    16  
    17  const testGroupFile = `# See the opendirectoryd(8) man page for additional 
    18  # information about Open Directory.
    19  ##
    20  nobody:*:-2:
    21  nogroup:*:-1:
    22  wheel:*:0:root
    23  emptyid:*::root
    24  invalidgid:*:notanumber:root
    25  +plussign:*:20:root
    26  -minussign:*:21:root
    27        
    28  daemon:*:1:root
    29      indented:*:7:
    30  # comment:*:4:found
    31       # comment:*:4:found
    32  kmem:*:2:root
    33  `
    34  
    35  var groupTests = []struct {
    36  	in   string
    37  	name string
    38  	gid  string
    39  }{
    40  	{testGroupFile, "nobody", "-2"},
    41  	{testGroupFile, "kmem", "2"},
    42  	{testGroupFile, "notinthefile", ""},
    43  	{testGroupFile, "comment", ""},
    44  	{testGroupFile, "plussign", ""},
    45  	{testGroupFile, "+plussign", ""},
    46  	{testGroupFile, "-minussign", ""},
    47  	{testGroupFile, "minussign", ""},
    48  	{testGroupFile, "emptyid", ""},
    49  	{testGroupFile, "invalidgid", ""},
    50  	{testGroupFile, "indented", "7"},
    51  	{testGroupFile, "# comment", ""},
    52  	{"", "emptyfile", ""},
    53  }
    54  
    55  func TestFindGroupName(t *testing.T) {
    56  	for _, tt := range groupTests {
    57  		got, err := findGroupName(tt.name, strings.NewReader(tt.in))
    58  		if tt.gid == "" {
    59  			if err == nil {
    60  				t.Errorf("findGroupName(%s): got nil error, expected err", tt.name)
    61  				continue
    62  			}
    63  			switch terr := err.(type) {
    64  			case UnknownGroupError:
    65  				if terr.Error() != "group: unknown group "+tt.name {
    66  					t.Errorf("findGroupName(%s): got %v, want %v", tt.name, terr, tt.name)
    67  				}
    68  			default:
    69  				t.Errorf("findGroupName(%s): got unexpected error %v", tt.name, terr)
    70  			}
    71  		} else {
    72  			if err != nil {
    73  				t.Fatalf("findGroupName(%s): got unexpected error %v", tt.name, err)
    74  			}
    75  			if got.Gid != tt.gid {
    76  				t.Errorf("findGroupName(%s): got gid %v, want %s", tt.name, got.Gid, tt.gid)
    77  			}
    78  			if got.Name != tt.name {
    79  				t.Errorf("findGroupName(%s): got name %s, want %s", tt.name, got.Name, tt.name)
    80  			}
    81  		}
    82  	}
    83  }
    84  
    85  var groupIdTests = []struct {
    86  	in   string
    87  	gid  string
    88  	name string
    89  }{
    90  	{testGroupFile, "-2", "nobody"},
    91  	{testGroupFile, "2", "kmem"},
    92  	{testGroupFile, "notinthefile", ""},
    93  	{testGroupFile, "comment", ""},
    94  	{testGroupFile, "7", "indented"},
    95  	{testGroupFile, "4", ""},
    96  	{testGroupFile, "20", ""}, // row starts with a plus
    97  	{testGroupFile, "21", ""}, // row starts with a minus
    98  	{"", "emptyfile", ""},
    99  }
   100  
   101  func TestFindGroupId(t *testing.T) {
   102  	for _, tt := range groupIdTests {
   103  		got, err := findGroupId(tt.gid, strings.NewReader(tt.in))
   104  		if tt.name == "" {
   105  			if err == nil {
   106  				t.Errorf("findGroupId(%s): got nil error, expected err", tt.gid)
   107  				continue
   108  			}
   109  			switch terr := err.(type) {
   110  			case UnknownGroupIdError:
   111  				if terr.Error() != "group: unknown groupid "+tt.gid {
   112  					t.Errorf("findGroupId(%s): got %v, want %v", tt.name, terr, tt.name)
   113  				}
   114  			default:
   115  				t.Errorf("findGroupId(%s): got unexpected error %v", tt.name, terr)
   116  			}
   117  		} else {
   118  			if err != nil {
   119  				t.Fatalf("findGroupId(%s): got unexpected error %v", tt.name, err)
   120  			}
   121  			if got.Gid != tt.gid {
   122  				t.Errorf("findGroupId(%s): got gid %v, want %s", tt.name, got.Gid, tt.gid)
   123  			}
   124  			if got.Name != tt.name {
   125  				t.Errorf("findGroupId(%s): got name %s, want %s", tt.name, got.Name, tt.name)
   126  			}
   127  		}
   128  	}
   129  }
   130  
   131  const testUserFile = `   # Example user file
   132  root:x:0:0:root:/root:/bin/bash
   133  daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
   134  bin:x:2:3:bin:/bin:/usr/sbin/nologin
   135       indented:x:3:3:indented:/dev:/usr/sbin/nologin
   136  sync:x:4:65534:sync:/bin:/bin/sync
   137  negative:x:-5:60:games:/usr/games:/usr/sbin/nologin
   138  man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
   139  allfields:x:6:12:mansplit,man2,man3,man4:/home/allfields:/usr/sbin/nologin
   140  +plussign:x:8:10:man:/var/cache/man:/usr/sbin/nologin
   141  -minussign:x:9:10:man:/var/cache/man:/usr/sbin/nologin
   142  
   143  malformed:x:27:12 # more:colons:after:comment
   144  
   145  struid:x:notanumber:12 # more:colons:after:comment
   146  
   147  # commented:x:28:12:commented:/var/cache/man:/usr/sbin/nologin
   148        # commentindented:x:29:12:commentindented:/var/cache/man:/usr/sbin/nologin
   149  
   150  struid2:x:30:badgid:struid2name:/home/struid:/usr/sbin/nologin
   151  `
   152  
   153  var userIdTests = []struct {
   154  	in   string
   155  	uid  string
   156  	name string
   157  }{
   158  	{testUserFile, "-5", "negative"},
   159  	{testUserFile, "2", "bin"},
   160  	{testUserFile, "100", ""}, // not in the file
   161  	{testUserFile, "8", ""},   // plus sign, glibc doesn't find it
   162  	{testUserFile, "9", ""},   // minus sign, glibc doesn't find it
   163  	{testUserFile, "27", ""},  // malformed
   164  	{testUserFile, "28", ""},  // commented out
   165  	{testUserFile, "29", ""},  // commented out, indented
   166  	{testUserFile, "3", "indented"},
   167  	{testUserFile, "30", ""}, // the Gid is not valid, shouldn't match
   168  	{"", "1", ""},
   169  }
   170  
   171  func TestInvalidUserId(t *testing.T) {
   172  	_, err := findUserId("notanumber", strings.NewReader(""))
   173  	if err == nil {
   174  		t.Fatalf("findUserId('notanumber'): got nil error")
   175  	}
   176  	if want := "user: invalid userid notanumber"; err.Error() != want {
   177  		t.Errorf("findUserId('notanumber'): got %v, want %s", err, want)
   178  	}
   179  }
   180  
   181  func TestLookupUserId(t *testing.T) {
   182  	for _, tt := range userIdTests {
   183  		got, err := findUserId(tt.uid, strings.NewReader(tt.in))
   184  		if tt.name == "" {
   185  			if err == nil {
   186  				t.Errorf("findUserId(%s): got nil error, expected err", tt.uid)
   187  				continue
   188  			}
   189  			switch terr := err.(type) {
   190  			case UnknownUserIdError:
   191  				if want := "user: unknown userid " + tt.uid; terr.Error() != want {
   192  					t.Errorf("findUserId(%s): got %v, want %v", tt.name, terr, want)
   193  				}
   194  			default:
   195  				t.Errorf("findUserId(%s): got unexpected error %v", tt.name, terr)
   196  			}
   197  		} else {
   198  			if err != nil {
   199  				t.Fatalf("findUserId(%s): got unexpected error %v", tt.name, err)
   200  			}
   201  			if got.Uid != tt.uid {
   202  				t.Errorf("findUserId(%s): got uid %v, want %s", tt.name, got.Uid, tt.uid)
   203  			}
   204  			if got.Username != tt.name {
   205  				t.Errorf("findUserId(%s): got name %s, want %s", tt.name, got.Username, tt.name)
   206  			}
   207  		}
   208  	}
   209  }
   210  
   211  func TestLookupUserPopulatesAllFields(t *testing.T) {
   212  	u, err := findUsername("allfields", strings.NewReader(testUserFile))
   213  	if err != nil {
   214  		t.Fatal(err)
   215  	}
   216  	want := &User{
   217  		Username: "allfields",
   218  		Uid:      "6",
   219  		Gid:      "12",
   220  		Name:     "mansplit",
   221  		HomeDir:  "/home/allfields",
   222  	}
   223  	if !reflect.DeepEqual(u, want) {
   224  		t.Errorf("findUsername: got %#v, want %#v", u, want)
   225  	}
   226  }
   227  
   228  var userTests = []struct {
   229  	in   string
   230  	name string
   231  	uid  string
   232  }{
   233  	{testUserFile, "negative", "-5"},
   234  	{testUserFile, "bin", "2"},
   235  	{testUserFile, "notinthefile", ""},
   236  	{testUserFile, "indented", "3"},
   237  	{testUserFile, "plussign", ""},
   238  	{testUserFile, "+plussign", ""},
   239  	{testUserFile, "minussign", ""},
   240  	{testUserFile, "-minussign", ""},
   241  	{testUserFile, "   indented", ""},
   242  	{testUserFile, "commented", ""},
   243  	{testUserFile, "commentindented", ""},
   244  	{testUserFile, "malformed", ""},
   245  	{testUserFile, "# commented", ""},
   246  	{"", "emptyfile", ""},
   247  }
   248  
   249  func TestLookupUser(t *testing.T) {
   250  	for _, tt := range userTests {
   251  		got, err := findUsername(tt.name, strings.NewReader(tt.in))
   252  		if tt.uid == "" {
   253  			if err == nil {
   254  				t.Errorf("lookupUser(%s): got nil error, expected err", tt.uid)
   255  				continue
   256  			}
   257  			switch terr := err.(type) {
   258  			case UnknownUserError:
   259  				if want := "user: unknown user " + tt.name; terr.Error() != want {
   260  					t.Errorf("lookupUser(%s): got %v, want %v", tt.name, terr, want)
   261  				}
   262  			default:
   263  				t.Errorf("lookupUser(%s): got unexpected error %v", tt.name, terr)
   264  			}
   265  		} else {
   266  			if err != nil {
   267  				t.Fatalf("lookupUser(%s): got unexpected error %v", tt.name, err)
   268  			}
   269  			if got.Uid != tt.uid {
   270  				t.Errorf("lookupUser(%s): got uid %v, want %s", tt.name, got.Uid, tt.uid)
   271  			}
   272  			if got.Username != tt.name {
   273  				t.Errorf("lookupUser(%s): got name %s, want %s", tt.name, got.Username, tt.name)
   274  			}
   275  		}
   276  	}
   277  }