1/*
2 * Copyright 2016, Data61
3 * Commonwealth Scientific and Industrial Research Organisation (CSIRO)
4 * ABN 41 687 119 230.
5 *
6 * This software may be distributed and modified according to the terms of
7 * the BSD 2-Clause license. Note that NO WARRANTY is provided.
8 * See "LICENSE_BSD2.txt" for details.
9 *
10 * @TAG(D61_BSD)
11 */
12
13#include <autoconf.h>
14#ifdef CONFIG_REFOS_RUN_TESTS
15
16#include <string.h>
17#include <sys/types.h>
18#include <sys/stat.h>
19#include <fcntl.h>
20#include "test_fileserv.h"
21#include <refos-util/walloc.h>
22#include <refos-rpc/serv_client.h>
23#include <refos-rpc/serv_client_helper.h>
24
25/* -------------------------------- File server tests -------------------------------------- */
26
27static int
28test_file_server_connect()
29{
30    test_start("fs connection");
31
32    /* Find the file server. */
33    nsv_mountpoint_t mp = nsv_resolve("fileserv/*");
34    test_assert(mp.success == true);
35    test_assert(mp.serverAnon != 0);
36
37    seL4_CPtr fileservAnon = mp.serverAnon;
38    const int fs_test_repeat = 5;
39
40    /* Attempt to ping the file server. */
41    for (int i = 0; i < fs_test_repeat; i++) {
42        int error = serv_ping(fileservAnon);
43        test_assert(error == ESUCCESS);
44    }
45
46    /* Repeatedly connect to and disconnect from the file server. */
47    for (int i = 0; i < fs_test_repeat; i++) {
48        int error;
49        seL4_CPtr fileservSession = serv_connect_direct(fileservAnon, REFOS_LIVENESS, &error);
50        test_assert(fileservSession && error == ESUCCESS);
51
52        if (fileservSession) {
53            serv_disconnect_direct(fileservSession);
54            seL4_CNode_Delete(REFOS_CSPACE, fileservSession, REFOS_CDEPTH);
55            csfree(fileservSession);
56        }
57    }
58
59    /* Release the resources stored in the valid mountpoint. */
60    nsv_mountpoint_release(&mp);
61    test_assert(mp.success == false);
62    test_assert(mp.serverAnon == 0);
63
64    return test_success();
65}
66
67static int
68test_file_server_dataspace()
69{
70    /* ---------- datamap test ------------ */
71    test_start("fs cpio dspace datamap");
72    int error;
73
74    /* Find the file server. */
75    nsv_mountpoint_t mp = nsv_resolve("fileserv/*");
76    test_assert(mp.success == true);
77    test_assert(mp.serverAnon != 0);
78    seL4_CPtr fileservAnon = mp.serverAnon;
79
80    seL4_CPtr fileservSession = serv_connect_direct(fileservAnon, REFOS_LIVENESS, &error);
81    test_assert(fileservSession && error == ESUCCESS);
82
83    /* Allocate a temporary window. */
84    seL4_CPtr tempWindow = 0;
85    seL4_Word tempWindowVaddr = walloc(2, &tempWindow);
86    test_assert(tempWindowVaddr && tempWindow);
87
88    /* Open a new dataspace on the fileserver to test with. */
89    seL4_CPtr dspace = data_open(fileservSession, "hello.txt", 0, O_RDWR, 0, &error);
90    test_assert(dspace && error == ESUCCESS);
91
92    /* Test datamap. */
93    error = data_datamap(fileservSession, dspace, tempWindow, 3);
94    test_assert(error == ESUCCESS);
95    int scmp = strncmp((char*) tempWindowVaddr, "lo world!", 9);
96    test_assert(scmp == 0);
97
98    test_success();
99
100    /* ---------- init_data test ------------ */
101    test_start("fs cpio dspace init_data");
102
103    /* Open a new anon dataspace to init data on */
104    seL4_CPtr anonDS = data_open(REFOS_PROCSERV_EP, "anon", 0, O_RDWR, 0x1000, &error);
105    test_assert(anonDS && error == ESUCCESS);
106
107    /* Inititialise content of this anon dataspace with our fileserv CPIO dataspace. */
108    error = data_init_data(fileservSession, anonDS, dspace , 3);
109    test_assert(error == ESUCCESS);
110
111    /* Allocate another temporary window. */
112    seL4_CPtr tempWindowAnon = 0;
113    seL4_Word tempWindowVaddrAnon = walloc(2, &tempWindowAnon);
114    test_assert(tempWindowVaddrAnon && tempWindowAnon);
115
116    /* Datamap initialised anon dataspace. */
117    error = data_datamap(REFOS_PROCSERV_EP, anonDS, tempWindowAnon, 0);
118    test_assert(error == ESUCCESS);
119    scmp = strncmp((char*) tempWindowVaddrAnon, "lo world!", 9);
120    test_assert(scmp == 0);
121
122    /* Clean up. */
123    data_dataunmap(fileservSession, tempWindow);
124    data_dataunmap(REFOS_PROCSERV_EP, tempWindowAnon);
125    if (tempWindow) {
126        walloc_free(tempWindowVaddr, 2);
127    }
128    if (tempWindowAnon) {
129        walloc_free(tempWindowVaddrAnon, 2);
130    }
131    if (fileservSession) {
132        serv_disconnect_direct(fileservSession);
133        seL4_CNode_Delete(REFOS_CSPACE, fileservSession, REFOS_CDEPTH);
134        csfree(fileservSession);
135    }
136
137    /* Release the resources stored in the valid mountpoint. */
138    nsv_mountpoint_release(&mp);
139    test_assert(mp.success == false);
140    test_assert(mp.serverAnon == 0);
141
142    return test_success();
143}
144
145static int
146test_file_server_serv_connect()
147{
148    test_start("fs serv_connect");
149    for (int i = 0; i < 5; i++) {
150        serv_connection_t c = serv_connect("/fileserv/*");
151        test_assert(c.error == ESUCCESS);
152        test_assert(c.paramBuffer.err == ESUCCESS);
153        strcpy(c.paramBuffer.vaddr, "test");
154        serv_disconnect(&c);
155    }
156    return test_success();
157}
158
159void
160test_file_server(void)
161{
162    test_file_server_connect();
163    test_file_server_dataspace();
164    test_file_server_serv_connect();
165}
166
167#endif /* CONFIG_REFOS_RUN_TESTS */
168