Imported Upstream version 16.3.2
[platform/upstream/libzypp.git] / tests / zypp / KeyRingTestReceiver.h
1
2 #ifndef ZYPP_KEYRING_TEST_RECEIVER_H
3 #define ZYPP_KEYRING_TEST_RECEIVER_H
4
5 #include "zypp/Callback.h"
6 #include "zypp/KeyRing.h"
7 #include "zypp/PublicKey.h"
8 #include "zypp/KeyContext.h"
9
10 /**
11  * Keyring Callback Receiver with some features
12  * Allows to simulate and configure user answer
13  * Can record which callbacks were called
14  */
15 struct KeyRingTestReceiver : public zypp::callback::ReceiveReport<zypp::KeyRingReport>
16 {
17   KeyRingTestReceiver()
18   {
19     reset();
20     connect();
21   }
22
23   void reset()
24   {
25     _answer_accept_unknown_key = false;
26     _answer_accept_key = KeyRingReport::KEY_DONT_TRUST;
27     _answer_ver_failed = false;
28     _answer_accept_unsigned_file = false;
29     _asked_user_to_accept_unknown_key = false;
30     _asked_user_to_accept_key = false;
31     _asked_user_to_accept_ver_failed = false;
32     _asked_user_to_accept_unsigned_file = false;
33   }
34   
35   ~KeyRingTestReceiver()
36   {
37     disconnect();
38   }
39   
40   void answerAcceptVerFailed( bool answer )
41   { _answer_ver_failed = answer; }
42   
43   bool askedAcceptVerFailed() const
44   { return _asked_user_to_accept_ver_failed; }
45   
46   void answerAcceptUnknownKey( bool answer )
47   { _answer_accept_unknown_key = answer; }
48   
49   bool askedAcceptUnknownKey() const
50   { return _asked_user_to_accept_unknown_key; }
51   
52   void answerAcceptKey( KeyRingReport::KeyTrust answer )
53   { _answer_accept_key = answer; }
54   
55   bool askedAcceptKey() const
56   { return _asked_user_to_accept_key; }
57
58   void answerAcceptUnsignedFile( bool answer )
59   { _answer_accept_unsigned_file = answer; }
60   
61   bool askedAcceptUnsignedFile() const
62   { return _asked_user_to_accept_unsigned_file; }
63     
64   virtual bool askUserToAcceptUnsignedFile( const std::string &file, const zypp::KeyContext &keycontext )
65   {
66     MIL << std::endl;
67     _asked_user_to_accept_unsigned_file = true;
68     return _answer_accept_unsigned_file;
69   }
70   
71   virtual bool askUserToAcceptUnknownKey( const std::string &file, const std::string &id, const zypp::KeyContext &keycontext )
72   {
73     MIL << std::endl;
74     _asked_user_to_accept_unknown_key = true;
75     return _answer_accept_unknown_key;
76   }
77
78   virtual KeyRingReport::KeyTrust askUserToAcceptKey( const zypp::PublicKey &key, const zypp::KeyContext &keycontext )
79   {
80     MIL << std::endl;
81     _asked_user_to_accept_key = true;
82     return _answer_accept_key;
83   }
84
85   virtual bool askUserToAcceptVerificationFailed( const std::string &file,  const zypp::PublicKey &key, const zypp::KeyContext &keycontext  )
86   {
87     MIL << std::endl;
88     _asked_user_to_accept_ver_failed = true;
89     return _answer_ver_failed;
90   }
91   
92   // how to answer
93   bool _answer_accept_unknown_key;
94   KeyRingReport::KeyTrust _answer_accept_key;
95   bool _answer_ver_failed;
96   bool _answer_accept_unsigned_file;
97   
98   // we use this variables to check that the
99   // callbacks were called
100   bool _asked_user_to_accept_unknown_key;
101   bool _asked_user_to_accept_key;
102   bool _asked_user_to_accept_ver_failed;
103   bool _asked_user_to_accept_unsigned_file;
104 };
105
106 /**
107  * Keyring Signal Receiver with some features
108  * Allows to simulate and configure user answer
109  * Can record which callbacks were called
110  */
111 struct KeyRingTestSignalReceiver : zypp::callback::ReceiveReport<zypp::KeyRingSignals>
112 {
113   KeyRingTestSignalReceiver(/*RpmDb &rpmdb*/)
114   : _trusted_key_added_called(false)
115   {
116     MIL << "KeyRing signals enabled" << std::endl;
117     connect();
118   }
119
120   ~KeyRingTestSignalReceiver()
121   {
122     disconnect();
123   }
124
125   virtual void trustedKeyAdded( const zypp::PublicKey &key )
126   {
127     MIL << "TEST: trusted key added to zypp Keyring. Synchronizing keys with fake rpm keyring" << std::endl;
128     _trusted_key_added_called = true;
129     //std::cout << "trusted key added to zypp Keyring. Synchronizing keys with rpm keyring" << std::endl;
130     //_rpmdb.importZyppKeyRingTrustedKeys();
131     //_rpmdb.exportTrustedKeysInZyppKeyRing();
132   }
133
134   virtual void trustedKeyRemoved( const zypp::PublicKey &key  )
135   {
136   }
137   
138   bool _trusted_key_added_called;
139   
140 };
141
142 #endif