Beispiel #1
0
func main() {

	db, err := sql.Open("postgres", "user=postgres host=localhost port=5432 dbname=example_db sslmode=disable")
	if err != nil {
		log.Fatalln(err)
	}

	defer func() {
		fmt.Println("close.")
		if err := db.Close(); err != nil {
			log.Fatalln(err)
		}
	}()

	//	resetDB(db)

	if tx, err := db.Begin(); err != nil {
		log.Fatalln("begin ", err)
	} else {
		var r *rand.Rand = rand.New(rand.NewSource(time.Now().UnixNano()))
		if res, err := tx.Exec(INSERT_TEST_TBL, r.Int31(), "hoge", "fugafuga"); err != nil {
			log.Fatalln("insert query ", err)
		} else {
			fmt.Printf("%+v\n", res)
		}

		defer func() {
			fmt.Println("commit.")
			if err := tx.Commit(); err != nil {
				log.Fatalln(err)
			}
		}()
	}
}
Beispiel #2
0
func GenerateDomain(r *rand.Rand, size int) []byte {
	dnLen := size % 70 // artificially limit size so there's less to intrepret if a failure occurs
	var dn []byte
	done := false
	for i := 0; i < dnLen && !done; {
		max := dnLen - i
		if max > 63 {
			max = 63
		}
		lLen := max
		if lLen != 0 {
			lLen = int(r.Int31()) % max
		}
		done = lLen == 0
		if done {
			continue
		}
		l := make([]byte, lLen+1)
		l[0] = byte(lLen)
		for j := 0; j < lLen; j++ {
			l[j+1] = byte(rand.Int31())
		}
		dn = append(dn, l...)
		i += 1 + lLen
	}
	return append(dn, 0)
}
Beispiel #3
0
// Generates a random number according to the distribution using the rng passed.
func (al *Alias) Gen(rng *rand.Rand) uint32 {
	ri := uint32(rng.Int31())
	w := ri % uint32(len(al.table))
	if ri > al.table[w].prob {
		return al.table[w].alias
	}
	return w
}
Beispiel #4
0
// Generate a Random secret encoded as a b32 string
// If the length is <= 0, a default length of 10 bytes will
// be used, which will generate a secret of length 16.
func RandomSecret(length int, rnd *rand.Rand) string {
	if 0 <= length {
		length = 10
	}
	secret := make([]byte, length)
	for i, _ := range secret {
		secret[i] = byte(rnd.Int31() % 256)
	}
	return base32.StdEncoding.EncodeToString(secret)
}
Beispiel #5
0
func randomNameList(rand *rand.Rand) []string {
	ret := make([]string, rand.Int31()&15)
	for i := range ret {
		s := make([]byte, 1+(rand.Int31()&15))
		for j := range s {
			s[j] = 'a' + uint8(rand.Int31()&15)
		}
		ret[i] = string(s)
	}
	return ret
}
func NonZeroRand32(rnd *rand.Rand) int32 {
	for {
		r := rnd.Int31()
		if r == 0 {
			continue
		}
		if rnd.Intn(1) != 0 {
			return -r
		}
		return r
	}
}
func randomBytes(n int, rand *rand.Rand) []byte {
	r := make([]byte, n)
	for i := 0; i < n; i++ {
		r[i] = byte(rand.Int31())
	}
	return r
}
func (*serverHelloMsg) Generate(rand *rand.Rand, size int) reflect.Value {
	m := &serverHelloMsg{}
	m.vers = uint16(rand.Intn(65536))
	m.random = randomBytes(32, rand)
	m.sessionId = randomBytes(rand.Intn(32), rand)
	m.cipherSuite = uint16(rand.Int31())
	m.compressionMethod = uint8(rand.Intn(256))

	if rand.Intn(10) > 5 {
		m.nextProtoNeg = true

		n := rand.Intn(10)
		m.nextProtos = make([]string, n)
		for i := 0; i < n; i++ {
			m.nextProtos[i] = randomString(20, rand)
		}
	}

	if rand.Intn(10) > 5 {
		m.ocspStapling = true
	}
	if rand.Intn(10) > 5 {
		m.ticketSupported = true
	}

	return reflect.ValueOf(m)
}
func (*clientHelloMsg) Generate(rand *rand.Rand, size int) reflect.Value {
	m := &clientHelloMsg{}
	m.vers = uint16(rand.Intn(65536))
	m.random = randomBytes(32, rand)
	m.sessionId = randomBytes(rand.Intn(32), rand)
	m.cipherSuites = make([]uint16, rand.Intn(63)+1)
	for i := 0; i < len(m.cipherSuites); i++ {
		m.cipherSuites[i] = uint16(rand.Int31())
	}
	m.compressionMethods = randomBytes(rand.Intn(63)+1, rand)
	if rand.Intn(10) > 5 {
		m.nextProtoNeg = true
	}
	if rand.Intn(10) > 5 {
		m.serverName = randomString(rand.Intn(255), rand)
	}
	m.ocspStapling = rand.Intn(10) > 5
	m.supportedPoints = randomBytes(rand.Intn(5)+1, rand)
	m.supportedCurves = make([]CurveID, rand.Intn(5)+1)
	for i := range m.supportedCurves {
		m.supportedCurves[i] = CurveID(rand.Intn(30000))
	}
	if rand.Intn(10) > 5 {
		m.ticketSupported = true
		if rand.Intn(10) > 5 {
			m.sessionTicket = randomBytes(rand.Intn(300), rand)
		}
	}
	if rand.Intn(10) > 5 {
		m.signatureAndHashes = supportedSKXSignatureAlgorithms
	}

	return reflect.ValueOf(m)
}
Beispiel #10
0
func GenerateTXT(r *rand.Rand, size int) []byte {
	rdLen := size % 300 // artificially limit size so there's less to intrepret if a failure occurs
	var rd []byte
	for i := 0; i < rdLen; {
		max := rdLen - 1
		if max > 255 {
			max = 255
		}
		sLen := max
		if max != 0 {
			sLen = int(r.Int31()) % max
		}
		s := make([]byte, sLen+1)
		s[0] = byte(sLen)
		for j := 0; j < sLen; j++ {
			s[j+1] = byte(rand.Int31())
		}
		rd = append(rd, s...)
		i += 1 + sLen
	}
	return rd
}
Beispiel #11
0
func (*kexInitMsg) Generate(rand *rand.Rand, size int) reflect.Value {
	ki := &kexInitMsg{}
	randomBytes(ki.Cookie[:], rand)
	ki.KexAlgos = randomNameList(rand)
	ki.ServerHostKeyAlgos = randomNameList(rand)
	ki.CiphersClientServer = randomNameList(rand)
	ki.CiphersServerClient = randomNameList(rand)
	ki.MACsClientServer = randomNameList(rand)
	ki.MACsServerClient = randomNameList(rand)
	ki.CompressionClientServer = randomNameList(rand)
	ki.CompressionServerClient = randomNameList(rand)
	ki.LanguagesClientServer = randomNameList(rand)
	ki.LanguagesServerClient = randomNameList(rand)
	if rand.Int31()&1 == 1 {
		ki.FirstKexFollows = true
	}
	return reflect.ValueOf(ki)
}
Beispiel #12
0
func timeGenerator(rand *rand.Rand) time.Time {
	months := []time.Month{
		time.January, time.February, time.March,
		time.April, time.May, time.June,
		time.July, time.August, time.September,
		time.October, time.November, time.December,
	}

	return time.Date(
		rand.Intn(9999),
		months[rand.Intn(len(months))],
		rand.Intn(31),
		rand.Intn(24),
		rand.Intn(60),
		rand.Intn(60),
		int(rand.Int31()),
		time.UTC,
	)
}
Beispiel #13
0
func randomBytes(out []byte, rand *rand.Rand) {
	for i := 0; i < len(out); i++ {
		out[i] = byte(rand.Int31())
	}
}
func NewPopulatedPreAccept(r *math_rand.Rand) *PreAccept {
	this := &PreAccept{}
	this.LeaderId = r.Int31()
	this.Replica = r.Int31()
	this.Instance = r.Int31()
	this.Ballot = r.Int31()
	v1 := r.Intn(100)
	this.Command = make([]byte, v1)
	for i := 0; i < v1; i++ {
		this.Command[i] = byte(r.Intn(256))
	}
	this.Seq = r.Int31()
	for i := 0; i < 5; i++ {
		this.Deps[i] = r.Int31()
	}
	return this
}
Beispiel #15
0
func RandomGenerate(r *rand.Rand, b []byte) {
	for i, _ := range b {
		// the common value in [0x0f, 0xf0]
		b[i] = 0x0f + byte(r.Int31()%(256-0x0f-0x0f))
	}
}
Beispiel #16
0
func getStructureOfOneFile(fileName string, iFile int, r *rand.Rand, H1Index_old *int) {
	fmt.Println("  ", fileName)

	// Store file name and default section/caption structure
	BookStructure.SectionFiles = append(BookStructure.SectionFiles,
		SectionFileType{fileName, make([]string, 0, 10), true, false, -1, false, make([]ElementType, 0, 10)})
	iSectionFile := len(BookStructure.SectionFiles) - 1

	// Open file
	file, err1 := os.Open(fileName)
	if err1 != nil {
		log.Fatal(err1)
	}
	defer file.Close()

	// Query section structure present in file
	doc, err := goquery.NewDocumentFromReader(file)
	if err != nil {
		log.Fatal(err)
	}

	element := false
	iNav := 0

	doc.Find("h1,h2,h3,h4,caption,figcaption,a,nav,div.equation,ul.references").Each(func(i int, s *goquery.Selection) {
		// Inquire whether nav element is present
		if s.Is("nav") {
			// Check that nav is before any other element
			if element {
				fmt.Println("Error: <nav> present after a section/caption/figcaption element on file:", fileName)
				fmt.Println("       This is not supported.")
				os.Exit(1)
			}

			// Mark that navigation bar is already present in file.
			BookStructure.SectionFiles[iSectionFile].NewNav = false

			// Inquire file references in navigation bar
			s.Find("a").Each(func(i int, ss *goquery.Selection) {
				// Save a new nav file reference
				BookStructure.SectionFiles[iSectionFile].NavList = append(BookStructure.SectionFiles[iSectionFile].NavList, ss.AttrOr("href", "???"))
				iNav++
			})
			return
		} else {
			element = true
		}

		if s.Is("a") { // Link detected
			// Check if link is pointing into the book
			if iNav > 0 {
				// Link from the navigation bar (ignore it)
				iNav--
				return
			}
			href, exists := s.Attr("href")
			if !exists {
				fmt.Printf("Warning: link <a> without href attribute is ignored in file %s\n", fileName)
				BookStructure.SectionFiles[iFile].Elements = append(BookStructure.SectionFiles[iFile].Elements,
					ElementType{"<a", "</a>", "", "", "", "", false, "", false})
				return
			}
			if strings.Index(href, "/") == -1 {
				// No "/", so link internal to the book
				var targetFileName string
				var targetID string
				tooltip := s.AttrOr("title", "")

				IDstart := strings.Index(href, "#")
				if IDstart == -1 {
					// No "#"
					targetFileName = href
					targetID = ""
				} else if IDstart == 0 {
					// "#xxx", so no file name
					if len(href) <= 1 {
						fmt.Printf("Error: Wrong link '<a href=\"#\">' in file %s\n", fileName)
						os.Exit(1)
					}
					targetFileName = fileName
					targetID = href[IDstart+1:]
				} else {
					// "xxx#yyy"
					if IDstart+1 >= len(href) {
						targetFileName = href[0:IDstart]
						targetID = ""
					} else {
						targetFileName = href[0:IDstart]
						targetID = href[IDstart+1:]
					}
				}
				BookStructure.SectionFiles[iFile].Elements = append(BookStructure.SectionFiles[iFile].Elements,
					ElementType{"<a", "</a>", s.Text(), href, targetFileName, tooltip, false, targetID, false})

			} else {
				BookStructure.SectionFiles[iFile].Elements = append(BookStructure.SectionFiles[iFile].Elements,
					ElementType{"<a", "</a>", "", "", "", "", false, "", false})
				/*
				   // External link, check whether it exists
				   _, err := http.Get(href);
				   if err != nil {
				   fmt.Printf("Error when opening link %s\n", href)
				*/
			}
			return
		}

		// Store id's of references
		if s.Is("ul.references") { // references detected
			s.Find("li").Each(func(i int, s2 *goquery.Selection) {
				id, exists := s2.Attr("id")
				if !exists || id == "" || id == "#" {
					// No id is present, ignore this list item
					return
				} else {
					// Find text between <strong> ... </strong>
					tooltip := ""
					s2.Find("strong").Each(func(i int, s3 *goquery.Selection) {
						tooltip = s3.Text()
					})

					// Store id as bookmark
					title, exists := s2.Attr("title")
					if exists && title != "" {
						addBookmark(id, fileName, title, tooltip)
					} else {
						addBookmark(id, fileName, "", tooltip)
					}
				}
			})
			return
		}

		// Inquire element id and content (= text + label)
		var label string
		newID := false
		id, exists := s.Attr("id")
		if !exists || id == "" || id == "#" {
			// If no id present, introduce a random value for id
			id = strconv.Itoa(int(r.Int31()))
			newID = true
		}
		// text := s.Text()
		text, _ := s.Html()
		modified := false // = true, if text is modified
		var newText string

		// Actual index of SectionFiles
		iFile := len(BookStructure.SectionFiles) - 1

		// Store information
		if s.Is("h1") {
			Counters.iFigCaption = 0
			Counters.iCaption = 0
			Counters.iEquation = 0

			// Determine chapter number
			isec := minInt(len("Chapter"), len(text))
			if text[0:isec] == "Chapter" {
				// Increment chapter number
				Counters.ih1_digit++
				Counters.last_h1_type = "Chapter"
			} else {
				isec = minInt(len("Appendix"), len(text))
				if text[0:isec] == "Appendix" {
					// Increment appendix number
					Counters.ih1_letter++
					Counters.last_h1_type = "Appendix"
				} else {
					Counters.last_h1_type = ""
				}
			}

			// Update h1 section number if necessary and make a new h1 entry in BookStructure
			newText, modified, label = updateSectionText(text, 1, 0, 0, 0)
			BookStructure.Sections = append(BookStructure.Sections,
				SectionType{fileName, id, label, newText, modified,
					make([]SectionType, 0, 5),
					make([]CaptionType, 0, 5),
					make([]EquationType, 0, 5)})
			BookStructure.SectionFiles[iFile].Elements = append(BookStructure.SectionFiles[iFile].Elements,
				ElementType{"<h1", "</h1>", text, "", newText, "", modified, id, newID})
			BookStructure.SectionFiles[iFile].H1Index = len(BookStructure.Sections) - 1
			*H1Index_old = len(BookStructure.Sections) - 1

		} else if s.Is("h2") {
			i1 := len(BookStructure.Sections) - 1
			if i1 < 0 {
				fmt.Println("h2 defined before h1 in file:", fileName)
				os.Exit(1)
			}
			i2 := len(BookStructure.Sections[i1].Sections)
			newText, modified, label = updateSectionText(text, 2, i2+1, 0, 0)
			BookStructure.Sections[i1].Sections =
				append(BookStructure.Sections[i1].Sections,
					SectionType{fileName, id, label, newText, modified,
						make([]SectionType, 0, 5),
						make([]CaptionType, 0, 5),
						make([]EquationType, 0, 5)})

			BookStructure.SectionFiles[iFile].Elements = append(BookStructure.SectionFiles[iFile].Elements,
				ElementType{"<h2", "</h2>", text, "", newText, "", modified, id, newID})
			BookStructure.SectionFiles[iFile].H1Index = *H1Index_old

		} else if s.Is("h3") {
			i1 := len(BookStructure.Sections) - 1
			if i1 < 0 {
				fmt.Println("h2 defined before h1 in file:", fileName)
				os.Exit(1)
			}
			i2 := len(BookStructure.Sections[i1].Sections) - 1
			if i2 < 0 {
				fmt.Println("h3 defined before h2 in file:", fileName)
				os.Exit(1)
			}
			i3 := len(BookStructure.Sections[i1].Sections[i2].Sections)
			newText, modified, label = updateSectionText(text, 3, i2+1, i3+1, 0)
			BookStructure.Sections[i1].Sections[i2].Sections =
				append(BookStructure.Sections[i1].Sections[i2].Sections,
					SectionType{fileName, id, label, newText, modified,
						make([]SectionType, 0, 5),
						make([]CaptionType, 0, 5),
						make([]EquationType, 0, 5)})
			BookStructure.SectionFiles[iFile].Elements = append(BookStructure.SectionFiles[iFile].Elements,
				ElementType{"<h3", "</h3>", text, "", newText, "", modified, id, newID})
			BookStructure.SectionFiles[iFile].H1Index = *H1Index_old

		} else if s.Is("h4") {
			i1 := len(BookStructure.Sections) - 1
			if i1 < 0 {
				fmt.Println("h2 defined before h1 in file:", fileName)
				os.Exit(1)
			}
			i2 := len(BookStructure.Sections[i1].Sections) - 1
			if i2 < 0 {
				fmt.Println("h3 defined before h2 in file:", fileName)
				os.Exit(1)
			}
			i3 := len(BookStructure.Sections[i1].Sections[i2].Sections) - 1
			if i3 < 0 {
				fmt.Println("h4 defined before h3 in file:", fileName)
				os.Exit(1)
			}
			i4 := len(BookStructure.Sections[i1].Sections[i2].Sections[i3].Sections)
			newText, modified, label = updateSectionText(text, 4, i2+1, i3+1, i4+1)
			BookStructure.Sections[i1].Sections[i2].Sections[i3].Sections =
				append(BookStructure.Sections[i1].Sections[i2].Sections[i3].Sections,
					SectionType{fileName, id, label, newText, modified,
						make([]SectionType, 0, 1),
						make([]CaptionType, 0, 1),
						make([]EquationType, 0, 5)})
			BookStructure.SectionFiles[iFile].Elements = append(BookStructure.SectionFiles[iFile].Elements,
				ElementType{"<h4", "</h4>", text, "", newText, "", modified, id, newID})
			BookStructure.SectionFiles[iFile].H1Index = *H1Index_old

		} else if s.Is("caption") || s.Is("figcaption") {
			var fig bool
			var iCap int
			if s.Is("caption") {
				fig = false
				Counters.iCaption++
				iCap = Counters.iCaption
			} else {
				fig = true
				Counters.iFigCaption++
				iCap = Counters.iFigCaption
			}

			i1 := len(BookStructure.Sections) - 1
			if i1 < 0 {
				fmt.Printf("caption/figcaption in file \"%s\" defined before first h1 defined in book", fileName)
				os.Exit(1)
			}

			newText, modified, label = updateCaptionText(text, fig, iCap)
			i2 := len(BookStructure.Sections[i1].Sections) - 1
			if i2 < 0 {
				BookStructure.Sections[i1].Captions =
					append(BookStructure.Sections[i1].Captions, CaptionType{fileName, id, newText, modified, fig})
			} else {
				i3 := len(BookStructure.Sections[i1].Sections[i2].Sections) - 1
				if i3 < 0 {
					BookStructure.Sections[i1].Sections[i2].Captions =
						append(BookStructure.Sections[i1].Sections[i2].Captions,
							CaptionType{fileName, id, newText, modified, fig})
				} else {
					i4 := len(BookStructure.Sections[i1].Sections[i2].Sections[i3].Sections) - 1
					if i4 < 0 {
						BookStructure.Sections[i1].Sections[i2].Sections[i3].Captions =
							append(BookStructure.Sections[i1].Sections[i2].Sections[i3].Captions,
								CaptionType{fileName, id, newText, modified, fig})
					} else {
						BookStructure.Sections[i1].Sections[i2].Sections[i3].Sections[i4].Captions =
							append(BookStructure.Sections[i1].Sections[i2].Sections[i3].Sections[i4].Captions,
								CaptionType{fileName, id, newText, modified, fig})
					}
				}
			}
			if fig {
				BookStructure.SectionFiles[iFile].Elements = append(BookStructure.SectionFiles[iFile].Elements,
					ElementType{"<figcaption", "</figcaption>", text, "", newText, "", modified, id, newID})
			} else {
				BookStructure.SectionFiles[iFile].Elements = append(BookStructure.SectionFiles[iFile].Elements,
					ElementType{"<caption", "</caption>", text, "", newText, "", modified, id, newID})
			}

		} else if s.Is("div.equation") {
			Counters.iEquation++

			i1 := len(BookStructure.Sections) - 1
			if i1 < 0 {
				fmt.Printf("<div class=\"equation\"> in file \"%s\" defined before first h1 defined in book", fileName)
				os.Exit(1)
			}

			newText, modified, label = updateEquationText(text)
			i2 := len(BookStructure.Sections[i1].Sections) - 1
			if i2 < 0 {
				BookStructure.Sections[i1].Equations =
					append(BookStructure.Sections[i1].Equations, EquationType{fileName, id, newText, modified})
			} else {
				i3 := len(BookStructure.Sections[i1].Sections[i2].Sections) - 1
				if i3 < 0 {
					BookStructure.Sections[i1].Sections[i2].Equations =
						append(BookStructure.Sections[i1].Sections[i2].Equations,
							EquationType{fileName, id, newText, modified})
				} else {
					i4 := len(BookStructure.Sections[i1].Sections[i2].Sections[i3].Sections) - 1
					if i4 < 0 {
						BookStructure.Sections[i1].Sections[i2].Sections[i3].Equations =
							append(BookStructure.Sections[i1].Sections[i2].Sections[i3].Equations,
								EquationType{fileName, id, newText, modified})
					} else {
						BookStructure.Sections[i1].Sections[i2].Sections[i3].Sections[i4].Equations =
							append(BookStructure.Sections[i1].Sections[i2].Sections[i3].Sections[i4].Equations,
								EquationType{fileName, id, newText, modified})
					}
				}
			}
			BookStructure.SectionFiles[iFile].Elements = append(BookStructure.SectionFiles[iFile].Elements,
				ElementType{"<div class=\"equation\"", "</div>", text, "", newText, "", modified, id, newID})
		}

		if modified || newID {
			BookStructure.SectionFiles[iFile].Modified = true
		}

		if newID {
			// Print information about introduced ID
			iElem := len(BookStructure.SectionFiles[iFile].Elements) - 1
			elem := BookStructure.SectionFiles[iFile].Elements[iElem]
			fmt.Printf("      Element id introduced: %s id=\"%s\">%s%s\n",
				elem.StartTag, id, newText, elem.EndTag)
		}

		// Store bookmark
		if s.Is("div.equation") {
			addBookmark(id, fileName, label, "") // no tool tip for a link to an equation
		} else {
			addBookmark(id, fileName, label, newText)
		}
	})
}