main_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. // Copyright 2018 Google Inc. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package main
  15. import (
  16. "fmt"
  17. "io/ioutil"
  18. "os"
  19. "path/filepath"
  20. "testing"
  21. "android/soong/ui/build/paths"
  22. )
  23. var tmpDir string
  24. var origPATH string
  25. func TestMain(m *testing.M) {
  26. os.Exit(func() int {
  27. var err error
  28. tmpDir, err = ioutil.TempDir("", "interposer_test")
  29. if err != nil {
  30. panic(err)
  31. }
  32. defer os.RemoveAll(tmpDir)
  33. origPATH = os.Getenv("PATH")
  34. err = os.Setenv("PATH", "")
  35. if err != nil {
  36. panic(err)
  37. }
  38. return m.Run()
  39. }())
  40. }
  41. func setup(t *testing.T) string {
  42. f, err := ioutil.TempFile(tmpDir, "interposer")
  43. if err != nil {
  44. t.Fatal(err)
  45. }
  46. defer f.Close()
  47. err = ioutil.WriteFile(f.Name()+"_origpath", []byte(origPATH), 0666)
  48. if err != nil {
  49. t.Fatal(err)
  50. }
  51. return f.Name()
  52. }
  53. func TestInterposer(t *testing.T) {
  54. interposer := setup(t)
  55. logConfig := func(name string) paths.PathConfig {
  56. if name == "true" {
  57. return paths.PathConfig{
  58. Log: false,
  59. Error: false,
  60. }
  61. } else if name == "path_interposer_test_not_allowed" {
  62. return paths.PathConfig{
  63. Log: false,
  64. Error: true,
  65. }
  66. }
  67. return paths.PathConfig{
  68. Log: true,
  69. Error: false,
  70. }
  71. }
  72. testCases := []struct {
  73. name string
  74. args []string
  75. exitCode int
  76. err error
  77. logEntry string
  78. }{
  79. {
  80. name: "direct call",
  81. args: []string{interposer},
  82. exitCode: 1,
  83. err: usage,
  84. },
  85. {
  86. name: "relative call",
  87. args: []string{filepath.Base(interposer)},
  88. exitCode: 1,
  89. err: usage,
  90. },
  91. {
  92. name: "true",
  93. args: []string{"/my/path/true"},
  94. },
  95. {
  96. name: "relative true",
  97. args: []string{"true"},
  98. },
  99. {
  100. name: "exit code",
  101. args: []string{"bash", "-c", "exit 42"},
  102. exitCode: 42,
  103. logEntry: "bash",
  104. },
  105. {
  106. name: "signal",
  107. args: []string{"bash", "-c", "kill -9 $$"},
  108. exitCode: 137,
  109. logEntry: "bash",
  110. },
  111. {
  112. name: "does not exist",
  113. args: []string{"path_interposer_test_does_not_exist"},
  114. exitCode: 1,
  115. err: fmt.Errorf(`exec: "path_interposer_test_does_not_exist": executable file not found in $PATH`),
  116. logEntry: "path_interposer_test_does_not_exist",
  117. },
  118. {
  119. name: "not allowed",
  120. args: []string{"path_interposer_test_not_allowed"},
  121. exitCode: 1,
  122. err: fmt.Errorf(`"path_interposer_test_not_allowed" is not allowed to be used. See https://android.googlesource.com/platform/build/+/master/Changes.md#PATH_Tools for more information.`),
  123. logEntry: "path_interposer_test_not_allowed",
  124. },
  125. }
  126. for _, testCase := range testCases {
  127. t.Run(testCase.name, func(t *testing.T) {
  128. logged := false
  129. logFunc := func(logSocket string, entry *paths.LogEntry, done chan interface{}) {
  130. defer close(done)
  131. logged = true
  132. if entry.Basename != testCase.logEntry {
  133. t.Errorf("unexpected log entry:\nwant: %q\n got: %q", testCase.logEntry, entry.Basename)
  134. }
  135. }
  136. exitCode, err := Main(ioutil.Discard, ioutil.Discard, interposer, testCase.args, mainOpts{
  137. sendLog: logFunc,
  138. config: logConfig,
  139. })
  140. errstr := func(err error) string {
  141. if err == nil {
  142. return ""
  143. }
  144. return err.Error()
  145. }
  146. if errstr(testCase.err) != errstr(err) {
  147. t.Errorf("unexpected error:\nwant: %v\n got: %v", testCase.err, err)
  148. }
  149. if testCase.exitCode != exitCode {
  150. t.Errorf("expected exit code %d, got %d", testCase.exitCode, exitCode)
  151. }
  152. if !logged && testCase.logEntry != "" {
  153. t.Errorf("no log entry, but expected %q", testCase.logEntry)
  154. }
  155. })
  156. }
  157. }
  158. func TestMissingPath(t *testing.T) {
  159. interposer := setup(t)
  160. err := os.Remove(interposer + "_origpath")
  161. if err != nil {
  162. t.Fatal("Failed to remove:", err)
  163. }
  164. exitCode, err := Main(ioutil.Discard, ioutil.Discard, interposer, []string{"true"}, mainOpts{})
  165. if err != usage {
  166. t.Errorf("Unexpected error:\n got: %v\nwant: %v", err, usage)
  167. }
  168. if exitCode != 1 {
  169. t.Errorf("expected exit code %d, got %d", 1, exitCode)
  170. }
  171. }