tizen 2.4 release
[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/secure_log.h>
20 #include <cstdio>
21
22 namespace
23 {
24
25 const std::string params = "WRT_TEST_MODE=1 ";
26 const std::string installCmd = params + "wrt-installer -i ";
27 const std::string uninstallCmd = params + "wrt-installer -un ";
28 const std::string redirection =  " 2>&1";
29 const std::string INSTALLER_MESSAGE_ID_LINE =
30     "## wrt-installer : %s installation was successful.\n";
31 const std::string INSTALLER_MESSAGE_ID_LINE_FAIL =
32     "## wrt-installer : %s installation has failed - package already installed";
33 const std::string INSTALLER_MESSSGE_START = "## wrt-installer : ";
34
35 std::string getAndCutInstallerLogLine(std::string &src)
36 {
37     size_t startIndex = src.find(INSTALLER_MESSSGE_START);
38     if (startIndex == std::string::npos)
39     {
40         _W("Installer message can not be found");
41         return std::string();
42     }
43     size_t newLineIndex = src.find("\n", startIndex);
44     std::string line = src.substr(startIndex, newLineIndex - startIndex + 1);
45     src.erase(0, newLineIndex + 1);
46     return line;
47 }
48
49 }
50
51 namespace InstallerWrapper
52 {
53
54 InstallResult install(
55         const std::string& path,
56         std::string& tizenId,
57         const std::string& user)
58 {
59     std::string msg;
60
61     auto cmd = installCmd + path + redirection;
62     if(user.length()) //if other user should be used
63     {
64         cmd = "su " + user + " -c '" + cmd + "'";
65     }
66     auto filehandle = popen(cmd.c_str(), "r");
67     if (!filehandle) {;
68         return OtherError;
69     }
70
71     char buffer[1024] = "";
72     while ( fread_unlocked(buffer, sizeof(char),
73             sizeof(buffer)/sizeof(char), filehandle) > 0 )
74     {
75         msg += buffer;
76     }
77     _D("%s", msg.c_str());
78     auto err = pclose(filehandle);
79     if (!WIFEXITED(err)) {
80         return OtherError;
81     }
82
83     char* id = NULL;
84     std::string line;
85
86     if (0 != WEXITSTATUS(err)) {
87         while ((line = getAndCutInstallerLogLine(msg)) != "")
88         {
89             if(line.find("failed") != std::string::npos)
90             {
91                 id = new char[line.length()];
92                 int nr = sscanf(line.c_str(), INSTALLER_MESSAGE_ID_LINE_FAIL.c_str(), id);
93                 if (1 != nr)
94                 {
95                     _W("Can not read widget ID from message: %s", line.c_str());
96                     delete[] id;
97                     return OtherError;
98                 }
99                 tizenId = id;
100                 delete[] id;
101             }
102         }
103
104         if (1 == WEXITSTATUS(err)) {
105             return WrongWidgetPackage;
106         }
107         return OtherError;
108     }
109
110     while ((line = getAndCutInstallerLogLine(msg)) != "")
111     {
112         if (line.find("successful") != std::string::npos)
113         {
114             id = new char[line.length()];
115             int nr = sscanf(line.c_str(), INSTALLER_MESSAGE_ID_LINE.c_str(), id);
116
117             if (1 != nr)
118             {
119                 _W("Can not read widget ID from message: %s", line.c_str());
120                 delete[] id;
121                 return OtherError;
122             }
123             tizenId = id;
124             delete[] id;
125             if (tizenId != "plugin")
126             {
127                 return Success;
128             }
129         }
130     }
131
132     return OtherError;
133 }
134
135 bool uninstall(const std::string& tizenId)
136 {
137     std::string cmd = uninstallCmd + tizenId + " > /dev/null 2>/dev/null";
138     _D("executing: %s", cmd.c_str());
139     return (system(cmd.c_str()) == EXIT_SUCCESS);
140 }
141
142 bool sigintWrtClients()
143 {
144     return (system("pkill -2 wrt-client") == 0);
145 }
146
147 }
148