mirror of
				https://github.com/tailscale/tailscale.git
				synced 2025-10-31 08:11:32 +01:00 
			
		
		
		
	Updates #11058 Change-Id: I35e7ef9b90e83cac04ca93fd964ad00ed5b48430 Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
		
			
				
	
	
		
			51 lines
		
	
	
		
			1.0 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			51 lines
		
	
	
		
			1.0 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| // Copyright (c) Tailscale Inc & AUTHORS
 | |
| // SPDX-License-Identifier: BSD-3-Clause
 | |
| 
 | |
| package pidowner
 | |
| 
 | |
| import (
 | |
| 	"math/rand"
 | |
| 	"os"
 | |
| 	"os/user"
 | |
| 	"testing"
 | |
| )
 | |
| 
 | |
| func TestOwnerOfPID(t *testing.T) {
 | |
| 	id, err := OwnerOfPID(os.Getpid())
 | |
| 	if err == ErrNotImplemented {
 | |
| 		t.Skip(err)
 | |
| 	}
 | |
| 	if err != nil {
 | |
| 		t.Fatal(err)
 | |
| 	}
 | |
| 	t.Logf("id=%q", id)
 | |
| 
 | |
| 	u, err := user.LookupId(id)
 | |
| 	if err != nil {
 | |
| 		t.Fatalf("LookupId: %v", err)
 | |
| 	}
 | |
| 	t.Logf("Got: %+v", u)
 | |
| }
 | |
| 
 | |
| // validate that OS implementation returns ErrProcessNotFound.
 | |
| func TestNotFoundError(t *testing.T) {
 | |
| 	// Try a bunch of times to stumble upon a pid that doesn't exist...
 | |
| 	const tries = 50
 | |
| 	for range tries {
 | |
| 		_, err := OwnerOfPID(rand.Intn(1e9))
 | |
| 		if err == ErrNotImplemented {
 | |
| 			t.Skip(err)
 | |
| 		}
 | |
| 		if err == nil {
 | |
| 			// We got unlucky and this pid existed. Try again.
 | |
| 			continue
 | |
| 		}
 | |
| 		if err == ErrProcessNotFound {
 | |
| 			// Pass.
 | |
| 			return
 | |
| 		}
 | |
| 		t.Fatalf("Error is not ErrProcessNotFound: %T %v", err, err)
 | |
| 	}
 | |
| 	t.Errorf("after %d tries, couldn't find a process that didn't exist", tries)
 | |
| }
 |