2022-01-09 06:59:28 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2022-04-03 07:54:09 +02:00
|
|
|
"io/ioutil"
|
2022-01-09 06:59:28 +01:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2022-04-09 22:02:30 +02:00
|
|
|
"strings"
|
2022-01-09 06:59:28 +01:00
|
|
|
"testing"
|
|
|
|
|
2022-04-08 05:59:40 +02:00
|
|
|
"github.com/ddworken/hishtory/client/data"
|
2022-01-09 06:59:28 +01:00
|
|
|
"github.com/ddworken/hishtory/shared"
|
2022-04-07 03:18:46 +02:00
|
|
|
"github.com/google/uuid"
|
2022-01-09 06:59:28 +01:00
|
|
|
)
|
|
|
|
|
2022-04-03 07:54:09 +02:00
|
|
|
func TestESubmitThenQuery(t *testing.T) {
|
|
|
|
// Set up
|
|
|
|
defer shared.BackupAndRestore(t)()
|
|
|
|
InitDB()
|
|
|
|
|
2022-04-03 19:08:18 +02:00
|
|
|
// Register a few devices
|
2022-04-08 05:59:40 +02:00
|
|
|
userId := data.UserId("key")
|
2022-04-04 06:00:46 +02:00
|
|
|
devId1 := uuid.Must(uuid.NewRandom()).String()
|
|
|
|
devId2 := uuid.Must(uuid.NewRandom()).String()
|
2022-04-08 05:59:40 +02:00
|
|
|
otherUser := data.UserId("otherkey")
|
2022-04-04 06:00:46 +02:00
|
|
|
otherDev := uuid.Must(uuid.NewRandom()).String()
|
2022-04-03 19:08:18 +02:00
|
|
|
deviceReq := httptest.NewRequest(http.MethodGet, "/?device_id="+devId1+"&user_id="+userId, nil)
|
2022-04-28 19:56:59 +02:00
|
|
|
apiRegisterHandler(nil, deviceReq)
|
2022-04-03 19:08:18 +02:00
|
|
|
deviceReq = httptest.NewRequest(http.MethodGet, "/?device_id="+devId2+"&user_id="+userId, nil)
|
2022-04-28 19:56:59 +02:00
|
|
|
apiRegisterHandler(nil, deviceReq)
|
2022-04-03 19:08:18 +02:00
|
|
|
deviceReq = httptest.NewRequest(http.MethodGet, "/?device_id="+otherDev+"&user_id="+otherUser, nil)
|
2022-04-28 19:56:59 +02:00
|
|
|
apiRegisterHandler(nil, deviceReq)
|
2022-04-03 19:08:18 +02:00
|
|
|
|
2022-04-03 07:54:09 +02:00
|
|
|
// Submit a few entries for different devices
|
2022-04-08 05:59:40 +02:00
|
|
|
entry := data.MakeFakeHistoryEntry("ls ~/")
|
|
|
|
encEntry, err := data.EncryptHistoryEntry("key", entry)
|
2022-04-03 07:54:09 +02:00
|
|
|
shared.Check(t, err)
|
|
|
|
reqBody, err := json.Marshal([]shared.EncHistoryEntry{encEntry})
|
|
|
|
shared.Check(t, err)
|
|
|
|
submitReq := httptest.NewRequest(http.MethodPost, "/", bytes.NewReader(reqBody))
|
2022-04-28 19:56:59 +02:00
|
|
|
apiSubmitHandler(nil, submitReq)
|
2022-04-03 07:54:09 +02:00
|
|
|
|
|
|
|
// Query for device id 1
|
|
|
|
w := httptest.NewRecorder()
|
2022-04-17 08:02:36 +02:00
|
|
|
searchReq := httptest.NewRequest(http.MethodGet, "/?device_id="+devId1+"&user_id="+userId, nil)
|
2022-04-28 19:56:59 +02:00
|
|
|
apiQueryHandler(w, searchReq)
|
2022-04-03 07:54:09 +02:00
|
|
|
res := w.Result()
|
|
|
|
defer res.Body.Close()
|
2022-04-08 05:59:40 +02:00
|
|
|
respBody, err := ioutil.ReadAll(res.Body)
|
2022-04-03 07:54:09 +02:00
|
|
|
shared.Check(t, err)
|
|
|
|
var retrievedEntries []*shared.EncHistoryEntry
|
2022-04-08 05:59:40 +02:00
|
|
|
shared.Check(t, json.Unmarshal(respBody, &retrievedEntries))
|
2022-04-03 07:54:09 +02:00
|
|
|
if len(retrievedEntries) != 1 {
|
|
|
|
t.Fatalf("Expected to retrieve 1 entry, found %d", len(retrievedEntries))
|
|
|
|
}
|
|
|
|
dbEntry := retrievedEntries[0]
|
2022-04-04 06:00:46 +02:00
|
|
|
if dbEntry.DeviceId != devId1 {
|
2022-04-03 07:54:09 +02:00
|
|
|
t.Fatalf("Response contains an incorrect device ID: %#v", *dbEntry)
|
|
|
|
}
|
2022-04-08 05:59:40 +02:00
|
|
|
if dbEntry.UserId != data.UserId("key") {
|
2022-04-03 07:54:09 +02:00
|
|
|
t.Fatalf("Response contains an incorrect device ID: %#v", *dbEntry)
|
|
|
|
}
|
|
|
|
if dbEntry.ReadCount != 1 {
|
|
|
|
t.Fatalf("db.ReadCount should have been 1, was %v", dbEntry.ReadCount)
|
|
|
|
}
|
2022-04-08 05:59:40 +02:00
|
|
|
decEntry, err := data.DecryptHistoryEntry("key", *dbEntry)
|
2022-04-03 07:54:09 +02:00
|
|
|
shared.Check(t, err)
|
2022-04-08 05:59:40 +02:00
|
|
|
if !data.EntryEquals(decEntry, entry) {
|
2022-04-05 07:07:01 +02:00
|
|
|
t.Fatalf("DB data is different than input! \ndb =%#v\ninput=%#v", *dbEntry, entry)
|
2022-04-03 07:54:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Same for device id 2
|
|
|
|
w = httptest.NewRecorder()
|
2022-04-17 08:02:36 +02:00
|
|
|
searchReq = httptest.NewRequest(http.MethodGet, "/?device_id="+devId2+"&user_id="+userId, nil)
|
2022-04-28 19:56:59 +02:00
|
|
|
apiQueryHandler(w, searchReq)
|
2022-04-03 07:54:09 +02:00
|
|
|
res = w.Result()
|
|
|
|
defer res.Body.Close()
|
2022-04-08 05:59:40 +02:00
|
|
|
respBody, err = ioutil.ReadAll(res.Body)
|
2022-04-03 07:54:09 +02:00
|
|
|
shared.Check(t, err)
|
2022-04-08 05:59:40 +02:00
|
|
|
shared.Check(t, json.Unmarshal(respBody, &retrievedEntries))
|
2022-04-03 07:54:09 +02:00
|
|
|
if len(retrievedEntries) != 1 {
|
|
|
|
t.Fatalf("Expected to retrieve 1 entry, found %d", len(retrievedEntries))
|
|
|
|
}
|
|
|
|
dbEntry = retrievedEntries[0]
|
2022-04-04 06:00:46 +02:00
|
|
|
if dbEntry.DeviceId != devId2 {
|
2022-04-03 07:54:09 +02:00
|
|
|
t.Fatalf("Response contains an incorrect device ID: %#v", *dbEntry)
|
|
|
|
}
|
2022-04-08 05:59:40 +02:00
|
|
|
if dbEntry.UserId != data.UserId("key") {
|
2022-04-03 07:54:09 +02:00
|
|
|
t.Fatalf("Response contains an incorrect device ID: %#v", *dbEntry)
|
|
|
|
}
|
|
|
|
if dbEntry.ReadCount != 1 {
|
|
|
|
t.Fatalf("db.ReadCount should have been 1, was %v", dbEntry.ReadCount)
|
|
|
|
}
|
2022-04-08 05:59:40 +02:00
|
|
|
decEntry, err = data.DecryptHistoryEntry("key", *dbEntry)
|
2022-04-03 07:54:09 +02:00
|
|
|
shared.Check(t, err)
|
2022-04-08 05:59:40 +02:00
|
|
|
if !data.EntryEquals(decEntry, entry) {
|
2022-04-05 07:07:01 +02:00
|
|
|
t.Fatalf("DB data is different than input! \ndb =%#v\ninput=%#v", *dbEntry, entry)
|
2022-04-03 07:54:09 +02:00
|
|
|
}
|
2022-04-28 19:56:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDumpRequestAndResponse(t *testing.T) {
|
|
|
|
// Set up
|
|
|
|
defer shared.BackupAndRestore(t)()
|
|
|
|
InitDB()
|
|
|
|
|
|
|
|
// Register a first device for two different users
|
|
|
|
userId := data.UserId("dkey")
|
|
|
|
devId1 := uuid.Must(uuid.NewRandom()).String()
|
|
|
|
otherUser := data.UserId("dOtherkey")
|
|
|
|
otherDev1 := uuid.Must(uuid.NewRandom()).String()
|
|
|
|
deviceReq := httptest.NewRequest(http.MethodGet, "/?device_id="+devId1+"&user_id="+userId, nil)
|
|
|
|
apiRegisterHandler(nil, deviceReq)
|
|
|
|
deviceReq = httptest.NewRequest(http.MethodGet, "/?device_id="+otherDev1+"&user_id="+otherUser, nil)
|
|
|
|
apiRegisterHandler(nil, deviceReq)
|
|
|
|
|
|
|
|
// Query for dump requests, there should be one for userId
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
apiGetPendingDumpRequestsHandler(w, httptest.NewRequest(http.MethodGet, "/?user_id="+userId, nil))
|
|
|
|
res := w.Result()
|
|
|
|
defer res.Body.Close()
|
|
|
|
respBody, err := ioutil.ReadAll(res.Body)
|
|
|
|
shared.Check(t, err)
|
|
|
|
var dumpRequests []*DumpRequest
|
|
|
|
shared.Check(t, json.Unmarshal(respBody, &dumpRequests))
|
|
|
|
if len(dumpRequests) != 1 {
|
|
|
|
t.Fatalf("expected one pending dump request, got %#v", dumpRequests)
|
|
|
|
}
|
|
|
|
dumpRequest := dumpRequests[0]
|
|
|
|
if dumpRequest.RequestingDeviceId != devId1 {
|
|
|
|
t.Fatalf("unexpected device ID")
|
|
|
|
}
|
|
|
|
if dumpRequest.UserId != userId {
|
|
|
|
t.Fatalf("unexpected user ID")
|
|
|
|
}
|
|
|
|
|
|
|
|
// And one for otherUser
|
|
|
|
w = httptest.NewRecorder()
|
|
|
|
apiGetPendingDumpRequestsHandler(w, httptest.NewRequest(http.MethodGet, "/?user_id="+otherUser, nil))
|
|
|
|
res = w.Result()
|
|
|
|
defer res.Body.Close()
|
|
|
|
respBody, err = ioutil.ReadAll(res.Body)
|
|
|
|
shared.Check(t, err)
|
|
|
|
dumpRequests = make([]*DumpRequest, 0)
|
|
|
|
shared.Check(t, json.Unmarshal(respBody, &dumpRequests))
|
|
|
|
if len(dumpRequests) != 1 {
|
|
|
|
t.Fatalf("expected one pending dump request, got %#v", dumpRequests)
|
|
|
|
}
|
|
|
|
dumpRequest = dumpRequests[0]
|
|
|
|
if dumpRequest.RequestingDeviceId != otherDev1 {
|
|
|
|
t.Fatalf("unexpected device ID")
|
|
|
|
}
|
|
|
|
if dumpRequest.UserId != otherUser {
|
|
|
|
t.Fatalf("unexpected user ID")
|
|
|
|
}
|
2022-04-03 07:54:09 +02:00
|
|
|
|
2022-04-28 19:56:59 +02:00
|
|
|
// And none if we query without a user ID
|
2022-04-03 07:54:09 +02:00
|
|
|
w = httptest.NewRecorder()
|
2022-04-28 19:56:59 +02:00
|
|
|
apiGetPendingDumpRequestsHandler(w, httptest.NewRequest(http.MethodGet, "/", nil))
|
2022-04-03 07:54:09 +02:00
|
|
|
res = w.Result()
|
|
|
|
defer res.Body.Close()
|
2022-04-08 05:59:40 +02:00
|
|
|
respBody, err = ioutil.ReadAll(res.Body)
|
2022-04-03 07:54:09 +02:00
|
|
|
shared.Check(t, err)
|
2022-04-28 19:56:59 +02:00
|
|
|
if string(respBody) != "[]" {
|
|
|
|
t.Fatalf("got unexpected respBody: %#v", string(respBody))
|
|
|
|
}
|
|
|
|
|
|
|
|
// And none if we query for a user ID that doesn't exit
|
|
|
|
w = httptest.NewRecorder()
|
|
|
|
apiGetPendingDumpRequestsHandler(w, httptest.NewRequest(http.MethodGet, "/?user_id=foo", nil))
|
|
|
|
res = w.Result()
|
|
|
|
defer res.Body.Close()
|
|
|
|
respBody, err = ioutil.ReadAll(res.Body)
|
|
|
|
shared.Check(t, err)
|
|
|
|
if string(respBody) != "[]" {
|
|
|
|
t.Fatalf("got unexpected respBody: %#v", string(respBody))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now submit a dump for userId
|
|
|
|
entry1Dec := data.MakeFakeHistoryEntry("ls ~/")
|
|
|
|
entry1, err := data.EncryptHistoryEntry("dkey", entry1Dec)
|
|
|
|
shared.Check(t, err)
|
|
|
|
entry2Dec := data.MakeFakeHistoryEntry("aaaaaaáaaa")
|
|
|
|
entry2, err := data.EncryptHistoryEntry("dkey", entry1Dec)
|
|
|
|
shared.Check(t, err)
|
|
|
|
reqBody, err := json.Marshal([]shared.EncHistoryEntry{entry1, entry2})
|
|
|
|
shared.Check(t, err)
|
|
|
|
submitReq := httptest.NewRequest(http.MethodPost, "/?user_id="+userId+"&requesting_device_id="+devId1, bytes.NewReader(reqBody))
|
|
|
|
apiSubmitDumpHandler(nil, submitReq)
|
|
|
|
|
|
|
|
// Check that the dump request is no longer there for userId
|
|
|
|
w = httptest.NewRecorder()
|
|
|
|
apiGetPendingDumpRequestsHandler(w, httptest.NewRequest(http.MethodGet, "/?user_id="+userId, nil))
|
|
|
|
res = w.Result()
|
|
|
|
defer res.Body.Close()
|
|
|
|
respBody, err = ioutil.ReadAll(res.Body)
|
|
|
|
shared.Check(t, err)
|
|
|
|
if string(respBody) != "[]" {
|
|
|
|
t.Fatalf("got unexpected respBody: %#v", string(respBody))
|
|
|
|
}
|
|
|
|
|
|
|
|
// But it is there for the other user
|
|
|
|
w = httptest.NewRecorder()
|
|
|
|
apiGetPendingDumpRequestsHandler(w, httptest.NewRequest(http.MethodGet, "/?user_id="+otherUser, nil))
|
|
|
|
res = w.Result()
|
|
|
|
defer res.Body.Close()
|
|
|
|
respBody, err = ioutil.ReadAll(res.Body)
|
|
|
|
shared.Check(t, err)
|
|
|
|
dumpRequests = make([]*DumpRequest, 0)
|
|
|
|
shared.Check(t, json.Unmarshal(respBody, &dumpRequests))
|
|
|
|
if len(dumpRequests) != 1 {
|
|
|
|
t.Fatalf("expected one pending dump request, got %#v", dumpRequests)
|
|
|
|
}
|
|
|
|
dumpRequest = dumpRequests[0]
|
|
|
|
if dumpRequest.RequestingDeviceId != otherDev1 {
|
|
|
|
t.Fatalf("unexpected device ID")
|
|
|
|
}
|
|
|
|
if dumpRequest.UserId != otherUser {
|
|
|
|
t.Fatalf("unexpected user ID")
|
|
|
|
}
|
|
|
|
|
|
|
|
// And finally, query to ensure that the dumped entries are in the DB
|
|
|
|
w = httptest.NewRecorder()
|
|
|
|
searchReq := httptest.NewRequest(http.MethodGet, "/?device_id="+devId1+"&user_id="+userId, nil)
|
|
|
|
apiQueryHandler(w, searchReq)
|
|
|
|
res = w.Result()
|
|
|
|
defer res.Body.Close()
|
|
|
|
respBody, err = ioutil.ReadAll(res.Body)
|
|
|
|
shared.Check(t, err)
|
|
|
|
var retrievedEntries []*shared.EncHistoryEntry
|
2022-04-08 05:59:40 +02:00
|
|
|
shared.Check(t, json.Unmarshal(respBody, &retrievedEntries))
|
2022-04-03 19:08:18 +02:00
|
|
|
if len(retrievedEntries) != 2 {
|
|
|
|
t.Fatalf("Expected to retrieve 2 entries, found %d", len(retrievedEntries))
|
2022-04-03 07:54:09 +02:00
|
|
|
}
|
2022-04-28 19:56:59 +02:00
|
|
|
for _, dbEntry := range retrievedEntries {
|
|
|
|
if dbEntry.DeviceId != devId1 {
|
|
|
|
t.Fatalf("Response contains an incorrect device ID: %#v", *dbEntry)
|
|
|
|
}
|
|
|
|
if dbEntry.UserId != userId {
|
|
|
|
t.Fatalf("Response contains an incorrect user ID: %#v", *dbEntry)
|
|
|
|
}
|
|
|
|
if dbEntry.ReadCount != 1 {
|
|
|
|
t.Fatalf("db.ReadCount should have been 1, was %v", dbEntry.ReadCount)
|
|
|
|
}
|
|
|
|
decEntry, err := data.DecryptHistoryEntry("dkey", *dbEntry)
|
|
|
|
shared.Check(t, err)
|
|
|
|
if !data.EntryEquals(decEntry, entry1Dec) && !data.EntryEquals(decEntry, entry2Dec) {
|
|
|
|
t.Fatalf("DB data is different than input! \ndb =%#v\nentry1=%#v\nentry2=%#v", *dbEntry, entry1Dec, entry2Dec)
|
|
|
|
}
|
|
|
|
}
|
2022-04-03 07:54:09 +02:00
|
|
|
}
|
2022-04-09 21:57:58 +02:00
|
|
|
|
2022-04-09 22:02:30 +02:00
|
|
|
func TestUpdateReleaseVersion(t *testing.T) {
|
|
|
|
// Set up
|
|
|
|
defer shared.BackupAndRestore(t)()
|
|
|
|
InitDB()
|
|
|
|
|
|
|
|
// Check that ReleaseVersion hasn't been set yet
|
|
|
|
if ReleaseVersion != "UNKNOWN" {
|
2022-04-14 06:30:27 +02:00
|
|
|
t.Fatalf("initial ReleaseVersion isn't as expected: %#v", ReleaseVersion)
|
2022-04-09 22:02:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update it
|
|
|
|
err := updateReleaseVersion()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("updateReleaseVersion failed: %v", err)
|
|
|
|
}
|
|
|
|
|
2022-04-15 05:18:49 +02:00
|
|
|
// If ReleaseVersion is still unknown, skip because we're getting rate limited
|
|
|
|
if ReleaseVersion == "UNKNOWN" {
|
|
|
|
t.Skip()
|
|
|
|
}
|
|
|
|
// Otherwise, check that the new value looks reasonable
|
2022-04-09 22:02:30 +02:00
|
|
|
if !strings.HasPrefix(ReleaseVersion, "v0.") {
|
2022-04-14 06:30:27 +02:00
|
|
|
t.Fatalf("ReleaseVersion wasn't updated to contain a version: %#v", ReleaseVersion)
|
2022-04-09 22:02:30 +02:00
|
|
|
}
|
|
|
|
}
|