@ -6,7 +6,6 @@ import (
"io"
"math/rand"
"path/filepath"
"runtime/debug"
"testing"
"dehub.dev/src/dehub.git/sigcred"
@ -21,40 +20,16 @@ type harness struct {
rand * rand . Rand
repo * Repo
cfg * Config
sig sigcred . SignifierInterface
}
func newHarness ( t * testing . T ) * harness {
rand := rand . New ( rand . NewSource ( 0xb4eadb01 ) )
sig , pubKeyBody := sigcred . TestSignifierPGP ( "root" , false , rand )
pubKeyPath := filepath . Join ( DehubDir , "root.asc" )
cfg := & Config {
Accounts : [ ] Account { {
ID : "root" ,
Signifiers : [ ] sigcred . Signifier { { PGPPublicKeyFile : & sigcred . SignifierPGPFile {
Path : pubKeyPath ,
} } } ,
} } ,
}
cfgBody , err := yaml . Marshal ( cfg )
if err != nil {
t . Fatal ( err )
}
h := & harness {
return & harness {
t : t ,
rand : rand ,
repo : InitMemRepo ( ) ,
cfg : cfg ,
sig : sig ,
cfg : new ( Config ) ,
}
h . stage ( map [ string ] string {
ConfigPath : string ( cfgBody ) ,
pubKeyPath : string ( pubKeyBody ) ,
} )
return h
}
func ( h * harness ) stage ( tree map [ string ] string ) {
@ -100,6 +75,27 @@ func (h *harness) stageCfg() {
h . stage ( map [ string ] string { ConfigPath : string ( cfgBody ) } )
}
func ( h * harness ) stageNewAccount ( accountID string , anon bool ) sigcred . SignifierInterface {
sig , pubKeyBody := sigcred . TestSignifierPGP ( accountID , anon , h . rand )
if ! anon {
h . cfg . Accounts = append ( h . cfg . Accounts , Account {
ID : accountID ,
Signifiers : [ ] sigcred . Signifier { { PGPPublicKey : & sigcred . SignifierPGP {
Body : string ( pubKeyBody ) ,
} } } ,
} )
h . stageCfg ( )
}
return sig
}
func ( h * harness ) stageAccessControls ( aclYAML string ) {
if err := yaml . Unmarshal ( [ ] byte ( aclYAML ) , & h . cfg . AccessControls ) ; err != nil {
h . t . Fatal ( err )
}
h . stageCfg ( )
}
func ( h * harness ) checkout ( branch plumbing . ReferenceName ) {
w , err := h . repo . GitRepo . Worktree ( )
@ -195,7 +191,8 @@ func (h *harness) tryCommit(
return gitCommit
}
func ( h * harness ) changeCommit (
func ( h * harness ) assertCommitChange (
shouldSucceed bool ,
msg string ,
sig sigcred . SignifierInterface ,
) GitCommit {
@ -203,18 +200,17 @@ func (h *harness) changeCommit(
if err != nil {
h . t . Fatalf ( "creating ChangeCommit: %v" , err )
}
return h . tryCommit ( true , commit , sig )
return h . tryCommit ( shouldSucceed , commit , sig )
}
func TestHasStagedChanges ( t * testing . T ) {
harness := newHarness ( t )
h := newHarness ( t )
rootSig := h . stageNewAccount ( "root" , false )
assertHasStaged := func ( expHasStaged bool ) {
hasStaged , err := harness . repo . HasStagedChanges ( )
hasStaged , err := h . repo . HasStagedChanges ( )
if err != nil {
debug . PrintStack ( )
t . Fatalf ( "error calling HasStagedChanges: %v" , err )
} else if hasStaged != expHasStaged {
debug . PrintStack ( )
t . Fatalf ( "expected HasStagedChanges to return %v" , expHasStaged )
}
}
@ -222,64 +218,17 @@ func TestHasStagedChanges(t *testing.T) {
// the harness starts with some staged changes
assertHasStaged ( true )
harness . stage ( map [ string ] string { "foo" : "bar" } )
h . stage ( map [ string ] string { "foo" : "bar" } )
assertHasStaged ( true )
harness . changeCommit ( "first commit" , harness . s ig )
h . assertCommitChange ( true , "first commit" , rootS ig)
assertHasStaged ( false )
harness . stage ( map [ string ] string { "foo" : "" } ) // delete foo
h . stage ( map [ string ] string { "foo" : "" } ) // delete foo
assertHasStaged ( true )
harness . changeCommit ( "second commit" , harness . s ig )
h . assertCommitChange ( true , "second commit" , rootS ig)
assertHasStaged ( false )
}
// TestOldConfig tests that having an older, now malformed, Config doesn't mess
// with the current parsing, as long as the default access controls still work.
func TestOldConfig ( t * testing . T ) {
harness := newHarness ( t )
// overwrite the currently staged config file with an older form
harness . stage ( map [ string ] string { ConfigPath : `
-- -
accounts :
- id : root
signifiers :
- type : pgp_public_key_file
path : ".dehub/root.asc"
access_controls :
- pattern : "**"
condition :
type : signature
account_ids :
- root
count : 0
` } )
// this commit should be created and verify fine
harness . changeCommit ( "first commit, this is going great" , harness . sig )
// this commit should not be verifiable, because toot isn't in accounts and
// the default access controls should be being used
harness . stage ( map [ string ] string { "foo" : "no rules!" } )
badCommit , err := harness . repo . NewCommitChange ( "ain't no laws" )
if err != nil {
t . Fatalf ( "creating CommitChange: %v" , err )
}
harness . tryCommit ( false , badCommit , nil )
// make a commit fixing the config. everything should still be fine.
harness . stage ( map [ string ] string { ConfigPath : `
-- -
accounts :
- id : root
signifiers :
- type : pgp_public_key_file
path : ".dehub/root.asc"
` } )
harness . changeCommit ( "Fix the config!" , harness . sig )
}
// TestThisRepoStillVerifies opens this actual repository and ensures that all
// commits in it still verify, given this codebase.
func TestThisRepoStillVerifies ( t * testing . T ) {
@ -310,10 +259,11 @@ func TestThisRepoStillVerifies(t *testing.T) {
}
func TestShortHashResolving ( t * testing . T ) {
// TODO ideally this test would test the conflicting hashes are noticed, but
// that's hard...
// TODO ideally this test would test that conflicting hashes are noticed,
// but that's hard...
h := newHarness ( t )
hash := h . changeCommit ( "first commit" , h . sig ) . GitCommit . Hash
rootSig := h . stageNewAccount ( "root" , false )
hash := h . assertCommitChange ( true , "first commit" , rootSig ) . GitCommit . Hash
hashStr := hash . String ( )
t . Log ( hashStr )