github.com/riscv/riscv-go@v0.0.0-20200123204226-124ebd6fcc8e/misc/cgo/test/env.go (about) 1 // Copyright 2011 The Go 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 cgotest 6 7 /* 8 #include <stdlib.h> 9 */ 10 import "C" 11 import ( 12 "os" 13 "runtime" 14 "testing" 15 "unsafe" 16 ) 17 18 // This is really an os package test but here for convenience. 19 func testSetEnv(t *testing.T) { 20 if runtime.GOOS == "windows" { 21 // Go uses SetEnvironmentVariable on windows. Howerver, 22 // C runtime takes a *copy* at process startup of thei 23 // OS environment, and stores it in environ/envp. 24 // It is this copy that getenv/putenv manipulate. 25 t.Logf("skipping test") 26 return 27 } 28 const key = "CGO_OS_TEST_KEY" 29 const val = "CGO_OS_TEST_VALUE" 30 os.Setenv(key, val) 31 keyc := C.CString(key) 32 defer C.free(unsafe.Pointer(keyc)) 33 v := C.getenv(keyc) 34 if uintptr(unsafe.Pointer(v)) == 0 { 35 t.Fatal("getenv returned NULL") 36 } 37 vs := C.GoString(v) 38 if vs != val { 39 t.Fatalf("getenv() = %q; want %q", vs, val) 40 } 41 }