merge enigmail-color-tests
authorKrista Grothoff <krista@pep-project.org>
Mon, 13 Feb 2017 10:43:56 +0100
branchenigmail-color-tests
changeset 1578a676a68136e3
parent 1577 3991870e35c9
parent 1575 ae0c4b0953a5
child 1579 267b874fc39f
child 1582 7dd855780eef
merge
     1.1 --- a/src/pEpEngine.c	Thu Feb 09 13:04:13 2017 +0100
     1.2 +++ b/src/pEpEngine.c	Mon Feb 13 10:43:56 2017 +0100
     1.3 @@ -42,8 +42,6 @@
     1.4      "update person set device_group = ?1 "
     1.5      "where id = '" PEP_OWN_USERID "';";
     1.6  
     1.7 -// TODO leave group
     1.8 -
     1.9  static const char *sql_get_device_group = 
    1.10      "select device_group from person "
    1.11      "where id = '" PEP_OWN_USERID "';";
    1.12 @@ -1199,14 +1197,18 @@
    1.13      int result;
    1.14  
    1.15      assert(session);
    1.16 -    assert(group_name);
    1.17  
    1.18      if (!(session && group_name))
    1.19          return PEP_ILLEGAL_VALUE;
    1.20  
    1.21      sqlite3_reset(session->set_device_group);
    1.22 -    sqlite3_bind_text(session->set_device_group, 1, group_name, -1,
    1.23 -            SQLITE_STATIC);
    1.24 +    if(group_name){
    1.25 +        sqlite3_bind_text(session->set_device_group, 1, group_name, -1,
    1.26 +                SQLITE_STATIC);
    1.27 +    } else {
    1.28 +        sqlite3_bind_null(session->set_device_group, 1);
    1.29 +    }
    1.30 +
    1.31      result = sqlite3_step(session->set_device_group);
    1.32      sqlite3_reset(session->set_device_group);
    1.33      if (result != SQLITE_DONE)
    1.34 @@ -1231,10 +1233,12 @@
    1.35      result = sqlite3_step(session->get_device_group);
    1.36      switch (result) {
    1.37      case SQLITE_ROW: {
    1.38 -        *group_name = strdup(
    1.39 -            (const char *) sqlite3_column_text(session->get_device_group, 0));
    1.40 -            if(*group_name == NULL)
    1.41 -                status = PEP_OUT_OF_MEMORY;
    1.42 +        const char *_group_name = (const char *)sqlite3_column_text(session->get_device_group, 0);
    1.43 +        if(_group_name){
    1.44 +            *group_name = strdup(_group_name);
    1.45 +                if(*group_name == NULL)
    1.46 +                    status = PEP_OUT_OF_MEMORY;
    1.47 +        }
    1.48          break;
    1.49      }
    1.50   
     2.1 --- a/src/sync_actions.c	Thu Feb 09 13:04:13 2017 +0100
     2.2 +++ b/src/sync_actions.c	Mon Feb 13 10:43:56 2017 +0100
     2.3 @@ -12,46 +12,24 @@
     2.4  
     2.5  // conditions
     2.6  
     2.7 -// TODO : move that SQL statement with other prepared SQL statements in pEpEngine.c
     2.8 -static const char *sql_stored_group_keys =
     2.9 -        "select count(device_group) from person where id = '" PEP_OWN_USERID "';"; 
    2.10 -
    2.11 -// TODO  is that necessary ?
    2.12 -static int _stored_group_keys(void *_gc, int count, char **text, char **name)
    2.13 -{
    2.14 -    assert(_gc);
    2.15 -    assert(count == 1);
    2.16 -    assert(text && text[0]);
    2.17 -    if (!(_gc && count == 1 && text && text[0]))
    2.18 -        return -1;
    2.19 -
    2.20 -    bool *gc = (bool *) _gc;
    2.21 -    *gc = atoi(text[0]) != 0;
    2.22 -    return 0;
    2.23 -}
    2.24 -
    2.25 -int storedGroupKeys(PEP_SESSION session)
    2.26 +int deviceGrouped(PEP_SESSION session)
    2.27  {
    2.28      assert(session);
    2.29      if (!session)
    2.30          return invalid_condition; // error
    2.31  
    2.32 -    bool gc = false;
    2.33 -    int int_result = sqlite3_exec(
    2.34 -        session->db,
    2.35 -        sql_stored_group_keys,
    2.36 -        _stored_group_keys,
    2.37 -        &gc,
    2.38 -        NULL
    2.39 -    );
    2.40 -    assert(int_result == SQLITE_OK);
    2.41 -    if (int_result != SQLITE_OK)
    2.42 -        return invalid_condition; // error
    2.43 +    char *devgrp = NULL;
    2.44 +    int res = 0;
    2.45 +    PEP_STATUS status;
    2.46  
    2.47 -    if (gc)
    2.48 -        return 1;
    2.49 -    else
    2.50 -        return 0;
    2.51 +    status = get_device_group(session, &devgrp);
    2.52 +
    2.53 +    if (status == PEP_STATUS_OK && devgrp && devgrp[0])
    2.54 +        res = 1;
    2.55 +
    2.56 +    free(devgrp);
    2.57 +
    2.58 +    return res;
    2.59  }
    2.60  
    2.61  int keyElectionWon(PEP_SESSION session, Identity partner)
    2.62 @@ -63,7 +41,7 @@
    2.63  
    2.64      // an already existing group always wins
    2.65  
    2.66 -    if (storedGroupKeys(session)) {
    2.67 +    if (deviceGrouped(session)) {
    2.68          assert(!(partner->flags & PEP_idf_devicegroup));
    2.69          return 1;
    2.70      }
    2.71 @@ -314,6 +292,40 @@
    2.72  
    2.73      // groups have no uuid for now
    2.74      status = set_device_group(session, "1");
    2.75 +
    2.76 +    // change sync_uuid when entering group 
    2.77 +    // thus ignoring unprocessed handshakes
    2.78 +    // addressed to previous self (sole) once in.
    2.79 +    pEpUUID uuid;
    2.80 +    uuid_generate_random(uuid);
    2.81 +    uuid_unparse_upper(uuid, session->sync_uuid);
    2.82      
    2.83      return status;
    2.84  }
    2.85 +
    2.86 +// leaveGroup() - 
    2.87 +//
    2.88 +//  params:
    2.89 +//      session (in)        session handle
    2.90 +//      state (in)          state the state machine is in
    2.91 +//      partner (in)        ignored
    2.92 +//      extra (in)          ignored
    2.93 +//
    2.94 +//  returns:
    2.95 +//      PEP_STATUS_OK or any other value on error
    2.96 +
    2.97 +PEP_STATUS leaveGroup(
    2.98 +        PEP_SESSION session,
    2.99 +        DeviceState_state state,
   2.100 +        Identity partner,
   2.101 +        void *extra
   2.102 +    )
   2.103 +{
   2.104 +    PEP_STATUS status = PEP_STATUS_OK;
   2.105 +
   2.106 +    assert(session);
   2.107 +
   2.108 +    status = set_device_group(session, NULL);
   2.109 +    
   2.110 +    return status;
   2.111 +}
     3.1 --- a/src/sync_impl.c	Thu Feb 09 13:04:13 2017 +0100
     3.2 +++ b/src/sync_impl.c	Mon Feb 13 10:43:56 2017 +0100
     3.3 @@ -63,6 +63,13 @@
     3.4                  break;
     3.5  
     3.6              case DeviceGroup_Protocol__payload_PR_handshakeRequest:
     3.7 +                // re-check uuid in case sync_uuid changed while in the queue
     3.8 +                if (strncmp(session->sync_uuid,
     3.9 +                            (const char *)msg->payload.choice.handshakeRequest.partner.user_id->buf,
    3.10 +                            msg->payload.choice.handshakeRequest.partner.user_id->size) != 0){
    3.11 +                    status = PEP_SYNC_ILLEGAL_MESSAGE;
    3.12 +                    goto error;
    3.13 +                }
    3.14                  event = HandshakeRequest;
    3.15                  break;
    3.16  
    3.17 @@ -71,6 +78,14 @@
    3.18                  break;
    3.19  
    3.20              case DeviceGroup_Protocol__payload_PR_groupKeys:
    3.21 +                // re-check uuid in case sync_uuid changed while in the queue
    3.22 +                if (strncmp(session->sync_uuid,
    3.23 +                            (const char *)msg->payload.choice.groupKeys.partner.user_id->buf,
    3.24 +                            msg->payload.choice.groupKeys.partner.user_id->size) != 0){
    3.25 +                    status = PEP_SYNC_ILLEGAL_MESSAGE;
    3.26 +                    goto error;
    3.27 +                }
    3.28 +                // no break
    3.29              case DeviceGroup_Protocol__payload_PR_groupUpdate:
    3.30              {
    3.31                  identity_list *group_keys = IdentityList_to_identity_list(
    3.32 @@ -603,7 +618,7 @@
    3.33  
    3.34      msg->header.state = (long) state;
    3.35  
    3.36 -    bool devicegroup = storedGroupKeys(session);
    3.37 +    bool devicegroup = deviceGrouped(session);
    3.38      if (devicegroup)
    3.39          msg->header.devicegroup = 1;
    3.40      else
     4.1 --- a/sync/devicegroup.fsm	Thu Feb 09 13:04:13 2017 +0100
     4.2 +++ b/sync/devicegroup.fsm	Mon Feb 13 10:43:56 2017 +0100
     4.3 @@ -18,13 +18,13 @@
     4.4      unencrypted sendBeacon;
     4.5  
     4.6      fsm DeviceState filename=sync {
     4.7 -        condition storedGroupKeys();
     4.8 +        condition deviceGrouped();
     4.9          condition keyElectionWon(Identity partner);
    4.10          condition sameIdentities(Identity a, Identity b);
    4.11  
    4.12          state InitState {
    4.13              on Init {
    4.14 -                if storedGroupKeys()
    4.15 +                if deviceGrouped()
    4.16                      go Grouped;
    4.17                  go Sole;
    4.18              }
    4.19 @@ -77,7 +77,6 @@
    4.20                      do notifyInitAddOurDevice(partner);
    4.21                  }
    4.22              }
    4.23 -
    4.24              on HandshakeRejected(Identity partner) {
    4.25                  do rejectHandshake(partner);
    4.26                  go Sole;
    4.27 @@ -174,6 +173,7 @@
    4.28  
    4.29                  go Grouped;
    4.30              }
    4.31 +            on Cancel go Grouped;
    4.32              on Timeout {
    4.33                  do notifyTimeout(expected);
    4.34                  go Grouped;
     5.1 --- a/sync/generated/sync_fsm.c	Thu Feb 09 13:04:13 2017 +0100
     5.2 +++ b/sync/generated/sync_fsm.c	Mon Feb 13 10:43:56 2017 +0100
     5.3 @@ -24,12 +24,12 @@
     5.4                  case Init:
     5.5                  {
     5.6                      DEBUG_LOG("FSM event", "sync_fsm.c, state=InitState", "event=Init")
     5.7 -                    cond_result = storedGroupKeys(session);
     5.8 +                    cond_result = deviceGrouped(session);
     5.9                      #ifndef NDEBUG
    5.10                      char resstr[11] = {0,};
    5.11                      snprintf(resstr,10,"result=%d",cond_result);
    5.12                      #endif
    5.13 -                    DEBUG_LOG("FSM condition", "sync_fsm.c, state=InitState, event=Init, condition=storedGroupKeys", resstr)
    5.14 +                    DEBUG_LOG("FSM condition", "sync_fsm.c, state=InitState, event=Init, condition=deviceGrouped", resstr)
    5.15                      if (cond_result < 0)
    5.16                          return cond_result;
    5.17                      if (cond_result) {
    5.18 @@ -663,6 +663,16 @@
    5.19                      DEBUG_LOG("FSM transition", "sync_fsm.c, state=HandshakingGrouped, event=HandshakeAccepted", "target=Grouped")
    5.20                      return Grouped;
    5.21                  }
    5.22 +                case Cancel:
    5.23 +                {
    5.24 +                    DEBUG_LOG("FSM event", "sync_fsm.c, state=HandshakingGrouped", "event=Cancel")
    5.25 +                    if(session->sync_state_payload){
    5.26 +                        free_identity((Identity)session->sync_state_payload);
    5.27 +                        session->sync_state_payload = NULL;
    5.28 +                    }
    5.29 +                    DEBUG_LOG("FSM transition", "sync_fsm.c, state=HandshakingGrouped, event=Cancel", "target=Grouped")
    5.30 +                    return Grouped;
    5.31 +                }
    5.32                  case Timeout:
    5.33                  {
    5.34                      DEBUG_LOG("FSM event", "sync_fsm.c, state=HandshakingGrouped", "event=Timeout")
     6.1 --- a/sync/generated/sync_fsm.h	Thu Feb 09 13:04:13 2017 +0100
     6.2 +++ b/sync/generated/sync_fsm.h	Mon Feb 13 10:43:56 2017 +0100
     6.3 @@ -30,7 +30,7 @@
     6.4  
     6.5  // conditions
     6.6  
     6.7 -int storedGroupKeys(PEP_SESSION session);
     6.8 +int deviceGrouped(PEP_SESSION session);
     6.9  int keyElectionWon(PEP_SESSION session, Identity partner);
    6.10  int sameIdentities(PEP_SESSION session, Identity a, Identity b);
    6.11