1 /*-------------------------------------------------------------------------
2 * Vulkan CTS Framework
3 * --------------------
4 *
5 * Copyright (c) 2021 The Khronos Group Inc.
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 *-------------------------------------------------------------------------*/
20
21 #include "vksNetwork.hpp"
22 #include "vksProtocol.hpp"
23 #include "vksClient.hpp"
24
25 #include <iostream>
26
27 #include "deSocket.hpp"
28 #include "deCommandLine.hpp"
29
30 using namespace vksc_server;
31
32 namespace opt
33 {
34
35 DE_DECLARE_COMMAND_LINE_OPT(Address, string);
36
37 const string DefaultAddress = "localhost:" + std::to_string(DefaultPort);
38
registerOptions(de::cmdline::Parser & parser)39 void registerOptions (de::cmdline::Parser& parser)
40 {
41 using de::cmdline::Option;
42 using de::cmdline::NamedValue;
43
44 parser << Option<Address> ("a", "address", "Address", DefaultAddress.c_str());
45 }
46
47 }
48
49 void RunTests (Server& server);
50
main(int argc,char ** argv)51 int main (int argc, char** argv)
52 {
53 de::cmdline::CommandLine cmdLine;
54
55 // Parse command line.
56 {
57 de::cmdline::Parser parser;
58 opt::registerOptions(parser);
59
60 if (!parser.parse(argc, argv, &cmdLine, std::cerr))
61 {
62 parser.help(std::cout);
63 return EXIT_FAILURE;
64 }
65 }
66
67 try
68 {
69 string address = cmdLine.getOption<opt::Address>();
70 std::cout << "connecting to " << address << "..." << std::endl;
71 Server server(address);
72 RunTests(server);
73 }
74 catch (const std::exception& e) { std::cout << e.what() << std::endl; }
75
76 return EXIT_SUCCESS;
77 }
78
operator <<(std::ostream & os,const vector<u8> & data)79 std::ostream& operator<<(std::ostream& os, const vector<u8>& data)
80 {
81 os << '{';
82 for (msize i{}; i < data.size(); ++i) os << (int)data[i] << ((i+1) < data.size() ? ", " : "");
83 os << '}';
84 return os;
85 }
86
87 template <typename T>
Except(const string & name,const T & value,T excepted,const string & message)88 void Except (const string& name, const T& value, T excepted, const string& message)
89 {
90 std::cout << message << std::endl;
91 if (value != excepted)
92 {
93 std::cout << name << " -> expected: " << excepted << " but got " << value << std::endl;
94 throw std::runtime_error("Test failed: " + message);
95 }
96 std::cout << "ok" << std::endl;
97 }
98
RunStoreContentTests(Server & server)99 void RunStoreContentTests (Server& server)
100 {
101 {
102 StoreContentRequest request;
103 request.data = {1, 2, 3, 4};
104 request.name = "@test1";
105 StoreContentResponse response;
106 server.SendRequest(request, response);
107
108 Except("StoreContentResponse::status", response.status, true, "After requesting to store data on a server we should received true");
109 }
110
111 {
112 StoreContentRequest request;
113 request.data = {5,6,7,8,9};
114 request.name = "@test1";
115 StoreContentResponse response;
116 server.SendRequest(request, response);
117
118 Except("StoreContentResponse::status", response.status, true, "After requesting to store data with a name that is already in use we should received false");
119 }
120 }
121
RunGetContentTests(Server & server)122 void RunGetContentTests (Server& server)
123 {
124 {
125 GetContentRequest request;
126 request.path = "@test1";
127 request.removeAfter = true;
128 GetContentResponse response;
129 server.SendRequest(request, response);
130
131 Except("StoreContentResponse::status", response.status, true, "After requesting to get data from server store we should received true");
132 Except("StoreContentResponse::data", response.data, {5,6,7,8,9}, "Received data must be correct");
133 }
134
135 {
136 GetContentRequest request;
137 request.path = "@test1";
138 request.removeAfter = true;
139 GetContentResponse response;
140 server.SendRequest(request, response);
141
142 Except("StoreContentResponse::status", response.status, false, "Requesting to get data from server memory that no longer exist should result in false");
143 }
144 }
145
RunCompileShaderTests(Server & server)146 void RunCompileShaderTests (Server& server)
147 {
148 {
149 CompileShaderRequest request;
150 request.source.active = "glsl";
151 request.source.glsl = {};
152 request.source.glsl.sources[glu::SHADERTYPE_VERTEX].push_back(
153 R"glsl(#version 450
154
155 vec2 positions[3] = vec2[](
156 vec2(0.0, -0.5),
157 vec2(0.5, 0.5),
158 vec2(-0.5, 0.5)
159 );
160
161 void main() {
162 gl_Position = vec4(positions[gl_VertexIndex], 0.0, 1.0);
163 }
164 )glsl");
165 request.commandLine = {};
166
167 CompileShaderResponse response;
168 server.SendRequest(request, response);
169
170 Except("StoreContentResponse::status", response.status, true, "After requesting server to compile glsl shader we should get true as a result");
171 Except("StoreContentResponse::binary.empty()", response.binary.empty(), false, "Received data must be not empty");
172 }
173 }
174
RunTests(Server & server)175 void RunTests (Server& server)
176 {
177 RunStoreContentTests(server);
178 RunGetContentTests(server);
179 RunCompileShaderTests(server);
180
181 std::cout << "All tests passed" << std::endl;
182 }
183