[Release] wrt-installer_0.1.9
[framework/web/wrt-installer.git] / tests / general / 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     while ( fread_unlocked(buffer, sizeof(char),
74             sizeof(buffer)/sizeof(char), filehandle) > 0 )
75     {
76         msg += buffer;
77     }
78     LogDebug(msg);
79     auto err = pclose(filehandle);
80     if (!WIFEXITED(err)) {
81         return OtherError;
82     }
83     if (0 != WEXITSTATUS(err)) {
84         if (1 == WEXITSTATUS(err)) {
85             return WrongWidgetPackage;
86         }
87         return OtherError;
88     }
89
90     char* id = NULL;
91     std::string line;
92
93     while ((line = getAndCutInstallerLogLine(msg)) != "")
94     {
95         if (line.find("successful") != std::string::npos)
96         {
97             id = new char[line.length()];
98             int nr = sscanf(line.c_str(), INSTALLER_MESSAGE_ID_LINE.c_str(), id);
99
100             if (1 != nr)
101             {
102                 LogWarning("Can not read widget ID from message: " << line);
103                 delete[] id;
104                 return OtherError;
105             }
106             tizenId = id;
107             delete[] id;
108             if (tizenId != "plugin")
109             {
110                 return Success;
111             }
112         }
113     }
114
115     return OtherError;
116 }
117
118 bool uninstall(const std::string& tizenId)
119 {
120     std::string cmd = uninstallCmd + tizenId + " > /dev/null 2>/dev/null";
121     LogDebug("executing: " << cmd);
122     return (system(cmd.c_str()) == EXIT_SUCCESS);
123 }
124
125 bool uninstallByGuid(const std::string& guid)
126 {
127     std::string cmd = uninstallByGuidCmd + guid + "\" > /dev/null 2>/dev/null";
128     LogDebug("executing: " << cmd);
129     return (system(cmd.c_str()) == EXIT_SUCCESS);
130 }
131
132 bool sigintWrtClients()
133 {
134     return (system("pkill -2 wrt-client") == 0);
135 }
136
137 }
138