aboutsummaryrefslogtreecommitdiffstats
path: root/src/tests/test_datastore.cpp
blob: ef1f55cd8ea3d1a64914087a8fea1718337fad02 (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
/*
* (C) 2018 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/

#include "tests.h"

#if defined(BOTAN_HAS_X509_CERTIFICATES)

#include <botan/datastor.h>

namespace Botan_Tests {

class Datastore_Tests : public Test
   {
   public:
      std::vector<Test::Result> run() override
         {
         Test::Result result("Data_Store");

         Botan::Data_Store ds1;
         Botan::Data_Store ds2;

         result.confirm("equality", ds1 == ds2);

         result.test_eq("has_value", ds1.has_value("key"), false);
         result.test_eq("get1 with default", ds1.get1("key", "default"), "default");

         result.test_throws("get1", "Data_Store::get1: No values set for missing_key",
                            [&ds1]() { ds1.get1("missing_key"); });

         result.test_eq("search_for", ds1.search_for([](std::string, std::string) { return true; }).size(), 0);

         ds1.add("key", "value");

         result.test_eq("search_for", ds1.search_for([](std::string, std::string) { return true; }).size(), 1);

         result.test_eq("equality", ds1 == ds2, false);

         result.test_eq("has_value", ds1.has_value("key"), true);
         result.test_eq("get1 with default", ds1.get1("key", "default"), "value");
         result.test_eq("get1", ds1.get1("key"), "value");

         result.test_eq("get1_memvec", ds1.get1_memvec("memvec").size(), 0);

         const std::vector<uint8_t> memvec = { 9, 1, 1, 4 };
         ds1.add("memvec", memvec);

         result.test_eq("get1_memvec", ds1.get1_memvec("memvec"), memvec);

         result.test_eq("get1_uint32", ds1.get1_uint32("memvec"), size_t(9010104));

         result.test_eq("get1_uint32", ds1.get1_uint32("missing", 999), size_t(999));

         ds1.add("key", "value2");

         result.test_throws("get1", "Data_Store::get1: More than one value for key",
                            [&ds1]() { ds1.get1("key"); });

         result.test_eq("get multiple", ds1.get("key").size(), 2);

         return {result};
         }
   };

BOTAN_REGISTER_TEST("x509_datastore", Datastore_Tests);

}

#endif