2015-07-23 05:31:47 +00:00
|
|
|
// Copyright 2013-2015 CoreOS, Inc.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2013-03-27 23:58:22 +00:00
|
|
|
package omaha
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/xml"
|
2013-04-01 16:53:53 +00:00
|
|
|
"fmt"
|
2017-05-04 20:00:46 +00:00
|
|
|
"reflect"
|
|
|
|
"strings"
|
2013-04-01 16:53:53 +00:00
|
|
|
"testing"
|
2013-03-27 23:58:22 +00:00
|
|
|
)
|
|
|
|
|
2017-05-04 20:00:46 +00:00
|
|
|
const (
|
|
|
|
sampleRequest = `<?xml version="1.0" encoding="UTF-8"?>
|
2015-07-23 05:11:49 +00:00
|
|
|
<request protocol="3.0" version="ChromeOSUpdateEngine-0.1.0.0" updaterversion="ChromeOSUpdateEngine-0.1.0.0" installsource="ondemandupdate" ismachine="1">
|
|
|
|
<os version="Indy" platform="Chrome OS" sp="ForcedUpdate_x86_64"></os>
|
|
|
|
<app appid="{87efface-864d-49a5-9bb3-4b050a7c227a}" bootid="{7D52A1CC-7066-40F0-91C7-7CB6A871BFDE}" machineid="{8BDE4C4D-9083-4D61-B41C-3253212C0C37}" oem="ec3000" version="ForcedUpdate" track="dev-channel" from_track="developer-build" lang="en-US" board="amd64-generic" hardware_class="" delta_okay="false" >
|
|
|
|
<ping active="1" a="-1" r="-1"></ping>
|
|
|
|
<updatecheck targetversionprefix=""></updatecheck>
|
|
|
|
<event eventtype="3" eventresult="2" previousversion=""></event>
|
|
|
|
</app>
|
|
|
|
</request>
|
|
|
|
`
|
2017-05-04 20:00:46 +00:00
|
|
|
sampleResponse = `<?xml version="1.0" encoding="UTF-8"?>
|
|
|
|
<response protocol="3.0">
|
|
|
|
<daystart elapsed_seconds="49008"/>
|
|
|
|
<app appid="{87efface-864d-49a5-9bb3-4b050a7c227a}" status="ok">
|
|
|
|
<ping status="ok"/>
|
|
|
|
<updatecheck status="ok">
|
|
|
|
<urls>
|
|
|
|
<url codebase="http://kam:8080/static/"/>
|
|
|
|
</urls>
|
|
|
|
<manifest version="9999.0.0">
|
|
|
|
<packages>
|
|
|
|
<package hash="+LXvjiaPkeYDLHoNKlf9qbJwvnk=" name="update.gz" size="67546213" required="true"/>
|
|
|
|
</packages>
|
|
|
|
<actions>
|
|
|
|
<action event="postinstall" DisplayVersion="9999.0.0" sha256="0VAlQW3RE99SGtSB5R4m08antAHO8XDoBMKDyxQT/Mg=" needsadmin="false" IsDeltaPayload="true" />
|
|
|
|
</actions>
|
|
|
|
</manifest>
|
|
|
|
</updatecheck>
|
|
|
|
</app>
|
|
|
|
</response>
|
|
|
|
`
|
|
|
|
)
|
2015-07-23 05:11:49 +00:00
|
|
|
|
2013-03-29 12:51:29 +00:00
|
|
|
func TestOmahaRequestUpdateCheck(t *testing.T) {
|
2017-05-04 20:00:46 +00:00
|
|
|
v, err := ParseRequest("", strings.NewReader(sampleRequest))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("ParseRequest failed: %v", err)
|
|
|
|
}
|
2013-03-27 23:58:22 +00:00
|
|
|
|
2015-07-24 06:14:36 +00:00
|
|
|
if v.OS.Version != "Indy" {
|
|
|
|
t.Error("Unexpected version", v.OS.Version)
|
2013-03-27 23:58:22 +00:00
|
|
|
}
|
|
|
|
|
2017-05-03 19:38:39 +00:00
|
|
|
if v.Apps[0].ID != "{87efface-864d-49a5-9bb3-4b050a7c227a}" {
|
|
|
|
t.Error("Expected an App ID")
|
2013-03-27 23:58:22 +00:00
|
|
|
}
|
|
|
|
|
2017-05-03 19:38:39 +00:00
|
|
|
if v.Apps[0].BootID != "{7D52A1CC-7066-40F0-91C7-7CB6A871BFDE}" {
|
|
|
|
t.Error("Expected a Boot ID")
|
2013-07-09 18:48:57 +00:00
|
|
|
}
|
|
|
|
|
2014-03-17 21:29:04 +00:00
|
|
|
if v.Apps[0].MachineID != "{8BDE4C4D-9083-4D61-B41C-3253212C0C37}" {
|
2017-05-03 19:38:39 +00:00
|
|
|
t.Error("Expected a Machine ID")
|
2013-09-03 23:21:08 +00:00
|
|
|
}
|
|
|
|
|
2014-03-17 04:04:57 +00:00
|
|
|
if v.Apps[0].OEM != "ec3000" {
|
2013-09-03 23:21:08 +00:00
|
|
|
t.Error("Expected an OEM")
|
|
|
|
}
|
|
|
|
|
2013-03-27 23:58:22 +00:00
|
|
|
if v.Apps[0].UpdateCheck == nil {
|
|
|
|
t.Error("Expected an UpdateCheck")
|
|
|
|
}
|
2013-03-29 12:51:29 +00:00
|
|
|
|
|
|
|
if v.Apps[0].Version != "ForcedUpdate" {
|
|
|
|
t.Error("Verison is ForcedUpdate")
|
|
|
|
}
|
|
|
|
|
|
|
|
if v.Apps[0].FromTrack != "developer-build" {
|
|
|
|
t.Error("developer-build")
|
|
|
|
}
|
|
|
|
|
2013-07-02 16:41:53 +00:00
|
|
|
if v.Apps[0].Track != "dev-channel" {
|
|
|
|
t.Error("dev-channel")
|
|
|
|
}
|
|
|
|
|
2015-07-24 06:14:36 +00:00
|
|
|
if v.Apps[0].Events[0].Type != EventTypeUpdateComplete {
|
|
|
|
t.Error("Expected EventTypeUpdateComplete")
|
|
|
|
}
|
|
|
|
|
|
|
|
if v.Apps[0].Events[0].Result != EventResultSuccessReboot {
|
|
|
|
t.Error("Expected EventResultSuccessReboot")
|
2013-03-29 12:51:29 +00:00
|
|
|
}
|
2013-03-27 23:58:22 +00:00
|
|
|
}
|
|
|
|
|
2017-05-04 20:00:46 +00:00
|
|
|
func TestOmahaResponseWithUpdate(t *testing.T) {
|
|
|
|
parsed, err := ParseResponse("", strings.NewReader(sampleResponse))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("ParseResponse failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := &Response{
|
|
|
|
XMLName: xml.Name{Local: "response"},
|
|
|
|
Protocol: "3.0",
|
|
|
|
DayStart: DayStart{ElapsedSeconds: "49008"},
|
|
|
|
Apps: []*AppResponse{&AppResponse{
|
|
|
|
ID: "{87efface-864d-49a5-9bb3-4b050a7c227a}",
|
|
|
|
Status: AppOK,
|
|
|
|
Ping: &PingResponse{"ok"},
|
|
|
|
UpdateCheck: &UpdateResponse{
|
|
|
|
Status: UpdateOK,
|
|
|
|
URLs: []*URL{&URL{
|
|
|
|
CodeBase: "http://kam:8080/static/",
|
|
|
|
}},
|
|
|
|
Manifest: &Manifest{
|
|
|
|
Version: "9999.0.0",
|
|
|
|
Packages: []*Package{&Package{
|
|
|
|
SHA1: "+LXvjiaPkeYDLHoNKlf9qbJwvnk=",
|
|
|
|
Name: "update.gz",
|
|
|
|
Size: 67546213,
|
|
|
|
Required: true,
|
|
|
|
}},
|
|
|
|
Actions: []*Action{&Action{
|
|
|
|
Event: "postinstall",
|
|
|
|
DisplayVersion: "9999.0.0",
|
|
|
|
SHA256: "0VAlQW3RE99SGtSB5R4m08antAHO8XDoBMKDyxQT/Mg=",
|
|
|
|
IsDeltaPayload: true,
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}},
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(parsed, expected) {
|
|
|
|
t.Errorf("parsed != expected\n%s\n%s", parsed, expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOmahaResponsAsRequest(t *testing.T) {
|
|
|
|
_, err := ParseRequest("", strings.NewReader(sampleResponse))
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("ParseRequest successfully parsed a response")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOmahaRequestAsResponse(t *testing.T) {
|
|
|
|
_, err := ParseResponse("", strings.NewReader(sampleRequest))
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("ParseResponse successfully parsed a request")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-16 20:53:45 +00:00
|
|
|
func ExampleNewResponse() {
|
2015-07-24 06:14:36 +00:00
|
|
|
response := NewResponse()
|
|
|
|
app := response.AddApp("{52F1B9BC-D31A-4D86-9276-CBC256AADF9A}", "ok")
|
2015-07-25 23:44:18 +00:00
|
|
|
app.AddPing()
|
|
|
|
u := app.AddUpdateCheck(UpdateOK)
|
2015-07-24 06:14:36 +00:00
|
|
|
u.AddURL("http://localhost/updates")
|
2013-04-14 04:35:44 +00:00
|
|
|
m := u.AddManifest("9999.0.0")
|
2015-07-24 06:14:36 +00:00
|
|
|
k := m.AddPackage()
|
2017-05-03 19:38:39 +00:00
|
|
|
k.SHA1 = "+LXvjiaPkeYDLHoNKlf9qbJwvnk="
|
2015-07-24 06:14:36 +00:00
|
|
|
k.Name = "update.gz"
|
|
|
|
k.Size = 67546213
|
|
|
|
k.Required = true
|
2013-04-01 16:53:53 +00:00
|
|
|
a := m.AddAction("postinstall")
|
2015-07-26 01:00:13 +00:00
|
|
|
a.DisplayVersion = "9999.0.0"
|
2017-05-03 19:38:39 +00:00
|
|
|
a.SHA256 = "0VAlQW3RE99SGtSB5R4m08antAHO8XDoBMKDyxQT/Mg="
|
2013-04-01 16:53:53 +00:00
|
|
|
a.NeedsAdmin = false
|
2015-07-26 01:00:13 +00:00
|
|
|
a.IsDeltaPayload = true
|
2013-06-26 17:23:06 +00:00
|
|
|
a.DisablePayloadBackoff = true
|
2013-03-27 23:58:22 +00:00
|
|
|
|
2013-04-01 16:53:53 +00:00
|
|
|
if raw, err := xml.MarshalIndent(response, "", " "); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
fmt.Printf("%s%s\n", xml.Header, raw)
|
|
|
|
}
|
2013-03-27 23:58:22 +00:00
|
|
|
|
2013-04-01 16:53:53 +00:00
|
|
|
// Output:
|
|
|
|
// <?xml version="1.0" encoding="UTF-8"?>
|
2017-04-24 19:07:47 +00:00
|
|
|
// <response protocol="3.0" server="go-omaha">
|
2013-04-01 16:53:53 +00:00
|
|
|
// <daystart elapsed_seconds="0"></daystart>
|
|
|
|
// <app appid="{52F1B9BC-D31A-4D86-9276-CBC256AADF9A}" status="ok">
|
|
|
|
// <ping status="ok"></ping>
|
2013-04-14 04:35:44 +00:00
|
|
|
// <updatecheck status="ok">
|
|
|
|
// <urls>
|
|
|
|
// <url codebase="http://localhost/updates"></url>
|
|
|
|
// </urls>
|
|
|
|
// <manifest version="9999.0.0">
|
|
|
|
// <packages>
|
2015-08-10 22:50:31 +00:00
|
|
|
// <package name="update.gz" hash="+LXvjiaPkeYDLHoNKlf9qbJwvnk=" size="67546213" required="true"></package>
|
2013-04-14 04:35:44 +00:00
|
|
|
// </packages>
|
|
|
|
// <actions>
|
2015-07-26 01:00:13 +00:00
|
|
|
// <action event="postinstall" DisplayVersion="9999.0.0" sha256="0VAlQW3RE99SGtSB5R4m08antAHO8XDoBMKDyxQT/Mg=" IsDeltaPayload="true" DisablePayloadBackoff="true"></action>
|
2013-04-14 04:35:44 +00:00
|
|
|
// </actions>
|
|
|
|
// </manifest>
|
|
|
|
// </updatecheck>
|
2013-04-01 16:53:53 +00:00
|
|
|
// </app>
|
|
|
|
// </response>
|
|
|
|
}
|
|
|
|
|
2015-11-16 20:53:45 +00:00
|
|
|
func ExampleNewRequest() {
|
2015-07-24 06:14:36 +00:00
|
|
|
request := NewRequest()
|
|
|
|
request.Version = ""
|
|
|
|
request.OS = &OS{
|
|
|
|
Platform: "Chrome OS",
|
|
|
|
Version: "Indy",
|
|
|
|
ServicePack: "ForcedUpdate_x86_64",
|
|
|
|
}
|
2013-04-01 16:53:53 +00:00
|
|
|
app := request.AddApp("{27BD862E-8AE8-4886-A055-F7F1A6460627}", "1.0.0.0")
|
|
|
|
app.AddUpdateCheck()
|
2013-03-27 23:58:22 +00:00
|
|
|
|
2013-06-25 15:08:19 +00:00
|
|
|
event := app.AddEvent()
|
2015-07-24 06:14:36 +00:00
|
|
|
event.Type = EventTypeDownloadComplete
|
|
|
|
event.Result = EventResultError
|
2013-06-25 15:08:19 +00:00
|
|
|
|
2013-03-27 23:58:22 +00:00
|
|
|
if raw, err := xml.MarshalIndent(request, "", " "); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
fmt.Printf("%s%s\n", xml.Header, raw)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Output:
|
|
|
|
// <?xml version="1.0" encoding="UTF-8"?>
|
2013-04-01 16:53:53 +00:00
|
|
|
// <request protocol="3.0">
|
|
|
|
// <os platform="Chrome OS" version="Indy" sp="ForcedUpdate_x86_64"></os>
|
2013-03-27 23:58:22 +00:00
|
|
|
// <app appid="{27BD862E-8AE8-4886-A055-F7F1A6460627}" version="1.0.0.0">
|
|
|
|
// <updatecheck></updatecheck>
|
2013-06-25 15:08:19 +00:00
|
|
|
// <event eventtype="1" eventresult="0"></event>
|
2013-03-27 23:58:22 +00:00
|
|
|
// </app>
|
2013-04-01 16:53:53 +00:00
|
|
|
// </request>
|
2013-03-27 23:58:22 +00:00
|
|
|
}
|