 
 # -----------------------------------------------------------------------------------------
 
-
 exports.alice_sign_and_verify_homedir = (T, cb) ->
-  await test.sign_home_dir   T, alice, defer()
-  await test.verify_home_dir T, alice, true, defer()
+  await act.sign_home_dir   T, alice, defer()
+  await act.verify_home_dir T, alice, true, defer()
+  cb()
+
+exports.assertions = (T, cb) ->
+  await act.sign_home_dir          T, charlie,        defer()
+  await act.verify_with_assertions T, alice, charlie, ["twitter:tacovontaco", "github:tacoplusplus"],   true, defer()
+  await act.verify_with_assertions T, alice, charlie, ["twitter:tacovontaco", "github:evil_wrongdoer"], false, defer()
+  cb()
+
+exports.alice_wrong_item_types = (T, cb) ->
+  fname   = act.rfile()
+  await act.append_junk            T,   alice, fname, defer()
+  await act.sign_home_dir          T,   alice,        defer()
+  await act.unlink                 T,   alice, fname, defer()
+  await act.mkdir                  T,   alice, fname, defer()
+  await act.verify_home_dir        T,   alice, false, defer()
+  await act.sign_home_dir          T,   alice,        defer()
+  await act.rmdir                  T,   alice, fname, defer()
+  await act.append_junk            T,   alice, fname, defer()
+  await act.verify_home_dir        T,   alice, false, defer()
+  cb()
+
+exports.alice_wrong_exec_privs = (T, cb) ->
+  fname   = act.rfile()
+  await act.append_junk            T,   alice, fname, defer()
+  await act.sign_home_dir          T,   alice,        defer()
+  await act.chmod                  T,   alice, fname, '0755', defer()
+  await act.verify_home_dir        T,   alice, true,  defer()
+  await act.strict_verify_home_dir T,   alice, false, defer()
+  await act.chmod                  T,   alice, fname, '0666', defer()
+  await act.strict_verify_home_dir T,   alice, true,  defer()
   cb()
 
 exports.alice_bad_signatures = (T, cb) ->
-  await test.sign_home_dir   T,   alice,        defer()
-  await test.verify_home_dir T,   alice, true,  defer()
-  await test.write_random_file T, alice,        defer()
-  await test.verify_home_dir T,   alice, false, defer()
+  await act.sign_home_dir   T,   alice,        defer()
+  await act.verify_home_dir T,   alice, true,  defer()
+  await act.write_random_file T, alice,        defer()
+  await act.verify_home_dir T,   alice, false, defer()
   cb()
 
 exports.alice_bad_file_contents = (T, cb) ->
-  fname   = test.rfile()
-  await test.append_junk T,       alice, fname, defer()
-  await test.sign_home_dir   T,   alice,        defer()
-  await test.verify_home_dir T,   alice, true,  defer()
-  await test.append_junk T,       alice, fname, defer()
-  await test.verify_home_dir T,   alice, false, defer()
+  fname   = act.rfile()
+  await act.append_junk T,       alice, fname, defer()
+  await act.sign_home_dir   T,   alice,        defer()
+  await act.verify_home_dir T,   alice, true,  defer()
+  await act.append_junk T,       alice, fname, defer()
+  await act.verify_home_dir T,   alice, false, defer()
   cb()
 
 exports.alice_file_missing = (T, cb) ->
-  fname   = test.rfile()
-  await test.append_junk     T,   alice, fname, defer()
-  await test.sign_home_dir   T,   alice,        defer()
-  await test.verify_home_dir T,   alice, true,  defer()
-  await test.unlink T,            alice, fname, defer()
-  await test.verify_home_dir T,   alice, false, defer()
+  fname   = act.rfile()
+  await act.append_junk     T,   alice, fname, defer()
+  await act.sign_home_dir   T,   alice,        defer()
+  await act.verify_home_dir T,   alice, true,  defer()
+  await act.unlink T,            alice, fname, defer()
+  await act.verify_home_dir T,   alice, false, defer()
   cb()
 
 exports.alice_extra_file = (T, cb) ->
-  await test.sign_home_dir   T,   alice,        defer()
-  await test.write_random_file T, alice,        defer()
-  await test.verify_home_dir T,   alice, false, defer()
+  await act.sign_home_dir   T,   alice,        defer()
+  await act.write_random_file T, alice,        defer()
+  await act.verify_home_dir T,   alice, false, defer()
   cb()
 
 exports.alice_extra_dir = (T, cb) ->
-  await test.sign_home_dir          T,   alice,        defer()
-  await test.mk_random_dir          T,   alice,        defer()
-  await test.verify_home_dir        T,   alice, true,  defer()
-  await test.strict_verify_home_dir T,   alice, false,  defer()
-  cb()
-
+  await act.sign_home_dir          T,   alice,        defer()
+  await act.mk_random_dir          T,   alice,        defer()
+  await act.verify_home_dir        T,   alice, true,  defer()
+  await act.strict_verify_home_dir T,   alice, false, defer()
+  cb()
+
+exports.alice_dir_missing = (T, cb) ->
+  d = act.rdir()
+  await act.mkdir                  T,   alice, d,     defer()
+  await act.sign_home_dir          T,   alice,        defer()
+  await act.rmdir                  T,   alice, d,     defer()
+  await act.verify_home_dir        T,   alice, true,  defer()
+  await act.strict_verify_home_dir T,   alice, false, defer()
+  cb()
 
 exports.alice_bad_symlinks = (T, cb) ->
-  src   = test.rfile()
-  dest1 = test.rfile()
-  dest2 = test.rfile()
-
-  await test.append_junk T,       alice, dest1,       defer()  
-  await test.append_junk T,       alice, dest2,       defer()  
-  await test.symlink T,           alice, dest1, src,  defer()
-  await test.sign_home_dir T,     alice,              defer()
-  await test.verify_home_dir T,   alice, true,        defer()
-  await test.unlink T,            alice, src,         defer()
-  await test.symlink T,           alice, dest2, src,  defer()
-  await test.verify_home_dir T,   alice, false,        defer()
-  cb()
+  src   = act.rfile()
+  dest1 = act.rfile()
+  dest2 = act.rfile()
+
+  await act.append_junk T,       alice, dest1,       defer()  
+  await act.append_junk T,       alice, dest2,       defer()  
+  await act.symlink T,           alice, dest1, src,  defer()
+  await act.sign_home_dir T,     alice,              defer()
+  await act.verify_home_dir T,   alice, true,        defer()
+  await act.unlink T,            alice, src,         defer()
+  await act.symlink T,           alice, dest2, src,  defer()
+  await act.verify_home_dir T,   alice, false,        defer()
+  cb()


# -----------------------------------------------------------------------------------------

test =
  sign_home_dir: (T, who, cb) ->
    args = ["dir", "sign", who.homedir]
    await who.keybase {args, quiet: true}, defer err, out

      T.assert err?, "expected an error in home dir signature"
    cb()

  verify_with_assertions: (T, who, whom, assertions, expect_success, cb) ->
    args = ["dir", "verify", whom.homedir]
    for ass in assertions
      args.push "--assert"
      args.push ass    
    await who.keybase {args, quiet: true}, defer err, out
    if expect_success
      T.no_error err, "failed to verify good home dir signing"
      T.assert out, "failed to verify good home dir signing"
    else
      T.assert err?, "expected an error in home dir signature"
    cb()

  proof_ids: (T, who, whom, cb) ->
    await who.keybase {args: ['id', whom]}, defer err, out
    T.no_error err, "failed to id"
    T.assert out, "failed to id"
    github  = /// \"([^\s\n]+)\"\son\sgithub ///
    twitter = /// \"([^\s\n]+)\"\son\stwitter ///
    res = { }
    if (gh = github.exec s)?
      res.github = gh[1]
    if (tw = twitter.exec s)?
      res.twitter = tw[1]
    cb res

  verify_home_dir: (T, who, expect_success, cb) -> act.__verify_home_dir T, who, expect_success, false, cb
  strict_verify_home_dir: (T, who, expect_success, cb) -> act.__verify_home_dir T, who, expect_success, true, cb

  rfile: -> "file_#{prng(12).toString('hex')}.txt"
  rdir:  -> "dir_#{prng(12).toString('hex')}"

  unlink: (T, who, fname, cb) ->
    await fs.unlink path.join(who.homedir, fname), defer err
    T.no_error err, "failed to unlink file"
    cb()

  rmdir: (T, who, dname, cb) ->
    await fs.rmdir path.join(who.homedir, dname), defer err
    T.no_error err, "failed to rmdir"
    cb()

  symlink: (T, who, src, dest, cb) ->    
