FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
test_serialize.c
1 /*
2 Author: Maximilian Huber
3 Copyright (C) 2018, TNG Technology Consulting GmbH
4 
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 version 2 as published by the Free Software Foundation.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation, Inc.,
16 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18 #define _GNU_SOURCE
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <libfocunit.h>
22 
23 #include "serialize.h"
24 #include "monk.h"
25 #include "match.h"
26 #include "license.h"
27 #include "libfocunit.h"
28 
29 Licenses* getNLicensesWithText2(int count, ...) {
30  GArray* licenseArray = g_array_new(TRUE, FALSE, sizeof(License));
31  va_list texts;
32  va_start(texts, count);
33  for (int i = 0; i < count; i++) {
34  char* text = g_strdup(va_arg(texts, char*));
36  license.refId = i;
37  license.shortname = g_strdup_printf("%d-testLic", i);
38  license.tokens = tokenize(text, "^" );
39 
40  g_array_append_val(licenseArray, license);
41  g_free(text);
42  }
43  va_end(texts);
44 
45  return buildLicenseIndexes(licenseArray, 1, 0);
46 }
47 
48 Licenses* roundtrip(Licenses* licenses) {
49  FILE *out, *in;
50  size_t size;
51  char *ptr;
52  out = open_memstream(&ptr, &size);
53  if (out == NULL){
54  return NULL;
55  }
56 
57  serialize(licenses, out);
58  fclose(out);
59 
60  in = fmemopen(ptr, size, "r");
61  Licenses* lics = deserialize(in, MIN_ADJACENT_MATCHES, MAX_LEADING_DIFF);
62 
63  free(ptr);
64 
65  return lics;
66 }
67 
68 void assert_Token(Token* token1, Token* token2) {
69  CU_ASSERT_EQUAL(token1->length, token2->length);
70  CU_ASSERT_EQUAL(token1->removedBefore, token2->removedBefore);
71  CU_ASSERT_EQUAL(token1->hashedContent, token2->hashedContent);
72 }
73 
74 void assert_License(License* lic1, License* lic2) {
75  CU_ASSERT_EQUAL(lic1->refId, lic2->refId);
76  CU_ASSERT_STRING_EQUAL(lic1->shortname, lic2->shortname);
77  CU_ASSERT_EQUAL(lic1->tokens->len, lic2->tokens->len);
78 
79  for (guint i = 0; i < lic1->tokens->len; i++) {
80  Token* tokenFrom1 = tokens_index(lic1->tokens, i);
81  Token* tokenFrom2 = tokens_index(lic2->tokens, i);
82 
83  assert_Token(tokenFrom1, tokenFrom2);
84  }
85 }
86 
87 void assert_Licenses(Licenses* lics1, Licenses* lics2) {
88  CU_ASSERT_EQUAL(lics1->licenses->len, lics2->licenses->len);
89 
90  for (guint i = 0; i < lics1->licenses->len; i++) {
91  License* licFrom1 = license_index(lics1->licenses, i);
92  License* licFrom2 = license_index(lics2->licenses, i);
93 
94  assert_License(licFrom1, licFrom2);
95  }
96 }
97 
98 void test_roundtrip_one() {
99  Licenses* licenses = getNLicensesWithText2(1,"a b cde f");
100  Licenses* returnedLicenses = roundtrip(licenses);
101 
102  assert_Licenses(licenses, returnedLicenses);
103 }
104 
105 void test_roundtrip() {
106  Licenses* licenses = getNLicensesWithText2(6, "a^b", "a^b^c^d", "d", "e", "f", "e^f^g");
107  Licenses* returnedLicenses = roundtrip(licenses);
108 
109  assert_Licenses(licenses, returnedLicenses);
110 }
111 
112 CU_TestInfo serialize_testcases[] = {
113  {"Test roundtrip with empty:", test_roundtrip_one},
114  {"Test roundtrip with some licenses with tokens:", test_roundtrip},
115  CU_TEST_INFO_NULL
116 };
Definition: monk.h:78
Definition: nomos.h:439
Definition: monk.h:66