gitlab.com/apertussolutions/u-root@v7.0.0+incompatible/cmds/core/chroot/chroot_test.go (about)

     1  // Copyright 2018 the u-root 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  package main
     6  
     7  import (
     8  	"fmt"
     9  	"testing"
    10  )
    11  
    12  func TestUserSpecSet(t *testing.T) {
    13  	var (
    14  		user  userSpec
    15  		input string
    16  		err   error
    17  	)
    18  	input = "1000:1001"
    19  
    20  	err = user.Set(input)
    21  	if err != nil {
    22  		t.Errorf("Unexpected error with input: %s", input)
    23  	} else if user.uid != 1000 || user.gid != 1001 {
    24  		t.Errorf("Expected uid 1000, gid 1001, got: uid %d, gid %d", user.uid, user.gid)
    25  	}
    26  
    27  	input = "test:1000"
    28  	err = user.Set(input)
    29  	if err == nil {
    30  		t.Errorf("Expected error, input %s got: uid %d, gid %d", input, user.uid, user.gid)
    31  	}
    32  
    33  	input = "1000:1001:"
    34  	err = user.Set(input)
    35  	if err == nil {
    36  		t.Errorf("Expected error, input %s got: uid %d, gid %d", input, user.uid, user.gid)
    37  	}
    38  
    39  	input = ":1000"
    40  	err = user.Set(input)
    41  	if err == nil {
    42  		t.Errorf("Expected error, input %s got: uid %d, gid %d", input, user.uid, user.gid)
    43  	}
    44  
    45  	input = "1000:"
    46  	if err = user.Set(input); err == nil {
    47  		t.Errorf("Expected error, input %s got: uid %d, gid %d", input, user.uid, user.gid)
    48  	}
    49  }
    50  
    51  func TestUserSpecString(t *testing.T) {
    52  	var (
    53  		user  userSpec
    54  		input string
    55  		err   error
    56  	)
    57  	input = "1000:1001"
    58  
    59  	err = user.Set(input)
    60  	if err != nil {
    61  		t.Errorf("Unexpected error with input: %s", input)
    62  	}
    63  
    64  	str := user.String()
    65  	if str != input {
    66  		t.Errorf("Unexpected error with input: %s, String method returned: %s", input, str)
    67  	}
    68  
    69  }
    70  
    71  func testGroupSet(input string, expected []uint32) error {
    72  	var groups groupsSpec
    73  	err := groups.Set(input)
    74  	if err != nil {
    75  		return fmt.Errorf("Unexpected error with input: %s, %s", input, err)
    76  	} else if len(groups.groups) != len(expected) {
    77  		return fmt.Errorf("Unexpected groups length with input %s, actual length %d", input, len(groups.groups))
    78  	} else {
    79  		for index, group := range groups.groups {
    80  			if expected[index] != group {
    81  				return fmt.Errorf("Unexpected error at index %d, was expecting %d , found %d", index, expected[index], group)
    82  			}
    83  		}
    84  	}
    85  	return nil
    86  }
    87  
    88  func TestGroupsSet(t *testing.T) {
    89  	var (
    90  		input    string
    91  		expected []uint32
    92  		err      error
    93  	)
    94  
    95  	input = "1000"
    96  	expected = []uint32{1000}
    97  	if err = testGroupSet(input, expected); err != nil {
    98  		t.Errorf(err.Error())
    99  	}
   100  
   101  	input = "1000,1001"
   102  	expected = []uint32{1000, 1001}
   103  	if err = testGroupSet(input, expected); err != nil {
   104  		t.Errorf(err.Error())
   105  	}
   106  
   107  	input = "1000,1001,"
   108  	expected = []uint32{}
   109  	if err = testGroupSet(input, expected); err == nil {
   110  		t.Errorf("Expected error on input: %s, got: %v", input, groups.groups)
   111  	}
   112  
   113  	input = "test,1001"
   114  	expected = []uint32{}
   115  	if err = testGroupSet(input, expected); err == nil {
   116  		t.Errorf("Expected error on input: %s, got: %v", input, groups.groups)
   117  	}
   118  
   119  	input = ",1000"
   120  	expected = []uint32{}
   121  	if err = testGroupSet(input, expected); err == nil {
   122  		t.Errorf("Expected error on input: %s, got: %v", input, groups.groups)
   123  	}
   124  
   125  	input = "1000,"
   126  	expected = []uint32{}
   127  	if err = testGroupSet(input, expected); err == nil {
   128  		t.Errorf("Expected error on input: %s, got: %v", input, groups.groups)
   129  	}
   130  
   131  }