1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
open Conex_utils
open Conex_resource

module SIGN = Conex_nocrypto.NC_S

let sset =
  let module M = struct
    type t = S.t
    let pp ppf v = Format.fprintf ppf "%a" (pp_list pp_id) (S.elements v)
    let equal = S.equal
  end in
  (module M: Alcotest.TESTABLE with type t = M.t)

let privkey = ref None

let gen_pub () =
  let priv = match !privkey with
    | Some p -> p
    | None ->
      let p = Conex_nocrypto.NC_S.generate ~bits:2048 Uint.zero () in
      privkey := Some p ;
      p
  in
  match Conex_nocrypto.NC_S.pub_of_priv priv with
  | Ok pub -> (pub, priv)
  | Error e -> Alcotest.fail e

let result (type a) (type e) a e =
  let (module A: Alcotest.TESTABLE with type t = a) = a in
  let (module E: Alcotest.TESTABLE with type t = e) = e in
  let module M = struct
    type t = (a, e) result
    let pp fmt t = match t with
      | Ok    t -> Format.fprintf fmt "Ok @[(%a)@]" A.pp t
      | Error e -> Format.fprintf fmt "Error @[(%a)@]" E.pp e
    let equal x y = match x, y with
      | Ok    x, Ok    y -> A.equal x y
      | Error x, Error y -> E.equal x y
      | _      , _       -> false
  end in
  (module M: Alcotest.TESTABLE with type t = M.t)

let team_eq a b =
  let open Team in
  id_equal a.name b.name && a.counter = b.counter && S.equal a.members b.members

let team =
  let module M = struct
    type t = Team.t
    let pp = Team.pp
    let equal = team_eq
  end in
  (module M : Alcotest.TESTABLE with type t = M.t)

let idx_eq a b =
  Author.equal a b && a.Author.counter = b.Author.counter

let ji =
  let module M = struct
    type t = Author.t
    let pp = Author.pp
    let equal = idx_eq
  end in
  (module M : Alcotest.TESTABLE with type t = M.t)

let id =
  let module M = struct
    type t = [ `Author of Author.t | `Team of Team.t ]
    let pp ppf = function `Team t -> Team.pp ppf t | `Author idx -> Author.pp ppf idx
    let equal a b = match a, b with
      | `Team t, `Team t' -> team_eq t t'
      | `Author k, `Author k' -> idx_eq k k'
      | _ -> false
  end in
  (module M : Alcotest.TESTABLE with type t = M.t)

let auth =
  let module M = struct
    type t = Authorisation.t
    let pp = Authorisation.pp
    let equal a b =
      let open Authorisation in
      a.counter = b.counter &&
      name_equal a.name b.name &&
      S.equal a.authorised b.authorised
  end in
  (module M : Alcotest.TESTABLE with type t = M.t)

let package =
  let module M = struct
    type t = Package.t
    let pp = Package.pp
    let equal a b =
      let open Package in
      a.counter = b.counter &&
      a.name = b.name &&
      S.equal a.releases b.releases
  end in
  (module M : Alcotest.TESTABLE with type t = M.t)

let rel =
  let module M = struct
    type t = Release.t
    let pp = Release.pp
    let equal a b = match Release.compare_t a b with Ok () -> true | _ -> false
  end in
  (module M : Alcotest.TESTABLE with type t = M.t)

let verr =
  let module M = struct
    type t = Conex_crypto.verification_error
    let pp = Conex_crypto.pp_verification_error
    let equal a b = match a, b with
      | `InvalidBase64Encoding, `InvalidBase64Encoding
      | `InvalidSignature, `InvalidSignature
      | `InvalidPublicKey, `InvalidPublicKey -> true
      (* for OpenSSL where we don't have detailed error reporting *)
      | `InvalidSignature, `InvalidPublicKey
      | `InvalidPublicKey, `InvalidSignature -> true
      (* until nocrypto >0.5.3 is not released *)
      | `InvalidSignature, `InvalidBase64Encoding
      | `InvalidBase64Encoding, `InvalidSignature -> true
      | _ -> false
  end in
  (module M : Alcotest.TESTABLE with type t = M.t)

let sign_idx idx p =
  let idx = List.fold_left Author.approve idx idx.Author.queued in
  match SIGN.sign Uint.zero idx p with
  | Ok idx -> idx
  | Error e -> Alcotest.fail e