2016-09-13 22:58:20 +00:00
|
|
|
// Copyright 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.
|
|
|
|
|
|
|
|
package omaha
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/xml"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func mkUpdateReq() (*bytes.Buffer, error) {
|
|
|
|
req := NewRequest()
|
2017-05-03 19:38:39 +00:00
|
|
|
app := req.AddApp(testAppID, testAppVer)
|
2016-09-13 22:58:20 +00:00
|
|
|
app.AddUpdateCheck()
|
|
|
|
|
|
|
|
buf := &bytes.Buffer{}
|
|
|
|
enc := xml.NewEncoder(buf)
|
|
|
|
enc.Indent("", "\t")
|
|
|
|
if err := enc.Encode(req); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTrivialServer(t *testing.T) {
|
|
|
|
tmp, err := ioutil.TempFile("", "")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer tmp.Close()
|
|
|
|
defer os.Remove(tmp.Name())
|
|
|
|
|
|
|
|
if _, err := tmp.WriteString("test"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
s, err := NewTrivialServer(":0")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer s.Destroy()
|
|
|
|
if err := s.AddPackage(tmp.Name(), "update.gz"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-06-26 22:57:41 +00:00
|
|
|
s.SetVersion(testAppVer)
|
2016-09-13 22:58:20 +00:00
|
|
|
go s.Serve()
|
|
|
|
|
|
|
|
buf, err := mkUpdateReq()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
endpoint := fmt.Sprintf("http://%s/v1/update/", s.Addr())
|
|
|
|
res, err := http.Post(endpoint, "text/xml", buf)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer res.Body.Close()
|
|
|
|
if res.StatusCode != 200 {
|
|
|
|
t.Fatalf("failed to post: %v", res.Status)
|
|
|
|
}
|
|
|
|
|
|
|
|
dec := xml.NewDecoder(res.Body)
|
|
|
|
resp := &Response{}
|
|
|
|
if err := dec.Decode(resp); err != nil {
|
|
|
|
t.Fatalf("failed to parse body: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-06-26 22:57:41 +00:00
|
|
|
// Should get zero update because the version is already the latest.
|
|
|
|
if len(resp.Apps) != 1 ||
|
|
|
|
resp.Apps[0].UpdateCheck == nil ||
|
|
|
|
resp.Apps[0].UpdateCheck.Status != NoUpdate {
|
|
|
|
t.Fatalf("unexpected response: %#v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should get an update now.
|
|
|
|
s.SetVersion("999.999.999")
|
|
|
|
|
|
|
|
buf, err = mkUpdateReq()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
endpoint = fmt.Sprintf("http://%s/v1/update/", s.Addr())
|
|
|
|
res, err = http.Post(endpoint, "text/xml", buf)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer res.Body.Close()
|
|
|
|
if res.StatusCode != 200 {
|
|
|
|
t.Fatalf("failed to post: %v", res.Status)
|
|
|
|
}
|
|
|
|
|
|
|
|
dec = xml.NewDecoder(res.Body)
|
|
|
|
resp = &Response{}
|
|
|
|
if err := dec.Decode(resp); err != nil {
|
|
|
|
t.Fatalf("failed to parse body: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-09-13 22:58:20 +00:00
|
|
|
if len(resp.Apps) != 1 ||
|
|
|
|
resp.Apps[0].UpdateCheck == nil ||
|
|
|
|
resp.Apps[0].UpdateCheck.Status != UpdateOK ||
|
|
|
|
len(resp.Apps[0].UpdateCheck.URLs) != 1 ||
|
|
|
|
resp.Apps[0].UpdateCheck.Manifest == nil ||
|
|
|
|
len(resp.Apps[0].UpdateCheck.Manifest.Packages) != 1 {
|
|
|
|
t.Fatalf("unexpected response: %#v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
pkgres, err := http.Get(resp.Apps[0].UpdateCheck.URLs[0].CodeBase +
|
|
|
|
resp.Apps[0].UpdateCheck.Manifest.Packages[0].Name)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
pkgdata, err := ioutil.ReadAll(pkgres.Body)
|
|
|
|
pkgres.Body.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if string(pkgdata) != "test" {
|
|
|
|
t.Fatalf("unexpected package data: %q", string(pkgdata))
|
|
|
|
}
|
|
|
|
}
|