sandbox_linux_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Copyright 2021 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 build
  15. import (
  16. "os"
  17. "testing"
  18. )
  19. func TestMain(m *testing.M) {
  20. // set src dir of sandbox
  21. sandboxConfig.srcDir = "/my/src/dir"
  22. os.Exit(m.Run())
  23. }
  24. func TestMountFlagsSrcDir(t *testing.T) {
  25. testCases := []struct {
  26. srcDirIsRO bool
  27. expectedSrcDirFlag string
  28. }{
  29. {
  30. srcDirIsRO: false,
  31. expectedSrcDirFlag: "-B",
  32. },
  33. {
  34. srcDirIsRO: true,
  35. expectedSrcDirFlag: "-R",
  36. },
  37. }
  38. for _, testCase := range testCases {
  39. c := testCmd()
  40. c.config.sandboxConfig.SetSrcDirIsRO(testCase.srcDirIsRO)
  41. c.wrapSandbox()
  42. if !isExpectedMountFlag(c.Args, sandboxConfig.srcDir, testCase.expectedSrcDirFlag) {
  43. t.Error("Mount flag of srcDir is not correct")
  44. }
  45. }
  46. }
  47. func TestMountFlagsSrcDirRWAllowlist(t *testing.T) {
  48. testCases := []struct {
  49. srcDirRWAllowlist []string
  50. }{
  51. {
  52. srcDirRWAllowlist: []string{},
  53. },
  54. {
  55. srcDirRWAllowlist: []string{"my/path"},
  56. },
  57. {
  58. srcDirRWAllowlist: []string{"my/path1", "my/path2"},
  59. },
  60. }
  61. for _, testCase := range testCases {
  62. c := testCmd()
  63. c.config.sandboxConfig.SetSrcDirIsRO(true)
  64. c.config.sandboxConfig.SetSrcDirRWAllowlist(testCase.srcDirRWAllowlist)
  65. c.wrapSandbox()
  66. for _, allowlistPath := range testCase.srcDirRWAllowlist {
  67. if !isExpectedMountFlag(c.Args, allowlistPath, "-B") {
  68. t.Error("Mount flag of srcDirRWAllowlist is not correct, expect -B")
  69. }
  70. }
  71. }
  72. }
  73. // utils for setting up test
  74. func testConfig() Config {
  75. // create a minimal testConfig
  76. env := Environment([]string{})
  77. sandboxConfig := SandboxConfig{}
  78. return Config{&configImpl{environ: &env,
  79. sandboxConfig: &sandboxConfig}}
  80. }
  81. func testCmd() *Cmd {
  82. return Command(testContext(), testConfig(), "sandbox_test", "path/to/nsjail")
  83. }
  84. func isExpectedMountFlag(cmdArgs []string, dirName string, expectedFlag string) bool {
  85. indexOfSrcDir := index(cmdArgs, dirName)
  86. return cmdArgs[indexOfSrcDir-1] == expectedFlag
  87. }
  88. func index(arr []string, target string) int {
  89. for idx, element := range arr {
  90. if element == target {
  91. return idx
  92. }
  93. }
  94. panic("element could not be located in input array")
  95. }