<commit_before>// Copyright 2016 Google Inc. All Rights Reserved.
//
// 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 keyserver

import (
	"bytes"
	"database/sql"
	"encoding/gob"
	"errors"
	"fmt"
	"reflect"
	"testing"

	"github.com/google/keytransparency/core/appender"
	"github.com/google/keytransparency/core/authentication"
	"github.com/google/keytransparency/core/mapserver"
	"github.com/google/keytransparency/core/transaction"
	"github.com/google/trillian"

	"github.com/golang/protobuf/proto"
	"golang.org/x/net/context"
	"google.golang.org/grpc"
	"google.golang.org/grpc/codes"

	tpb "github.com/google/keytransparency/core/proto/keytransparency_v1_types"
)

const (
	logID = int64(0)
	mapID = int64(0)
)

type env struct {
	c    *fakeCommitter
	tree *fakeSparseHist
	sths appender.Local
	srv  *Server
}

func newEnv() *env {
	c := &fakeCommitter{make(map[string]*tpb.Committed)}
	tree := &fakeSparseHist{make(map[int64][]byte)}
	sths := &fakeSequenced{make([][]byte, 0)}
	mapsvr := mapserver.NewReadonly(mapID, tree, fakeFactory{}, sths)
	srv := New(logID, mapID, mapsvr, c, fakePrivateKey{}, fakeMutator{},
		authentication.NewFake(), fakeFactory{}, fakeMutation{})
	return &env{c, tree, sths, srv}
}

func TestListEntryHistory(t *testing.T) {
	profileCount := 25
	ctx := context.Background()
	for i, tc := range []struct {
		start       int64
		page        int32
		wantNext    int64
		wantHistory []int
		err         codes.Code
	}{
		{1, 1, 2, []int{1}, codes.OK},                                                            // one entry per page.
		{1, 10, 11, []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, codes.OK},                              // 10 entries per page.
		{4, 10, 14, []int{4, 5, 6, 7, 8, 9, 10, 11, 12, 13}, codes.OK},                           // start epoch is not 1.
		{1, 0, 17, []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}, codes.OK},       // zero page size.
		{20, 10, 0, []int{20, 21, 22, 23, 24}, codes.OK},                                         // end of list.
		{24, 10, 0, []int{24}, codes.OK},                                                         // requesting the very last entry.
		{1, 1000000, 17, []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}, codes.OK}, // DOS prevention.
		{40, 10, 0, []int{}, codes.InvalidArgument},                                              // start epoch is beyond current epoch.
		{0, 1, 1, []int{0}, codes.OK},                                                            // start epoch is less than 1.
	} {
		// Test case setup.
		e := newEnv()
		if err := addProfiles(profileCount, e.c, e.tree, e.sths); err != nil {
			t.Fatalf("addProfile(%v, _, _, _)=%v", profileCount, err)
		}

		// Run test case.
		req := &tpb.ListEntryHistoryRequest{
			UserId:   "",
			Start:    tc.start,
			PageSize: tc.page,
		}
		resp, err := e.srv.ListEntryHistory(ctx, req)
		if got, want := grpc.Code(err), tc.err; got != want {
			t.Errorf("%v: ListEntryHistory(%v): %v, want %v", i, req, err, tc.err)
		}
		// Skip the rest of the test if there is an error.
		if err != nil {
			continue
		}

		// Check next epoch.
		if got, want := resp.NextStart, tc.wantNext; got != want {
			t.Errorf("%v: NextEpoch=%v, want %v", i, got, want)
		}

		// Ensure that history has the correct number of entries.
		if got, want := len(resp.Values), len(tc.wantHistory); got != want {
			t.Errorf("%v: len(resp.Values)=%v, want %v", i, got, want)
			// Skip the rest of the test if the returned history is
			// not of the expected length.<commit_msg>Revert keyserver_test.go<commit_after>// Copyright 2016 Google Inc. All Rights Reserved.
//
// 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 keyserver

import (
	"bytes"
	"database/sql"
	"encoding/gob"
	"errors"
	"fmt"
	"reflect"
	"testing"

	"github.com/google/keytransparency/core/appender"
	"github.com/google/keytransparency/core/authentication"
	"github.com/google/keytransparency/core/mapserver"
	"github.com/google/keytransparency/core/transaction"
	"github.com/google/trillian"

	"github.com/golang/protobuf/proto"
	"golang.org/x/net/context"
	"google.golang.org/grpc"
	"google.golang.org/grpc/codes"

	tpb "github.com/google/keytransparency/core/proto/keytransparency_v1_types"
)

const (
	logID = int64(0)
	mapID = int64(0)
)

func TestListEntryHistory(t *testing.T) {
	profileCount := 25
	ctx := context.Background()
	for i, tc := range []struct {
		start       int64
		page        int32
		wantNext    int64
		wantHistory []int
		err         codes.Code
	}{
		{1, 1, 2, []int{1}, codes.OK},                                                            // one entry per page.
		{1, 10, 11, []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, codes.OK},                              // 10 entries per page.
		{4, 10, 14, []int{4, 5, 6, 7, 8, 9, 10, 11, 12, 13}, codes.OK},                           // start epoch is not 1.
		{1, 0, 17, []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}, codes.OK},       // zero page size.
		{20, 10, 0, []int{20, 21, 22, 23, 24}, codes.OK},                                         // end of list.
		{24, 10, 0, []int{24}, codes.OK},                                                         // requesting the very last entry.
		{1, 1000000, 17, []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}, codes.OK}, // DOS prevention.
		{40, 10, 0, []int{}, codes.InvalidArgument},                                              // start epoch is beyond current epoch.
		{0, 1, 1, []int{0}, codes.OK},                                                            // start epoch is less than 1.
	} {
		// Test case setup.
		c := &fakeCommitter{make(map[string]*tpb.Committed)}
		tree := &fakeSparseHist{make(map[int64][]byte)}
		sths := &fakeSequenced{make([][]byte, 0)}
		mapsvr := mapserver.NewReadonly(mapID, tree, fakeFactory{}, sths)

		srv := New(logID, mapID, mapsvr, c, fakePrivateKey{}, fakeMutator{},
			authentication.NewFake(), fakeFactory{}, fakeMutation{})
		if err := addProfiles(profileCount, c, tree, sths); err != nil {
			t.Fatalf("addProfile(%v, _, _, _)=%v", profileCount, err)
		}

		// Run test case.
		req := &tpb.ListEntryHistoryRequest{
			UserId:   "",
			Start:    tc.start,
			PageSize: tc.page,
		}
		resp, err := srv.ListEntryHistory(ctx, req)
		if got, want := grpc.Code(err), tc.err; got != want {
			t.Errorf("%v: ListEntryHistory(%v): %v, want %v", i, req, err, tc.err)
		}
		// Skip the rest of the test if there is an error.
		if err != nil {
			continue
		}

		// Check next epoch.
		if got, want := resp.NextStart, tc.wantNext; got != want {
			t.Errorf("%v: NextEpoch=%v, want %v", i, got, want)
		}

		// Ensure that history has the correct number of entries.
		if got, want := len(resp.Values), len(tc.wantHistory); got != want {
			t.Errorf("%v: len(resp.Values)=%v, want %v", i, got, want)
			// Skip the rest of the test if the returned history is
			// not of the expected length.
{'fraction_non_alphanumeric': 0.15145385587863464, 'fraction_numerical': 0.040455120101137804, 'mean_word_length': 2.4276429809358753, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 26, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4100504', 'n_tokens_mistral': 3017, 'n_tokens_neox': 2630, 'n_words': 835}