FOSSology  4.4.0
Open Source License Compliance by Open Source Software
test_serialize.c
1 /*
2  Author: Maximilian Huber
3  SPDX-FileCopyrightText: © 2018 TNG Technology Consulting GmbH
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
8 #define _GNU_SOURCE
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <libfocunit.h>
12 
13 #include "serialize.h"
14 #include "monk.h"
15 #include "match.h"
16 #include "license.h"
17 #include "libfocunit.h"
18 
19 Licenses* getNLicensesWithText2(int count, ...) {
20  GArray* licenseArray = g_array_new(TRUE, FALSE, sizeof(License));
21  va_list texts;
22  va_start(texts, count);
23  for (int i = 0; i < count; i++) {
24  char* text = g_strdup(va_arg(texts, char*));
26  license.refId = i;
27  license.shortname = g_strdup_printf("%d-testLic", i);
28  license.tokens = tokenize(text, "^" );
29 
30  g_array_append_val(licenseArray, license);
31  g_free(text);
32  }
33  va_end(texts);
34 
35  return buildLicenseIndexes(licenseArray, 1, 0);
36 }
37 
38 Licenses* roundtrip(Licenses* licenses) {
39  FILE *out, *in;
40  size_t size;
41  char *ptr;
42  out = open_memstream(&ptr, &size);
43  if (out == NULL){
44  return NULL;
45  }
46 
47  serialize(licenses, out);
48  fclose(out);
49 
50  in = fmemopen(ptr, size, "r");
51  Licenses* lics = deserialize(in, MIN_ADJACENT_MATCHES, MAX_LEADING_DIFF);
52 
53  free(ptr);
54 
55  return lics;
56 }
57 
58 void assert_Token(Token* token1, Token* token2) {
59  CU_ASSERT_EQUAL(token1->length, token2->length);
60  CU_ASSERT_EQUAL(token1->removedBefore, token2->removedBefore);
61  CU_ASSERT_EQUAL(token1->hashedContent, token2->hashedContent);
62 }
63 
64 void assert_License(License* lic1, License* lic2) {
65  CU_ASSERT_EQUAL(lic1->refId, lic2->refId);
66  CU_ASSERT_STRING_EQUAL(lic1->shortname, lic2->shortname);
67  CU_ASSERT_EQUAL(lic1->tokens->len, lic2->tokens->len);
68 
69  for (guint i = 0; i < lic1->tokens->len; i++) {
70  Token* tokenFrom1 = tokens_index(lic1->tokens, i);
71  Token* tokenFrom2 = tokens_index(lic2->tokens, i);
72 
73  assert_Token(tokenFrom1, tokenFrom2);
74  }
75 }
76 
77 void assert_Licenses(Licenses* lics1, Licenses* lics2) {
78  CU_ASSERT_EQUAL(lics1->licenses->len, lics2->licenses->len);
79 
80  for (guint i = 0; i < lics1->licenses->len; i++) {
81  License* licFrom1 = license_index(lics1->licenses, i);
82  License* licFrom2 = license_index(lics2->licenses, i);
83 
84  assert_License(licFrom1, licFrom2);
85  }
86 }
87 
88 void test_roundtrip_one() {
89  Licenses* licenses = getNLicensesWithText2(1,"a b cde f");
90  Licenses* returnedLicenses = roundtrip(licenses);
91 
92  assert_Licenses(licenses, returnedLicenses);
93 }
94 
95 void test_roundtrip() {
96  Licenses* licenses = getNLicensesWithText2(6, "a^b", "a^b^c^d", "d", "e", "f", "e^f^g");
97  Licenses* returnedLicenses = roundtrip(licenses);
98 
99  assert_Licenses(licenses, returnedLicenses);
100 }
101 
102 CU_TestInfo serialize_testcases[] = {
103  {"Test roundtrip with empty:", test_roundtrip_one},
104  {"Test roundtrip with some licenses with tokens:", test_roundtrip},
105  CU_TEST_INFO_NULL
106 };
Definition: monk.h:55
Definition: monk.h:67
Definition: nomos.h:426