test/src/pEpTestDevice.cc
branchsync_test_refactor
changeset 3314 18a40b81649a
parent 3313 b11d8d025466
child 3315 4d8f93b6cdcb
     1.1 --- a/test/src/pEpTestDevice.cc	Sun Feb 24 22:41:22 2019 +0100
     1.2 +++ b/test/src/pEpTestDevice.cc	Mon Feb 25 12:28:51 2019 +0100
     1.3 @@ -27,8 +27,78 @@
     1.4  #include "KeySync_fsm.h"
     1.5  #include <chrono>
     1.6  
     1.7 +// Static stuff
     1.8  pEpTestDevice* pEpTestDevice::active = NULL;
     1.9  
    1.10 +PEP_STATUS pEpTestDevice::message_to_send(struct _message* msg) {
    1.11 +    pEpTestDevice::active->add_message_to_send_queue(msg);
    1.12 +    return PEP_STATUS_OK;
    1.13 +}
    1.14 +
    1.15 +PEP_STATUS pEpTestDevice::notify_handshake(pEp_identity* me,
    1.16 +                                    pEp_identity* partner,
    1.17 +                                    sync_handshake_signal signal) {
    1.18 +    // 
    1.19 +    // switch(signal) {
    1.20 +    //     case SYNC_NOTIFY_UNDEFINED:
    1.21 +    //     case SYNC_NOTIFY_TIMEOUT:
    1.22 +    //     case SYNC_NOTIFY_OVERTAKEN:
    1.23 +    //     case SYNC_NOTIFY_ACCEPTED_DEVICE_ADDED:
    1.24 +    //     case SYNC_NOTIFY_ACCEPTED_GROUP_CREATED:
    1.25 +    //     case SYNC_NOTIFY_ACCEPTED_DEVICE_MOVED:   
    1.26 +    //         return SYNC_HANDSHAKE_CANCEL;
    1.27 +    // 
    1.28 +    //     // We don't simulate the handshake process here.
    1.29 +    //     // If we need to, this is a TODO.
    1.30 +    //     case SYNC_NOTIFY_INIT_ADD_OUR_DEVICE:
    1.31 +    //     case SYNC_NOTIFY_INIT_ADD_OTHER_DEVICE:
    1.32 +    //     case SYNC_NOTIFY_INIT_FORM_GROUP:
    1.33 +    //     case SYNC_NOTIFY_INIT_MOVE_OUR_DEVICE:
    1.34 +    //         return SYNC_HANDSHAKE_ACCEPTED;
    1.35 +    // 
    1.36 +    //     default:    
    1.37 +    //         return SYNC_HANDSHAKE_REJECTED;
    1.38 +    // }
    1.39 +    return PEP_STATUS_OK;
    1.40 +}
    1.41 +
    1.42 +Sync_event_t* pEpTestDevice::retrieve_next_sync_event(void *management, unsigned int threshold)
    1.43 +{
    1.44 +    time_t started = time(nullptr);
    1.45 +    bool timeout = false;
    1.46 +    
    1.47 +    while (active->q.empty()) {
    1.48 +        int i = 0;
    1.49 +        ++i;
    1.50 +        if (i > 10) {
    1.51 +            if (time(nullptr) > started + threshold) {
    1.52 +                timeout = true;
    1.53 +                break;
    1.54 +            }   
    1.55 +            i = 0;
    1.56 +        }   
    1.57 +        nanosleep((const struct timespec[]){{0, 100000000L}}, NULL);
    1.58 +    }
    1.59 +    if (timeout)
    1.60 +        return new_sync_timeout_event();
    1.61 +
    1.62 +    return pEpTestDevice::active->q.pop_front();
    1.63 +}
    1.64 +
    1.65 +int pEpTestDevice::inject_sync_event(Sync_event* ev, void* management) {
    1.66 +    Sync_event_t *_ev = ev;
    1.67 +    switch (_ev->fsm) {
    1.68 +        case Sync_PR_keysync:
    1.69 +//            cout << "injecting event " << KeySync_event_name(_ev->event) << "\n";
    1.70 +            break;
    1.71 +        default:
    1.72 +            throw std::runtime_error("Unknown state machine.");
    1.73 +    }
    1.74 +    pEpTestDevice::active->q.push_front(ev);
    1.75 +    return 0;
    1.76 +}
    1.77 +// End static stuff
    1.78 +
    1.79  pEpTestDevice::pEpTestDevice(string test_dir, 
    1.80                               string my_name)                
    1.81  {
    1.82 @@ -102,7 +172,7 @@
    1.83      gpg_conf(true);
    1.84      gpg_agent_conf(true);
    1.85          
    1.86 -    PEP_STATUS status = init(session, message_to_send, inject_sync_event);
    1.87 +    PEP_STATUS status = init(&session, message_to_send, inject_sync_event);
    1.88      if (status != PEP_STATUS_OK)
    1.89          throw std::runtime_error("init() exploded! Bad!");
    1.90      status = register_sync_callbacks(session, (void*)(&(active->q)),
    1.91 @@ -339,69 +409,4 @@
    1.92      send_queue.push_back(msg);
    1.93  }
    1.94  
    1.95 -PEP_STATUS pEpTestDevice::message_to_send(struct _message* msg) {
    1.96 -    pEpTestDevice::active->add_message_to_send_queue(msg);
    1.97 -}
    1.98 -
    1.99 -int pEpTestDevice::notify_handshake(pEp_identity* me,
   1.100 -                                    pEp_identity* partner,
   1.101 -                                    sync_handshake_signal signal) {
   1.102 -                                        
   1.103 -    switch(signal) {
   1.104 -        case SYNC_NOTIFY_UNDEFINED:
   1.105 -        case SYNC_NOTIFY_TIMEOUT:
   1.106 -        case SYNC_NOTIFY_OVERTAKEN:
   1.107 -        case SYNC_NOTIFY_ACCEPTED_DEVICE_ADDED:
   1.108 -        case SYNC_NOTIFY_ACCEPTED_GROUP_CREATED:
   1.109 -        case SYNC_NOTIFY_ACCEPTED_DEVICE_MOVED:   
   1.110 -            return SYNC_HANDSHAKE_CANCEL;
   1.111 -
   1.112 -        // We don't simulate the handshake process here.
   1.113 -        // If we need to, this is a TODO.
   1.114 -        case SYNC_NOTIFY_INIT_ADD_OUR_DEVICE:
   1.115 -        case SYNC_NOTIFY_INIT_ADD_OTHER_DEVICE:
   1.116 -        case SYNC_NOTIFY_INIT_FORM_GROUP:
   1.117 -        case SYNC_NOTIFY_INIT_MOVE_OUR_DEVICE:
   1.118 -            return SYNC_HANDSHAKE_ACCEPTED;
   1.119 -        
   1.120 -        default:    
   1.121 -            return SYNC_HANDSHAKE_REJECTED;
   1.122 -    }
   1.123 -}
   1.124 -
   1.125 -Sync_event_t* pEpTestDevice::retrieve_next_sync_event(void *management, time_t threshold)
   1.126 -{
   1.127 -    time_t started = time(nullptr);
   1.128 -    bool timeout = false;
   1.129      
   1.130 -    while (active->q.empty()) {
   1.131 -        int i = 0;
   1.132 -        ++i;
   1.133 -        if (i > 10) {
   1.134 -            if (time(nullptr) > started + threshold) {
   1.135 -                timeout = true;
   1.136 -                break;
   1.137 -            }   
   1.138 -            i = 0;
   1.139 -        }   
   1.140 -        nanosleep((const struct timespec[]){{0, 100000000L}}, NULL);
   1.141 -    }
   1.142 -    if (timeout)
   1.143 -        return new_sync_timeout_event();
   1.144 -
   1.145 -    return pEpTestDevice::active->q.pop_front();
   1.146 -}
   1.147 -
   1.148 -int inject_sync_event(SYNC_EVENT ev, void* management) {
   1.149 -    Sync_event_t *_ev = ev;
   1.150 -    switch (_ev->fsm) {
   1.151 -        case Sync_PR_keysync:
   1.152 -//            cout << "injecting event " << KeySync_event_name(_ev->event) << "\n";
   1.153 -            break;
   1.154 -        default:
   1.155 -            throw std::runtime_error("Unknown state machine.");
   1.156 -    }
   1.157 -    pEpTestDevice::active->q.push_front(ev);
   1.158 -    return 0;
   1.159 -}
   1.160 -