"net/http"
"net/http/httptest"
"os"
+ "path/filepath"
"reflect"
"runtime"
"strings"
const (
configPath = "testdata/config.toml"
- statePath = "testdata/state.json"
+ statePath = "testdata/var/state.json"
passwdPath = "testdata/passwd.nsscash"
plainPath = "testdata/plain"
groupPath = "testdata/group.nsscash"
}
}
-// mustMakeOld verifies that all paths have a modification time in the past,
-// as set by mustMakeOld().
+// mustBeOld verifies that all paths have a modification time in the past, as
+// set by mustMakeOld.
func mustBeOld(t *testing.T, paths ...string) {
for _, p := range paths {
i, err := os.Stat(p)
// Perform most tests with passwd for simplicity
fetchPasswdCacheFileDoesNotExist,
fetchPasswd404,
+ fetchPasswdUnexpected304,
fetchPasswdEmpty,
fetchPasswdInvalid,
fetchPasswdLimits,
// Remove the file at the end of this test run, if it
// was created
defer os.Remove(p)
+
+ dir := filepath.Dir(p)
+ err = os.MkdirAll(dir, 0755)
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer os.Remove(dir) // remove empty directories
}
var handler func(http.ResponseWriter, *http.Request)
mustBeOld(a.t, passwdPath)
}
+func fetchPasswdUnexpected304(a args) {
+ t := a.t
+ mustWritePasswdConfig(t, a.url)
+ mustCreate(t, passwdPath)
+
+ *a.handler = func(w http.ResponseWriter, r *http.Request) {
+ // 304
+ w.WriteHeader(http.StatusNotModified)
+ }
+
+ err := mainFetch(configPath)
+ mustBeErrorWithSubstring(t, err,
+ "status code 304 but did not send If-Modified-Since")
+
+ mustNotExist(t, statePath, plainPath, groupPath)
+ mustBeOld(a.t, passwdPath)
+}
+
func fetchPasswdEmpty(a args) {
t := a.t
mustWritePasswdConfig(t, a.url)
mustHaveHash(t, groupPath, "da39a3ee5e6b4b0d3255bfef95601890afd80709")
*a.handler = func(w http.ResponseWriter, r *http.Request) {
- // To prevent mainFetch() from trying to update groupPath
- // which will also fail
- w.WriteHeader(http.StatusNotModified)
+ if r.URL.Path != "/group" {
+ return
+ }
+
+ fmt.Fprintln(w, "root:x:0:")
+ fmt.Fprintln(w, "daemon:x:1:andariel,duriel,mephisto,diablo,baal")
}
- err := os.Chmod("testdata", 0500)
+ err := os.Chmod(filepath.Dir(statePath), 0500)
if err != nil {
t.Fatal(err)
}
- defer os.Chmod("testdata", 0755)
+ defer os.Chmod(filepath.Dir(statePath), 0755)
err = mainFetch(configPath)
mustBeErrorWithSubstring(t, err,
"permission denied")
mustNotExist(t, statePath, passwdPath, plainPath)
- mustBeOld(t, groupPath)
+ mustBeNew(t, groupPath)
+ mustHaveHash(t, groupPath, "8c27a8403278ba2e392b86d98d4dff1fdefcafdd")
}
func fetchCannotDeploy(a args) {