[Spice-commits] 2 commits - client/controller.cpp client/tests

Arnon Gilboa agilboa at kemper.freedesktop.org
Mon Oct 25 04:30:30 PDT 2010


 client/controller.cpp                               |    9 
 client/tests/controller_test/build                  |    4 
 client/tests/controller_test/controller_test.cpp    |  202 ++++++++++++++++++++
 client/tests/controller_test/controller_test.sln    |   20 +
 client/tests/controller_test/controller_test.vcproj |  198 +++++++++++++++++++
 5 files changed, 428 insertions(+), 5 deletions(-)

New commits:
commit d9ea4242b13d2014c68f735cc9f77fc63066592b
Author: Arnon Gilboa <agilboa at redhat.com>
Date:   Mon Oct 25 13:28:49 2010 +0200

    spicec-tests: add controller_test (v2)
    
    use chars for title & menu instead of wchars

diff --git a/client/tests/controller_test/build b/client/tests/controller_test/build
new file mode 100755
index 0000000..995372f
--- /dev/null
+++ b/client/tests/controller_test/build
@@ -0,0 +1,4 @@
+#!/bin/bash
+
+g++ -I ../../../../spice-protocol/ -o spice_controller controller_test.cpp
+
diff --git a/client/tests/controller_test/controller_test.cpp b/client/tests/controller_test/controller_test.cpp
new file mode 100644
index 0000000..24db9f4
--- /dev/null
+++ b/client/tests/controller_test/controller_test.cpp
@@ -0,0 +1,202 @@
+#include <stdio.h>
+#include <stdint.h>
+#include <spice/controller_prot.h>
+
+#ifdef WIN32
+
+#include <windows.h>
+
+#define PIPE_NAME TEXT("\\\\.\\pipe\\SpiceController-%lu")
+
+static HANDLE pipe = INVALID_HANDLE_VALUE;
+
+#else
+
+#include <sys/socket.h>
+#include <sys/types.h>
+#include <sys/un.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <errno.h>
+
+typedef void *LPVOID;
+typedef const void *LPCVOID;
+typedef unsigned long DWORD;
+typedef char TCHAR;
+
+#define PIPE_NAME "/tmp/SpiceController-%lu.uds"
+
+static int sock = -1;
+
+#endif
+
+#define PIPE_NAME_MAX_LEN 256
+
+void write_to_pipe(LPCVOID data, DWORD size)
+{
+#ifdef WIN32
+    DWORD written;
+    if (!WriteFile(pipe, data, size, &written, NULL) || written != size) {
+        printf("Write to pipe failed %u\n", GetLastError());
+    }
+#else
+    if (send(sock, data, size, 0) != size) {
+        printf("send failed, (%d) %s\n", errno, strerror(errno));
+    }
+#endif
+}
+
+void send_init()
+{
+    ControllerInit msg = {{CONTROLLER_MAGIC, CONTROLLER_VERSION, sizeof(msg)}, 0,
+        CONTROLLER_FLAG_EXCLUSIVE};
+    write_to_pipe((LPCVOID)&msg, sizeof(msg));
+}
+
+void send_msg(uint32_t id)
+{
+    ControllerMsg msg = {id, sizeof(msg)};
+    write_to_pipe((LPCVOID)&msg, sizeof(msg));
+}
+
+void send_value(uint32_t id, uint32_t value)
+{
+    ControllerValue msg = {{id, sizeof(msg)}, value};
+    write_to_pipe((LPCVOID)&msg, sizeof(msg));
+}
+
+void send_data(uint32_t id, uint8_t* data, size_t data_size)
+{
+    size_t size = sizeof(ControllerData) + data_size;
+    ControllerData* msg = (ControllerData*)malloc(size);
+    msg->base.id = id;
+    msg->base.size = (uint32_t)size;
+    memcpy(msg->data, data, data_size);
+    write_to_pipe((LPCVOID)msg, (DWORD)size);
+    free(msg);
+}
+
+DWORD read_from_pipe(LPVOID data, DWORD size)
+{
+    DWORD read;
+#ifdef WIN32
+    if (!ReadFile(pipe, data, size, &read, NULL)) {
+        printf("Read from pipe failed %u\n", GetLastError());
+    }
+#else
+    if (read = recv(sock, data, size, 0) && (read == -1 || read == 0)) {
+        printf("recv failed, (%d) %s\n", errno, strerror(errno));
+    }
+#endif
+    return read;
+}
+
+#define HOST "localhost"
+#define PORT 5931
+#define SPORT 0
+#define PWD ""
+#define SECURE_CHANNELS "main,inputs,playback"
+#define DISABLED_CHANNELS "playback,record"
+#define TITLE "Hello from controller"
+#define HOTKEYS "toggle-fullscreen=shift+f1,release-cursor=shift+f2"
+#define MENU "0\r4864\rS&end Ctrl+Alt+Del\tCtrl+Alt+End\r0\r\n" \
+    "0\r5120\r&Toggle full screen\tShift+F11\r0\r\n" \
+    "0\r1\r&Special keys\r4\r\n" \
+    "1\r5376\r&Send Shift+F11\r0\r\n" \
+    "1\r5632\r&Send Shift+F12\r0\r\n" \
+    "1\r5888\r&Send Ctrl+Alt+End\r0\r\n" \
+    "0\r1\r-\r1\r\n" \
+    "0\r2\rChange CD\r4\r\n" \
+    "2\r3\rNo CDs\r0\r\n" \
+    "2\r4\r[Eject]\r0\r\n" \
+    "0\r5\r-\r1\r\n" \
+    "0\r6\rPlay\r0\r\n" \
+    "0\r7\rSuspend\r0\r\n" \
+    "0\r8\rStop\r0\r\n"
+
+#define TLS_CIPHERS "NONE"
+#define CA_FILE "NONE"
+#define HOST_SUBJECT "NONE"
+
+int main(int argc, char *argv[])
+{
+    int spicec_pid = (argc > 1 ? atoi(argv[1]) : 0);
+    char* host = (argc > 2 ? argv[2] : (char*)HOST);
+    int port = (argc > 3 ? atoi(argv[3]) : PORT);
+    TCHAR pipe_name[PIPE_NAME_MAX_LEN];
+    ControllerValue msg;
+    DWORD read;
+
+#ifdef WIN32
+    _snwprintf_s(pipe_name, PIPE_NAME_MAX_LEN, PIPE_NAME_MAX_LEN, PIPE_NAME, spicec_pid);
+    printf("Creating Spice controller connection %S\n", pipe_name);
+    pipe = CreateFile(pipe_name, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
+    if (pipe == INVALID_HANDLE_VALUE) {
+        printf("Could not open pipe %u\n", GetLastError());
+        return -1;
+    }
+#else
+    snprintf(pipe_name, PIPE_NAME_MAX_LEN, PIPE_NAME, spicec_pid);
+    printf("Creating a controller connection %s\n", pipe_name);
+    struct sockaddr_un remote;
+    if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
+        printf("Could not open socket, (%d) %s\n", errno, strerror(errno));
+        return -1;
+    }
+    remote.sun_family = AF_UNIX;
+    strcpy(remote.sun_path, pipe_name);
+    if (connect(sock, (struct sockaddr *)&remote,
+                strlen(remote.sun_path) + sizeof(remote.sun_family)) == -1) {
+        printf("Socket connect failed, (%d) %s\n", errno, strerror(errno));
+        close(sock);
+        return -1;
+    }
+#endif
+    send_init();
+    printf("Setting Spice parameters\n");
+    send_data(CONTROLLER_HOST, (uint8_t*)host, strlen(host) + 1);
+    send_value(CONTROLLER_PORT, port);
+    send_value(CONTROLLER_SPORT, SPORT);
+    send_data(CONTROLLER_PASSWORD, (uint8_t*)PWD, sizeof(PWD));
+    //send_data(CONTROLLER_SECURE_CHANNELS, (uint8_t*)SECURE_CHANNELS, sizeof(SECURE_CHANNELS));
+    send_data(CONTROLLER_DISABLE_CHANNELS, (uint8_t*)DISABLED_CHANNELS, sizeof(DISABLED_CHANNELS));
+    //send_data(CONTROLLER_TLS_CIPHERS, (uint8_t*)TLS_CIPHERS, sizeof(TLS_CIPHERS));
+    //send_data(CONTROLLER_CA_FILE, (uint8_t*)CA_FILE, sizeof(CA_FILE));
+    //send_data(CONTROLLER_HOST_SUBJECT, (uint8_t*)HOST_SUBJECT, sizeof(HOST_SUBJECT));
+    send_data(CONTROLLER_SET_TITLE, (uint8_t*)TITLE, sizeof(TITLE));
+    send_data(CONTROLLER_HOTKEYS, (uint8_t*)HOTKEYS, sizeof(HOTKEYS));
+    
+    send_data(CONTROLLER_CREATE_MENU, (uint8_t*)MENU, sizeof(MENU));
+
+    send_value(CONTROLLER_FULL_SCREEN, /*CONTROLLER_SET_FULL_SCREEN |*/ CONTROLLER_AUTO_DISPLAY_RES);
+    printf("Show...\n");
+    getchar();
+    send_msg(CONTROLLER_SHOW);
+
+    printf("Connect...\n");
+    getchar();
+    send_msg(CONTROLLER_CONNECT);
+
+    printf("Hide...\n");
+    getchar();
+    send_msg(CONTROLLER_HIDE);
+
+    printf("Show...\n");
+    getchar();
+    send_msg(CONTROLLER_SHOW);
+
+    //send_msg(CONTROLLER_DELETE_MENU);
+    //send_msg(CONTROLLER_HIDE);
+    while ((read = read_from_pipe(&msg, sizeof(msg))) == sizeof(msg)) {
+        printf("Received id %u, size %u, value %u\n", msg.base.id, msg.base.size, msg.value);
+    }
+    printf("Press <Enter> to close connection\n");
+    getchar();
+#ifdef WIN32
+    CloseHandle(pipe); 
+#else
+    close(sock);
+#endif
+    return 0;
+}
diff --git a/client/tests/controller_test/controller_test.sln b/client/tests/controller_test/controller_test.sln
new file mode 100644
index 0000000..f274757
--- /dev/null
+++ b/client/tests/controller_test/controller_test.sln
@@ -0,0 +1,20 @@
+
+Microsoft Visual Studio Solution File, Format Version 10.00
+# Visual Studio 2008
+Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "controller_test", "controller_test.vcproj", "{6DFCEAF5-D081-46F0-AE15-42C5D3240CFD}"
+EndProject
+Global
+	GlobalSection(SolutionConfigurationPlatforms) = preSolution
+		Debug|Win32 = Debug|Win32
+		Release|Win32 = Release|Win32
+	EndGlobalSection
+	GlobalSection(ProjectConfigurationPlatforms) = postSolution
+		{6DFCEAF5-D081-46F0-AE15-42C5D3240CFD}.Debug|Win32.ActiveCfg = Debug|Win32
+		{6DFCEAF5-D081-46F0-AE15-42C5D3240CFD}.Debug|Win32.Build.0 = Debug|Win32
+		{6DFCEAF5-D081-46F0-AE15-42C5D3240CFD}.Release|Win32.ActiveCfg = Release|Win32
+		{6DFCEAF5-D081-46F0-AE15-42C5D3240CFD}.Release|Win32.Build.0 = Release|Win32
+	EndGlobalSection
+	GlobalSection(SolutionProperties) = preSolution
+		HideSolutionNode = FALSE
+	EndGlobalSection
+EndGlobal
diff --git a/client/tests/controller_test/controller_test.vcproj b/client/tests/controller_test/controller_test.vcproj
new file mode 100644
index 0000000..a71fa5a
--- /dev/null
+++ b/client/tests/controller_test/controller_test.vcproj
@@ -0,0 +1,198 @@
+<?xml version="1.0" encoding="Windows-1252"?>
+<VisualStudioProject
+	ProjectType="Visual C++"
+	Version="9.00"
+	Name="controller_test"
+	ProjectGUID="{6DFCEAF5-D081-46F0-AE15-42C5D3240CFD}"
+	RootNamespace="controller_test"
+	Keyword="Win32Proj"
+	TargetFrameworkVersion="131072"
+	>
+	<Platforms>
+		<Platform
+			Name="Win32"
+		/>
+	</Platforms>
+	<ToolFiles>
+	</ToolFiles>
+	<Configurations>
+		<Configuration
+			Name="Debug|Win32"
+			OutputDirectory="$(SolutionDir)$(ConfigurationName)"
+			IntermediateDirectory="$(ConfigurationName)"
+			ConfigurationType="1"
+			CharacterSet="1"
+			>
+			<Tool
+				Name="VCPreBuildEventTool"
+			/>
+			<Tool
+				Name="VCCustomBuildTool"
+			/>
+			<Tool
+				Name="VCXMLDataGeneratorTool"
+			/>
+			<Tool
+				Name="VCWebServiceProxyGeneratorTool"
+			/>
+			<Tool
+				Name="VCMIDLTool"
+			/>
+			<Tool
+				Name="VCCLCompilerTool"
+				Optimization="0"
+				AdditionalIncludeDirectories="..\..\windows;..\..\..\..\spice-protocol"
+				PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE"
+				MinimalRebuild="true"
+				BasicRuntimeChecks="3"
+				RuntimeLibrary="1"
+				UsePrecompiledHeader="0"
+				WarningLevel="3"
+				Detect64BitPortabilityProblems="false"
+				DebugInformationFormat="4"
+			/>
+			<Tool
+				Name="VCManagedResourceCompilerTool"
+			/>
+			<Tool
+				Name="VCResourceCompilerTool"
+			/>
+			<Tool
+				Name="VCPreLinkEventTool"
+			/>
+			<Tool
+				Name="VCLinkerTool"
+				LinkIncremental="2"
+				GenerateDebugInformation="true"
+				SubSystem="1"
+				RandomizedBaseAddress="1"
+				DataExecutionPrevention="0"
+				TargetMachine="1"
+			/>
+			<Tool
+				Name="VCALinkTool"
+			/>
+			<Tool
+				Name="VCManifestTool"
+			/>
+			<Tool
+				Name="VCXDCMakeTool"
+			/>
+			<Tool
+				Name="VCBscMakeTool"
+			/>
+			<Tool
+				Name="VCFxCopTool"
+			/>
+			<Tool
+				Name="VCAppVerifierTool"
+			/>
+			<Tool
+				Name="VCPostBuildEventTool"
+			/>
+		</Configuration>
+		<Configuration
+			Name="Release|Win32"
+			OutputDirectory="$(SolutionDir)$(ConfigurationName)"
+			IntermediateDirectory="$(ConfigurationName)"
+			ConfigurationType="1"
+			CharacterSet="1"
+			WholeProgramOptimization="1"
+			>
+			<Tool
+				Name="VCPreBuildEventTool"
+			/>
+			<Tool
+				Name="VCCustomBuildTool"
+			/>
+			<Tool
+				Name="VCXMLDataGeneratorTool"
+			/>
+			<Tool
+				Name="VCWebServiceProxyGeneratorTool"
+			/>
+			<Tool
+				Name="VCMIDLTool"
+			/>
+			<Tool
+				Name="VCCLCompilerTool"
+				AdditionalIncludeDirectories="..\..\windows;..\..\..\..\spice-protocol"
+				PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE"
+				RuntimeLibrary="2"
+				UsePrecompiledHeader="0"
+				WarningLevel="3"
+				Detect64BitPortabilityProblems="false"
+				DebugInformationFormat="3"
+			/>
+			<Tool
+				Name="VCManagedResourceCompilerTool"
+			/>
+			<Tool
+				Name="VCResourceCompilerTool"
+			/>
+			<Tool
+				Name="VCPreLinkEventTool"
+			/>
+			<Tool
+				Name="VCLinkerTool"
+				LinkIncremental="1"
+				GenerateDebugInformation="true"
+				SubSystem="1"
+				OptimizeReferences="2"
+				EnableCOMDATFolding="2"
+				RandomizedBaseAddress="1"
+				DataExecutionPrevention="0"
+				TargetMachine="1"
+			/>
+			<Tool
+				Name="VCALinkTool"
+			/>
+			<Tool
+				Name="VCManifestTool"
+			/>
+			<Tool
+				Name="VCXDCMakeTool"
+			/>
+			<Tool
+				Name="VCBscMakeTool"
+			/>
+			<Tool
+				Name="VCFxCopTool"
+			/>
+			<Tool
+				Name="VCAppVerifierTool"
+			/>
+			<Tool
+				Name="VCPostBuildEventTool"
+			/>
+		</Configuration>
+	</Configurations>
+	<References>
+	</References>
+	<Files>
+		<Filter
+			Name="Source Files"
+			Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx"
+			UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
+			>
+			<File
+				RelativePath=".\controller_test.cpp"
+				>
+			</File>
+		</Filter>
+		<Filter
+			Name="Header Files"
+			Filter="h;hpp;hxx;hm;inl;inc;xsd"
+			UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"
+			>
+		</Filter>
+		<Filter
+			Name="Resource Files"
+			Filter="rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav"
+			UniqueIdentifier="{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}"
+			>
+		</Filter>
+	</Files>
+	<Globals>
+	</Globals>
+</VisualStudioProject>
commit 429fae02ba60d1b55b73617688f4bdb94e830d8e
Author: Arnon Gilboa <agilboa at redhat.com>
Date:   Mon Oct 25 12:02:06 2010 +0200

    spicec-win: remove redundent strdup & buggy free
    
    text refered a substr of item_dup and was used after free(item_dup).
    no need to strdup, we can destroy the resource string.

diff --git a/client/controller.cpp b/client/controller.cpp
index eabbcd4..cf06aa3 100644
--- a/client/controller.cpp
+++ b/client/controller.cpp
@@ -348,7 +348,7 @@ bool ControllerConnection::create_menu(char* resource)
 {
     bool ret = true;
     char* item_state = 0;
-    char* item_dup;
+    char* next_item;
     const char* param;
     const char* text;
     int parent_id;
@@ -361,15 +361,14 @@ bool ControllerConnection::create_menu(char* resource)
     AutoRef<Menu> menu(new Menu((*app_menu)->get_target(), ""));
     char* item = next_tok(resource, CONTROLLER_MENU_ITEM_DELIMITER, &item_state);
     while (item != NULL) {
-        item_dup = strdup(item);
-        ret = ret && (param = next_tok(item_dup, CONTROLLER_MENU_PARAM_DELIMITER, &item_state)) &&
+        next_item = item + strlen(item) + 1;
+        ret = ret && (param = next_tok(item, CONTROLLER_MENU_PARAM_DELIMITER, &item_state)) &&
               sscanf(param, "%d", &parent_id);
         ret = ret && (param = next_tok(NULL, CONTROLLER_MENU_PARAM_DELIMITER, &item_state)) &&
               sscanf(param, "%d", &id);
         ret = ret && (text = next_tok(NULL, CONTROLLER_MENU_PARAM_DELIMITER, &item_state));
         ret = ret && (param = next_tok(NULL, CONTROLLER_MENU_PARAM_DELIMITER, &item_state)) &&
               sscanf(param, "%d", &flags);
-        free(item_dup);
 
         if (!ret) {
             DBG(0, "item parsing failed %s", item);
@@ -403,7 +402,7 @@ bool ControllerConnection::create_menu(char* resource)
             }
             (*sub_menu)->add_command(text, id, state);
         }
-        item = next_tok(item + strlen(item) + 1, CONTROLLER_MENU_ITEM_DELIMITER, &item_state);
+        item = next_tok(next_item, CONTROLLER_MENU_ITEM_DELIMITER, &item_state);
     }
     if (ret) {
         _handler->set_menu(*menu);


More information about the Spice-commits mailing list