Пример #1
0
// GenerateUniqueConfirmationCode generates a unique confirmation code that
// can be used for confirming users.
func (state *UserState) GenerateUniqueConfirmationCode() (string, error) {
	const maxConfirmationCodeLength = 100 // when are the generated confirmation codes unreasonably long
	length := minConfirmationCodeLength
	confirmationCode := cookie.RandomHumanFriendlyString(length)
	for state.AlreadyHasConfirmationCode(confirmationCode) {
		// Increase the length of the confirmationCode random string every time there is a collision
		length++
		confirmationCode = cookie.RandomHumanFriendlyString(length)
		if length > maxConfirmationCodeLength {
			// This should never happen
			return confirmationCode, errors.New("Too many generated confirmation codes are not unique!")
		}
	}
	return confirmationCode, nil
}
Пример #2
0
func TestRandomStoreGet(t *testing.T) {
	const cacheSize = 5
	cache := newFileCache(5, false, 0)
	cache.cacheWarningGiven = true // Silence warning when the cache is full
	filenames := []string{"a", "b", "c"}
	datasets := [][]byte{[]byte{0, 1, 2}, []byte{3, 4, 5, 6}, []byte{7}}
	for i := 0; i < 100; i++ {
		switch rand.Intn(4) {
		case 0, 1: // Add data to the cache
			// Select one at random
			n := rand.Intn(3)
			filename := filenames[n]
			data := datasets[n]
			id := cache.normalize(filename)
			if !cache.hasFile(id) {
				//fmt.Printf("adding %s (%v)\n", filename, id)
				_, err := cache.storeData(filename, data)
				if err != nil {
					t.Errorf("Could not add %s: %s\n", filename, err)
					//} else {
					//	fmt.Printf("added %s (%v)\n", filename, id)
				}
				//fmt.Println(cache.stats())
			}
		case 2: // Add, get and remove data
			filename := cookie.RandomHumanFriendlyString(rand.Intn(20))
			data := []byte(cookie.RandomString(rand.Intn(cacheSize + 1)))
			_, err := cache.storeData(filename, data)
			if err != nil {
				t.Fatal(err)
			}
			datablock2, err := cache.fetchAndCache(filename)
			if err != nil {
				t.Fatal(err)
			}
			id := cache.normalize(filename)
			err = cache.remove(id)
			if err != nil {
				t.Fatal(err)
			}
			if len(data) != datablock2.Length() {
				t.Fatal("WRONG LENGTH!")
			}
			data2 := datablock2.MustData()
			for i := 0; i < len(data); i++ {
				if data[i] != data2[i] {
					t.Fatal("WRONG BYTE!")
				}
			}
		default: // Read data from the cache
			// Select one at random
			n := rand.Intn(3)
			filename := filenames[n]
			data := datasets[n]
			//id := cache.normalize(filename)
			//fmt.Printf("retrieving %s (%v)\n", filename, id)
			retDataBlock, err := cache.fetchAndCache(filename)
			if err == nil {
				//fmt.Printf("retrieved %s (%v)\n", filename, id)
				//fmt.Println(cache.stats())
				if retDataBlock.Length() != len(data) {
					t.Errorf("Wrong length of data: %d vs %d\n", retDataBlock.Length(), len(data))
				}
				retData := retDataBlock.MustData()
				for x := 0; x < len(data); x++ {
					if retData[x] != data[x] {
						t.Error("Wrong contents in cache!")
					}
				}
			}
		}
	}
}