diff --git a/src/map_asn1.c b/src/map_asn1.c index 02306c23..7513c0bd 100644 --- a/src/map_asn1.c +++ b/src/map_asn1.c @@ -227,18 +227,16 @@ enomem: return NULL; } -stringpair_t *StringPair_to_Struct(StringPair_t *value, stringpair_t *result) +stringpair_t *StringPair_to_Struct(StringPair_t *value) { - bool allocated = !result; - assert(value); if (!value) return NULL; - if (allocated) - result = new_stringpair(NULL, NULL); + stringpair_t *result = (stringpair_t *) calloc(1, sizeof(stringpair_t)); + assert(result); if (!result) - return NULL; + goto enomem; result->key = strndup((char *) value->key.buf, value->key.size); @@ -255,8 +253,7 @@ stringpair_t *StringPair_to_Struct(StringPair_t *value, stringpair_t *result) return result; enomem: - if (allocated) - free_stringpair(result); + free_stringpair(result); return NULL; } @@ -315,7 +312,7 @@ stringpair_list_t *StringPairList_to_stringpair_list( stringpair_list_t *r = result; for (int i=0; ilist.count; i++) { - stringpair_t *value = StringPair_to_Struct(list->list.array[i], NULL); + stringpair_t *value = StringPair_to_Struct(list->list.array[i]); r = stringpair_list_add(r, value); if (!r) goto enomem; @@ -351,7 +348,11 @@ PStringList_t *PStringList_from_stringlist( } for (const stringlist_t *l = list; l && l->value; l=l->next) { - PString_t *element = NULL; + PString_t *element = (PString_t *) calloc(1, sizeof(PString_t)); + assert(element); + if (!element) + goto enomem; + int r = OCTET_STRING_fromBuf(element, l->value, -1); if (r) goto enomem; @@ -369,35 +370,39 @@ enomem: return NULL; } -stringlist_t *PStringList_to_stringlist( - PStringList_t *list, - stringlist_t *result - ) +stringlist_t *PStringList_to_stringlist(PStringList_t *list) { - bool allocated = !result; - assert(list); if (!list) return NULL; - if (allocated) - result = new_stringlist(NULL); + stringlist_t *result = (stringlist_t *) calloc(1, sizeof(stringlist_t)); + assert(result); if (!result) - return NULL; + goto enomem; + + stringlist_t *r = result; for (int i=0; ilist.count; i++) { - result->value = strndup((char *) list->list.array[i]->buf, + char *s = strndup((char *) list->list.array[i]->buf, list->list.array[i]->size); - assert(result->value); - if (!result->value) + assert(s); + if (!s) goto enomem; + r->value = s; + if (i < list->list.count-1) { + r->next = (stringlist_t *) calloc(1, sizeof(stringlist_t)); + assert(r->next); + if (!r->next) + goto enomem; + r = r->next; + } } return result; enomem: - if (allocated) - free_stringlist(result); + free_stringlist(result); return NULL; } @@ -961,7 +966,7 @@ message *PEPMessage_to_message( } if (msg->in_reply_to) { - stringlist_t *l = PStringList_to_stringlist(msg->in_reply_to, NULL); + stringlist_t *l = PStringList_to_stringlist(msg->in_reply_to); if (!l) goto enomem; @@ -969,7 +974,7 @@ message *PEPMessage_to_message( } if (msg->references) { - stringlist_t *l = PStringList_to_stringlist(msg->references, NULL); + stringlist_t *l = PStringList_to_stringlist(msg->references); if (!l) goto enomem; @@ -977,7 +982,7 @@ message *PEPMessage_to_message( } if (msg->keywords) { - stringlist_t *l = PStringList_to_stringlist(msg->keywords, NULL); + stringlist_t *l = PStringList_to_stringlist(msg->keywords); if (!l) goto enomem; @@ -1005,7 +1010,7 @@ message *PEPMessage_to_message( if (msg->sender_fpr) { char *_sender_fpr = strndup((char *) msg->sender_fpr->buf, msg->sender_fpr->size); - if (_sender_fpr) + if (!_sender_fpr) goto enomem; result->_sender_fpr = _sender_fpr; diff --git a/src/map_asn1.h b/src/map_asn1.h index 19037f94..86c0b3e1 100644 --- a/src/map_asn1.h +++ b/src/map_asn1.h @@ -114,15 +114,14 @@ StringPair_t *StringPair_from_Struct( * @brief Convert ASN.1 StringPair_t into stringpair_t * * @param value[in] StringPair_t to convert - * @param result[inout] stringpair_t to update or NULL to alloc a new one * * @retval pointer to updated or allocated result * - * @warning if a new struct is allocated, the ownership goes to the caller + * @warning a new struct is allocated, the ownership goes to the caller * */ -stringpair_t *StringPair_to_Struct(StringPair_t *value, stringpair_t *result); +stringpair_t *StringPair_to_Struct(StringPair_t *value); /** @@ -189,18 +188,14 @@ PStringList_t *PStringList_from_stringlist( * @brief Convert ASN.1 PStringList_t to stringlist_t * * @param list[in] ASN.1 PStringList_t to convert - * @param result[inout] stringlist_t to update or NULL to alloc a new one * * @retval pointer to updated or allocated result * - * @warning if a new struct is allocated, the ownership goes to the caller + * @warning a new struct is allocated, the ownership goes to the caller * */ -stringlist_t *PStringList_to_stringlist( - PStringList_t *list, - stringlist_t *result - ); +stringlist_t *PStringList_to_stringlist(PStringList_t *list); /** diff --git a/test/src/MapAsn1Test.cc b/test/src/MapAsn1Test.cc index 90a61ee8..d5fdd2b8 100644 --- a/test/src/MapAsn1Test.cc +++ b/test/src/MapAsn1Test.cc @@ -119,3 +119,72 @@ TEST_F(MapAsn1Test, check_map_asn1) { free_identity(ident1); free_identity(ident2); } + +TEST_F(MapAsn1Test, check_map_asn1_message) { + output_stream << "testing PEPMessage...\n"; + + message *msg = new_message(PEP_dir_outgoing); + msg->id = strdup("423"); + msg->shortmsg = strdup("hello, world"); + msg->longmsg = strdup("long message"); + msg->longmsg_formatted = strdup("

long message

"); + msg->attachments = new_bloblist(strdup("blob"), 5, "text/plain", "test.txt"); + bloblist_add(msg->attachments, strdup("bla"), 4, "application/octet-stream", "data.dat"); + msg->sent = new_timestamp(23); + msg->recv = new_timestamp(42); + msg->from = new_identity("alice@mail.com", "2342234223422342", "23", "Alice Miller"); + msg->from->comm_type = PEP_ct_pEp; + msg->from->lang[0] = 'd'; msg->from->lang[1] = 'e'; + msg->to = new_identity_list(new_identity("bob@mail.com", "4223422342234223", "42", "Bob Smith")); + identity_list_add(msg->to, new_identity("alice@mail.com", "2342234223422342", "23", "Alice Miller")); + msg->recv_by = new_identity("bob@mail.com", "4223422342234223", "42", "Bob Smith"); + msg->cc = new_identity_list(new_identity("bob@mail.com", "4223422342234223", "42", "Bob Smith")); + identity_list_add(msg->cc, new_identity("alice@mail.com", "2342234223422342", "23", "Alice Miller")); + msg->bcc = new_identity_list(new_identity("bob@mail.com", "4223422342234223", "42", "Bob Smith")); + identity_list_add(msg->bcc, new_identity("alice@mail.com", "2342234223422342", "23", "Alice Miller")); + msg->reply_to = new_identity_list(new_identity("bob@mail.com", "4223422342234223", "42", "Bob Smith")); + identity_list_add(msg->reply_to, new_identity("alice@mail.com", "2342234223422342", "23", "Alice Miller")); + msg->in_reply_to = new_stringlist("23234242"); + stringlist_add(msg->in_reply_to, "323234242"); + msg->references = new_stringlist("23234242"); + stringlist_add(msg->references , "323234242"); + msg->keywords = new_stringlist("something"); + stringlist_add(msg->keywords, "else"); + msg->comments = strdup("hello there"); + msg->opt_fields = new_stringpair_list(new_stringpair("key", "value")); + stringpair_list_add(msg->opt_fields, new_stringpair("otherkey", "othervalue")); + msg->_sender_fpr = strdup("2342234223422342"); + + PEPMessage_t *pm = PEPMessage_from_message(msg, NULL, false, 1024); + message *msg2 = PEPMessage_to_message(pm, NULL, false, 1024); + + ASSERT_STREQ(msg2->id, "423"); + ASSERT_STREQ(msg2->shortmsg, "hello, world"); + ASSERT_STREQ(msg2->longmsg, "long message"); + ASSERT_STREQ(msg2->longmsg_formatted, "

long message

"); + ASSERT_STREQ(msg2->attachments->mime_type, "text/plain"); + ASSERT_EQ(msg2->attachments->next->value[0], 'b'); + ASSERT_NULL(msg2->attachments->next->next); + ASSERT_EQ(msg2->sent->tm_sec, 23); + ASSERT_EQ(msg2->recv->tm_sec, 42); + ASSERT_STREQ(msg2->from->user_id, "23"); + ASSERT_STREQ(msg2->to->ident->user_id, "42"); + ASSERT_STREQ(msg2->to->next->ident->user_id, "23"); + ASSERT_STREQ(msg2->recv_by->user_id, "42"); + ASSERT_STREQ(msg2->cc->next->ident->user_id, "23"); + ASSERT_STREQ(msg2->bcc->next->ident->user_id, "23"); + ASSERT_STREQ(msg2->reply_to->next->ident->user_id, "23"); + ASSERT_STREQ(msg2->in_reply_to->value, "23234242"); + ASSERT_STREQ(msg2->in_reply_to->next->value, "323234242"); + ASSERT_STREQ(msg2->references->next->value, "323234242"); + ASSERT_STREQ(msg2->keywords->next->value, "else"); + ASSERT_STREQ(msg2->comments, "hello there"); + ASSERT_STREQ(msg2->opt_fields->value->key, "key"); + ASSERT_STREQ(msg2->opt_fields->next->value->value, "othervalue"); + ASSERT_STREQ(msg2->_sender_fpr, "2342234223422342"); + + ASN_STRUCT_FREE(asn_DEF_PEPMessage, pm); + free_message(msg); + free_message(msg2); +} +