os_user_api.mx raw

   1  //:wasmtest skip
   2  /*
   3  WASM TEST SKIPPED, and the reason is the target, not the fixture: os/user reads
   4  /etc/passwd and /etc/group through the filesystem, and the standalone wasm
   5  validator host has no such files, so every lookup returns "no such file". The
   6  native run is where this package's behaviour can be asserted (see below), and
   7  the wasm target's os/user COMPILATION is covered by phase 1c.
   8  os/user resolves users and groups with the pure-Moxie implementation.
   9  
  10  The package used to carry both routes the way Go does: four `cgo && !osusergo &&
  11  unix` files (`cgo_lookup_cgo.mx`, `cgo_lookup_unix.mx`, `cgo_listgroups_unix.mx`,
  12  `getgrouplist_unix.mx`) beside the `!cgo || osusergo` ones. Moxie has no cgo and
  13  no `osusergo` build tag, and evalBuildLine treats an unknown tag as false, so the
  14  cgo route was dead: `cgo_lookup_cgo.mx` imports "C" (the loader refuses it) and
  15  the other three reference `_C_*` names that ONLY that file defines, so none of
  16  the four could ever compile. They were removed in round 125 - dead references,
  17  not a fallback. The live implementation is `lookup_unix.mx`,
  18  `listgroups_unix.mx`, `lookup.mx`, `lookup_stubs.mx` and `user.mx`, which is what
  19  the compiler's own `discover os/user [...]` line reports, and it is verified here
  20  by running it: "the package compiles" was exactly the claim that turned out to be
  21  worth nothing for four other packages in round 103 (the sweep listed packages
  22  that do not exist and reported them ok).
  23  
  24  `lookup_stubs.mx` and `lookup_unix.mx` are BOTH selected by `!cgo`; the stub
  25  file holds only the declarations the unix file does not define, which is why
  26  the pair links.
  27  
  28  Every lookup gets its own error name: Moxie rejects a redeclaration that
  29  shadows an enclosing one, so `u, err := ...` twice in one scope is a compile
  30  error, not the Go idiom it looks like.
  31  */
  32  package main
  33  
  34  import (
  35  	"os/user"
  36  )
  37  
  38  func main() {
  39  	// Termination: a batch program must still be stoppable, so the entry
  40  	// handles the channel the runtime closes on SIGINT/SIGTERM.
  41  	select {
  42  	case <-shutdown:
  43  		return
  44  	default:
  45  	}
  46  
  47  	u, cerr := user.Current()
  48  	if cerr != nil {
  49  		panic("user.Current: " | cerr.Error())
  50  	}
  51  	if len(u.Uid) == 0 || len(u.Username) == 0 {
  52  		panic("user.Current returned an empty uid/username")
  53  	}
  54  
  55  	r, lerr := user.Lookup("root")
  56  	if lerr != nil {
  57  		panic("user.Lookup(root): " | lerr.Error())
  58  	}
  59  	if r.Uid != "0" || r.Gid != "0" {
  60  		panic("user.Lookup(root) uid=" | r.Uid | " gid=" | r.Gid)
  61  	}
  62  	if len(r.Username) == 0 || len(r.HomeDir) == 0 {
  63  		panic("user.Lookup(root) returned an empty username/home")
  64  	}
  65  
  66  	g, gerr := user.LookupGroup("root")
  67  	if gerr != nil {
  68  		panic("user.LookupGroup(root): " | gerr.Error())
  69  	}
  70  	if g.Name != "root" || g.Gid != "0" {
  71  		panic("user.LookupGroup(root) name=" | g.Name | " gid=" | g.Gid)
  72  	}
  73  
  74  	missing, merr := user.Lookup("moxie-no-such-user-42")
  75  	if merr == nil {
  76  		panic("user.Lookup of a nonexistent user did not fail, returned " | missing.Username)
  77  	}
  78  
  79  	println("os/user ok")
  80  }
  81