blob: 83c696e23123517608452136d5f31318772d6114 [file] [log] [blame]
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Unit tests for the apiHandler implementation of SysAPIHandler.
package sysapi
import (
"io/ioutil"
"os"
"reflect"
"testing"
)
// TestReadFile tests that we can correctly read a file.
func TestReadFile(t *testing.T) {
handler, err := NewHandler()
if err != nil {
t.Fatal(err)
}
// Create a temporary file and tests that we can successfully read its
// content.
f, err := ioutil.TempFile("", "policy_manager_test_")
if err != nil {
t.Error(err)
}
want := []byte("hello world!\n\x12\x03")
if err := ioutil.WriteFile(f.Name(), want, 0644); err != nil {
t.Error(err)
}
if result, err := handler.ReadFile(f.Name()); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(result, want) {
t.Errorf("got %v, want %v", result, want)
}
if err := os.Remove(f.Name()); err != nil {
t.Error(err)
}
}
// TestAtmoicWriteFile tests that a file is correctly written. It does not test
// the atomicity of the write.
func TestAtomicWriteFile(t *testing.T) {
handler, err := NewHandler()
if err != nil {
t.Fatal(err)
}
// Create a temporary directory to write the test file.
dir, err := ioutil.TempDir("", "policy_manager_test_")
if err != nil {
t.Fatal(err)
}
// Make the test file, to be replaced atomically later.
testFileName := dir + "/test"
err = ioutil.WriteFile(testFileName, []byte("abc"), 0644)
if err != nil {
t.Error(err)
}
// Replace the test file atomically.
data := []byte("hello")
err = handler.AtomicWriteFile(testFileName, data, 0644)
if err != nil {
t.Error(err)
}
// Make sure the file content is updated.
content, err := ioutil.ReadFile(testFileName)
if err != nil {
t.Error(err)
} else if !reflect.DeepEqual(content, data) {
t.Errorf("got %s, want %s", string(content), string(data))
}
// Clean up.
if err := os.RemoveAll(dir); err != nil {
t.Error(err)
}
}
// TestRunCommandOutput tests whether we can get the correct stdout and stderr
// from executing different commands.
func TestRunCommandOutput(t *testing.T) {
handler, err := NewHandler()
if err != nil {
t.Fatal(err)
}
tests := []struct {
name string
expectStdout string
expectStderr string
prog string
args []string
expectErr bool
}{
{
"EchoStdout",
"Hello World!",
"",
"sh",
[]string{
"-c",
`echo -n "Hello World!"`,
},
false,
},
{
"EchoStderr",
"",
"Hello World!",
"sh",
[]string{
"-c",
`echo -n "Hello World!" 1>&2`,
},
false,
},
{
"NonExistentCommand",
"",
"",
"doesnotexistdon'trunme",
nil,
true,
},
{
"OnlyStdout",
"Hello World!\n",
"",
"echo",
[]string{
`Hello World!`,
},
false,
},
}
for _, test := range tests {
stdout, stderr, err := handler.RunCommand(test.prog, test.args...)
if err == nil {
if test.expectErr {
t.Fatalf("Test %s got stdout: %s and stderr: %s, want error",
test.name,
string(stdout),
string(stderr))
}
if test.expectStdout != string(stdout) {
t.Errorf("Test %s got stdout %s, want %s",
test.name,
string(stdout),
test.expectStdout)
}
if test.expectStderr != string(stderr) {
t.Errorf("Test %s got stderr %s, want %s",
test.name,
string(stderr),
test.expectStderr)
}
} else if !test.expectErr {
t.Errorf("Test %s got unexpected error %v",
test.name,
err)
}
}
}
// TestFileExists test that the FileExists correctly identifies if a file
// exists or not.
func TestFileExists(t *testing.T) {
handler, err := NewHandler()
if err != nil {
t.Fatal(err)
}
// Create a temp file.
f, err := ioutil.TempFile(".", "policy_manager-sysapi-test")
if err != nil {
t.Fatal(err)
}
// Verify that the temp file exists.
if !handler.FileExists(f.Name()) {
t.Errorf("Got %s doesn't exists, expect to exist", f.Name())
}
// Remove the temp file.
if err := os.Remove(f.Name()); err != nil {
t.Error(err)
}
// Verify that a non-existent file doesn't exist.
if handler.FileExists("/var/ewoif/feiwjfij") {
t.Error("A non-existent file is reported as existing")
}
}