lib9p

Go 9P library.
Log | Files | Refs | LICENSE

uid_plan9_test.go (1974B)


      1 //go:build plan9
      2 
      3 package diskfs
      4 
      5 import (
      6 	"os/exec"
      7 	"os/user"
      8 	"strconv"
      9 	"strings"
     10 	"testing"
     11 )
     12 
     13 func TestIsGroupLeader(t *testing.T) {
     14 	usr, err := user.Current()
     15 	if err != nil {
     16 		t.Fatalf("get current user: %v", err)
     17 	}
     18 	cmd := exec.Command("rc", "-c", "whois "+usr.Username+" | awk -F: '{print }'")
     19 	b, err := cmd.Output()
     20 	if err != nil {
     21 		t.Fatal(err)
     22 	}
     23 	groups := strings.Split(string(b), "\n")
     24 	groups = groups[:len(groups)-1]
     25 	cmd = exec.Command("rc", "-c", "whois "+usr.Username+" | awk -F: '{print }'")
     26 	b, err = cmd.Output()
     27 	if err != nil {
     28 		t.Fatal(err)
     29 	}
     30 	leaders := strings.Split(string(b), "\n")
     31 	leaders = leaders[:len(leaders)-1]
     32 	tests := []struct {
     33 		name  string
     34 		uid   string
     35 		group string
     36 		want  bool
     37 	}{}
     38 	for i, grp := range groups {
     39 		tests = append(tests, struct {
     40 			name  string
     41 			uid   string
     42 			group string
     43 			want  bool
     44 		}{strconv.Itoa(i), usr.Username, grp, usr.Username == leaders[i]})
     45 	}
     46 	for _, test := range tests {
     47 		got := isGroupLeader(test.group, test.uid)
     48 		if got != test.want {
     49 			t.Errorf("%s: isGroupLeader(%s, %s) = %t, want: %t",
     50 				test.name, test.group, test.uid, got, test.want)
     51 		}
     52 	}
     53 }
     54 
     55 func TestIsGroupMember(t *testing.T) {
     56 	usr, err := user.Current()
     57 	if err != nil {
     58 		t.Fatalf("get current user: %v", err)
     59 	}
     60 	cmd := exec.Command("rc", "-c", "whois "+usr.Username+" | awk -F: '{print }'")
     61 	b, err := cmd.Output()
     62 	if err != nil {
     63 		t.Fatal(err)
     64 	}
     65 	groups := strings.Split(string(b), "\n")
     66 	groups = groups[:len(groups)-1]
     67 	tests := []struct {
     68 		name  string
     69 		uid   string
     70 		group string
     71 		want  bool
     72 	}{}
     73 	for i, grp := range groups {
     74 		tests = append(tests, struct {
     75 			name  string
     76 			uid   string
     77 			group string
     78 			want  bool
     79 		}{strconv.Itoa(i), usr.Username, grp, true})
     80 	}
     81 	for _, test := range tests {
     82 		got := isGroupMember(test.group, test.uid)
     83 		if got != test.want {
     84 			t.Errorf("%s: isGroupMember(%s, %s) = %t, want: %t",
     85 				test.name, test.group, test.uid, got, test.want)
     86 		}
     87 	}
     88 }