sequence update keysync
authorVolker Birk <vb@pep.foundation>
Mon, 29 Aug 2016 11:14:13 +0200
branchkeysync
changeset 10863de792de7bba
parent 1085 9db2d7650ddc
child 1087 cea3d0fe64d8
sequence update
src/pEpEngine.c
src/pEpEngine.h
src/pEp_internal.h
test/sequence_test.cc
     1.1 --- a/src/pEpEngine.c	Mon Aug 29 10:50:48 2016 +0200
     1.2 +++ b/src/pEpEngine.c	Mon Aug 29 11:14:13 2016 +0200
     1.3 @@ -383,11 +383,11 @@
     1.4                              "       and pgp_keypair_fpr = identity.main_key_id"
     1.5                              "   where identity.user_id = '" PEP_OWN_USERID "';";
     1.6          
     1.7 -        sql_sequence_value1 = "insert or replace into sequences (name, value) "
     1.8 +        sql_sequence_value1 = "insert or replace into sequences (name, value, own) "
     1.9                                "values (?1, "
    1.10                                "(select coalesce((select value + 1 from sequences "
    1.11 -                              "where name = ?1), 1 ))) ; ";
    1.12 -        sql_sequence_value2 = "select value from sequences where name = ?1 ;";
    1.13 +                              "where name = ?1), 1 )), ?2) ; ";
    1.14 +        sql_sequence_value2 = "select value, own from sequences where name = ?1 ;";
    1.15          sql_sequence_value3 = "update sequences set value = ?1 where name = ?2 ;";
    1.16          
    1.17          sql_set_revoked =     "insert or replace into revoked_keys ("
    1.18 @@ -1709,6 +1709,10 @@
    1.19  static PEP_STATUS _get_sequence_value(PEP_SESSION session, const char *name,
    1.20          int32_t *value)
    1.21  {
    1.22 +    assert(session && name && value);
    1.23 +    if (!(session && name && value))
    1.24 +        return PEP_ILLEGAL_VALUE;
    1.25 +
    1.26      PEP_STATUS status = PEP_STATUS_OK;
    1.27  
    1.28      sqlite3_reset(session->sequence_value2);
    1.29 @@ -1718,8 +1722,12 @@
    1.30      switch (result) {
    1.31          case SQLITE_ROW: {
    1.32              int32_t _value = (int32_t)
    1.33 -                    sqlite3_column_int64(session->sequence_value2, 0);
    1.34 +                    sqlite3_column_int(session->sequence_value2, 0);
    1.35 +            int _own = (int)
    1.36 +                    sqlite3_column_int(session->sequence_value2, 1);
    1.37              *value = _value;
    1.38 +            if (_own)
    1.39 +                status = PEP_OWN_SEQUENCE;
    1.40              break;
    1.41          }
    1.42          default:
    1.43 @@ -1730,10 +1738,16 @@
    1.44      return status;
    1.45  }
    1.46  
    1.47 -static PEP_STATUS _increment_sequence_value(PEP_SESSION session, const char *name)
    1.48 +static PEP_STATUS _increment_sequence_value(PEP_SESSION session,
    1.49 +        const char *name, int own)
    1.50  {
    1.51 +    assert(session && name);
    1.52 +    if (!(session && name))
    1.53 +        return PEP_ILLEGAL_VALUE;
    1.54 +
    1.55      sqlite3_reset(session->sequence_value1);
    1.56      sqlite3_bind_text(session->sequence_value1, 1, name, -1, SQLITE_STATIC);
    1.57 +    sqlite3_bind_int(session->sequence_value1, 2, own);
    1.58      int result = sqlite3_step(session->sequence_value1);
    1.59      assert(result == SQLITE_DONE);
    1.60      sqlite3_reset(session->sequence_value1);
    1.61 @@ -1757,10 +1771,12 @@
    1.62      if (!(session && name && value && *value >= 0))
    1.63          return PEP_ILLEGAL_VALUE;
    1.64  
    1.65 +    int own = 0;
    1.66      if (!name[0]) {
    1.67          unsigned char uuid[16];
    1.68          uuid_generate_random(uuid);
    1.69          uuid_unparse_upper(uuid, name);
    1.70 +        own = 1;
    1.71      }
    1.72  
    1.73      if (*value) {
    1.74 @@ -1776,9 +1792,13 @@
    1.75      }
    1.76  
    1.77      assert(*value == 0);
    1.78 -    status = _increment_sequence_value(session, name);
    1.79 -    if (status == PEP_STATUS_OK)
    1.80 +    status = _increment_sequence_value(session, name, own);
    1.81 +    if (status == PEP_STATUS_OK) {
    1.82          status = _get_sequence_value(session, name, value);
    1.83 +        assert(*value < INT32_MAX);
    1.84 +        if (*value == INT32_MAX)
    1.85 +            return PEP_CANNOT_INCREASE_SEQUENCE;
    1.86 +    }
    1.87      return status;
    1.88  }
    1.89  
     2.1 --- a/src/pEpEngine.h	Mon Aug 29 10:50:48 2016 +0200
     2.2 +++ b/src/pEpEngine.h	Mon Aug 29 11:14:13 2016 +0200
     2.3 @@ -77,6 +77,7 @@
     2.4  
     2.5      PEP_SEQUENCE_VIOLATED                           = 0x0970,
     2.6      PEP_CANNOT_INCREASE_SEQUENCE                    = 0x0971,
     2.7 +    PEP_OWN_SEQUENCE                                = 0x0972,
     2.8  
     2.9      PEP_SYNC_STATEMACHINE_ERROR                     = 0x0980,
    2.10      PEP_STATEMACHINE_INVALID_STATE                  = 0x0982,
    2.11 @@ -863,13 +864,15 @@
    2.12  //  parameters:
    2.13  //      session (in)            session handle
    2.14  //      name (inout)            name of sequence or char[37] set to {0, }
    2.15 -//                              for new own sequence
    2.16 +//                              for new own sequence named as UUID
    2.17  //      value (inout)           value of sequence value to test or 0 for
    2.18  //                              getting next value
    2.19  //
    2.20  //  returns:
    2.21 -//      PEP_STATUS_OK           if sequence not violated
    2.22 -//      PEP_SEQUENCE_VIOLATED   if sequence violated
    2.23 +//      PEP_STATUS_OK                   no error, not own sequence
    2.24 +//      PEP_SEQUENCE_VIOLATED           if sequence violated
    2.25 +//      PEP_CANNOT_INCREASE_SEQUENCE    if sequence cannot be increased
    2.26 +//      PEP_OWN_SEQUENCE                if own sequence
    2.27  
    2.28  DYNAMIC_API PEP_STATUS sequence_value(
    2.29          PEP_SESSION session,
     3.1 --- a/src/pEp_internal.h	Mon Aug 29 10:50:48 2016 +0200
     3.2 +++ b/src/pEp_internal.h	Mon Aug 29 11:14:13 2016 +0200
     3.3 @@ -137,6 +137,7 @@
     3.4  
     3.5      // state machines
     3.6      DeviceState_state sync_state;
     3.7 +    char *sync_uuid;
     3.8  
     3.9      // runtime config
    3.10  
     4.1 --- a/test/sequence_test.cc	Mon Aug 29 10:50:48 2016 +0200
     4.2 +++ b/test/sequence_test.cc	Mon Aug 29 11:14:13 2016 +0200
     4.3 @@ -48,7 +48,7 @@
     4.4      int32_t value5 = 0;
     4.5      char name2[37] = { 0, };
     4.6      PEP_STATUS status6 = sequence_value(session, name2, &value5);
     4.7 -    assert(status6 == PEP_STATUS_OK);
     4.8 +    assert(status6 == PEP_OWN_SEQUENCE);
     4.9      cout << "UUID created: " << name2 << "\n";
    4.10  
    4.11      cout << "calling release()\n";