1809f0800SChristian Ehrhardt/*-
2809f0800SChristian Ehrhardt *   BSD LICENSE
3809f0800SChristian Ehrhardt *
4809f0800SChristian Ehrhardt *   Copyright(c) 2016 RehiveTech. All rights reserved.
5809f0800SChristian Ehrhardt *   All rights reserved.
6809f0800SChristian Ehrhardt *
7809f0800SChristian Ehrhardt *   Redistribution and use in source and binary forms, with or without
8809f0800SChristian Ehrhardt *   modification, are permitted provided that the following conditions
9809f0800SChristian Ehrhardt *   are met:
10809f0800SChristian Ehrhardt *
11809f0800SChristian Ehrhardt *     * Redistributions of source code must retain the above copyright
12809f0800SChristian Ehrhardt *       notice, this list of conditions and the following disclaimer.
13809f0800SChristian Ehrhardt *     * Redistributions in binary form must reproduce the above copyright
14809f0800SChristian Ehrhardt *       notice, this list of conditions and the following disclaimer in
15809f0800SChristian Ehrhardt *       the documentation and/or other materials provided with the
16809f0800SChristian Ehrhardt *       distribution.
17809f0800SChristian Ehrhardt *     * Neither the name of RehiveTech nor the names of its
18809f0800SChristian Ehrhardt *       contributors may be used to endorse or promote products derived
19809f0800SChristian Ehrhardt *       from this software without specific prior written permission.
20809f0800SChristian Ehrhardt *
21809f0800SChristian Ehrhardt *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22809f0800SChristian Ehrhardt *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23809f0800SChristian Ehrhardt *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24809f0800SChristian Ehrhardt *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25809f0800SChristian Ehrhardt *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26809f0800SChristian Ehrhardt *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27809f0800SChristian Ehrhardt *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28809f0800SChristian Ehrhardt *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29809f0800SChristian Ehrhardt *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30809f0800SChristian Ehrhardt *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31809f0800SChristian Ehrhardt *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32809f0800SChristian Ehrhardt */
33809f0800SChristian Ehrhardt
34809f0800SChristian Ehrhardt#include <stdio.h>
35809f0800SChristian Ehrhardt#include <string.h>
36809f0800SChristian Ehrhardt
37809f0800SChristian Ehrhardt#include "test.h"
38809f0800SChristian Ehrhardt#include "resource.h"
39809f0800SChristian Ehrhardt
40809f0800SChristian Ehrhardtconst char test_resource_dpdk_blob[] = {
41809f0800SChristian Ehrhardt	'\x44', '\x50', '\x44', '\x4b', '\x00'
42809f0800SChristian Ehrhardt};
43809f0800SChristian Ehrhardt
44809f0800SChristian EhrhardtREGISTER_RESOURCE(test_resource_dpdk,
45809f0800SChristian Ehrhardt		test_resource_dpdk_blob, test_resource_dpdk_blob + 4);
46809f0800SChristian Ehrhardt
47809f0800SChristian Ehrhardtstatic int test_resource_dpdk(void)
48809f0800SChristian Ehrhardt{
49809f0800SChristian Ehrhardt	const struct resource *r;
50809f0800SChristian Ehrhardt
51809f0800SChristian Ehrhardt	r = resource_find("test_resource_dpdk");
52809f0800SChristian Ehrhardt	TEST_ASSERT_NOT_NULL(r, "Could not find test_resource_dpdk");
53809f0800SChristian Ehrhardt	TEST_ASSERT(!strcmp(r->name, "test_resource_dpdk"),
54809f0800SChristian Ehrhardt			"Found resource %s, expected test_resource_dpdk",
55809f0800SChristian Ehrhardt			r->name);
56809f0800SChristian Ehrhardt
57809f0800SChristian Ehrhardt	TEST_ASSERT(!strncmp("DPDK", r->begin, 4),
58809f0800SChristian Ehrhardt			"Unexpected payload: %.4s...", r->begin);
59809f0800SChristian Ehrhardt
60809f0800SChristian Ehrhardt	return 0;
61809f0800SChristian Ehrhardt}
62809f0800SChristian Ehrhardt
63809f0800SChristian EhrhardtREGISTER_LINKED_RESOURCE(test_resource_c);
64809f0800SChristian Ehrhardt
65809f0800SChristian Ehrhardtstatic int test_resource_c(void)
66809f0800SChristian Ehrhardt{
67809f0800SChristian Ehrhardt	const struct resource *r;
68809f0800SChristian Ehrhardt	FILE *f;
69809f0800SChristian Ehrhardt
70809f0800SChristian Ehrhardt	r = resource_find("test_resource_c");
71809f0800SChristian Ehrhardt	TEST_ASSERT_NOT_NULL(r, "No test_resource_c found");
72809f0800SChristian Ehrhardt	TEST_ASSERT(!strcmp(r->name, "test_resource_c"),
73809f0800SChristian Ehrhardt			"Found resource %s, expected test_resource_c",
74809f0800SChristian Ehrhardt			r->name);
75809f0800SChristian Ehrhardt
76809f0800SChristian Ehrhardt	TEST_ASSERT_SUCCESS(resource_fwrite_file(r, "test_resource.c"),
77809f0800SChristian Ehrhardt			"Failed to to write file %s", r->name);
78809f0800SChristian Ehrhardt
79809f0800SChristian Ehrhardt	f = fopen("test_resource.c", "r");
80809f0800SChristian Ehrhardt	TEST_ASSERT_NOT_NULL(f,
81809f0800SChristian Ehrhardt			"Missing extracted file resource.c");
82809f0800SChristian Ehrhardt	fclose(f);
83809f0800SChristian Ehrhardt	remove("test_resource.c");
84809f0800SChristian Ehrhardt
85809f0800SChristian Ehrhardt	return 0;
86809f0800SChristian Ehrhardt}
87809f0800SChristian Ehrhardt
88809f0800SChristian Ehrhardt#ifdef RTE_APP_TEST_RESOURCE_TAR
89809f0800SChristian EhrhardtREGISTER_LINKED_RESOURCE(test_resource_tar);
90809f0800SChristian Ehrhardt
91809f0800SChristian Ehrhardtstatic int test_resource_tar(void)
92809f0800SChristian Ehrhardt{
93809f0800SChristian Ehrhardt	const struct resource *r;
94809f0800SChristian Ehrhardt	FILE *f;
95809f0800SChristian Ehrhardt
96809f0800SChristian Ehrhardt	r = resource_find("test_resource_tar");
97809f0800SChristian Ehrhardt	TEST_ASSERT_NOT_NULL(r, "No test_resource_tar found");
98809f0800SChristian Ehrhardt	TEST_ASSERT(!strcmp(r->name, "test_resource_tar"),
99809f0800SChristian Ehrhardt			"Found resource %s, expected test_resource_tar",
100809f0800SChristian Ehrhardt			r->name);
101809f0800SChristian Ehrhardt
102809f0800SChristian Ehrhardt	TEST_ASSERT_SUCCESS(resource_untar(r),
103809f0800SChristian Ehrhardt			"Failed to to untar %s", r->name);
104809f0800SChristian Ehrhardt
105809f0800SChristian Ehrhardt	f = fopen("test_resource.c", "r");
106809f0800SChristian Ehrhardt	TEST_ASSERT_NOT_NULL(f,
107809f0800SChristian Ehrhardt			"Missing extracted file test_resource.c");
108809f0800SChristian Ehrhardt	fclose(f);
109809f0800SChristian Ehrhardt
110809f0800SChristian Ehrhardt	TEST_ASSERT_SUCCESS(resource_rm_by_tar(r),
111809f0800SChristian Ehrhardt			"Failed to remove extracted contents of %s", r->name);
112809f0800SChristian Ehrhardt	return 0;
113809f0800SChristian Ehrhardt}
114809f0800SChristian Ehrhardt
115809f0800SChristian Ehrhardt#endif /* RTE_APP_TEST_RESOURCE_TAR */
116809f0800SChristian Ehrhardt
117809f0800SChristian Ehrhardtstatic int test_resource(void)
118809f0800SChristian Ehrhardt{
119809f0800SChristian Ehrhardt	if (test_resource_dpdk())
120809f0800SChristian Ehrhardt		return -1;
121809f0800SChristian Ehrhardt
122809f0800SChristian Ehrhardt	if (test_resource_c())
123809f0800SChristian Ehrhardt		return -1;
124809f0800SChristian Ehrhardt
125809f0800SChristian Ehrhardt#ifdef RTE_APP_TEST_RESOURCE_TAR
126809f0800SChristian Ehrhardt	if (test_resource_tar())
127809f0800SChristian Ehrhardt		return -1;
128809f0800SChristian Ehrhardt#endif /* RTE_APP_TEST_RESOURCE_TAR */
129809f0800SChristian Ehrhardt
130809f0800SChristian Ehrhardt	return 0;
131809f0800SChristian Ehrhardt}
132809f0800SChristian Ehrhardt
1339ecc306dSRicardo SalvetiREGISTER_TEST_COMMAND(resource_autotest, test_resource);
134