Renaming tests binaries
[framework/web/wrt-installer.git] / tests / general / common / src / InstallerWrapper.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16
17 #include "InstallerWrapper.h"
18
19 #include <dpl/log/log.h>
20
21 #include <cstdio>
22
23 namespace
24 {
25
26 const std::string params = "DPL_USE_OLD_STYLE_LOGS=0 "
27     "DPL_USE_OLD_STYLE_PEDANTIC_LOGS=0 WRT_TEST_MODE=1 ";
28 const std::string installCmd = params + "wrt-installer -if ";
29 const std::string uninstallCmd = params + "wrt-installer -un ";
30 const std::string uninstallByGuidCmd = params + "wrt-installer -ug \"";
31 const std::string redirection =  " 2>&1";
32 const std::string INSTALLER_MESSAGE_ID_LINE =
33     "## wrt-installer : %s installation was successful.\n";
34 const std::string INSTALLER_MESSSGE_START = "## wrt-installer : ";
35
36 std::string getAndCutInstallerLogLine(std::string &src)
37 {
38     size_t startIndex = src.find(INSTALLER_MESSSGE_START);
39     if (startIndex == std::string::npos)
40     {
41         LogWarning("Installer message can not be found");
42         return std::string();
43     }
44     size_t newLineIndex = src.find("\n", startIndex);
45     std::string line = src.substr(startIndex, newLineIndex - startIndex + 1);
46     src.erase(0, newLineIndex + 1);
47     return line;
48 }
49
50 }
51
52 namespace InstallerWrapper
53 {
54
55 InstallResult install(
56         const std::string& path,
57         std::string& tizenId,
58         const std::string& user)
59 {
60     std::string msg;
61
62     auto cmd = installCmd + path + redirection;
63     if(user.length()) //if other user should be used
64     {
65         cmd = "su " + user + " -c '" + cmd + "'";
66     }
67     auto filehandle = popen(cmd.c_str(), "r");
68     if (!filehandle) {;
69         return OtherError;
70     }
71
72     char buffer[1024] = "";
73     int ret;
74     while ((ret = fread_unlocked(buffer,
75                                  sizeof(char),
76                                  sizeof(buffer)/sizeof(char),
77                                  filehandle)) > 0)
78     {
79         msg += buffer;
80     }
81     LogDebug(msg);
82     auto err = pclose(filehandle);
83     if (!WIFEXITED(err)) {
84         return OtherError;
85     }
86     if (0 != WEXITSTATUS(err)) {
87         if (1 == WEXITSTATUS(err)) {
88             return WrongWidgetPackage;
89         }
90         return OtherError;
91     }
92
93     char* id = NULL;
94     std::string line;
95
96     while ((line = getAndCutInstallerLogLine(msg)) != "")
97     {
98         if (line.find("successful") != std::string::npos)
99         {
100             id = new char[line.length()];
101             int nr = sscanf(line.c_str(), INSTALLER_MESSAGE_ID_LINE.c_str(), id);
102
103             if (1 != nr)
104             {
105                 LogWarning("Can not read widget ID from message: " << line);
106                 delete[] id;
107                 return OtherError;
108             }
109             tizenId = id;
110             delete[] id;
111             if (tizenId != "plugin")
112             {
113                 return Success;
114             }
115         }
116     }
117
118     return OtherError;
119 }
120
121 bool uninstall(const std::string& tizenId)
122 {
123     std::string cmd = uninstallCmd + tizenId + " > /dev/null 2>/dev/null";
124     LogDebug("executing: " << cmd);
125     return (system(cmd.c_str()) == EXIT_SUCCESS);
126 }
127
128 bool uninstallByGuid(const std::string& guid)
129 {
130     std::string cmd = uninstallByGuidCmd + guid + "\" > /dev/null 2>/dev/null";
131     LogDebug("executing: " << cmd);
132     return (system(cmd.c_str()) == EXIT_SUCCESS);
133 }
134
135 bool sigintWrtClients()
136 {
137     return (system("pkill -2 wrt-client") == 0);
138 }
139
140 }
141