aboutsummaryrefslogtreecommitdiff
path: root/tests/sha256/driver.cxx
blob: 43c990500a93b54d3aa3b2bad8e5d4b574713f20 (plain)
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
// file      : tests/sha256/driver.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2019 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#include <cassert>

#ifndef __cpp_lib_modules
#include <string>
#include <cstddef> // size_t
#endif

// Other includes.

#ifdef __cpp_modules
#ifdef __cpp_lib_modules
import std.core;
#endif
import butl.path;
import butl.sha256;
import butl.fdstream;
import butl.filesystem;
#else
#include <libbutl/path.mxx>
#include <libbutl/sha256.mxx>
#include <libbutl/fdstream.mxx>
#include <libbutl/filesystem.mxx> // auto_rmfile
#endif

using namespace std;
using namespace butl;

int
main ()
{
  assert (string (sha256 ().string ()) ==
          "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");

  assert (string (sha256 ("").string ()) !=
          "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");

  assert (string (sha256 ("123", 3).string ()) ==
          "a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3");

  {
    string s;
    for (size_t i (0); i < 1024; ++i)
      s += "0123456789";

    path p (path::temp_path ("butl-sha256"));

    auto_rmfile pr;       // Always remove the file after the stream is closed.
    ofdstream os (p);
    pr = auto_rmfile (p);

    os << s;
    os.close ();

    ifdstream is (p);

    assert (string (sha256 (is).string ()) ==
            sha256 (s.c_str (), s.size ()).string ());

    assert (is.eof ());
    is.close ();
  }

  {
    sha256 h ("123");

    assert (string (h.string ()) ==
            "a787b6772e3e4df1b2a04d5eee56f8570ab38825eed1b6a9bda288429b7f29a1");

    assert (h.abbreviated_string (10) == "a787b6772e");
    assert (h.abbreviated_string (65) == h.string ());
  }

  {
    sha256 h;
    h.append ("1");
    h.append (string ("2"));
    h.append ("3", 1);

    auto& b (h.binary ());
    assert (b[0] == 0x20 && b[31] == 0x9d);

    assert (string (h.string ()) ==
            "204d9db65789fbede7829ed77f72ba1f0fe21a833d95abad4849b82f33a69b9d");
  }

  // Test fast path.
  //
  {
    char c ('X');
    sha256 h;
    h.append (c);
    assert (string (h.string ()) == sha256 (&c, 1).string ());
  }

  //
  //
  string fp ("F4:9D:C0:02:C6:B6:62:06:A5:48:AE:87:35:32:95:64:C2:B8:C9:6D:9B:"
             "28:85:6D:EF:CA:FA:7F:04:B5:4F:A6");

  string sh (
    "f49dc002c6b66206a548ae8735329564c2b8c96d9b28856defcafa7f04b54fa6");

  assert (fingerprint_to_sha256 (fp) == sh);
  assert (fingerprint_to_sha256 (fp, 65) == sh);
  assert (fingerprint_to_sha256 (fp, 10) == sh.substr (0, 10));

  assert (sha256_to_fingerprint (sh) == fp);
}