github.com/ratrocket/u-root@v0.0.0-20180201221235-1cf9f48ee2cf/cmds/chroot/chroot_test.go (about)

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